J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 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 5016508 |
| 27 | * @summary Tests the default JAAS configuration for authenticating RMI clients |
| 28 | * @author Luis-Miguel Alventosa |
| 29 | * @run clean RMIPasswdAuthTest |
| 30 | * @run build RMIPasswdAuthTest SimpleStandard SimpleStandardMBean |
| 31 | * @run main RMIPasswdAuthTest |
| 32 | */ |
| 33 | |
| 34 | import java.io.File; |
| 35 | import java.rmi.RemoteException; |
| 36 | import java.rmi.registry.Registry; |
| 37 | import java.rmi.registry.LocateRegistry; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Properties; |
| 40 | |
| 41 | import javax.management.Attribute; |
| 42 | import javax.management.MBeanServer; |
| 43 | import javax.management.MBeanServerConnection; |
| 44 | import javax.management.MBeanServerFactory; |
| 45 | import javax.management.Notification; |
| 46 | import javax.management.NotificationListener; |
| 47 | import javax.management.ObjectName; |
| 48 | import javax.management.remote.JMXConnector; |
| 49 | import javax.management.remote.JMXConnectorFactory; |
| 50 | import javax.management.remote.JMXConnectorServer; |
| 51 | import javax.management.remote.JMXConnectorServerFactory; |
| 52 | import javax.management.remote.JMXServiceURL; |
| 53 | import com.sun.jmx.remote.security.JMXPluggableAuthenticator; |
| 54 | |
| 55 | public class RMIPasswdAuthTest { |
| 56 | |
| 57 | public static void main(String[] args) { |
| 58 | try { |
| 59 | |
| 60 | // Set the default password file |
| 61 | // |
| 62 | final String passwordFile = System.getProperty("test.src") + |
| 63 | File.separator + "jmxremote.password"; |
| 64 | System.out.println("Password file = " + passwordFile); |
| 65 | |
| 66 | // Create an RMI registry |
| 67 | // |
| 68 | System.out.println("Start RMI registry..."); |
| 69 | Registry reg = null; |
| 70 | int port = 5800; |
| 71 | while (port++ < 6000) { |
| 72 | try { |
| 73 | reg = LocateRegistry.createRegistry(port); |
| 74 | System.out.println("RMI registry running on port " + port); |
| 75 | break; |
| 76 | } catch (RemoteException e) { |
| 77 | // Failed to create RMI registry... |
| 78 | System.out.println("Failed to create RMI registry " + |
| 79 | "on port " + port); |
| 80 | } |
| 81 | } |
| 82 | if (reg == null) { |
| 83 | System.exit(1); |
| 84 | } |
| 85 | |
| 86 | // Instantiate the MBean server |
| 87 | // |
| 88 | System.out.println("Create the MBean server"); |
| 89 | MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
| 90 | // Register the ClassPathClassLoaderMBean |
| 91 | // |
| 92 | System.out.println("Create ClassPathClassLoader MBean"); |
| 93 | ObjectName cpcl = |
| 94 | new ObjectName("ClassLoader:name=ClassPathClassLoader"); |
| 95 | mbs.createMBean("javax.management.loading.MLet", cpcl); |
| 96 | // Register the SimpleStandardMBean |
| 97 | // |
| 98 | System.out.println("Create SimpleStandard MBean"); |
| 99 | mbs.createMBean("SimpleStandard", |
| 100 | new ObjectName("MBeans:name=SimpleStandard")); |
| 101 | // Create Properties containing the location of the password file |
| 102 | // |
| 103 | Properties props = new Properties(); |
| 104 | props.setProperty("jmx.remote.x.password.file", passwordFile); |
| 105 | // Initialize environment map to be passed to the connector server |
| 106 | // |
| 107 | System.out.println("Initialize environment map"); |
| 108 | HashMap env = new HashMap(); |
| 109 | env.put("jmx.remote.authenticator", |
| 110 | new JMXPluggableAuthenticator(props)); |
| 111 | // Create an RMI connector server |
| 112 | // |
| 113 | System.out.println("Create an RMI connector server"); |
| 114 | JMXServiceURL url = |
| 115 | new JMXServiceURL("rmi", null, 0, |
| 116 | "/jndi/rmi://:" + port + "/server" + port); |
| 117 | JMXConnectorServer rcs = |
| 118 | JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); |
| 119 | rcs.start(); |
| 120 | |
| 121 | // Create an RMI connector client |
| 122 | // |
| 123 | System.out.println("Create an RMI connector client"); |
| 124 | HashMap cli_env = new HashMap(); |
| 125 | // These credentials must match those in the supplied password file |
| 126 | // |
| 127 | String[] credentials = new String[] { "monitorRole" , "QED" }; |
| 128 | cli_env.put("jmx.remote.credentials", credentials); |
| 129 | JMXConnector jmxc = JMXConnectorFactory.connect(url, cli_env); |
| 130 | MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); |
| 131 | // Get domains from MBeanServer |
| 132 | // |
| 133 | System.out.println("Domains:"); |
| 134 | String domains[] = mbsc.getDomains(); |
| 135 | for (int i = 0; i < domains.length; i++) { |
| 136 | System.out.println("\tDomain[" + i + "] = " + domains[i]); |
| 137 | } |
| 138 | // Get MBean count |
| 139 | // |
| 140 | System.out.println("MBean count = " + mbsc.getMBeanCount()); |
| 141 | // Get State attribute |
| 142 | // |
| 143 | String oldState = |
| 144 | (String) mbsc.getAttribute( |
| 145 | new ObjectName("MBeans:name=SimpleStandard"), |
| 146 | "State"); |
| 147 | System.out.println("Old State = \"" + oldState + "\""); |
| 148 | // Set State attribute |
| 149 | // |
| 150 | System.out.println("Set State to \"changed state\""); |
| 151 | mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"), |
| 152 | new Attribute("State", "changed state")); |
| 153 | // Get State attribute |
| 154 | // |
| 155 | String newState = |
| 156 | (String) mbsc.getAttribute( |
| 157 | new ObjectName("MBeans:name=SimpleStandard"), |
| 158 | "State"); |
| 159 | System.out.println("New State = \"" + newState + "\""); |
| 160 | if (!newState.equals("changed state")) { |
| 161 | System.out.println("Invalid State = \"" + newState + "\""); |
| 162 | System.exit(1); |
| 163 | } |
| 164 | // Add notification listener on SimpleStandard MBean |
| 165 | // |
| 166 | System.out.println("Add notification listener..."); |
| 167 | mbsc.addNotificationListener( |
| 168 | new ObjectName("MBeans:name=SimpleStandard"), |
| 169 | new NotificationListener() { |
| 170 | public void handleNotification(Notification notification, |
| 171 | Object handback) { |
| 172 | System.out.println("Received notification: " + |
| 173 | notification); |
| 174 | } |
| 175 | }, |
| 176 | null, |
| 177 | null); |
| 178 | // Unregister SimpleStandard MBean |
| 179 | // |
| 180 | System.out.println("Unregister SimpleStandard MBean..."); |
| 181 | mbsc.unregisterMBean(new ObjectName("MBeans:name=SimpleStandard")); |
| 182 | // Close MBeanServer connection |
| 183 | // |
| 184 | jmxc.close(); |
| 185 | System.out.println("Bye! Bye!"); |
| 186 | } catch (Exception e) { |
| 187 | System.out.println("Unexpected exception caught = " + e); |
| 188 | e.printStackTrace(); |
| 189 | System.exit(1); |
| 190 | } |
| 191 | } |
| 192 | } |