Skip to content

Commit

Permalink
Merge pull request #50 from RedHatInsights/fix/MWTELE-51
Browse files Browse the repository at this point in the history
Fix for MWTELE-51
  • Loading branch information
kittylyst authored Mar 23, 2023
2 parents 21c6f70 + 0b3ef9a commit ff0dd02
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ The following environment variables are available to be overriden when using `En
|------------------------------------------------------|-----------------------------------------|----------------------------------------------------------------------|
| `RHT_INSIGHTS_JAVA_OPT_OUT` | `false` | Opt out of Red Hat Insights reporting when `true` |
| `RHT_INSIGHTS_JAVA_IDENTIFICATION_NAME` | N/A, must be defined | Identification name for reporting |
| `RHT_INSIGHTS_JAVA_CERT_FILE_PATH` | `/etc/pki/consumer/cert.pem` | Certificate path file |
| `RHT_INSIGHTS_JAVA_KEY_FILE_PATH` | `/etc/pki/consumer/key.pem` | Key path file |
| `RHT_INSIGHTS_JAVA_CERT_FILE_PATH` | `/etc/pki/consumer/cert.pem` | Certificate file path |
| `RHT_INSIGHTS_JAVA_KEY_FILE_PATH` | `/etc/pki/consumer/key.pem` | Key file path |
| `RHT_INSIGHTS_JAVA_CERT_HELPER_BINARY` | `/opt/jboss-cert-helper` | JBoss certificate retrieval helper |
| `RHT_INSIGHTS_JAVA_AUTH_TOKEN` | (empty) | Authentication token for token-based auth, if used |
| `RHT_INSIGHTS_JAVA_UPLOAD_BASE_URL` | `https://cert.console.stage.redhat.com` | Server endpoint URL |
Expand All @@ -78,7 +78,7 @@ The following environment variables are available to be overriden when using `En
| `RHT_INSIGHTS_JAVA_CONNECT_PERIOD` | 1 day (`P1D`) | Connect period, see `java.time.Duration::parse` for the syntax |
| `RHT_INSIGHTS_JAVA_UPDATE_PERIOD` | 5 minutes (`PT5M`) | Update period, see `java.time.Duration::parse` for the syntax |
| `RHT_INSIGHTS_JAVA_HTTP_CLIENT_RETRY_INITIAL_DELAY` | 2000 (milliseconds as `long`) | HTTP client exponential backoff: initial retry delay in milliseconds |
| `RHT_INSIGHTS_JAVA_HTTP_CLIENT_RETRY_BACKOFF_FACTOR` | 2.0 (`double`) | HTTP client exponential backoff: factor |
| `RHT_INSIGHTS_JAVA_HTTP_CLIENT_RETRY_BACKOFF_FACTOR` | 2.0 (`double`) | HTTP client exponential backoff: factor |
| `RHT_INSIGHTS_JAVA_HTTP_CLIENT_RETRY_MAX_ATTEMPTS` | 10 (`int`) | HTTP client exponential backoff: maximum number of retry attempts |
| `RHT_INSIGHTS_JAVA_ARCHIVE_UPLOAD_DIR` | `/var/tmp/insights-runtimes/uploads` | Filesystem location to place archives if HTTP upload fails |

Expand All @@ -90,16 +90,19 @@ Note that environment variables take priority over system properties.
## Testing & coverage report

To run tests simply use maven command:

```
mvn clean test
```

This project is configured with JaCoCo coverage reporting, to get a coverage report run:

```
mvn clean test -Pcoverage
```

Report will be placed on:

```
(module)/target/site/jacoco/index.html
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface InsightsConfiguration {

String DEFAULT_RHEL_CERT_FILE_PATH = "/etc/pki/consumer/cert.pem";
String DEFAULT_RHEL_KEY_FILE_PATH = "/etc/pki/consumer/key.pem";
String DEFAULT_RHEL_MACHINE_ID_FILE_PATH = "/etc/insights-client/machine-id";

// FIXME
String DEFAULT_UPLOAD_BASE_URL = "https://cert.console.redhat.com";
String DEFAULT_UPLOAD_URI = "/api/ingress/v1/upload";
Expand Down Expand Up @@ -40,6 +42,10 @@ default String getKeyFilePath() {
return DEFAULT_RHEL_KEY_FILE_PATH;
}

default String getMachineIdFilePath() {
return DEFAULT_RHEL_MACHINE_ID_FILE_PATH;
}

default Optional<String> getMaybeAuthToken() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void sendInsightsReport(String filename, InsightsReport report) {
@Override
public boolean isReadyToSend() {
return (new File(config.getCertFilePath()).exists()
&& new File(config.getKeyFilePath()).exists());
&& new File(config.getKeyFilePath()).exists())
&& new File(config.getMachineIdFilePath()).exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class MockInsightsConfiguration extends DefaultInsightsConfiguratio
private final String keyFilePath;
private final String uploadURL;
private final String uploadPath;
private final String machineIdFilePath;
private final Optional<ProxyConfiguration> proxyConfiguration;
private final boolean optingOut;
private final Duration connectPeriod;
Expand All @@ -26,6 +27,7 @@ private MockInsightsConfiguration(
String keyFilePath,
String uploadURL,
String uploadPath,
String machineIdFilePath,
Optional<ProxyConfiguration> proxyConfiguration,
boolean optingOut,
Duration connectPeriod,
Expand All @@ -38,6 +40,7 @@ private MockInsightsConfiguration(
this.keyFilePath = keyFilePath;
this.uploadURL = uploadURL;
this.uploadPath = uploadPath;
this.machineIdFilePath = machineIdFilePath;
this.proxyConfiguration = proxyConfiguration;
this.optingOut = optingOut;
this.connectPeriod = connectPeriod;
Expand Down Expand Up @@ -78,6 +81,7 @@ public static InsightsConfiguration of(
"/fake",
"https://127.0.0.1:999999",
"/fake",
DEFAULT_RHEL_MACHINE_ID_FILE_PATH,
Optional.empty(),
optedOut,
connectPeriod,
Expand All @@ -101,6 +105,7 @@ public static InsightsConfiguration of(
"/fake",
"https://127.0.0.1:999999",
"/fake",
DEFAULT_RHEL_MACHINE_ID_FILE_PATH,
Optional.empty(),
optedOut,
connectPeriod,
Expand All @@ -125,6 +130,11 @@ public String getKeyFilePath() {
return keyFilePath;
}

@Override
public String getMachineIdFilePath() {
return machineIdFilePath;
}

@Override
public String getUploadBaseURL() {
return uploadURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public String getKeyFilePath() {
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert").toString();
}

@Override
public String getMachineIdFilePath() {
return getPathFromResource("com/redhat/insights/machine-id").toString();
}
};
InsightsConfiguration wrongKeyConfig =
new InsightsConfiguration() {
Expand Down Expand Up @@ -104,6 +109,28 @@ public String getCertFilePath() {
.toString();
}
};
InsightsConfiguration wrongMachineIdConfig =
new InsightsConfiguration() {
@Override
public String getIdentificationName() {
return "GOOD";
}

@Override
public String getKeyFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.key").toString();
}

@Override
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert").toString();
}

@Override
public String getMachineIdFilePath() {
return "BAD";
}
};

InsightsLogger logger = new NoopInsightsLogger();
InsightsHttpClient client = new InsightsFileWritingClient(logger, goodConfig);
Expand All @@ -115,6 +142,10 @@ public String getCertFilePath() {
client = new InsightsFileWritingClient(logger, wrongKeyConfig);
assertFalse(
client.isReadyToSend(), "Client shouldn't be ready to send because of wrong key path");
client = new InsightsFileWritingClient(logger, wrongMachineIdConfig);
assertFalse(
client.isReadyToSend(),
"Client shouldn't be ready to send because of wrong machine-id path");
}

private Path getPathFromResource(String path) {
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/com/redhat/insights/machine-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345-abcdef
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ protected void sendInsightsReportWithClient(
public boolean isReadyToSend() {
return !configuration.useMTLS()
|| (new File(configuration.getCertFilePath()).exists()
&& new File(configuration.getKeyFilePath()).exists());
&& new File(configuration.getKeyFilePath()).exists()
&& new File(configuration.getMachineIdFilePath()).exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,21 @@ public String getCertFilePath() {
public Optional<String> getMaybeAuthToken() {
return Optional.empty();
}

@Override
public String getMachineIdFilePath() {
return getPathFromResource("com/redhat/insights/machine-id").toString();
}
};
assertFalse(config.getMaybeAuthToken().isPresent());
assertTrue(new File(config.getCertFilePath()).exists());
assertTrue(new File(config.getKeyFilePath()).exists());
assertTrue(new File(config.getMachineIdFilePath()).exists());
assertTrue(
config.getMaybeAuthToken().isPresent()
|| (new File(config.getCertFilePath()).exists()
&& new File(config.getKeyFilePath()).exists()));
&& new File(config.getKeyFilePath()).exists()
&& new File(config.getMachineIdFilePath()).exists()));
assertTrue(
new InsightsJdkHttpClient(logger, config, () -> mock(SSLContext.class)).isReadyToSend(),
"Client should be ready to send");
Expand Down
1 change: 1 addition & 0 deletions runtime/src/test/resources/com/redhat/insights/machine-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345-abcdef

0 comments on commit ff0dd02

Please sign in to comment.