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