56002
|
1 |
import org.testng.annotations.AfterClass;
|
|
2 |
import org.testng.annotations.BeforeClass;
|
|
3 |
import org.testng.annotations.Test;
|
|
4 |
|
56026
|
5 |
import jdk.internal.management.remote.rest.PlatformRestAdapter;
|
56002
|
6 |
import javax.net.ssl.HttpsURLConnection;
|
|
7 |
import javax.net.ssl.KeyManagerFactory;
|
|
8 |
import javax.net.ssl.SSLContext;
|
|
9 |
import javax.net.ssl.TrustManagerFactory;
|
|
10 |
import java.io.*;
|
56006
|
11 |
import java.lang.reflect.Method;
|
56002
|
12 |
import java.net.InetAddress;
|
|
13 |
import java.net.UnknownHostException;
|
|
14 |
import java.security.KeyStore;
|
|
15 |
import java.util.Properties;
|
56006
|
16 |
import java.util.Set;
|
|
17 |
import java.util.stream.Collectors;
|
|
18 |
import java.util.stream.Stream;
|
56003
|
19 |
|
|
20 |
/* @test
|
|
21 |
* @summary Configuration test for rest adapter
|
|
22 |
* @library /test/lib
|
56026
|
23 |
* @modules jdk.management.rest/jdk.internal.management.remote.rest.http
|
|
24 |
* jdk.management.rest/jdk.internal.management.remote.rest.json
|
|
25 |
* jdk.management.rest/jdk.internal.management.remote.rest.json.parser
|
|
26 |
* jdk.management.rest/jdk.internal.management.remote.rest.mapper
|
|
27 |
* jdk.management.rest/jdk.internal.management.remote.rest
|
56003
|
28 |
* @build RestAdapterConfigTest RestAdapterTest
|
|
29 |
* @run testng/othervm RestAdapterConfigTest
|
|
30 |
*/
|
56002
|
31 |
|
|
32 |
@Test
|
|
33 |
public class RestAdapterConfigTest {
|
|
34 |
private static String sslAgentConfig;
|
|
35 |
private static String sslClientConfig;
|
|
36 |
private static String passwordFile;
|
|
37 |
private static String configFile;
|
56006
|
38 |
private static RestAdapterTest restAdapterTest = new RestAdapterTest();
|
|
39 |
private static final Set<Method> tests;
|
56002
|
40 |
|
|
41 |
static {
|
56006
|
42 |
tests = Stream.of(RestAdapterTest.class.getMethods())
|
|
43 |
.filter(a -> a.getName().startsWith("test"))
|
|
44 |
.collect(Collectors.toSet());
|
56002
|
45 |
}
|
|
46 |
|
|
47 |
private void createAgentSslConfigFile(String fileName) throws IOException {
|
|
48 |
File f = new File(fileName);
|
|
49 |
if (f.exists()) {
|
|
50 |
f.delete();
|
|
51 |
}
|
|
52 |
|
|
53 |
Properties props = new Properties();
|
|
54 |
String testDir = System.getProperty("test.src");
|
|
55 |
props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
|
|
56 |
props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
|
|
57 |
props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
|
|
58 |
props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
|
|
59 |
|
|
60 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
61 |
props.store(writer, "");
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
private void createClientSslConfigFile(String fileName) throws IOException {
|
|
66 |
File f = new File(fileName);
|
|
67 |
if (f.exists()) {
|
|
68 |
f.delete();
|
|
69 |
}
|
|
70 |
Properties props = new Properties();
|
|
71 |
String testDir = System.getProperty("test.src");
|
|
72 |
props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
|
|
73 |
props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
|
|
74 |
props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
|
|
75 |
props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
|
|
76 |
|
|
77 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
78 |
props.store(writer, "");
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
private void setupMgmtConfig(String fileName, boolean isSSL, boolean isAuth) throws IOException {
|
|
83 |
Properties props = new Properties();
|
|
84 |
File f = new File(fileName);
|
|
85 |
if (f.exists()) {
|
|
86 |
f.delete();
|
|
87 |
}
|
|
88 |
|
|
89 |
props.setProperty("com.sun.management.jmxremote.ssl", isSSL ? "true" : "false");
|
|
90 |
if(isSSL) {
|
|
91 |
props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
|
|
92 |
}
|
|
93 |
props.setProperty("com.sun.management.jmxremote.authenticate", isAuth ? "true" : "false");
|
|
94 |
if (isAuth) {
|
|
95 |
props.setProperty("com.sun.management.jmxremote.password.file", passwordFile);
|
|
96 |
}
|
|
97 |
props.setProperty("com.sun.management.jmxremote.rest.port", "0");
|
|
98 |
|
|
99 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
100 |
props.store(writer, "");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
@BeforeClass
|
|
105 |
public void init() throws Exception {
|
|
106 |
String testSrcRoot = System.getProperty("test.src") + File.separator;
|
|
107 |
sslAgentConfig = testSrcRoot + "sslConfigAgent";
|
|
108 |
sslClientConfig = testSrcRoot + "sslConfigClient";
|
|
109 |
passwordFile = testSrcRoot + "password.properties";
|
|
110 |
configFile = testSrcRoot + "mgmt.properties";
|
|
111 |
|
|
112 |
createAgentSslConfigFile(sslAgentConfig);
|
|
113 |
createClientSslConfigFile(sslClientConfig);
|
|
114 |
|
|
115 |
SSLContext ctx = getSSlContext(sslClientConfig);
|
|
116 |
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
|
|
117 |
HttpsURLConnection.setDefaultHostnameVerifier(
|
|
118 |
(String hostname, javax.net.ssl.SSLSession sslSession) -> {
|
|
119 |
try {
|
|
120 |
return hostname.equals(InetAddress.getLocalHost().getHostName());
|
|
121 |
} catch (UnknownHostException ex) {
|
|
122 |
throw new RuntimeException(ex);
|
|
123 |
}
|
|
124 |
});
|
|
125 |
}
|
|
126 |
|
|
127 |
private static SSLContext getSSlContext(String sslConfigFileName) {
|
|
128 |
final String keyStore, keyStorePassword, trustStore, trustStorePassword;
|
|
129 |
|
|
130 |
try {
|
|
131 |
Properties p = new Properties();
|
|
132 |
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
|
|
133 |
p.load(bin);
|
|
134 |
keyStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_FILE);
|
|
135 |
keyStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_PASSWORD);
|
|
136 |
trustStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_FILE);
|
|
137 |
trustStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_PASSWORD);
|
|
138 |
|
|
139 |
char[] keyStorePasswd = null;
|
|
140 |
if (keyStorePassword.length() != 0) {
|
|
141 |
keyStorePasswd = keyStorePassword.toCharArray();
|
|
142 |
}
|
|
143 |
|
|
144 |
char[] trustStorePasswd = null;
|
|
145 |
if (trustStorePassword.length() != 0) {
|
|
146 |
trustStorePasswd = trustStorePassword.toCharArray();
|
|
147 |
}
|
|
148 |
|
|
149 |
KeyStore ks = null;
|
|
150 |
if (keyStore != null) {
|
|
151 |
ks = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
152 |
FileInputStream ksfis = new FileInputStream(keyStore);
|
|
153 |
ks.load(ksfis, keyStorePasswd);
|
|
154 |
|
|
155 |
}
|
|
156 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
|
|
157 |
KeyManagerFactory.getDefaultAlgorithm());
|
|
158 |
kmf.init(ks, keyStorePasswd);
|
|
159 |
|
|
160 |
KeyStore ts = null;
|
|
161 |
if (trustStore != null) {
|
|
162 |
ts = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
163 |
FileInputStream tsfis = new FileInputStream(trustStore);
|
|
164 |
ts.load(tsfis, trustStorePasswd);
|
|
165 |
}
|
|
166 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
|
|
167 |
TrustManagerFactory.getDefaultAlgorithm());
|
|
168 |
tmf.init(ts);
|
|
169 |
|
|
170 |
SSLContext ctx = SSLContext.getInstance("SSL");
|
|
171 |
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
172 |
return ctx;
|
|
173 |
} catch (Exception ex) {
|
|
174 |
}
|
|
175 |
return null;
|
|
176 |
}
|
|
177 |
|
|
178 |
@Test
|
|
179 |
public void testHttpNoAuth() throws Exception {
|
|
180 |
setupMgmtConfig(configFile, false, false);
|
56006
|
181 |
restAdapterTest.setupServers();
|
|
182 |
for (Method m : tests) {
|
|
183 |
m.invoke(restAdapterTest);
|
|
184 |
}
|
|
185 |
restAdapterTest.tearDownServers();
|
56002
|
186 |
}
|
|
187 |
|
|
188 |
public void testHttpsNoAuth() throws Exception {
|
|
189 |
setupMgmtConfig(configFile, true, false);
|
56006
|
190 |
restAdapterTest.setupServers();
|
|
191 |
for (Method m : tests) {
|
|
192 |
m.invoke(restAdapterTest);
|
|
193 |
}
|
|
194 |
restAdapterTest.tearDownServers();
|
56002
|
195 |
}
|
|
196 |
|
|
197 |
public void testHttpAuth() throws Exception {
|
|
198 |
setupMgmtConfig(configFile, false, true);
|
56006
|
199 |
restAdapterTest.setupServers();
|
|
200 |
for (Method m : tests) {
|
|
201 |
m.invoke(restAdapterTest);
|
|
202 |
}
|
|
203 |
restAdapterTest.tearDownServers();
|
56002
|
204 |
}
|
|
205 |
|
|
206 |
public void testHttpsAuth() throws Exception {
|
|
207 |
setupMgmtConfig(configFile, true, true);
|
56006
|
208 |
restAdapterTest.setupServers();
|
|
209 |
for (Method m : tests) {
|
|
210 |
m.invoke(restAdapterTest);
|
|
211 |
}
|
|
212 |
restAdapterTest.tearDownServers();
|
56002
|
213 |
}
|
|
214 |
|
|
215 |
@AfterClass
|
|
216 |
public void tearDown() {
|
|
217 |
File f = new File(sslAgentConfig);
|
|
218 |
if (f.exists())
|
|
219 |
f.delete();
|
|
220 |
f = new File(sslClientConfig);
|
|
221 |
if (f.exists())
|
|
222 |
f.delete();
|
|
223 |
f = new File(configFile);
|
|
224 |
if (f.exists())
|
|
225 |
f.delete();
|
|
226 |
}
|
56006
|
227 |
|
56002
|
228 |
}
|