Skip to content

Commit

Permalink
fix: prep for 1.0.0 - RC1 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kittylyst authored Jan 26, 2024
1 parent eba6e75 commit 1e524d3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
13 changes: 2 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.redhat.insights</groupId>
<artifactId>runtimes-agent</artifactId>
<version>0.9.2-SNAPSHOT</version>
<version>0.9.3-SNAPSHOT</version>

<name>Red Hat Insights Java Agent</name>
<description>Red Hat Insights Java Agent</description>
Expand Down Expand Up @@ -49,7 +49,7 @@
<skip.spotless>false</skip.spotless>

<!-- Core dependencies versions -->
<runtimes.java.api.version>2.0.1</runtimes.java.api.version>
<runtimes.java.api.version>2.0.2</runtimes.java.api.version>
<apache.httpclient.version>4.5.14</apache.httpclient.version>
<apache.httpcore.version>4.4.16</apache.httpcore.version>
<apache.httpmime.version>4.5.14</apache.httpmime.version>
Expand Down Expand Up @@ -123,15 +123,6 @@
<version>${org.slf4j.version}</version>
</dependency>

<!-- Tools jar -->
<!-- <dependency>-->
<!-- <groupId>com.sun</groupId>-->
<!-- <artifactId>tools</artifactId>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${toolsjar}</systemPath>-->
<!-- <version>1.8.0</version>-->
<!-- </dependency>-->

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/redhat/insights/agent/AgentConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import com.redhat.insights.config.InsightsConfiguration;
Expand All @@ -7,6 +7,9 @@

public final class AgentConfiguration implements InsightsConfiguration {

static final String TRUE = "true";
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";
Expand All @@ -17,6 +20,11 @@ public final class AgentConfiguration implements InsightsConfiguration {
static final String AGENT_ARG_PROXY_PORT = "proxy_port";
static final String AGENT_ARG_OPT_OUT = "optOut";

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";

private final Map<String, String> args;

public AgentConfiguration(Map<String, String> args) {
Expand Down Expand Up @@ -92,20 +100,20 @@ public String toString() {
}

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

public boolean isFileOnly() {
return "true".equalsIgnoreCase(args.getOrDefault("file_only", "false"));
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("is_ocp", "false"));
return TRUE.equalsIgnoreCase(args.getOrDefault(AGENT_ARG_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"));
return TRUE.equalsIgnoreCase(args.getOrDefault(AGENT_ARG_SHOULD_DEFER, FALSE));
}
}
15 changes: 8 additions & 7 deletions src/main/java/com/redhat/insights/agent/AgentMain.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import com.redhat.insights.InsightsException;
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
}

if (agentArgs == null || "".equals(agentArgs)) {
logger.error("Unable to start Red Hat Insights client: Need config arguments");
logger.error("Unable to start Red Hat Insights agent: Need config arguments");
return;
}
Optional<AgentConfiguration> oArgs = parseArgs(agentArgs);
Expand All @@ -68,12 +68,13 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
AgentConfiguration config = oArgs.get();

if (!shouldContinue(config)) {
logger.info("Config indicates Red Hat Insights agent is not to be run. Stopping.");
return;
}

final BlockingQueue<JarInfo> jarsToSend = new LinkedBlockingQueue<>();
try {
logger.info("Starting Red Hat Insights client");
logger.info("Starting Red Hat Insights agent");
new AgentMain(config, jarsToSend).start();
ClassNoticer noticer = new ClassNoticer(jarsToSend);
instrumentation.addTransformer(noticer);
Expand Down Expand Up @@ -128,7 +129,7 @@ static Optional<AgentConfiguration> parseArgs(String agentArgs) {
String[] kv = pair.split("=");
if (kv.length != 2) {
logger.error(
"Unable to start Red Hat Insights client: Malformed config arguments (should be"
"Unable to start Red Hat Insights agent: Malformed config arguments (should be"
+ " key-value pairs)");
return Optional.empty();
}
Expand All @@ -138,7 +139,7 @@ static Optional<AgentConfiguration> parseArgs(String agentArgs) {

if (config.getIdentificationName() == null || "".equals(config.getIdentificationName())) {
logger.error(
"Unable to start Red Hat Insights client: App requires a name for identification");
"Unable to start Red Hat Insights agent: App requires a name for identification");
return Optional.empty();
}
logger.debug(config.toString());
Expand All @@ -147,7 +148,7 @@ static Optional<AgentConfiguration> parseArgs(String agentArgs) {
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 client: Missing certificate or key files");
logger.error("Unable to start Red Hat Insights agent: Missing certificate or key files");
return Optional.empty();
}
}
Expand Down Expand Up @@ -177,7 +178,7 @@ private void start() {
logger, configuration, report, httpClientSupplier, waitingJars);
controller.generate();
} catch (InsightsException e) {
logger.info("Unable to start Red Hat Insights client: " + e.getMessage());
logger.info("Unable to start Red Hat Insights agent: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import static com.redhat.insights.InsightsErrorCode.*;
Expand Down Expand Up @@ -86,7 +86,7 @@ void sendCompressedInsightsReport(String filename, byte[] bytes) {
if (useMTLS) {
post = createPost();
} else {
post = createAuthTokenPost(configuration.getMaybeAuthToken().get());
post = createAuthTokenPost();
}
post.setHeader("Cache-Control", "no-store");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
Expand Down Expand Up @@ -136,14 +136,15 @@ void sendCompressedInsightsReport(String filename, byte[] bytes) {
}
}

private HttpPost createAuthTokenPost(String token) {
HttpPost createAuthTokenPost() {
String token = configuration.getMaybeAuthToken().get();
HttpPost post =
new HttpPost(assembleURI(configuration.getUploadBaseURL(), configuration.getUploadUri()));
post.setHeader("Authorization", "Basic " + token);
post.setHeader("Authorization", "Bearer " + token);
return post;
}

private HttpPost createPost() {
HttpPost createPost() {
return new HttpPost(
assembleURI(configuration.getUploadBaseURL(), configuration.getUploadUri()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (C) Red Hat 2024 */
package com.redhat.insights.agent;

import static com.redhat.insights.InsightsErrorCode.ERROR_SSL_CREATING_CONTEXT;
import static com.redhat.insights.agent.AgentMain.parseArgs;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.redhat.insights.InsightsException;
import java.util.*;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpPost;
import org.junit.jupiter.api.Test;

public class InsightsAgentHttpClientTest {

@Test
void testBearerToken() {
Optional<AgentConfiguration> oConfig = parseArgs("name=foo;token=bar;is_ocp=true");
InsightsAgentHttpClient client =
new InsightsAgentHttpClient(
oConfig.get(),
() -> {
throw new InsightsException(
ERROR_SSL_CREATING_CONTEXT,
"Illegal attempt to create SSLContext for token auth");
});
HttpPost post = client.createAuthTokenPost();
List<Header> headers = Arrays.asList(post.getAllHeaders());
assertTrue(
headers.stream()
.anyMatch(
h -> h.getName().equals("Authorization") && h.getValue().equals("Bearer bar")));
}
}

0 comments on commit 1e524d3

Please sign in to comment.