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