Skip to content

Commit 83cd80a

Browse files
committed
add support for keystore/truststore to containers
1 parent c53e356 commit 83cd80a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/JmxScraperContainer.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import com.google.errorprone.annotations.CanIgnoreReturnValue;
11+
import java.nio.file.Path;
1112
import java.time.Duration;
1213
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.Collections;
1316
import java.util.HashSet;
1417
import java.util.List;
1518
import java.util.Locale;
@@ -29,6 +32,10 @@ public class JmxScraperContainer extends GenericContainer<JmxScraperContainer> {
2932
private String password;
3033
private final List<String> extraJars;
3134
private boolean testJmx;
35+
private Path keyStore;
36+
private String keyStorePassword;
37+
private Path trustStore;
38+
private String trustStorePassword;
3239

3340
public JmxScraperContainer(String otlpEndpoint, String baseImage) {
3441
super(baseImage);
@@ -112,6 +119,20 @@ public JmxScraperContainer withTestJmx() {
112119
return this;
113120
}
114121

122+
@CanIgnoreReturnValue
123+
public JmxScraperContainer withKeyStore(Path keyStore, String password) {
124+
this.keyStore = keyStore;
125+
this.keyStorePassword = password;
126+
return this;
127+
}
128+
129+
@CanIgnoreReturnValue
130+
public JmxScraperContainer withTrustStore(Path trustStore, String password) {
131+
this.trustStore = trustStore;
132+
this.trustStorePassword = password;
133+
return this;
134+
}
135+
115136
@Override
116137
public void start() {
117138
// for now only configure through JVM args
@@ -138,6 +159,9 @@ public void start() {
138159
arguments.add("-Dotel.jmx.password=" + password);
139160
}
140161

162+
arguments.addAll(addKeyStore(keyStore, keyStorePassword, /* keyStore= */ true));
163+
arguments.addAll(addKeyStore(trustStore, trustStorePassword, /* keyStore= */ false));
164+
141165
if (!customYamlFiles.isEmpty()) {
142166
for (String yaml : customYamlFiles) {
143167
this.withCopyFileToContainer(MountableFile.forClasspathResource(yaml), yaml);
@@ -171,4 +195,15 @@ public void start() {
171195

172196
super.start();
173197
}
198+
199+
private List<String> addKeyStore(Path path, String password, boolean keyStore) {
200+
if (path == null) {
201+
return Collections.emptyList();
202+
}
203+
String containerPath = "/" + path.getFileName().toString();
204+
this.withCopyFileToContainer(MountableFile.forHostPath(path), containerPath);
205+
206+
String prefix = String.format("-Djavax.net.ssl.%sStore", keyStore ? "key" : "trust");
207+
return Arrays.asList(prefix + "=" + containerPath, prefix + "Password=" + password);
208+
}
174209
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/TestAppContainer.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public class TestAppContainer extends GenericContainer<TestAppContainer> {
2828
private String login;
2929
private String pwd;
3030
private boolean jmxSsl;
31+
private Path keyStore;
32+
private String keyStorePassword;
33+
private Path trustStore;
34+
private String trustStorePassword;
3135

3236
public TestAppContainer() {
3337
super("openjdk:8u272-jre-slim");
@@ -68,6 +72,20 @@ public TestAppContainer withJmxSsl() {
6872
return this;
6973
}
7074

75+
@CanIgnoreReturnValue
76+
public TestAppContainer withKeyStore(Path keyStore, String password) {
77+
this.keyStore = keyStore;
78+
this.keyStorePassword = password;
79+
return this;
80+
}
81+
82+
@CanIgnoreReturnValue
83+
public TestAppContainer withTrustStore(Path trustStore, String password) {
84+
this.trustStore = trustStore;
85+
this.trustStorePassword = password;
86+
return this;
87+
}
88+
7189
@Override
7290
public void start() {
7391
properties.put("com.sun.management.jmxremote.ssl", Boolean.toString(jmxSsl));
@@ -86,6 +104,9 @@ public void start() {
86104
properties.put("com.sun.management.jmxremote.access.file", "/jmx.access");
87105
}
88106

107+
addKeyStore(keyStore, keyStorePassword, /* keyStore= */ true, properties);
108+
addKeyStore(trustStore, trustStorePassword, /* keyStore= */ false, properties);
109+
89110
String confArgs =
90111
properties.entrySet().stream()
91112
.map(
@@ -105,6 +126,19 @@ public void start() {
105126
super.start();
106127
}
107128

129+
private void addKeyStore(
130+
Path path, String password, boolean keyStore, Map<String, String> properties) {
131+
if (path == null) {
132+
return;
133+
}
134+
String containerPath = "/" + path.getFileName().toString();
135+
this.withCopyFileToContainer(MountableFile.forHostPath(path), containerPath);
136+
137+
String prefix = String.format("javax.net.ssl.%sStore", keyStore ? "key" : "trust");
138+
properties.put(prefix, containerPath);
139+
properties.put(prefix + "Password", password);
140+
}
141+
108142
private static Path createPwdFile(String login, String pwd) {
109143
try {
110144
Path path = Files.createTempFile("test", ".pwd");

0 commit comments

Comments
 (0)