Skip to content

Commit 29ca67a

Browse files
committed
add support & test client certificate
1 parent 0e07360 commit 29ca67a

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class JmxConnectionTest {
3636
private static final int JMX_PORT = 9999;
3737
private static final String APP_HOST = "app";
3838

39+
// key/trust stores passwords
40+
private static final String CLIENT_PASSWORD = "client";
41+
private static final String SERVER_PASSWORD = "server";
42+
3943
private static final Logger jmxScraperLogger = LoggerFactory.getLogger("JmxScraperContainer");
4044
private static final Logger appLogger = LoggerFactory.getLogger("TestAppContainer");
4145

@@ -90,27 +94,67 @@ private static void testServerSsl(Path tempDir, boolean sslRmiRegistry) {
9094
// server keystore with public/private key pair
9195
// client trust store with certificate from server
9296

93-
String clientPassword = "client";
94-
String serverPassword = "server";
95-
9697
Path serverKeystore = tempDir.resolve("server.jks");
97-
Path clientKeystore = tempDir.resolve("client.jks");
98+
Path clientTrustStore = tempDir.resolve("client.jks");
9899

99-
X509Certificate serverCertificate = createKeyStore(serverKeystore, serverPassword);
100+
X509Certificate serverCertificate = createKeyStore(serverKeystore, SERVER_PASSWORD);
100101

101-
createKeyStore(clientKeystore, clientPassword);
102-
addTrustedCertificate(clientKeystore, clientPassword, serverCertificate);
102+
createKeyStore(clientTrustStore, CLIENT_PASSWORD);
103+
addTrustedCertificate(clientTrustStore, CLIENT_PASSWORD, serverCertificate);
103104

104105
connectionTest(
105106
app ->
106107
(sslRmiRegistry ? app.withSslRmiRegistry(4242) : app)
107108
.withJmxPort(JMX_PORT)
108109
.withJmxSsl()
109-
.withKeyStore(serverKeystore, serverPassword),
110+
.withKeyStore(serverKeystore, SERVER_PASSWORD),
110111
scraper ->
111112
(sslRmiRegistry ? scraper.withSslRmiRegistry() : scraper)
112113
.withRmiServiceUrl(APP_HOST, JMX_PORT)
113-
.withTrustStore(clientKeystore, clientPassword));
114+
.withTrustStore(clientTrustStore, CLIENT_PASSWORD));
115+
}
116+
117+
@Test
118+
void serverSslClientSsl(@TempDir Path tempDir) {
119+
// Note: this could have been made simpler by relying on the fact that keystore could be used
120+
// as a trust store, but having clear split provides also some extra clarity
121+
//
122+
// 4 keystores:
123+
// server keystore with public/private key pair
124+
// server truststore with client certificate
125+
// client key store with public/private key pair
126+
// client trust store with certificate from server
127+
128+
Path serverKeystore = tempDir.resolve("server-keystore.jks");
129+
Path serverTrustStore = tempDir.resolve("server-truststore.jks");
130+
131+
X509Certificate serverCertificate = createKeyStore(serverKeystore, SERVER_PASSWORD);
132+
createKeyStore(serverTrustStore, SERVER_PASSWORD);
133+
134+
Path clientKeystore = tempDir.resolve("client-keystore.jks");
135+
Path clientTrustStore = tempDir.resolve("client-truststore.jks");
136+
137+
X509Certificate clientCertificate = createKeyStore(clientKeystore, CLIENT_PASSWORD);
138+
createKeyStore(clientTrustStore, CLIENT_PASSWORD);
139+
140+
// adding certificates in trust stores
141+
addTrustedCertificate(serverTrustStore, SERVER_PASSWORD, clientCertificate);
142+
addTrustedCertificate(clientTrustStore, CLIENT_PASSWORD, serverCertificate);
143+
144+
connectionTest(
145+
app ->
146+
app
147+
.withJmxPort(JMX_PORT)
148+
.withJmxSsl()
149+
.withClientSslCertificate()
150+
.withKeyStore(serverKeystore, SERVER_PASSWORD)
151+
.withTrustStore(serverTrustStore, SERVER_PASSWORD),
152+
scraper ->
153+
scraper
154+
.withRmiServiceUrl(APP_HOST, JMX_PORT)
155+
.withKeyStore(clientKeystore, CLIENT_PASSWORD)
156+
.withTrustStore(clientTrustStore, CLIENT_PASSWORD)
157+
);
114158
}
115159

116160
private static void connectionTest(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class TestAppContainer extends GenericContainer<TestAppContainer> {
3535
private String trustStorePassword;
3636
private int jmxPort;
3737
private int jmxRmiPort;
38+
private boolean clientCertificate;
3839

3940
public TestAppContainer() {
4041
super("openjdk:8u272-jre-slim");
@@ -82,6 +83,12 @@ public TestAppContainer withSslRmiRegistry(int registryPort) {
8283
return this;
8384
}
8485

86+
@CanIgnoreReturnValue
87+
public TestAppContainer withClientSslCertificate() {
88+
this.clientCertificate = true;
89+
return this;
90+
}
91+
8592
@CanIgnoreReturnValue
8693
public TestAppContainer withKeyStore(Path keyStore, String password) {
8794
this.keyStore = keyStore;
@@ -110,6 +117,9 @@ public void start() {
110117
"RMI with SSL registry requires a distinct port from JMX: " + jmxRmiPort);
111118
}
112119
}
120+
if (jmxSsl && clientCertificate) {
121+
properties.put("com.sun.management.jmxremote.ssl.need.client.auth", "true");
122+
}
113123

114124
if (pwd == null) {
115125
properties.put("com.sun.management.jmxremote.authenticate", "false");

0 commit comments

Comments
 (0)