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