55994
|
1 |
|
|
2 |
import java.io.BufferedInputStream;
|
|
3 |
import java.io.BufferedReader;
|
|
4 |
import java.io.BufferedWriter;
|
|
5 |
import java.io.File;
|
|
6 |
import java.io.FileInputStream;
|
|
7 |
import java.io.FileWriter;
|
|
8 |
import java.io.IOException;
|
|
9 |
import java.io.InputStreamReader;
|
|
10 |
import java.net.URL;
|
|
11 |
import java.net.URLDecoder;
|
|
12 |
import java.net.URLEncoder;
|
|
13 |
import java.security.KeyStore;
|
|
14 |
import java.util.Base64;
|
|
15 |
import java.util.Properties;
|
|
16 |
import java.util.logging.Level;
|
|
17 |
import java.util.logging.Logger;
|
|
18 |
import javax.management.remote.rest.PlatformRestAdapter;
|
|
19 |
import javax.net.ssl.HttpsURLConnection;
|
|
20 |
import javax.net.ssl.KeyManagerFactory;
|
|
21 |
import javax.net.ssl.SSLContext;
|
|
22 |
import javax.net.ssl.TrustManagerFactory;
|
|
23 |
import org.testng.annotations.Test;
|
|
24 |
import org.testng.Assert;
|
|
25 |
import org.testng.annotations.AfterClass;
|
|
26 |
import org.testng.annotations.BeforeClass;
|
|
27 |
import org.testng.annotations.DataProvider;
|
|
28 |
import org.testng.annotations.Test;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* @test @modules java.logging java.management java.management.rest
|
|
32 |
* @run testng/othervm RestAdapterTest
|
|
33 |
*
|
|
34 |
*/
|
|
35 |
@Test
|
|
36 |
public class RestAdapterTest {
|
|
37 |
|
|
38 |
private static final String CHARSET = "UTF-8";
|
|
39 |
private static String sslAgentConfig;
|
|
40 |
private static String sslClientConfig;
|
|
41 |
private static String passwordFile;
|
|
42 |
private static String configFile;
|
|
43 |
|
|
44 |
private void createAgentSslConfigFile(String fileName) throws IOException {
|
|
45 |
File f = new File(fileName);
|
|
46 |
if (f.exists()) {
|
|
47 |
return;
|
|
48 |
}
|
|
49 |
Properties props = new Properties();
|
|
50 |
String testDir = System.getProperty("test.src");
|
|
51 |
props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
|
|
52 |
props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
|
|
53 |
props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
|
|
54 |
props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
|
|
55 |
|
|
56 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
57 |
props.store(writer, "");
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
private void createClientSslConfigFile(String fileName) throws IOException {
|
|
62 |
File f = new File(fileName);
|
|
63 |
if (f.exists()) {
|
|
64 |
return;
|
|
65 |
}
|
|
66 |
Properties props = new Properties();
|
|
67 |
String testDir = System.getProperty("test.src");
|
|
68 |
props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
|
|
69 |
props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
|
|
70 |
props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
|
|
71 |
props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
|
|
72 |
|
|
73 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
74 |
props.store(writer, "");
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
private void setupMgmtConfig(String fileName) throws IOException {
|
|
79 |
Properties props = new Properties();
|
|
80 |
|
|
81 |
props.setProperty("com.sun.management.jmxremote.ssl", "true");
|
|
82 |
props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
|
|
83 |
props.setProperty("com.sun.management.jmxremote.password.file", passwordFile);
|
|
84 |
props.setProperty("com.sun.management.jmxremote.rest.port", "0");
|
|
85 |
|
|
86 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
|
|
87 |
props.store(writer, "");
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
private void setupConfig() throws Exception {
|
|
92 |
String testSrcRoot = System.getProperty("test.src") + File.separator;
|
|
93 |
sslAgentConfig = testSrcRoot + "sslConfigAgent";
|
|
94 |
sslClientConfig = testSrcRoot + "sslConfigClient";
|
|
95 |
passwordFile = testSrcRoot + "password.properties";
|
|
96 |
configFile = testSrcRoot + "mgmt.properties";
|
|
97 |
createAgentSslConfigFile(sslAgentConfig);
|
|
98 |
createClientSslConfigFile(sslClientConfig);
|
|
99 |
setupMgmtConfig(configFile);
|
|
100 |
}
|
|
101 |
|
|
102 |
@BeforeClass
|
|
103 |
public void setupAdapter() throws Exception {
|
|
104 |
setupConfig();
|
|
105 |
File file = new File(configFile);
|
|
106 |
Properties props = new Properties();
|
|
107 |
props.load(new FileInputStream(file));
|
|
108 |
if (props.get("com.sun.management.jmxremote.rest.port") != null) {
|
|
109 |
PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
|
|
110 |
}
|
|
111 |
SSLContext ctx = getSSlContext(sslClientConfig);
|
|
112 |
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
|
|
113 |
HttpsURLConnection.setDefaultHostnameVerifier(
|
|
114 |
(String hostname, javax.net.ssl.SSLSession sslSession) -> hostname.equals("Harsha-Wardhana-B"));
|
|
115 |
}
|
|
116 |
|
|
117 |
@AfterClass
|
|
118 |
public void tearDown() {
|
|
119 |
PlatformRestAdapter.stop();
|
|
120 |
}
|
|
121 |
|
|
122 |
private static SSLContext getSSlContext(String sslConfigFileName) {
|
|
123 |
final String keyStore, keyStorePassword, trustStore, trustStorePassword;
|
|
124 |
|
|
125 |
try {
|
|
126 |
Properties p = new Properties();
|
|
127 |
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
|
|
128 |
p.load(bin);
|
|
129 |
keyStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_FILE);
|
|
130 |
keyStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_PASSWORD);
|
|
131 |
trustStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_FILE);
|
|
132 |
trustStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_PASSWORD);
|
|
133 |
|
|
134 |
char[] keyStorePasswd = null;
|
|
135 |
if (keyStorePassword.length() != 0) {
|
|
136 |
keyStorePasswd = keyStorePassword.toCharArray();
|
|
137 |
}
|
|
138 |
|
|
139 |
char[] trustStorePasswd = null;
|
|
140 |
if (trustStorePassword.length() != 0) {
|
|
141 |
trustStorePasswd = trustStorePassword.toCharArray();
|
|
142 |
}
|
|
143 |
|
|
144 |
KeyStore ks = null;
|
|
145 |
if (keyStore != null) {
|
|
146 |
ks = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
147 |
FileInputStream ksfis = new FileInputStream(keyStore);
|
|
148 |
ks.load(ksfis, keyStorePasswd);
|
|
149 |
}
|
|
150 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
|
|
151 |
KeyManagerFactory.getDefaultAlgorithm());
|
|
152 |
kmf.init(ks, keyStorePasswd);
|
|
153 |
|
|
154 |
KeyStore ts = null;
|
|
155 |
if (trustStore != null) {
|
|
156 |
ts = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
157 |
FileInputStream tsfis = new FileInputStream(trustStore);
|
|
158 |
ts.load(tsfis, trustStorePasswd);
|
|
159 |
}
|
|
160 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
|
|
161 |
TrustManagerFactory.getDefaultAlgorithm());
|
|
162 |
tmf.init(ts);
|
|
163 |
|
|
164 |
SSLContext ctx = SSLContext.getInstance("SSL");
|
|
165 |
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
166 |
return ctx;
|
|
167 |
} catch (Exception ex) {
|
|
168 |
Logger.getLogger(PlatformRestAdapter.class.getName()).log(Level.SEVERE, null, ex);
|
|
169 |
}
|
|
170 |
return null;
|
|
171 |
}
|
|
172 |
|
|
173 |
private String executeHttpRequest(String inputUrl, String charset) throws IOException {
|
|
174 |
if (inputUrl != null) {
|
|
175 |
URL url = new URL(PlatformRestAdapter.getBaseURL() + (charset != null ? URLEncoder.encode(inputUrl, charset) : inputUrl));
|
|
176 |
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
177 |
con.setDoOutput(false);
|
|
178 |
String userCredentials = "username1:password1";
|
|
179 |
String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
|
|
180 |
con.setRequestProperty("Authorization", basicAuth);
|
|
181 |
if (charset != null && !charset.isEmpty()) {
|
|
182 |
con.setRequestProperty("Content-Type", "application/json; charset=" + charset);
|
|
183 |
}
|
|
184 |
try {
|
|
185 |
int status = con.getResponseCode();
|
|
186 |
if (status == 200) {
|
|
187 |
|
|
188 |
StringBuilder sbuf;
|
|
189 |
try (BufferedReader br = new BufferedReader(
|
|
190 |
new InputStreamReader(con.getInputStream()))) {
|
|
191 |
sbuf = new StringBuilder();
|
|
192 |
String input;
|
|
193 |
while ((input = br.readLine()) != null) {
|
|
194 |
sbuf.append(charset != null ? URLDecoder.decode(input, charset) : input);
|
|
195 |
}
|
|
196 |
}
|
|
197 |
return sbuf.toString();
|
|
198 |
} else {
|
|
199 |
StringBuilder sbuf;
|
|
200 |
try (BufferedReader br = new BufferedReader(
|
|
201 |
new InputStreamReader(con.getErrorStream()))) {
|
|
202 |
sbuf = new StringBuilder();
|
|
203 |
String input;
|
|
204 |
while ((input = br.readLine()) != null) {
|
|
205 |
sbuf.append(charset != null ? URLDecoder.decode(input, charset) : input);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
return sbuf.toString();
|
|
209 |
}
|
|
210 |
} catch (IOException e) {
|
|
211 |
}
|
|
212 |
}
|
|
213 |
return null;
|
|
214 |
}
|
|
215 |
|
|
216 |
@Test
|
|
217 |
public void testMBeanServerCollection() throws IOException {
|
|
218 |
System.out.println(executeHttpRequest("/platform/mbeans/java.lang:type=Runtime",null));
|
|
219 |
// System.out.println(executeHttpRequest("/platform/mbeans/java.lang:name=CodeHeap%20'non-nmethods',type=MemoryPool/info", null));
|
|
220 |
}
|
|
221 |
}
|