test/jdk/javax/management/remote/rest/PlatformAdapterTest.java
branchjmx-rest-api
changeset 55994 9721e36abeb0
equal deleted inserted replaced
55987:0daf2ad15492 55994:9721e36abeb0
       
     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.HttpURLConnection;
       
    11 import java.net.URL;
       
    12 import java.security.KeyStore;
       
    13 import java.security.cert.Certificate;
       
    14 import java.util.Base64;
       
    15 import java.util.Properties;
       
    16 import javax.management.remote.rest.JmxRestAdapter;
       
    17 import javax.management.remote.rest.PlatformRestAdapter;
       
    18 import javax.net.ssl.HttpsURLConnection;
       
    19 import javax.net.ssl.KeyManagerFactory;
       
    20 import javax.net.ssl.SSLContext;
       
    21 import javax.net.ssl.SSLPeerUnverifiedException;
       
    22 import javax.net.ssl.TrustManagerFactory;
       
    23 import org.testng.annotations.Test;
       
    24 import org.testng.annotations.BeforeClass;
       
    25 
       
    26 /**
       
    27  * @test 
       
    28  * @modules java.management.rest
       
    29  * @run testng/othervm PlatformAdapterTest
       
    30  */
       
    31 @Test
       
    32 public class PlatformAdapterTest {
       
    33 
       
    34     private static final String MBEANS = "mbeans";
       
    35     private static String sslAgentConfig;
       
    36     private static String sslClientConfig;
       
    37     private static String passwordFile;
       
    38     
       
    39 
       
    40     @BeforeClass
       
    41     public void setup() throws IOException {
       
    42         String testSrcRoot = System.getProperty("test.src") + File.separator;
       
    43         sslAgentConfig = testSrcRoot + "sslConfigAgent";
       
    44         sslClientConfig = testSrcRoot + "sslConfigClient";
       
    45         passwordFile = testSrcRoot + "password.properties";
       
    46         createAgentSslConfigFile(sslAgentConfig);
       
    47         createClientSslConfigFile(sslClientConfig);
       
    48     }
       
    49     
       
    50     @Test
       
    51     public void testHttpNoAuth() throws Exception {
       
    52         Properties props = new Properties();
       
    53         props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
       
    54         props.setProperty("com.sun.management.jmxremote.ssl", "false");
       
    55         props.setProperty("com.sun.management.jmxremote.authenticate", "false");
       
    56         if (props.get("com.sun.management.jmxremote.rest.port") != null) {
       
    57             PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
       
    58         }
       
    59 
       
    60         JmxRestAdapter adapter = PlatformRestAdapter.getInstance();
       
    61         adapter.start();
       
    62         URL url = new URL(adapter.getBaseUrl() + MBEANS);
       
    63         HttpURLConnection con = (HttpURLConnection) url.openConnection();
       
    64         con.setDoOutput(false);
       
    65         print_content(con);
       
    66         PlatformRestAdapter.stop();
       
    67     }
       
    68 
       
    69     @Test
       
    70     public void testHttpAuth() throws Exception {
       
    71         Properties props = new Properties();
       
    72         props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
       
    73         props.setProperty("com.sun.management.jmxremote.ssl", "false");
       
    74         props.setProperty("com.sun.management.jmxremote.authenticate", "true");
       
    75         props.setProperty("com.sun.management.jmxremote.password.file", passwordFile);
       
    76         if (props.get("com.sun.management.jmxremote.rest.port") != null) {
       
    77             PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
       
    78         }
       
    79 
       
    80         JmxRestAdapter adapter = PlatformRestAdapter.getInstance();
       
    81         adapter.start();
       
    82         URL url = new URL(adapter.getBaseUrl() + MBEANS);
       
    83         HttpURLConnection con = (HttpURLConnection) url.openConnection();
       
    84         con.setDoOutput(false);
       
    85 
       
    86         String userCredentials = "username1:password1";
       
    87 
       
    88         String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
       
    89         con.setRequestProperty("Authorization", basicAuth);
       
    90         print_content(con);
       
    91 
       
    92         PlatformRestAdapter.stop();
       
    93     }
       
    94 
       
    95     private void createAgentSslConfigFile(String fileName) throws IOException {
       
    96         Properties props = new Properties();
       
    97         String testDir = System.getProperty("test.src");
       
    98         props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
       
    99         props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
       
   100         props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
       
   101         props.setProperty("javax.net.ssl.trustStorePassword","glopglop");
       
   102 
       
   103         try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
       
   104             props.store(writer, "");
       
   105         }
       
   106     }
       
   107 
       
   108     private void createClientSslConfigFile(String fileName) throws IOException {
       
   109         Properties props = new Properties();
       
   110         String testDir = System.getProperty("test.src");
       
   111         props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
       
   112         props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
       
   113         props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
       
   114         props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
       
   115 
       
   116         try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
       
   117             props.store(writer, "");
       
   118         }
       
   119     }
       
   120 
       
   121     @Test
       
   122     public void testHttpsNoAuth() throws Exception {
       
   123         Properties props = new Properties();
       
   124         props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
       
   125         props.setProperty("com.sun.management.jmxremote.ssl", "true");
       
   126         props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
       
   127         props.setProperty("com.sun.management.jmxremote.authenticate", "false");
       
   128         if (props.get("com.sun.management.jmxremote.rest.port") != null) {
       
   129             PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
       
   130         }
       
   131         JmxRestAdapter adapter = PlatformRestAdapter.getInstance();
       
   132         adapter.start();
       
   133         SSLContext ctx = getSSlContext(sslClientConfig);
       
   134         HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
       
   135         HttpsURLConnection.setDefaultHostnameVerifier(
       
   136                 (String hostname, javax.net.ssl.SSLSession sslSession) -> hostname.equals("Harsha-Wardhana-B"));
       
   137 
       
   138         URL url = new URL(adapter.getBaseUrl() + MBEANS);
       
   139         HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
       
   140         con.setDoOutput(false);
       
   141         print_https_cert(con);
       
   142         print_content(con);
       
   143         PlatformRestAdapter.stop();
       
   144     }
       
   145 
       
   146     @Test
       
   147     public void testHttpsAuth() throws Exception {
       
   148         Properties props = new Properties();
       
   149         props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
       
   150         props.setProperty("com.sun.management.jmxremote.ssl", "true");
       
   151         props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
       
   152         props.setProperty("com.sun.management.jmxremote.authenticate", "true");
       
   153         props.setProperty("com.sun.management.jmxremote.password.file", passwordFile);
       
   154         if (props.get("com.sun.management.jmxremote.rest.port") != null) {
       
   155             PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
       
   156         }
       
   157 
       
   158         JmxRestAdapter adapter = PlatformRestAdapter.getInstance();
       
   159         adapter.start();
       
   160         SSLContext ctx = getSSlContext(sslClientConfig);
       
   161         HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
       
   162         HttpsURLConnection.setDefaultHostnameVerifier(
       
   163                 (String hostname, javax.net.ssl.SSLSession sslSession) -> hostname.equals("Harsha-Wardhana-B"));
       
   164 
       
   165         URL url = new URL(adapter.getBaseUrl() + MBEANS);
       
   166         HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
       
   167         con.setDoOutput(false);
       
   168 
       
   169         String userCredentials = "username1:password1";
       
   170 
       
   171         String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
       
   172         con.setRequestProperty("Authorization", basicAuth);
       
   173         print_https_cert(con);
       
   174         print_content(con);
       
   175         PlatformRestAdapter.stop();
       
   176     }
       
   177 
       
   178     private void print_content(HttpURLConnection con) {
       
   179         if (con != null) {
       
   180             try {
       
   181                 System.out.println("****** Content of the URL ********");
       
   182                 int status = con.getResponseCode();
       
   183                 System.out.println("Status = " + status);
       
   184                 BufferedReader br
       
   185                         = new BufferedReader(
       
   186                                 new InputStreamReader(con.getInputStream()));
       
   187                 String input;
       
   188                 while ((input = br.readLine()) != null) {
       
   189                     System.out.println(input);
       
   190                 }
       
   191                 br.close();
       
   192             } catch (IOException e) {
       
   193                 e.printStackTrace();
       
   194             }
       
   195         }
       
   196     }
       
   197 
       
   198     private void print_https_cert(HttpsURLConnection con) {
       
   199         if (con != null) {
       
   200             try {
       
   201                 System.out.println("Response Code : " + con.getResponseCode());
       
   202                 System.out.println("Cipher Suite : " + con.getCipherSuite());
       
   203                 System.out.println("\n");
       
   204 
       
   205                 Certificate[] certs = con.getServerCertificates();
       
   206                 for (Certificate cert : certs) {
       
   207                     System.out.println("Cert Type : " + cert.getType());
       
   208                     System.out.println("Cert Hash Code : " + cert.hashCode());
       
   209                     System.out.println("Cert Public Key Algorithm : "
       
   210                             + cert.getPublicKey().getAlgorithm());
       
   211                     System.out.println("Cert Public Key Format : "
       
   212                             + cert.getPublicKey().getFormat());
       
   213                     System.out.println("\n");
       
   214                 }
       
   215 
       
   216             } catch (SSLPeerUnverifiedException e) {
       
   217                 e.printStackTrace();
       
   218             } catch (IOException e) {
       
   219                 e.printStackTrace();
       
   220             }
       
   221         }
       
   222     }
       
   223 
       
   224     private static SSLContext getSSlContext(String sslConfigFileName) {
       
   225         String keyStore, keyStorePassword, trustStore, trustStorePassword;
       
   226         System.out.println("SSL Config file : " + sslConfigFileName);
       
   227 
       
   228         try {
       
   229             Properties p = new Properties();
       
   230             BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
       
   231             p.load(bin);
       
   232             keyStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_FILE);
       
   233             keyStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_PASSWORD);
       
   234             trustStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_FILE);
       
   235             trustStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_PASSWORD);
       
   236 
       
   237             char[] keyStorePasswd = null;
       
   238             if (keyStorePassword.length() != 0) {
       
   239                 keyStorePasswd = keyStorePassword.toCharArray();
       
   240             }
       
   241 
       
   242             char[] trustStorePasswd = null;
       
   243             if (trustStorePassword.length() != 0) {
       
   244                 trustStorePasswd = trustStorePassword.toCharArray();
       
   245             }
       
   246 
       
   247             KeyStore ks = null;
       
   248             if (keyStore != null) {
       
   249                 ks = KeyStore.getInstance(KeyStore.getDefaultType());
       
   250                 FileInputStream ksfis = new FileInputStream(keyStore);
       
   251                 ks.load(ksfis, keyStorePasswd);
       
   252 
       
   253             }
       
   254             KeyManagerFactory kmf = KeyManagerFactory.getInstance(
       
   255                     KeyManagerFactory.getDefaultAlgorithm());
       
   256             kmf.init(ks, keyStorePasswd);
       
   257 
       
   258             KeyStore ts = null;
       
   259             if (trustStore != null) {
       
   260                 ts = KeyStore.getInstance(KeyStore.getDefaultType());
       
   261                 FileInputStream tsfis = new FileInputStream(trustStore);
       
   262                 ts.load(tsfis, trustStorePasswd);
       
   263             }
       
   264             TrustManagerFactory tmf = TrustManagerFactory.getInstance(
       
   265                     TrustManagerFactory.getDefaultAlgorithm());
       
   266             tmf.init(ts);
       
   267 
       
   268             SSLContext ctx = SSLContext.getInstance("SSL");
       
   269             ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
       
   270             return ctx;
       
   271         } catch (Exception ex) {
       
   272         }
       
   273         return null;
       
   274     }
       
   275 }