0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6261831
0N/A * @summary Tests the use of the subject delegation feature in the
0N/A * RMI connector
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean SubjectDelegation1Test SimpleStandard SimpleStandardMBean
0N/A * @run build SubjectDelegation1Test SimpleStandard SimpleStandardMBean
0N/A * @run main SubjectDelegation1Test policy11 ok
0N/A * @run main SubjectDelegation1Test policy12 ko
0N/A * @run main SubjectDelegation1Test policy13 ko
0N/A * @run main SubjectDelegation1Test policy14 ko
0N/A * @run main SubjectDelegation1Test policy15 ok
0N/A * @run main SubjectDelegation1Test policy16 ko
0N/A */
0N/A
0N/Aimport com.sun.jmx.remote.security.JMXPluggableAuthenticator;
0N/Aimport java.io.File;
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Properties;
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerConnection;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXPrincipal;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/Aimport javax.security.auth.Subject;
0N/A
0N/Apublic class SubjectDelegation1Test {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A // Check for supported operating systems: Solaris
0N/A //
0N/A // This test runs only on Solaris due to CR 6285916
0N/A //
0N/A String osName = System.getProperty("os.name");
0N/A System.out.println("os.name = " + osName);
0N/A if (!osName.equals("SunOS")) {
0N/A System.out.println("This test runs on Solaris only.");
0N/A System.out.println("Bye! Bye!");
0N/A return;
0N/A }
0N/A String policyFile = args[0];
0N/A String testResult = args[1];
0N/A System.out.println("Policy file = " + policyFile);
0N/A System.out.println("Expected test result = " + testResult);
0N/A JMXConnectorServer jmxcs = null;
0N/A JMXConnector jmxc = null;
0N/A try {
0N/A // Create an RMI registry
0N/A //
0N/A System.out.println("Start RMI registry...");
0N/A Registry reg = null;
0N/A int port = 5800;
0N/A while (port++ < 6000) {
0N/A try {
0N/A reg = LocateRegistry.createRegistry(port);
0N/A System.out.println("RMI registry running on port " + port);
0N/A break;
0N/A } catch (RemoteException e) {
0N/A // Failed to create RMI registry...
0N/A System.out.println("Failed to create RMI registry " +
0N/A "on port " + port);
0N/A }
0N/A }
0N/A if (reg == null) {
0N/A System.exit(1);
0N/A }
0N/A // Set the default password file
0N/A //
0N/A final String passwordFile = System.getProperty("test.src") +
0N/A File.separator + "jmxremote.password";
0N/A System.out.println("Password file = " + passwordFile);
0N/A // Set policy file
0N/A //
0N/A final String policy = System.getProperty("test.src") +
0N/A File.separator + policyFile;
0N/A System.out.println("PolicyFile = " + policy);
0N/A System.setProperty("java.security.policy", policy);
0N/A // Instantiate the MBean server
0N/A //
0N/A System.out.println("Create the MBean server");
0N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A // Register the SimpleStandardMBean
0N/A //
0N/A System.out.println("Create SimpleStandard MBean");
0N/A SimpleStandard s = new SimpleStandard("delegate");
0N/A mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
0N/A // Create Properties containing the username/password entries
0N/A //
0N/A Properties props = new Properties();
0N/A props.setProperty("jmx.remote.x.password.file", passwordFile);
0N/A // Initialize environment map to be passed to the connector server
0N/A //
0N/A System.out.println("Initialize environment map");
0N/A HashMap env = new HashMap();
0N/A env.put("jmx.remote.authenticator",
0N/A new JMXPluggableAuthenticator(props));
0N/A // Create an RMI connector server
0N/A //
0N/A System.out.println("Create an RMI connector server");
0N/A JMXServiceURL url =
0N/A new JMXServiceURL("rmi", null, 0,
0N/A "/jndi/rmi://:" + port + "/server" + port);
0N/A jmxcs =
0N/A JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
0N/A jmxcs.start();
0N/A // Create an RMI connector client
0N/A //
0N/A System.out.println("Create an RMI connector client");
0N/A HashMap cli_env = new HashMap();
0N/A // These credentials must match those in the default password file
0N/A //
0N/A String[] credentials = new String[] { "monitorRole" , "QED" };
0N/A cli_env.put("jmx.remote.credentials", credentials);
0N/A jmxc = JMXConnectorFactory.connect(url, cli_env);
0N/A Subject delegationSubject =
0N/A new Subject(true,
0N/A Collections.singleton(new JMXPrincipal("delegate")),
0N/A Collections.EMPTY_SET,
0N/A Collections.EMPTY_SET);
0N/A MBeanServerConnection mbsc =
0N/A jmxc.getMBeanServerConnection(delegationSubject);
0N/A // Get domains from MBeanServer
0N/A //
0N/A System.out.println("Domains:");
0N/A String domains[] = mbsc.getDomains();
0N/A for (int i = 0; i < domains.length; i++) {
0N/A System.out.println("\tDomain[" + i + "] = " + domains[i]);
0N/A }
0N/A // Get MBean count
0N/A //
0N/A System.out.println("MBean count = " + mbsc.getMBeanCount());
0N/A // Get State attribute
0N/A //
0N/A String oldState =
0N/A (String) mbsc.getAttribute(
0N/A new ObjectName("MBeans:type=SimpleStandard"),
0N/A "State");
0N/A System.out.println("Old State = \"" + oldState + "\"");
0N/A // Set State attribute
0N/A //
0N/A System.out.println("Set State to \"changed state\"");
0N/A mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),
0N/A new Attribute("State", "changed state"));
0N/A // Get State attribute
0N/A //
0N/A String newState =
0N/A (String) mbsc.getAttribute(
0N/A new ObjectName("MBeans:type=SimpleStandard"),
0N/A "State");
0N/A System.out.println("New State = \"" + newState + "\"");
0N/A if (!newState.equals("changed state")) {
0N/A System.out.println("Invalid State = \"" + newState + "\"");
0N/A System.exit(1);
0N/A }
0N/A // Add notification listener on SimpleStandard MBean
0N/A //
0N/A System.out.println("Add notification listener...");
0N/A mbsc.addNotificationListener(
0N/A new ObjectName("MBeans:type=SimpleStandard"),
0N/A new NotificationListener() {
0N/A public void handleNotification(Notification notification,
0N/A Object handback) {
0N/A System.out.println("Received notification: " +
0N/A notification);
0N/A }
0N/A },
0N/A null,
0N/A null);
0N/A // Unregister SimpleStandard MBean
0N/A //
0N/A System.out.println("Unregister SimpleStandard MBean...");
0N/A mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
0N/A } catch (SecurityException e) {
0N/A if (testResult.equals("ko")) {
0N/A System.out.println("Got expected security exception = " + e);
0N/A } else {
0N/A System.out.println("Got unexpected security exception = " + e);
0N/A e.printStackTrace();
0N/A throw e;
0N/A }
0N/A } catch (Exception e) {
0N/A System.out.println("Unexpected exception caught = " + e);
0N/A e.printStackTrace();
0N/A throw e;
0N/A } finally {
0N/A // Close connector client
0N/A //
0N/A if (jmxc != null)
0N/A jmxc.close();
0N/A // Stop connector server
0N/A //
0N/A if (jmxcs != null)
0N/A jmxcs.stop();
0N/A // Say goodbye
0N/A //
0N/A System.out.println("Bye! Bye!");
0N/A }
0N/A }
0N/A}