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