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