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

Fix MWTELE-231 #31

Merged
merged 1 commit into from
Mar 19, 2024
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
27 changes: 12 additions & 15 deletions src/main/java/com/redhat/insights/agent/AgentConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public final class AgentConfiguration implements InsightsConfiguration {
static final String FALSE = "false";

static final String AGENT_ARG_NAME = "name";
static final String AGENT_ARG_CERT = "cert";
static final String AGENT_ARG_KEY = "key";
static final String AGENT_ARG_TOKEN = "token";
static final String AGENT_ARG_TOKEN_FILE = "token_file";
static final String AGENT_ARG_BASE_URL = "base_url";
Expand All @@ -25,7 +23,6 @@ public final class AgentConfiguration implements InsightsConfiguration {
static final String AGENT_ARG_PROXY_PORT = "proxy_port";
static final String AGENT_ARG_OPT_OUT = "opt_out";
static final String AGENT_ARG_DEBUG = "debug";
static final String AGENT_ARG_FILE_ONLY = "file_only";
static final String AGENT_ARG_IS_OCP = "is_ocp";
static final String AGENT_ARG_SHOULD_DEFER = "should_defer";

Expand Down Expand Up @@ -65,20 +62,24 @@ public String getIdentificationName() {
return args.get(AGENT_ARG_NAME);
}

/**
* This is not used in the agent, so we return an empty string.
*
* @return
*/
@Override
public String getCertFilePath() {
if (args.containsKey(AGENT_ARG_CERT)) {
return args.get(AGENT_ARG_CERT);
}
return InsightsConfiguration.DEFAULT_RHEL_CERT_FILE_PATH;
return "";
}

/**
* This is not used in the agent, so we return an empty string.
*
* @return
*/
@Override
public String getKeyFilePath() {
if (args.containsKey(AGENT_ARG_KEY)) {
return args.get(AGENT_ARG_KEY);
}
return InsightsConfiguration.DEFAULT_RHEL_KEY_FILE_PATH;
return "";
}

@Override
Expand Down Expand Up @@ -124,10 +125,6 @@ public boolean isDebug() {
return TRUE.equalsIgnoreCase(args.getOrDefault(AGENT_ARG_DEBUG, FALSE));
}

public boolean isFileOnly() {
return TRUE.equalsIgnoreCase(args.getOrDefault(AGENT_ARG_FILE_ONLY, FALSE));
}

// See https://issues.redhat.com/browse/MWTELE-93 for more information
public boolean isOCP() {
return TRUE.equalsIgnoreCase(args.getOrDefault(AGENT_ARG_IS_OCP, FALSE));
Expand Down
41 changes: 12 additions & 29 deletions src/main/java/com/redhat/insights/agent/AgentMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import com.redhat.insights.http.InsightsHttpClient;
import com.redhat.insights.jars.JarInfo;
import com.redhat.insights.reports.InsightsReport;
import com.redhat.insights.tls.PEMSupport;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -56,7 +52,7 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
loaded = true;
}

if (agentArgs == null || "".equals(agentArgs)) {
if (agentArgs == null || agentArgs.isEmpty()) {
logger.error("Unable to start Red Hat Insights agent: Need config arguments");
return;
}
Expand Down Expand Up @@ -88,9 +84,10 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
}

// 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
// the agent. This is belt-and-braces in case this agent is ever loaded into an
// app that has built-in Insights support.
//
// If we detect the unshaded client, we should defer to that. If we don't, we should continue.

// See https://issues.redhat.com/browse/MWTELE-93 for more information
static boolean shouldContinue(AgentConfiguration config) {
Expand Down Expand Up @@ -152,23 +149,9 @@ static Optional<AgentConfiguration> parseArgs(String agentArgs) {
}
logger.debug(config.toString());

if (shouldLookForCerts(config)) {
Path certPath = Paths.get(config.getCertFilePath());
Path keyPath = Paths.get(config.getKeyFilePath());
if (!Files.exists(certPath) || !Files.exists(keyPath)) {
logger.error("Unable to start Red Hat Insights agent: 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.isFileOnly() && !config.isOptingOut();
}

private void start() {
final InsightsReport report = AgentBasicReport.of(configuration);

Expand All @@ -182,16 +165,16 @@ private void start() {
}
}

/*
* There are only two possibilities - either we're running in OCP or we're not. If we are, we
* need an HTTP client that can talk through the proxy to the Insights service. If we're not, we
* are running on RHEL and need to put a report somewhere the RHEL Insights client can pick it up.
*/
private Supplier<InsightsHttpClient> getInsightsClientSupplier() {
final Supplier<InsightsHttpClient> out;
if (configuration.isFileOnly()) {
out = () -> new InsightsFileWritingClient(logger, configuration);
} else if (configuration.isOCP()) {
out = () -> new InsightsAgentHttpClient(configuration);
if (configuration.isOCP()) {
return () -> new InsightsAgentHttpClient(configuration);
} else {
final PEMSupport pem = new PEMSupport(logger, configuration);
out = () -> new InsightsAgentHttpClient(configuration, pem::createTLSContext);
return () -> new InsightsFileWritingClient(logger, configuration);
}
return out;
}
}