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 with 29 * subject delegation. 30 * @author Luis-Miguel Alventosa 31 * @modules java.management/com.sun.jmx.remote.security 32 * @run clean SubjectDelegation3Test SimpleStandard SimpleStandardMBean 33 * @run build SubjectDelegation3Test SimpleStandard SimpleStandardMBean 34 * @run main SubjectDelegation3Test policy31 ok 35 * @run main SubjectDelegation3Test policy32 ko 36 * @run main SubjectDelegation3Test policy33 ko 37 * @run main SubjectDelegation3Test policy34 ok 38 * @run main SubjectDelegation3Test policy35 ko 39 */ 40 41 import com.sun.jmx.remote.security.JMXPluggableAuthenticator; 42 import java.io.File; 43 import java.lang.management.ManagementFactory; 44 import java.rmi.RemoteException; 45 import java.rmi.registry.LocateRegistry; 46 import java.rmi.registry.Registry; 47 import java.util.Collections; 48 import java.util.HashMap; 49 import java.util.Properties; 50 import javax.management.Attribute; 51 import javax.management.MBeanServer; 52 import javax.management.MBeanServerConnection; 53 import javax.management.Notification; 54 import javax.management.NotificationListener; 55 import javax.management.ObjectName; 56 import javax.management.remote.JMXConnector; 57 import javax.management.remote.JMXConnectorFactory; 58 import javax.management.remote.JMXConnectorServer; 59 import javax.management.remote.JMXConnectorServerFactory; 60 import javax.management.remote.JMXPrincipal; 61 import javax.management.remote.JMXServiceURL; 62 import javax.security.auth.Subject; 63 64 public class SubjectDelegation3Test { 65 66 public static void main(String[] args) throws Exception { 67 // Check for supported operating systems: Solaris 68 // 69 // This test runs only on Solaris due to CR 6285916 70 // 71 String osName = System.getProperty("os.name"); 72 System.out.println("os.name = " + osName); 73 if (!osName.equals("SunOS")) { 74 System.out.println("This test runs on Solaris only."); 75 System.out.println("Bye! Bye!"); 76 return; 77 } 78 String policyFile = args[0]; 79 String testResult = args[1]; 80 System.out.println("Policy file = " + policyFile); 81 System.out.println("Expected test result = " + testResult); 82 JMXConnectorServer jmxcs = null; 83 JMXConnector jmxc = null; 84 try { 85 // Create an RMI registry 86 // 87 System.out.println("Start RMI registry..."); 88 Registry reg = null; 89 int port = 5800; 90 while (port++ < 6000) { 91 try { 92 reg = LocateRegistry.createRegistry(port); 93 System.out.println("RMI registry running on port " + port); 94 break; 95 } catch (RemoteException e) { 96 // Failed to create RMI registry... 97 System.out.println("Failed to create RMI registry " + 98 "on port " + port); 99 } 100 } 101 if (reg == null) { 102 System.exit(1); 103 } 104 // Set the default password file 105 // 106 final String passwordFile = System.getProperty("test.src") + 107 File.separator + "jmxremote.password"; 108 System.out.println("Password file = " + passwordFile); 109 // Set policy file 110 // 111 final String policy = System.getProperty("test.src") + 112 File.separator + policyFile; 113 System.out.println("PolicyFile = " + policy); 114 System.setProperty("java.security.policy", policy); 115 // Instantiate the MBean server 116 // 117 System.out.println("Create the MBean server"); 118 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 119 // Register the SimpleStandardMBean 120 // 121 System.out.println("Create SimpleStandard MBean"); 122 SimpleStandard s = new SimpleStandard("delegate"); 123 mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard")); 124 // Create Properties containing the username/password entries 125 // 126 Properties props = new Properties(); 127 props.setProperty("jmx.remote.x.password.file", passwordFile); 128 // Initialize environment map to be passed to the connector server 129 // 130 System.out.println("Initialize environment map"); 131 HashMap env = new HashMap(); 132 env.put("jmx.remote.authenticator", 133 new JMXPluggableAuthenticator(props)); 134 // Set Security Manager 135 // 136 System.setSecurityManager(new SecurityManager()); 137 // Create an RMI connector server 138 // 139 System.out.println("Create an RMI connector server"); 140 JMXServiceURL url = 141 new JMXServiceURL("rmi", null, 0, 142 "/jndi/rmi://:" + port + "/server" + port); 143 jmxcs = 144 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); 145 jmxcs.start(); 146 // Create an RMI connector client 147 // 148 System.out.println("Create an RMI connector client"); 149 HashMap cli_env = new HashMap(); 150 // These credentials must match those in the default password file 151 // 152 String[] credentials = new String[] { "monitorRole" , "QED" }; 153 cli_env.put("jmx.remote.credentials", credentials); 154 jmxc = JMXConnectorFactory.connect(url, cli_env); 155 Subject delegationSubject = 156 new Subject(true, 157 Collections.singleton(new JMXPrincipal("delegate")), 158 Collections.EMPTY_SET, 159 Collections.EMPTY_SET); 160 MBeanServerConnection mbsc = 161 jmxc.getMBeanServerConnection(delegationSubject); 162 // Get domains from MBeanServer 163 // 164 System.out.println("Domains:"); 165 String domains[] = mbsc.getDomains(); 166 for (int i = 0; i < domains.length; i++) { 167 System.out.println("\tDomain[" + i + "] = " + domains[i]); 168 } 169 // Get MBean count 170 // 171 System.out.println("MBean count = " + mbsc.getMBeanCount()); 172 // Get State attribute 173 // 174 String oldState = 175 (String) mbsc.getAttribute( 176 new ObjectName("MBeans:type=SimpleStandard"), 177 "State"); 178 System.out.println("Old State = \"" + oldState + "\""); 179 // Set State attribute 180 // 181 System.out.println("Set State to \"changed state\""); 182 mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"), 183 new Attribute("State", "changed state")); 184 // Get State attribute 185 // 186 String newState = 187 (String) mbsc.getAttribute( 188 new ObjectName("MBeans:type=SimpleStandard"), 189 "State"); 190 System.out.println("New State = \"" + newState + "\""); 191 if (!newState.equals("changed state")) { 192 System.out.println("Invalid State = \"" + newState + "\""); 193 System.exit(1); 194 } 195 // Add notification listener on SimpleStandard MBean 196 // 197 System.out.println("Add notification listener..."); 198 mbsc.addNotificationListener( 199 new ObjectName("MBeans:type=SimpleStandard"), 200 new NotificationListener() { 201 public void handleNotification(Notification notification, 202 Object handback) { 203 System.out.println("Received notification: " + 204 notification); 205 } 206 }, 207 null, 208 null); 209 // Unregister SimpleStandard MBean 210 // 211 System.out.println("Unregister SimpleStandard MBean..."); 212 mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard")); 213 } catch (SecurityException e) { 214 if (testResult.equals("ko")) { 215 System.out.println("Got expected security exception = " + e); 216 } else { 217 System.out.println("Got unexpected security exception = " + e); 218 e.printStackTrace(); 219 throw e; 220 } 221 } catch (Exception e) { 222 System.out.println("Unexpected exception caught = " + e); 223 e.printStackTrace(); 224 throw e; 225 } finally { 226 // Close connector client 227 // 228 if (jmxc != null) 229 jmxc.close(); 230 // Stop connector server 231 // 232 if (jmxcs != null) 233 jmxcs.stop(); 234 // Say goodbye 235 // 236 System.out.println("Bye! Bye!"); 237 } 238 } 239 }