Skip to content

Commit

Permalink
Split agent to separate module (RedHatInsights#29)
Browse files Browse the repository at this point in the history
* Move to a separate module. One Disabled test.

* Tests pass

* Add in Apache HTTP Client for agent

* Add token support to agent

* Add docs
  • Loading branch information
kittylyst authored Mar 16, 2023
1 parent c5f8e00 commit c837fd7
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 234 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Compiled Go code
jboss-cert-helper/jboss-cert-helper
jboss-cert-helper/bin

# Compiled class file
*.class
Expand Down
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ The only authentication flows we support in this release are:
1. mTLS using certs managed by Red Hat Subscription Manager (RHSM)
2. Bearer token authentication in OpenShift Container Platform (OCP)

There are four modules within the project:

* agent - A Java agent, supporting Java 8. Implementations need not depend on this
* api - The core API (Java 8). All uses cases will need to depend on this
* jboss-cert-helper - A standalone Go binary that is used to provide access to RHEL certs
* runtime - A Java 11 module that provides an HTTP client and some top-level reports. Most implementations will depend on this.

## Building

- Requires Java 11
Expand All @@ -30,7 +37,7 @@ This project contains some code comprising derivative works based upon open-sour

The original work is also licensed under the Apache 2 License.

## How to test against an ephemeral environment
## How to test against a token-based (e.g. ephemeral) environment

Here's a command-line guide to uploading some payload:

Expand All @@ -54,6 +61,7 @@ type='application/vnd.redhat.runtimes-java-general.analytics+tgz' \

## Environment variables and system properties

For standard, in-process clients, a combination of environment vars & system properties are used to configure the client.
The following environment variables are available to be overriden when using `EnvAndSysPropsInsightsConfiguration`.

| Name | Default value | Description |
Expand All @@ -80,6 +88,33 @@ For instance `RHT_INSIGHTS_JAVA_KEY_FILE_PATH` becomes `rht.insights.java.key.fi

Note that environment variables take priority over system properties.

## Java agent args string

When using the agent in the startup configuration, the usual agent args string technique is used.
That is, the path to the agent jar is followed by an `=` and then the rest of the argument is passed as a single string to the agent.

In our case, the args are passed as key-value pairs, separated by `;`. For example:

```
-javaagent:runtimes-java-agent-1.0.0.jar=name=my_app;token=amXXXXYYYYZZZZj
```

Note that the use of `;` means that on Unix, means that the javaagent argument will typically need to be quoted.

The available key-value pairs are:

| Name | Default value | Description |
|--------------|-----------------------------------------|----------------------------------------------------|
| `optOut` | `false` | Opt out of Red Hat Insights reporting when `true` |
| `name` | N/A, must be defined | Identification name for reporting |
| `cert` | `/etc/pki/consumer/cert.pem` | Certificate path file |
| `key` | `/etc/pki/consumer/key.pem` | Certificate key file |
| `token` | (empty) | Authentication token for token-based auth, if used |
| `base_url` | `https://cert.console.stage.redhat.com` | Server endpoint URL |
| `uri` | `/api/ingress/v1/upload` | Request URI at the server endpoint |
| `proxy` | (empty) | Proxy host, if any |
| `proxy_port` | (empty) | Proxy port, if any |

## Testing & coverage report

To run tests simply use maven command:
Expand Down
228 changes: 228 additions & 0 deletions agent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.redhat.insights</groupId>
<artifactId>runtimes-java</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>runtimes-java-agent</artifactId>

<properties>
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

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

<!-- Test libraries versions -->
<byte-buddy.version>1.14.0</byte-buddy.version>

</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>runtimes-java-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-x500-cert</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${apache.httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${apache.httpmime.version}</version>
</dependency>

<!-- Test dependencies -->

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>runtimes-java-api</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<!-- Byte Buddy is used for testing the agent -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${byte-buddy.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<archive>
<manifest></manifest>
<manifestEntries>
<Premain-Class>com.redhat.insights.agent.AgentMain</Premain-Class>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>

<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless-maven-plugin.version}</version>
<configuration>
<skip>${skip.spotless}</skip>
<!-- optional: limit format enforcement to just the files changed by this feature branch -->
<ratchetFrom>origin/main</ratchetFrom>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>*.md</include>
<include>.gitignore</include>
</includes>
<!-- define the steps to apply to those files -->
<trimTrailingWhitespace></trimTrailingWhitespace>
<endWithNewline></endWithNewline>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<!-- define a language-specific format -->
<java>
<!-- no need to specify files, inferred automatically, but you can if you want -->

<!-- apply a specific flavor of google-java-format and reflow long strings -->
<googleJavaFormat>
<version>${google-java-format.version}</version>
<style>GOOGLE</style>
<reflowLongStrings>true</reflowLongStrings>
<groupArtifact>com.google.googlejavaformat:google-java-format</groupArtifact>
</googleJavaFormat>

<!-- make sure every file has the following copyright header.
optionally, Spotless can set copyright years by digging
through git history (see "license" section below) -->
<licenseHeader>
<content>/* Copyright (C) Red Hat $YEAR */</content>
<!-- or <file>${project.basedir}/license-header</file> -->
</licenseHeader>
</java>
<pom>
<includes>
<include>pom.xml</include>
</includes>
<sortPom></sortPom>
</pom>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>

</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (C) Red Hat 2023 */
package com.redhat.insights.agent;

import com.redhat.insights.AbstractTopLevelReportBase;
import com.redhat.insights.InsightsSubreport;
import com.redhat.insights.config.InsightsConfiguration;
import com.redhat.insights.logging.InsightsLogger;
import java.util.Collections;
import java.util.Map;

public class AgentBasicReport extends AbstractTopLevelReportBase {
private AgentBasicReport(
InsightsLogger logger,
InsightsConfiguration config,
Map<String, InsightsSubreport> subReports) {
super(logger, config, subReports);
}

public static AgentBasicReport of(InsightsLogger logger, InsightsConfiguration configuration) {
return new AgentBasicReport(logger, configuration, Collections.emptyMap());
}

@Override
protected long getProcessPID() {
return Long.parseLong(
java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
}

@Override
protected Package[] getPackages() {
return Package.getPackages();
}
}
Loading

0 comments on commit c837fd7

Please sign in to comment.