Skip to content

Commit

Permalink
Fix mwtele 230 (#32)
Browse files Browse the repository at this point in the history
* Fix MWTELE-231

* Minor change to logging

* Tighten up logging, special-case WEB-INF/classes
  • Loading branch information
kittylyst authored Mar 20, 2024
1 parent a73e0d7 commit ab057ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/redhat/insights/agent/ClassNoticer.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ public byte[] transform(
return bytes;
}
URL jarUrl = protectionDomain.getCodeSource().getLocation();
String jarLoc = jarUrl.toString();

// Special-case WEB-INF/classes/ for upfront attach to .war files
if (jarLoc.endsWith("WEB-INF/classes!/")) {
return bytes;
}

// If we haven't seen it before, add it to the set and enqueue it
try {
String jarLoc = jarUrl.toString();
if (!seenUrls.contains(jarLoc)) {
seenUrls.add(jarLoc);
Optional<JarInfo> oJar = analyzer.process(jarUrl);
Expand All @@ -76,7 +81,7 @@ public byte[] transform(
}
} catch (URISyntaxException e) {
// Shouldn't be possible - so just log and carry on
logger.error("Jar with bad URI seen, should not be possible: " + jarUrl);
logger.info("Jar with unrecognized URI seen: " + jarUrl);
}

// Return unmodified bytes
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/com/redhat/insights/agent/ClassNoticerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent; /* Copyright (C) Red Hat 2023 */

import static org.junit.jupiter.api.Assertions.*;

import com.redhat.insights.InsightsCustomScheduledExecutor;
import com.redhat.insights.InsightsReportController;
import com.redhat.insights.InsightsScheduler;
Expand All @@ -11,10 +13,12 @@
import com.redhat.insights.jars.JarInfo;
import com.redhat.insights.logging.InsightsLogger;
import com.redhat.insights.reports.InsightsReport;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.time.Duration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.jar.JarFile;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
Expand All @@ -25,6 +29,8 @@

public class ClassNoticerTest {

private static final String WAR_PATH = "src/test/resources/spring-boot-2-app-war-1.0.0.war";

@Test
@Disabled
public void testNoticer() throws InterruptedException {
Expand Down Expand Up @@ -57,4 +63,20 @@ public void testNoticer() throws InterruptedException {
.sendInsightsReport(
ArgumentMatchers.any(), (InsightsReport) ArgumentMatchers.any()));
}

@Test
public void testWar_mwtele_230() throws IOException, ClassNotFoundException {
// Setup Byte Buddy agent
Instrumentation instrumentation = ByteBuddyAgent.install();
JarFile jarFile = new JarFile(WAR_PATH);
instrumentation.appendToSystemClassLoaderSearch(jarFile);

BlockingQueue<JarInfo> jarsToSend = new LinkedBlockingQueue<>();
ClassNoticer noticer = new ClassNoticer(jarsToSend);
instrumentation.addTransformer(noticer);

// OK, agent is setup, now do the test
Class<?> clz = Class.forName("org.springframework.boot.loader.JarLauncher");
assertNotNull(clz);
}
}
Binary file not shown.

0 comments on commit ab057ef

Please sign in to comment.