Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RC1 for 0.9.0 #14

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<relocations>
<relocation>
<pattern>com.redhat.insights</pattern>
<shadedPattern>${shade.prefix}</shadedPattern>
ebaron marked this conversation as resolved.
Show resolved Hide resolved
<excludes>
<exclude>com.redhat.insights.agent.*</exclude>
</excludes>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>${shade.prefix}.org.apache</shadedPattern>
Expand Down Expand Up @@ -343,6 +350,7 @@
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>com.redhat.insights.agent.AgentMain</Premain-Class>
<Agent-Class>com.redhat.insights.agent.AgentMain</Agent-Class>
<Multi-Release>true</Multi-Release>
</manifestEntries>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/com/redhat/insights/agent/AgentConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,26 @@ public boolean isOptingOut() {
return false;
}

@Override
public String toString() {
return "AgentConfiguration{" + "args=" + args + '}';
}

public boolean isDebug() {
return "true".equalsIgnoreCase(args.getOrDefault("debug", "false"));
}

@Override
public String toString() {
return "AgentConfiguration{" + "args=" + args + '}';
public boolean isFileOnly() {
return "true".equalsIgnoreCase(args.getOrDefault("file_only", "false"));
}

// See https://issues.redhat.com/browse/MWTELE-93 for more information
public boolean isOCP() {
return "true".equalsIgnoreCase(args.getOrDefault("is_ocp", "false"));
}

// See https://issues.redhat.com/browse/MWTELE-93 for more information
public boolean shouldDefer() {
return "true".equalsIgnoreCase(args.getOrDefault("should_defer", "false"));
}
}
57 changes: 49 additions & 8 deletions src/main/java/com/redhat/insights/agent/AgentMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,57 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
if (!oArgs.isPresent()) {
return;
}
AgentConfiguration args = oArgs.get();
AgentConfiguration config = oArgs.get();

if (!shouldContinue(logger, config)) {
return;
}

BlockingQueue<JarInfo> jarsToSend = new LinkedBlockingQueue<>();
try {
logger.info("Starting Red Hat Insights client");
new AgentMain(logger, args, jarsToSend).start();
new AgentMain(logger, config, jarsToSend).start();
ClassNoticer noticer = new ClassNoticer(logger, jarsToSend);
instrumentation.addTransformer(noticer);
} catch (Throwable t) {
logger.error("Unable to start Red Hat Insights client", t);
}
}

// Now we have the config, we need to check for the existence of the unshaded client, not just
// the agent.
// This is belt-and-braces in case this agent is ever loaded into an
// app that has built-in Insights support.

// See https://issues.redhat.com/browse/MWTELE-93 for more information
static boolean shouldContinue(InsightsLogger logger, AgentConfiguration config) {
try {
// This obfuscation is necessary to work around the shader plugin which will try to helpfully
// rename the class name when we don't want it to.
String obfuscatedStem = "com.redhat";
String obfuscatedSubPackageAndClass = ".insights.InsightsReportController";

Class.forName(obfuscatedStem + obfuscatedSubPackageAndClass);
if (config.isOCP()) {
if (config.shouldDefer()) {
logger.warning("Insights builtin support is available, deferring to that");
return false;
} else {
logger.warning(
"Starting Red Hat Insights client: Builtin support for OpenShift is available, but"
+ " the agent is configured to run anyway. Ensure that this configuration is correct.");
}
} else {
// Always defer if we're on RHEL
logger.warning("Insights builtin support is available, deferring to that");
return false;
}
} catch (ClassNotFoundException __) {
// Builtin support not found, continue
}
return true;
}

/**
* Parse the agent arguments from the form "key1=value1;key2=value2;..."
*
Expand Down Expand Up @@ -108,26 +146,29 @@ static Optional<AgentConfiguration> parseArgs(InsightsLogger logger, String agen
}
logger.debug(config.toString());

if (!config.getMaybeAuthToken().isPresent()) {
if (shouldLookForCerts(config)) {
Path certPath = Paths.get(config.getCertFilePath());
Path keyPath = Paths.get(config.getKeyFilePath());
if (!Files.exists(certPath) || !Files.exists(keyPath)) {
if (!out.containsKey("debug") || !"true".equals(out.get("debug"))) {
logger.error("Unable to start Red Hat Insights client: Missing certificate or key files");
return Optional.empty();
}
logger.error("Unable to start Red Hat Insights client: Missing certificate or key files");
return Optional.empty();
}
}

return Optional.of(config);
}

private static boolean shouldLookForCerts(AgentConfiguration config) {
boolean hasToken = !config.getMaybeAuthToken().isPresent();
return !hasToken && !config.isDebug() && !config.isFileOnly();
}

private void start() {
final InsightsReport report = AgentBasicReport.of(logger, configuration);
final PEMSupport pem = new PEMSupport(logger, configuration);

Supplier<InsightsHttpClient> httpClientSupplier;
if (configuration.isDebug()) {
if (configuration.isDebug() || configuration.isFileOnly()) {
httpClientSupplier = () -> new InsightsFileWritingClient(logger, configuration);
} else {
httpClientSupplier =
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/redhat/insights/agent/AgentSubreport.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AgentSubreport implements InsightsSubreport {
activeGuesses.put("org.jboss.modules.Module", AgentSubreport::fingerprintJBoss);
activeGuesses.put(
"io.quarkus.bootstrap.runner.QuarkusEntryPoint", AgentSubreport::fingerprintQuarkus);
activeGuesses.put("org.apache.catalina.Server", __ -> "Tomcat / JWS");
activeGuesses.put("org.apache.catalina.Server", AgentSubreport::fingerprintTomcat);
}

private AgentSubreport(InsightsLogger logger, ClasspathJarInfoSubreport jarsReport) {
Expand Down Expand Up @@ -75,6 +75,15 @@ private void fingerprintReflectively(Collection<JarInfo> jarInfos) {
}
}

static String fingerprintTomcat(Class<?> qClazz) {
try {
Class.forName("org.apache.tomcat.vault.VaultInteraction");
} catch (ClassNotFoundException __) {
return "Tomcat";
}
return "JWS";
}

static String fingerprintQuarkus(Class<?> qClazz) {
String quarkusVersion = qClazz.getPackage().getImplementationVersion();
return "Quarkus " + quarkusVersion;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/redhat/insights/agent/ClassNoticerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.redhat.insights.InsightsCustomScheduledExecutor;
import com.redhat.insights.InsightsReportController;
import com.redhat.insights.InsightsScheduler;
import com.redhat.insights.agent.doubles.MockInsightsConfiguration;
import com.redhat.insights.agent.doubles.NoopInsightsLogger;
import com.redhat.insights.config.InsightsConfiguration;
import com.redhat.insights.doubles.MockInsightsConfiguration;
import com.redhat.insights.doubles.NoopInsightsLogger;
import com.redhat.insights.http.InsightsHttpClient;
import com.redhat.insights.jars.JarInfo;
import com.redhat.insights.logging.InsightsLogger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (C) Red Hat 2023 */
package com.redhat.insights.doubles;
package com.redhat.insights.agent.doubles;

import com.redhat.insights.config.DefaultInsightsConfiguration;
import com.redhat.insights.config.InsightsConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (C) Red Hat 2023 */
package com.redhat.insights.doubles;
package com.redhat.insights.agent.doubles;

import com.redhat.insights.logging.InsightsLogger;

Expand Down