0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4924664
0N/A * @summary Tests the use of the "jmx.remote.x.password.file" and
0N/A * "jmx.remote.x.access.file" environment map properties.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean PasswordAccessFileTest SimpleStandard SimpleStandardMBean
0N/A * @run build PasswordAccessFileTest SimpleStandard SimpleStandardMBean
0N/A * @run main PasswordAccessFileTest
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.util.HashMap;
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerConnection;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.MBeanServerInvocationHandler;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/A
0N/Apublic class PasswordAccessFileTest {
0N/A
0N/A public static void main(String[] args) {
0N/A try {
0N/A //------------------------------------------------------------------
0N/A // SERVER
0N/A //------------------------------------------------------------------
0N/A
0N/A // Instantiate the MBean server
0N/A //
0N/A System.out.println("Create the MBean server");
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A
0N/A // Create SimpleStandard MBean
0N/A //
0N/A ObjectName mbeanName = new ObjectName("MBeans:type=SimpleStandard");
0N/A System.out.println("Create SimpleStandard MBean...");
0N/A mbs.createMBean("SimpleStandard", mbeanName, null, null);
0N/A
0N/A // Server's environment map
0N/A //
0N/A System.out.println(">>> Initialize the server's environment map");
0N/A HashMap sEnv = new HashMap();
0N/A
0N/A // Provide the password file used by the connector server to
0N/A // perform user authentication. The password file is a properties
0N/A // based text file specifying username/password pairs. This
0N/A // properties based password authenticator has been implemented
0N/A // using the JMXAuthenticator interface and is passed to the
0N/A // connector through the "jmx.remote.authenticator" property
0N/A // in the map.
0N/A //
0N/A // This property is implementation-dependent and might not be
0N/A // supported by all implementations of the JMX Remote API.
0N/A //
0N/A sEnv.put("jmx.remote.x.password.file",
0N/A System.getProperty("test.src") +
0N/A File.separator +
0N/A "password.properties");
0N/A
0N/A // Provide the access level file used by the connector server to
0N/A // perform user authorization. The access level file is a properties
0N/A // based text file specifying username/access level pairs where
0N/A // access level is either "readonly" or "readwrite" access to the
0N/A // MBeanServer operations. This properties based access control
0N/A // checker has been implemented using the MBeanServerForwarder
0N/A // interface which wraps the real MBean server inside an access
0N/A // controller MBean server which performs the access control checks
0N/A // before forwarding the requests to the real MBean server.
0N/A //
0N/A // This property is implementation-dependent and might not be
0N/A // supported by all implementations of the JMX Remote API.
0N/A //
0N/A sEnv.put("jmx.remote.x.access.file",
0N/A System.getProperty("test.src") +
0N/A File.separator +
0N/A "access.properties");
0N/A
0N/A // Create an RMI connector server
0N/A //
0N/A System.out.println("Create an RMI connector server");
0N/A JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
0N/A JMXConnectorServer cs =
0N/A JMXConnectorServerFactory.newJMXConnectorServer(url, sEnv, mbs);
0N/A
0N/A // Start the RMI connector server
0N/A //
0N/A System.out.println("Start the RMI connector server");
0N/A cs.start();
0N/A System.out.println("RMI connector server successfully started");
0N/A System.out.println("Waiting for incoming connections...");
0N/A
0N/A //------------------------------------------------------------------
0N/A // CLIENT : Invalid authentication credentials
0N/A //------------------------------------------------------------------
0N/A
0N/A final String invalidCreds[][] = {
0N/A {"admin1", "adminPassword"},
0N/A {"admin", "adminPassword1"},
0N/A {"user1", "userPassword"},
0N/A {"user", "userPassword1"}
0N/A };
0N/A
0N/A // Try to connect to the server using the invalid credentials.
0N/A // All the connect calls should get SecurityException.
0N/A //
0N/A for (int i = 0 ; i < invalidCreds.length ; i++) {
0N/A // Client environment map
0N/A //
0N/A System.out.println(">>> Initialize the client environment map" +
0N/A " for user [" +
0N/A invalidCreds[i][0] +
0N/A "] with password [" +
0N/A invalidCreds[i][1] + "]");
0N/A HashMap cEnv = new HashMap();
0N/A cEnv.put("jmx.remote.credentials", invalidCreds[i]);
0N/A
0N/A // Create an RMI connector client and
0N/A // connect it to the RMI connector server
0N/A //
0N/A System.out.println("Create an RMI connector client and " +
0N/A "connect it to the RMI connector server");
0N/A try {
0N/A JMXConnector jmxc =
0N/A JMXConnectorFactory.connect(cs.getAddress(), cEnv);
0N/A } catch (SecurityException e) {
0N/A System.out.println("Got expected security exception: " + e);
0N/A } catch (Exception e) {
0N/A System.out.println("Got unexpected exception: " + e);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A //------------------------------------------------------------------
0N/A // CLIENT (admin)
0N/A //------------------------------------------------------------------
0N/A
0N/A // Admin client environment map
0N/A //
0N/A String[] adminCreds = new String[] { "admin" , "adminPassword" };
0N/A System.out.println(">>> Initialize the client environment map for" +
0N/A " user [" + adminCreds[0] + "] with " +
0N/A "password [" + adminCreds[1] + "]");
0N/A HashMap adminEnv = new HashMap();
0N/A adminEnv.put("jmx.remote.credentials", adminCreds);
0N/A
0N/A // Create an RMI connector client and
0N/A // connect it to the RMI connector server
0N/A //
0N/A System.out.println("Create an RMI connector client and " +
0N/A "connect it to the RMI connector server");
0N/A JMXConnector adminConnector =
0N/A JMXConnectorFactory.connect(cs.getAddress(), adminEnv);
0N/A
0N/A // Get an MBeanServerConnection
0N/A //
0N/A System.out.println("Get an MBeanServerConnection");
0N/A MBeanServerConnection adminConnection =
0N/A adminConnector.getMBeanServerConnection();
0N/A
0N/A // Get the proxy for the Simple MBean
0N/A //
0N/A SimpleStandardMBean adminProxy = (SimpleStandardMBean)
0N/A MBeanServerInvocationHandler.newProxyInstance(
0N/A adminConnection,
0N/A mbeanName,
0N/A SimpleStandardMBean.class,
0N/A false);
0N/A
0N/A // Get State attribute
0N/A //
0N/A System.out.println("State = " + adminProxy.getState());
0N/A
0N/A // Set State attribute
0N/A //
0N/A adminProxy.setState("changed state");
0N/A
0N/A // Get State attribute
0N/A //
0N/A System.out.println("State = " + adminProxy.getState());
0N/A
0N/A // Invoke "reset" in SimpleStandard MBean
0N/A //
0N/A System.out.println("Invoke reset() in SimpleStandard MBean...");
0N/A adminProxy.reset();
0N/A
0N/A // Close MBeanServer connection
0N/A //
0N/A System.out.println("Close the admin connection to the server");
0N/A adminConnector.close();
0N/A
0N/A //------------------------------------------------------------------
0N/A // CLIENT (user)
0N/A //------------------------------------------------------------------
0N/A
0N/A // User client environment map
0N/A //
0N/A String[] userCreds = new String[] { "user" , "userPassword" };
0N/A System.out.println(">>> Initialize the client environment map for" +
0N/A " user [" + userCreds[0] + "] with " +
0N/A "password [" + userCreds[1] + "]");
0N/A HashMap userEnv = new HashMap();
0N/A userEnv.put("jmx.remote.credentials", userCreds);
0N/A
0N/A // Create an RMI connector client and
0N/A // connect it to the RMI connector server
0N/A //
0N/A System.out.println("Create an RMI connector client and " +
0N/A "connect it to the RMI connector server");
0N/A JMXConnector userConnector =
0N/A JMXConnectorFactory.connect(cs.getAddress(), userEnv);
0N/A
0N/A // Get an MBeanServerConnection
0N/A //
0N/A System.out.println("Get an MBeanServerConnection");
0N/A MBeanServerConnection userConnection =
0N/A userConnector.getMBeanServerConnection();
0N/A
0N/A // Get the proxy for the Simple MBean
0N/A //
0N/A SimpleStandardMBean userProxy = (SimpleStandardMBean)
0N/A MBeanServerInvocationHandler.newProxyInstance(
0N/A userConnection,
0N/A mbeanName,
0N/A SimpleStandardMBean.class,
0N/A false);
0N/A
0N/A // Get State attribute
0N/A //
0N/A System.out.println("State = " + userProxy.getState());
0N/A
0N/A // Set State attribute
0N/A //
0N/A try {
0N/A userProxy.setState("changed state");
0N/A } catch (SecurityException e) {
0N/A System.out.println("Got expected security exception: " + e);
0N/A } catch (Exception e) {
0N/A System.out.println("Got unexpected exception: " + e);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A
0N/A // Get State attribute
0N/A //
0N/A System.out.println("State = " + userProxy.getState());
0N/A
0N/A // Invoke "reset" in SimpleStandard MBean
0N/A //
0N/A try {
0N/A System.out.println("Invoke reset() in SimpleStandard MBean...");
0N/A userProxy.reset();
0N/A } catch (SecurityException e) {
0N/A System.out.println("Got expected security exception: " + e);
0N/A } catch (Exception e) {
0N/A System.out.println("Got unexpected exception: " + e);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A
0N/A // Close MBeanServer connection
0N/A //
0N/A System.out.println("Close the user connection to the server");
0N/A userConnector.close();
0N/A
0N/A //------------------------------------------------------------------
0N/A // SERVER
0N/A //------------------------------------------------------------------
0N/A
0N/A // Stop the connector server
0N/A //
0N/A System.out.println(">>> Stop the connector server");
0N/A cs.stop();
0N/A
0N/A System.out.println("Bye! Bye!");
0N/A } catch (Exception e) {
0N/A System.out.println("Got unexpected exception: " + e);
0N/A e.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A }
0N/A}