forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestAppContainer.java
More file actions
234 lines (203 loc) · 6.91 KB
/
TestAppContainer.java
File metadata and controls
234 lines (203 loc) · 6.91 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
/*
* 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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.shaded.com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.testcontainers.utility.MountableFile;
/** Test container that allows to execute TestApp in an isolated container */
public class TestAppContainer extends GenericContainer<TestAppContainer> {
private final Map<String, String> properties;
private String login;
private String pwd;
private boolean jmxSsl;
private boolean jmxSslRegistry;
private TestKeyStore keyStore;
private TestKeyStore trustStore;
private int jmxPort;
private int jmxRmiPort;
private boolean clientCertificate;
@SuppressWarnings("this-escape")
public TestAppContainer() {
super("openjdk:8u272-jre-slim");
this.properties = new HashMap<>();
String appJar = System.getProperty("app.jar.path");
assertThat(Paths.get(appJar)).isNotEmptyFile().isReadable();
this.withCopyFileToContainer(MountableFile.forHostPath(appJar), "/app.jar")
.waitingFor(
Wait.forLogMessage("app started\\n", 1).withStartupTimeout(Duration.ofSeconds(5)))
.withCommand("java", "-jar", "/app.jar");
}
/**
* Configures app container for container-to-container access
*
* @param port mapped port to use
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withJmxPort(int port) {
this.jmxPort = port;
return this;
}
/**
* Enables and configure JMX login/pwd authentication
*
* @param login user login
* @param pwd user password
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withUserAuth(String login, String pwd) {
this.login = login;
this.pwd = pwd;
return this;
}
/**
* Enables SSL for JMX endpoint, will require JMX client to trust remote JVM certificate
*
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withJmxSsl() {
this.jmxSsl = true;
return this;
}
/**
* Enables SSL-protected RMI registry, which requires a distinct port from JMX
*
* @param registryPort registry port
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withSslRmiRegistry(int registryPort) {
this.jmxSslRegistry = true;
this.jmxRmiPort = registryPort;
return this;
}
/**
* Enables client certificate verification by the remote JVM
*
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withClientSslCertificate() {
this.clientCertificate = true;
return this;
}
/**
* Configure key store for the remote JVM
*
* @param keyStore key store
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withKeyStore(TestKeyStore keyStore) {
this.keyStore = keyStore;
return this;
}
/**
* Configure trust store for the remote JVM
*
* @param trustStore trust store
* @return this
*/
@CanIgnoreReturnValue
public TestAppContainer withTrustStore(TestKeyStore trustStore) {
this.trustStore = trustStore;
return this;
}
@Override
public void start() {
properties.put("com.sun.management.jmxremote.port", Integer.toString(jmxPort));
properties.put("com.sun.management.jmxremote.ssl", Boolean.toString(jmxSsl));
if (jmxSslRegistry) {
properties.put("com.sun.management.jmxremote.registry.ssl", "true");
properties.put("com.sun.management.jmxremote.rmi.port", Integer.toString(jmxRmiPort));
if (jmxRmiPort == jmxPort) {
// making it harder to attempt using the same port
// doing so results in a "port busy" error which can be confusing
throw new IllegalStateException(
"RMI with SSL registry requires a distinct port from JMX: " + jmxRmiPort);
}
}
if (jmxSsl && clientCertificate) {
properties.put("com.sun.management.jmxremote.ssl.need.client.auth", "true");
}
if (pwd == null) {
// no authentication
properties.put("com.sun.management.jmxremote.authenticate", "false");
} else {
// authentication enabled
properties.put("com.sun.management.jmxremote.authenticate", "true");
Path pwdFile = createPwdFile(login, pwd);
this.withCopyFileToContainer(MountableFile.forHostPath(pwdFile), "/jmx.password");
properties.put("com.sun.management.jmxremote.password.file", "/jmx.password");
Path accessFile = createAccessFile(login);
this.withCopyFileToContainer(MountableFile.forHostPath(accessFile), "/jmx.access");
properties.put("com.sun.management.jmxremote.access.file", "/jmx.access");
}
// add optional key and trust stores
addSecureStore(keyStore, /* isKeyStore= */ true, properties);
addSecureStore(trustStore, /* isKeyStore= */ false, properties);
String confArgs =
properties.entrySet().stream()
.map(
e -> {
String s = "-D" + e.getKey();
if (!e.getValue().isEmpty()) {
s += "=" + e.getValue();
}
return s;
})
.collect(Collectors.joining(" "));
this.withEnv("JAVA_TOOL_OPTIONS", confArgs);
logger().info("Test application JAVA_TOOL_OPTIONS = {}", confArgs);
super.start();
}
private void addSecureStore(
TestKeyStore keyStore, boolean isKeyStore, Map<String, String> properties) {
if (keyStore == null) {
return;
}
Path path = keyStore.getPath();
String containerPath = "/" + path.getFileName().toString();
this.withCopyFileToContainer(MountableFile.forHostPath(path), containerPath);
String prefix = String.format("javax.net.ssl.%sStore", isKeyStore ? "key" : "trust");
properties.put(prefix, containerPath);
properties.put(prefix + "Password", keyStore.getPassword());
}
private static Path createPwdFile(String login, String pwd) {
try {
Path path = Files.createTempFile("test", ".pwd");
writeLine(path, String.format("%s %s", login, pwd));
return path;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Path createAccessFile(String login) {
try {
Path path = Files.createTempFile("test", ".pwd");
writeLine(path, String.format("%s %s", login, "readwrite"));
return path;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void writeLine(Path path, String line) throws IOException {
line = line + "\n";
Files.write(path, line.getBytes(StandardCharsets.UTF_8));
}
}