1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6261831
  27  * @summary Tests the use of the subject delegation feature on the authenticated
  28  *          principals within the RMI connector server's creator codebase.
  29  * @author Luis-Miguel Alventosa
  30  * @modules java.management/com.sun.jmx.remote.security
  31  * @run clean SubjectDelegation2Test SimpleStandard SimpleStandardMBean
  32  * @run build SubjectDelegation2Test SimpleStandard SimpleStandardMBean
  33  * @run main SubjectDelegation2Test policy21 ok
  34  * @run main SubjectDelegation2Test policy22 ko
  35  * @run main SubjectDelegation2Test policy23 ko
  36  * @run main SubjectDelegation2Test policy24 ok
  37  * @run main SubjectDelegation2Test policy25 ko
  38  */
  39 
  40 import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
  41 import java.io.File;
  42 import java.lang.management.ManagementFactory;
  43 import java.rmi.RemoteException;
  44 import java.rmi.registry.LocateRegistry;
  45 import java.rmi.registry.Registry;
  46 import java.util.Collections;
  47 import java.util.HashMap;
  48 import java.util.Properties;
  49 import javax.management.Attribute;
  50 import javax.management.MBeanServer;
  51 import javax.management.MBeanServerConnection;
  52 import javax.management.Notification;
  53 import javax.management.NotificationListener;
  54 import javax.management.ObjectName;
  55 import javax.management.remote.JMXConnector;
  56 import javax.management.remote.JMXConnectorFactory;
  57 import javax.management.remote.JMXConnectorServer;
  58 import javax.management.remote.JMXConnectorServerFactory;
  59 import javax.management.remote.JMXPrincipal;
  60 import javax.management.remote.JMXServiceURL;
  61 import javax.security.auth.Subject;
  62 
  63 public class SubjectDelegation2Test {
  64 
  65     public static void main(String[] args) throws Exception {
  66         // Check for supported operating systems: Solaris
  67         //
  68         // This test runs only on Solaris due to CR 6285916
  69         //
  70         String osName = System.getProperty("os.name");
  71         System.out.println("os.name = " + osName);
  72         if (!osName.equals("SunOS")) {
  73             System.out.println("This test runs on Solaris only.");
  74             System.out.println("Bye! Bye!");
  75             return;
  76         }
  77         String policyFile = args[0];
  78         String testResult = args[1];
  79         System.out.println("Policy file = " + policyFile);
  80         System.out.println("Expected test result = " + testResult);
  81         JMXConnectorServer jmxcs = null;
  82         JMXConnector jmxc = null;
  83         try {
  84             // Create an RMI registry
  85             //
  86             System.out.println("Start RMI registry...");
  87             Registry reg = null;
  88             int port = 5800;
  89             while (port++ < 6000) {
  90                 try {
  91                     reg = LocateRegistry.createRegistry(port);
  92                     System.out.println("RMI registry running on port " + port);
  93                     break;
  94                 } catch (RemoteException e) {
  95                     // Failed to create RMI registry...
  96                     System.out.println("Failed to create RMI registry " +
  97                                        "on port " + port);
  98                 }
  99             }
 100             if (reg == null) {
 101                 System.exit(1);
 102             }
 103             // Set the default password file
 104             //
 105             final String passwordFile = System.getProperty("test.src") +
 106                 File.separator + "jmxremote.password";
 107             System.out.println("Password file = " + passwordFile);
 108             // Set policy file
 109             //
 110             final String policy = System.getProperty("test.src") +
 111                 File.separator + policyFile;
 112             System.out.println("PolicyFile = " + policy);
 113             System.setProperty("java.security.policy", policy);
 114             // Instantiate the MBean server
 115             //
 116             System.out.println("Create the MBean server");
 117             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 118             // Register the SimpleStandardMBean
 119             //
 120             System.out.println("Create SimpleStandard MBean");
 121             SimpleStandard s = new SimpleStandard("monitorRole");
 122             mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
 123             // Create Properties containing the username/password entries
 124             //
 125             Properties props = new Properties();
 126             props.setProperty("jmx.remote.x.password.file", passwordFile);
 127             // Initialize environment map to be passed to the connector server
 128             //
 129             System.out.println("Initialize environment map");
 130             HashMap env = new HashMap();
 131             env.put("jmx.remote.authenticator",
 132                     new JMXPluggableAuthenticator(props));
 133             // Set Security Manager
 134             //
 135             System.setSecurityManager(new SecurityManager());
 136             // Create an RMI connector server
 137             //
 138             System.out.println("Create an RMI connector server");
 139             JMXServiceURL url =
 140                 new JMXServiceURL("rmi", null, 0,
 141                                   "/jndi/rmi://:" + port + "/server" + port);
 142             jmxcs =
 143                 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
 144             jmxcs.start();
 145             // Create an RMI connector client
 146             //
 147             System.out.println("Create an RMI connector client");
 148             HashMap cli_env = new HashMap();
 149             // These credentials must match those in the default password file
 150             //
 151             String[] credentials = new String[] { "monitorRole" , "QED" };
 152             cli_env.put("jmx.remote.credentials", credentials);
 153             jmxc = JMXConnectorFactory.connect(url, cli_env);
 154             MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
 155             // Get domains from MBeanServer
 156             //
 157             System.out.println("Domains:");
 158             String domains[] = mbsc.getDomains();
 159             for (int i = 0; i < domains.length; i++) {
 160                 System.out.println("\tDomain[" + i + "] = " + domains[i]);
 161             }
 162             // Get MBean count
 163             //
 164             System.out.println("MBean count = " + mbsc.getMBeanCount());
 165             // Get State attribute
 166             //
 167             String oldState =
 168                 (String) mbsc.getAttribute(
 169                               new ObjectName("MBeans:type=SimpleStandard"),
 170                               "State");
 171             System.out.println("Old State = \"" + oldState + "\"");
 172             // Set State attribute
 173             //
 174             System.out.println("Set State to \"changed state\"");
 175             mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),
 176                               new Attribute("State", "changed state"));
 177             // Get State attribute
 178             //
 179             String newState =
 180                 (String) mbsc.getAttribute(
 181                               new ObjectName("MBeans:type=SimpleStandard"),
 182                               "State");
 183             System.out.println("New State = \"" + newState + "\"");
 184             if (!newState.equals("changed state")) {
 185                 System.out.println("Invalid State = \"" + newState + "\"");
 186                 System.exit(1);
 187             }
 188             // Add notification listener on SimpleStandard MBean
 189             //
 190             System.out.println("Add notification listener...");
 191             mbsc.addNotificationListener(
 192                  new ObjectName("MBeans:type=SimpleStandard"),
 193                  new NotificationListener() {
 194                      public void handleNotification(Notification notification,
 195                                                     Object handback) {
 196                          System.out.println("Received notification: " +
 197                                             notification);
 198                      }
 199                  },
 200                  null,
 201                  null);
 202             // Unregister SimpleStandard MBean
 203             //
 204             System.out.println("Unregister SimpleStandard MBean...");
 205             mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
 206         } catch (SecurityException e) {
 207             if (testResult.equals("ko")) {
 208                 System.out.println("Got expected security exception = " + e);
 209             } else {
 210                 System.out.println("Got unexpected security exception = " + e);
 211                 e.printStackTrace();
 212                 throw e;
 213             }
 214         } catch (Exception e) {
 215             System.out.println("Unexpected exception caught = " + e);
 216             e.printStackTrace();
 217             throw e;
 218         } finally {
 219             // Close connector client
 220             //
 221             if (jmxc != null)
 222                 jmxc.close();
 223             // Stop connector server
 224             //
 225             if (jmxcs != null)
 226                 jmxcs.stop();
 227             // Say goodbye
 228             //
 229             System.out.println("Bye! Bye!");
 230         }
 231     }
 232 }