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