1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import sun.management.jmxremote.ConnectorBootstrap;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Properties;
import java.util.Iterator;
import java.util.Set;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Enumeration;
import javax.management.remote.*;
import javax.management.*;
import jdk.internal.agent.AgentConfigurationError;
/**
* <p>This class implements unit test for RMI Bootstrap.
* When called with no arguments main() looks in the directory indicated
* by the "test.src" system property for files called management*ok.properties
* or management*ko.properties. The *ok.properties files are assumed to be
* valid Java M&M config files for which the bootstrap should succeed.
* The *ko.properties files are assumed to be configurations for which the
* bootstrap & connection test will fail.</p>
*
* <p>The rmi port number can be specified with the "rmi.port" system property.
* If not, this test will use 12424</p>
*
* <p>When called with some argument, the main() will interprete its args to
* be Java M&M configuration file names. The filenames are expected to end
* with ok.properties or ko.properties - and are interpreted as above.</p>
*
* <p>Note that a limitation of the RMI registry (bug 4267864) prevent
* this test from succeeding if more than 1 configuration is used.
* As long as 4267864 isn't fix, this test must be called as many times
* as needed but with a single argument (no arguments, or several arguments
* will fail).</p>
*
* <p>Debug traces are logged in "sun.management.test"</p>
**/
public class RmiSslNoKeyStoreTest {
static TestLogger log =
new TestLogger("RmiSslNoKeyStoreTest");
/**
* When launching several registries, we increment the port number
* to avoid falling into "port number already in use" problems.
**/
static int testPort = 0;
/**
* Default values for RMI configuration properties.
**/
public static interface DefaultValues {
public static final String PORT="0";
public static final String CONFIG_FILE_NAME="management.properties";
public static final String USE_SSL="true";
public static final String USE_AUTHENTICATION="true";
public static final String PASSWORD_FILE_NAME="jmxremote.password";
public static final String ACCESS_FILE_NAME="jmxremote.access";
public static final String KEYSTORE="keystore";
public static final String KEYSTORE_PASSWD="password";
public static final String TRUSTSTORE="truststore";
public static final String TRUSTSTORE_PASSWD="trustword";
}
/**
* Names of RMI configuration properties.
**/
public static interface PropertyNames {
public static final String PORT="com.sun.management.jmxremote.port";
public static final String CONFIG_FILE_NAME=
"com.sun.management.config.file";
public static final String USE_SSL="com.sun.management.jmxremote.ssl";
public static final String USE_AUTHENTICATION=
"com.sun.management.jmxremote.authenticate";
public static final String PASSWORD_FILE_NAME=
"com.sun.management.jmxremote.password.file";
public static final String ACCESS_FILE_NAME=
"com.sun.management.jmxremote.access.file";
public static final String INSTRUMENT_ALL=
"com.sun.management.instrumentall";
public static final String CREDENTIALS =
"jmx.remote.credentials";
public static final String KEYSTORE="javax.net.ssl.keyStore";
public static final String KEYSTORE_PASSWD=
"javax.net.ssl.keyStorePassword";
public static final String KEYSTORE_TYPE="javax.net.ssl.keyStoreType";
public static final String TRUSTSTORE="javax.net.ssl.trustStore";
public static final String TRUSTSTORE_PASSWD=
"javax.net.ssl.trustStorePassword";
}
/**
* Compute the full path name for a default file.
* @param basename basename (with extension) of the default file.
* @return ${JRE}/conf/management/${basename}
**/
private static String getDefaultFileName(String basename) {
final String fileSeparator = File.separator;
final StringBuffer defaultFileName =
new StringBuffer(System.getProperty("java.home")).
append(fileSeparator).append("conf").append(fileSeparator).
append("management").append(fileSeparator).
append(basename);
return defaultFileName.toString();
}
/**
* Compute the full path name for a default file.
* @param basename basename (with extension) of the default file.
* @return ${JRE}/conf/management/${basename}
**/
private static String getDefaultStoreName(String basename) {
final String fileSeparator = File.separator;
final StringBuffer defaultFileName =
new StringBuffer(System.getProperty("test.src")).
append(fileSeparator).append("ssl").append(fileSeparator).
append(basename);
return defaultFileName.toString();
}
private static void checkKeystore(Properties props)
throws IOException, GeneralSecurityException {
if (log.isDebugOn())
log.debug("checkKeystore","Checking Keystore configuration");
final String keyStore =
System.getProperty(PropertyNames.KEYSTORE);
if (keyStore == null)
throw new IllegalArgumentException("System property " +
PropertyNames.KEYSTORE +
" not specified");
final String keyStorePass =
System.getProperty(PropertyNames.KEYSTORE_PASSWD);
if (keyStorePass == null) {
// We don't have the password, we can only check whether the
// file exists...
//
final File ksf = new File(keyStore);
if (! ksf.canRead())
throw new IOException(keyStore + ": not readable");
if (log.isDebugOn())
log.debug("checkSSL", "No password.");
throw new IllegalArgumentException("System property " +
PropertyNames.KEYSTORE_PASSWD +
" not specified");
}
// Now we're going to load the keyStore - just to check it's
// correct.
//
final String keyStoreType =
System.getProperty(PropertyNames.KEYSTORE_TYPE,
KeyStore.getDefaultType());
final KeyStore ks = KeyStore.getInstance(keyStoreType);
final FileInputStream fin = new FileInputStream(keyStore);
final char keypassword[] = keyStorePass.toCharArray();
try {
ks.load(fin,keypassword);
} finally {
Arrays.fill(keypassword,' ');
fin.close();
}
if (log.isDebugOn())
log.debug("checkSSL","SSL configuration successfully checked");
}
private void checkSslConfiguration() throws Exception {
final String defaultConf =
getDefaultFileName(DefaultValues.CONFIG_FILE_NAME);
final String confname =
System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf);
final Properties props = new Properties();
final File conf = new File(confname);
if (conf.exists()) {
FileInputStream fin = new FileInputStream(conf);
try {props.load(fin);} finally {fin.close();}
}
// Do we use SSL?
final String useSslStr =
props.getProperty(PropertyNames.USE_SSL,
DefaultValues.USE_SSL);
final boolean useSsl =
Boolean.valueOf(useSslStr).booleanValue();
log.debug("checkSslConfiguration",PropertyNames.USE_SSL+"="+useSsl);
if (useSsl == false) {
final String msg =
PropertyNames.USE_SSL+"="+useSsl+", can't run test";
throw new IllegalArgumentException(msg);
}
try {
checkKeystore(props);
} catch (Exception x) {
// Ok!
log.debug("checkSslConfiguration","Test configuration OK: " + x);
return;
}
final String msg = "KeyStore properly configured, can't run test";
throw new IllegalArgumentException(msg);
}
/**
* Test the configuration indicated by `file'.
* Sets the appropriate System properties for config file and
* port and then calls ConnectorBootstrap.initialize().
* eventually cleans up by calling ConnectorBootstrap.terminate().
* @return null if the test succeeds, an error message otherwise.
**/
private String testConfiguration(File file,int port) {
final String path = (file==null)?null:file.getAbsolutePath();
final String config = (path==null)?"Default config file":path;
try {
System.out.println("***");
System.out.println("*** Testing configuration (port="+
port + "): "+ path);
System.out.println("***");
System.setProperty("com.sun.management.jmxremote.port",
Integer.toString(port));
if (path != null)
System.setProperty("com.sun.management.config.file", path);
else
System.getProperties().
remove("com.sun.management.config.file");
log.trace("testConfiguration","com.sun.management.jmxremote.port="+port);
if (path != null && log.isDebugOn())
log.trace("testConfiguration",
"com.sun.management.config.file="+path);
checkSslConfiguration();
final JMXConnectorServer cs;
try {
cs = ConnectorBootstrap.initialize();
} catch (AgentConfigurationError x) {
final String err = "Failed to initialize connector:" +
"\n\tcom.sun.management.jmxremote.port=" + port +
((path!=null)?"\n\tcom.sun.management.config.file="+path:
"\n\t"+config) +
"\n\tError is: " + x;
log.trace("testConfiguration","Expected failure: " + err);
log.debug("testConfiguration",x);
System.out.println("Got expected failure: " + x);
return null;
} catch (Exception x) {
log.debug("testConfiguration",x);
return x.toString();
}
try {
JMXConnector cc =
JMXConnectorFactory.connect(cs.getAddress(), null);
cc.close();
} catch (IOException x) {
final String err = "Failed to initialize connector:" +
"\n\tcom.sun.management.jmxremote.port=" + port +
((path!=null)?"\n\tcom.sun.management.config.file="+path:
"\n\t"+config) +
"\n\tError is: " + x;
log.trace("testConfiguration","Expected failure: " + err);
log.debug("testConfiguration",x);
System.out.println("Got expected failure: " + x);
return null;
} catch (Exception x) {
log.debug("testConfiguration",x);
return x.toString();
}
try {
cs.stop();
} catch (Exception x) {
final String err = "Failed to terminate: "+x;
log.trace("testConfiguration",err);
log.debug("testConfiguration",x);
}
final String err = "Bootstrap should have failed:" +
"\n\tcom.sun.management.jmxremote.port=" + port +
((path!=null)?"\n\tcom.sun.management.config.file="+path:
"\n\t"+config);
log.trace("testConfiguration",err);
return err;
} catch (Exception x) {
final String err = "Failed to test bootstrap for:" +
"\n\tcom.sun.management.jmxremote.port=" + port +
((path!=null)?"\n\tcom.sun.management.config.file="+path:
"\n\t"+config)+
"\n\tError is: " + x;
log.trace("testConfiguration",err);
log.debug("testConfiguration",x);
return err;
}
}
/**
* Test a configuration file. Determines whether the bootstrap
* should succeed or fail depending on the file name:
* *ok.properties: bootstrap should succeed.
* *ko.properties: bootstrap or connection should fail.
* @return null if the test succeeds, an error message otherwise.
**/
private String testConfigurationFile(String fileName) {
File file = new File(fileName);
final String portStr = System.getProperty("rmi.port","12424");
final int port = Integer.parseInt(portStr);
return testConfiguration(file,port+testPort++);
}
/**
* Tests the specified configuration files.
* If args[] is not empty, each element in args[] is expected to be
* a filename ending either by ok.properties or ko.properties.
* Otherwise, the configuration files will be automatically determined
* by looking at all *.properties files located in the directory
* indicated by the System property "test.src".
* @throws RuntimeException if the test fails.
**/
public void run(String args[]) {
final String defaultKeyStore =
getDefaultStoreName(DefaultValues.KEYSTORE);
final String keyStore =
System.getProperty(PropertyNames.KEYSTORE, defaultKeyStore);
for (int i=0; i<args.length; i++) {
String errStr =testConfigurationFile(args[i]);
if (errStr != null) {
throw new RuntimeException(errStr);
}
if ((System.getProperty(PropertyNames.KEYSTORE) == null) &&
(System.getProperty(PropertyNames.KEYSTORE_PASSWD) == null)) {
try {
// Specify the keystore, but don't specify the
// password.
//
System.setProperty(PropertyNames.KEYSTORE,keyStore);
log.trace("run",PropertyNames.KEYSTORE+"="+keyStore);
errStr =testConfigurationFile(args[i]);
if (errStr != null) {
throw new RuntimeException(errStr);
}
} finally {
System.getProperties().remove(PropertyNames.KEYSTORE);
}
}
}
}
/**
* Calls run(args[]).
* exit(1) if the test fails.
**/
public static void main(String args[]) {
RmiSslNoKeyStoreTest manager = new RmiSslNoKeyStoreTest();
try {
manager.run(args);
} catch (RuntimeException r) {
System.err.println("Test Failed: "+ r.getMessage());
System.exit(1);
} catch (Throwable t) {
System.err.println("Test Failed: "+ t);
t.printStackTrace();
System.exit(2);
}
System.out.println("**** Test RmiSslNoKeyStoreTest Passed ****");
}
}
|