@@ -81,10 +81,14 @@ public class JmxTest {
81
81
static final String ACCESS_PROPERTY = "com.sun.management.jmxremote.access.file" ;
82
82
static final String PASSWORD_PROPERTY = "com.sun.management.jmxremote.password.file" ;
83
83
static final String SSL_PROPERTY = "com.sun.management.jmxremote.ssl" ;
84
- static final String KEYSTORE_PROPERTY = "javax.net.ssl.keyStore" ;
84
+ static final String KEYSTORE_FILENAME = "clientkeystore" ;
85
+ static final String KEYSTORE_PASSWORD = "clientpass" ;
85
86
static final String KEYSTORE_PASSWORD_PROPERTY = "javax.net.ssl.keyStorePassword" ;
86
- static final String TRUSTSTORE_PROPERTY = "javax.net.ssl.trustStore" ;
87
+ static final String KEYSTORE_PROPERTY = "javax.net.ssl.keyStore" ;
88
+ static final String TRUSTSTORE_FILENAME = "servertruststore" ;
89
+ static final String TRUSTSTORE_PASSWORD = "servertrustpass" ;
87
90
static final String TRUSTSTORE_PASSWORD_PROPERTY = "javax.net.ssl.trustStorePassword" ;
91
+ static final String TRUSTSTORE_PROPERTY = "javax.net.ssl.trustStore" ;
88
92
static final String REGISTRY_SSL_PROPERTY = "com.sun.management.jmxremote.registry.ssl" ;
89
93
static final String SOCKET_FACTORY_PROPERTY = "com.sun.jndi.rmi.factory.socket" ;
90
94
static final String TEST_PORT = "12345" ;
@@ -104,34 +108,30 @@ public static void setup() throws IOException {
104
108
System .setProperty (SSL_PROPERTY , TRUE );
105
109
System .setProperty (REGISTRY_SSL_PROPERTY , TRUE );
106
110
111
+ // Prepare temp directory with files required for testing authentification.
107
112
Path tempDirectory = Files .createTempDirectory ("jmxtest" );
113
+ Path jmxRemoteAccess = tempDirectory .resolve ("jmxremote.access" );
114
+ Path jmxRemotePassword = tempDirectory .resolve ("jmxremote.password" );
115
+ Path clientKeyStore = tempDirectory .resolve (KEYSTORE_FILENAME );
116
+ Path serverTrustStore = tempDirectory .resolve (TRUSTSTORE_FILENAME );
108
117
109
- // Generate SSL keystore, client cert, and truststore
118
+ // Generate SSL keystore, client cert, and truststore for testing SSL connection.
110
119
createClientKey (tempDirectory );
111
120
createClientCert (tempDirectory );
121
+ assertTrue ("Failed to create " + KEYSTORE_FILENAME , Files .exists (clientKeyStore ));
122
+ System .setProperty (KEYSTORE_PROPERTY , clientKeyStore .toString ());
123
+ System .setProperty (KEYSTORE_PASSWORD_PROPERTY , KEYSTORE_PASSWORD );
112
124
createServerTrustStore (tempDirectory );
113
- // Copy resources into tempDirectory
114
- Path jmxRemoteAccess = tempDirectory .resolve ("jmxremote.access" );
115
- Path jmxRemotePassword = tempDirectory .resolve ("jmxremote.password" );
116
- Path clientkeystore = tempDirectory .resolve ("clientkeystore" );
117
- Path servertruststore = tempDirectory .resolve ("servertruststore" );
118
- // Note: full paths are used to ensure analysis includes the resources automatically
119
- Files .writeString (jmxRemoteAccess , TEST_ROLE + " readwrite" );
120
- Files .writeString (jmxRemotePassword , TEST_ROLE + " " + TEST_ROLE_PASSWORD );
125
+ assertTrue ("Failed to create " + TRUSTSTORE_FILENAME , Files .exists (serverTrustStore ));
126
+ System .setProperty (TRUSTSTORE_PROPERTY , serverTrustStore .toString ());
127
+ System .setProperty (TRUSTSTORE_PASSWORD_PROPERTY , TRUSTSTORE_PASSWORD );
121
128
122
- // The following are dummy password and access files required for testing authentication.
129
+ // The following are dummy access and password files required for testing authentication.
130
+ Files .writeString (jmxRemoteAccess , TEST_ROLE + " readwrite" );
123
131
System .setProperty (ACCESS_PROPERTY , jmxRemoteAccess .toString ());
132
+ Files .writeString (jmxRemotePassword , TEST_ROLE + " " + TEST_ROLE_PASSWORD );
124
133
System .setProperty (PASSWORD_PROPERTY , jmxRemotePassword .toString ());
125
134
126
- /*
127
- * The following are dummy SSL keystore and truststore files required for testing connection
128
- * using SSL. See resources/jmxremote/README.md for more information.
129
- */
130
- System .setProperty (KEYSTORE_PROPERTY , clientkeystore .toString ());
131
- System .setProperty (KEYSTORE_PASSWORD_PROPERTY , "clientpass" );
132
- System .setProperty (TRUSTSTORE_PROPERTY , servertruststore .toString ());
133
- System .setProperty (TRUSTSTORE_PASSWORD_PROPERTY , "servertrustpass" );
134
-
135
135
// Password file must have restricted access.
136
136
Files .setPosixFilePermissions (jmxRemotePassword , Set .of (PosixFilePermission .OWNER_READ , PosixFilePermission .OWNER_WRITE ));
137
137
@@ -140,35 +140,35 @@ public static void setup() throws IOException {
140
140
ManagementAgentStartupHook startupHook = new ManagementAgentStartupHook ();
141
141
startupHook .execute (false );
142
142
} catch (Exception e ) {
143
- Assert .fail ("Failed to start server Cause: " + e .getMessage ());
143
+ Assert .fail ("Failed to start server. Cause: " + e .getMessage ());
144
144
}
145
145
}
146
146
147
147
private static void createClientKey (Path tempDirectory ) throws IOException {
148
148
runCommand (tempDirectory , List .of ("keytool" , "-genkey" ,
149
- "-keystore" , "clientkeystore" ,
149
+ "-keystore" , KEYSTORE_FILENAME ,
150
150
"-alias" , "clientkey" ,
151
- "-storepass" , "clientpass" ,
152
- "-keypass" , "clientpass" ,
151
+ "-storepass" , KEYSTORE_PASSWORD ,
152
+ "-keypass" , KEYSTORE_PASSWORD ,
153
153
"-dname" , "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test" ,
154
154
"-validity" , "99999" ,
155
155
"-keyalg" , "rsa" ));
156
156
}
157
157
158
158
private static void createClientCert (Path tempDirectory ) throws IOException {
159
159
runCommand (tempDirectory , List .of ("keytool" , "-exportcert" ,
160
- "-keystore" , "clientkeystore" ,
160
+ "-keystore" , KEYSTORE_FILENAME ,
161
161
"-alias" , "clientkey" ,
162
- "-storepass" , "clientpass" ,
162
+ "-storepass" , KEYSTORE_PASSWORD ,
163
163
"-file" , "client.cer" ));
164
164
}
165
165
166
166
private static void createServerTrustStore (Path tempDirectory ) throws IOException {
167
167
runCommand (tempDirectory , List .of ("keytool" , "-importcert" ,
168
168
"-noprompt" ,
169
169
"-file" , "client.cer" ,
170
- "-keystore" , "servertruststore" ,
171
- "-storepass" , "servertrustpass" ));
170
+ "-keystore" , TRUSTSTORE_FILENAME ,
171
+ "-storepass" , TRUSTSTORE_PASSWORD ));
172
172
}
173
173
174
174
private static void runCommand (Path tempDirectory , List <String > command ) throws IOException {
0 commit comments