forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJmxConnectionTest.java
More file actions
243 lines (207 loc) · 8.57 KB
/
JmxConnectionTest.java
File metadata and controls
243 lines (207 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.jmxscraper;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.file.Path;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
/**
* Tests all supported ways to connect to remote JMX interface. This indirectly tests
* JmxConnectionBuilder and relies on containers to minimize the JMX/RMI network complications which
* are not NAT-friendly.
*/
public class JmxConnectionTest {
// OTLP endpoint is not used in test mode, but still has to be provided
private static final String DUMMY_OTLP_ENDPOINT = "http://dummy-otlp-endpoint:8080/";
private static final String SCRAPER_BASE_IMAGE = "openjdk:8u342-jre-slim";
private static final int JMX_PORT = 9999;
private static final String APP_HOST = "app";
// key/trust stores passwords
private static final String CLIENT_PASSWORD = "client";
private static final String SERVER_PASSWORD = "server";
private static final Logger jmxScraperLogger = LoggerFactory.getLogger("JmxScraperContainer");
private static final Logger appLogger = LoggerFactory.getLogger("TestAppContainer");
private static Network network;
// temporary folder for files that are copied to container
@TempDir private Path tempDir;
@BeforeAll
static void beforeAll() {
network = Network.newNetwork();
}
@AfterAll
static void afterAll() {
network.close();
}
@Test
void connectionError() {
try (JmxScraperContainer scraper = scraperContainer().withRmiServiceUrl("unknown_host", 1234)) {
scraper.start();
waitTerminated(scraper);
checkConnectionLogs(scraper, /* expectedOk= */ false);
}
}
@ParameterizedTest
@EnumSource
void connectNoAuth(JmxScraperContainer.ConfigSource configSource) {
connectionTest(
app -> app.withJmxPort(JMX_PORT),
scraper -> scraper.withRmiServiceUrl(APP_HOST, JMX_PORT).withConfigSource(configSource));
}
@ParameterizedTest
@EnumSource
void userPassword(JmxScraperContainer.ConfigSource configSource) {
String login = "user";
String pwd = "t0p!Secret";
connectionTest(
app -> app.withJmxPort(JMX_PORT).withUserAuth(login, pwd),
scraper ->
scraper
.withRmiServiceUrl(APP_HOST, JMX_PORT)
.withUser(login)
.withPassword(pwd)
.withConfigSource(configSource));
}
@ParameterizedTest
@EnumSource
void serverSsl(JmxScraperContainer.ConfigSource configSource) {
testServerSsl(/* sslRmiRegistry= */ false, configSource);
}
@ParameterizedTest
@EnumSource
void serverSslWithSslRmiRegistry(JmxScraperContainer.ConfigSource configSource) {
testServerSsl(/* sslRmiRegistry= */ true, configSource);
}
private void testServerSsl(
boolean sslRmiRegistry, JmxScraperContainer.ConfigSource configSource) {
// two keystores:
// server keystore with public/private key pair
// client trust store with certificate from server
TestKeyStore serverKeyStore =
TestKeyStore.newKeyStore(tempDir.resolve("server.jks"), SERVER_PASSWORD);
TestKeyStore clientTrustStore =
TestKeyStore.newKeyStore(tempDir.resolve("client.jks"), CLIENT_PASSWORD);
X509Certificate serverCertificate = serverKeyStore.addKeyPair();
clientTrustStore.addTrustedCertificate(serverCertificate);
connectionTest(
app ->
(sslRmiRegistry ? app.withSslRmiRegistry(4242) : app)
.withJmxPort(JMX_PORT)
.withJmxSsl()
.withKeyStore(serverKeyStore),
scraper ->
(sslRmiRegistry ? scraper.withSslRmiRegistry() : scraper)
.withRmiServiceUrl(APP_HOST, JMX_PORT)
.withTrustStore(clientTrustStore)
.withConfigSource(configSource));
}
@ParameterizedTest
@EnumSource(value = JmxScraperContainer.ConfigSource.class)
void serverSslClientSsl(JmxScraperContainer.ConfigSource configSource) {
// Note: this could have been made simpler by relying on the fact that keystore could be used
// as a trust store, but having clear split provides also some extra clarity
//
// 4 keystores:
// server keystore with public/private key pair
// server truststore with client certificate
// client key store with public/private key pair
// client trust store with certificate from server
TestKeyStore serverKeyStore =
TestKeyStore.newKeyStore(tempDir.resolve("server-keystore.jks"), SERVER_PASSWORD);
TestKeyStore serverTrustStore =
TestKeyStore.newKeyStore(tempDir.resolve("server-truststore.jks"), SERVER_PASSWORD);
X509Certificate serverCertificate = serverKeyStore.addKeyPair();
TestKeyStore clientKeyStore =
TestKeyStore.newKeyStore(tempDir.resolve("client-keystore.jks"), CLIENT_PASSWORD);
TestKeyStore clientTrustStore =
TestKeyStore.newKeyStore(tempDir.resolve("client-truststore.jks"), CLIENT_PASSWORD);
X509Certificate clientCertificate = clientKeyStore.addKeyPair();
// adding certificates in trust stores
clientTrustStore.addTrustedCertificate(serverCertificate);
serverTrustStore.addTrustedCertificate(clientCertificate);
connectionTest(
app ->
app.withJmxPort(JMX_PORT)
.withJmxSsl()
.withClientSslCertificate()
.withKeyStore(serverKeyStore)
.withTrustStore(serverTrustStore),
scraper ->
scraper
.withRmiServiceUrl(APP_HOST, JMX_PORT)
.withKeyStore(clientKeyStore)
.withTrustStore(clientTrustStore)
.withConfigSource(configSource));
}
private static void connectionTest(
Function<TestAppContainer, TestAppContainer> customizeApp,
Function<JmxScraperContainer, JmxScraperContainer> customizeScraper) {
try (TestAppContainer app = customizeApp.apply(appContainer())) {
app.start();
try (JmxScraperContainer scraper = customizeScraper.apply(scraperContainer())) {
scraper.start();
waitTerminated(scraper);
checkConnectionLogs(scraper, /* expectedOk= */ true);
}
}
}
private static void checkConnectionLogs(JmxScraperContainer scraper, boolean expectedOk) {
String[] logLines = scraper.getLogs().split("\n");
// usually only the last line can be checked, however when it fails with an exception
// the stack trace is last in the output, so it's simpler to check all lines of log output
assertThat(logLines)
.anySatisfy(
line -> {
if (expectedOk) {
assertThat(line)
.describedAs("should log connection success")
.contains("JMX connection test OK");
} else {
assertThat(line)
.describedAs("should log connection failure")
.contains("JMX connection test ERROR");
}
});
}
private static void waitTerminated(GenericContainer<?> container) {
long retryUntil = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
while (container.isRunning() && System.currentTimeMillis() < retryUntil) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
assertThat(container.isRunning())
.describedAs("container should stop when testing connection")
.isFalse();
}
private static JmxScraperContainer scraperContainer() {
return new JmxScraperContainer(DUMMY_OTLP_ENDPOINT, SCRAPER_BASE_IMAGE)
.withLogConsumer(new Slf4jLogConsumer(jmxScraperLogger))
.withNetwork(network)
// mandatory to have a target system even if we don't collect metrics
.withTargetSystem("jvm")
// we are only testing JMX connection here
.withTestJmx();
}
private static TestAppContainer appContainer() {
return new TestAppContainer()
.withLogConsumer(new Slf4jLogConsumer(appLogger))
.withNetwork(network)
.withNetworkAliases(APP_HOST);
}
}