Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/subjectDelegation/SubjectDelegation2Test.java
38867 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 626183126* @summary Tests the use of the subject delegation feature on the authenticated27* principals within the RMI connector server's creator codebase.28* @author Luis-Miguel Alventosa29* @run clean SubjectDelegation2Test SimpleStandard SimpleStandardMBean30* @run build SubjectDelegation2Test SimpleStandard SimpleStandardMBean31* @run main SubjectDelegation2Test policy21 ok32* @run main SubjectDelegation2Test policy22 ko33* @run main SubjectDelegation2Test policy23 ko34* @run main SubjectDelegation2Test policy24 ok35* @run main SubjectDelegation2Test policy25 ko36*/3738import com.sun.jmx.remote.security.JMXPluggableAuthenticator;39import java.io.File;40import java.lang.management.ManagementFactory;41import java.rmi.RemoteException;42import java.rmi.registry.LocateRegistry;43import java.rmi.registry.Registry;44import java.util.Collections;45import java.util.HashMap;46import java.util.Properties;47import javax.management.Attribute;48import javax.management.MBeanServer;49import javax.management.MBeanServerConnection;50import javax.management.Notification;51import javax.management.NotificationListener;52import javax.management.ObjectName;53import javax.management.remote.JMXConnector;54import javax.management.remote.JMXConnectorFactory;55import javax.management.remote.JMXConnectorServer;56import javax.management.remote.JMXConnectorServerFactory;57import javax.management.remote.JMXPrincipal;58import javax.management.remote.JMXServiceURL;59import javax.security.auth.Subject;6061public class SubjectDelegation2Test {6263public static void main(String[] args) throws Exception {64// Check for supported operating systems: Solaris65//66// This test runs only on Solaris due to CR 628591667//68String osName = System.getProperty("os.name");69System.out.println("os.name = " + osName);70if (!osName.equals("SunOS")) {71System.out.println("This test runs on Solaris only.");72System.out.println("Bye! Bye!");73return;74}75String policyFile = args[0];76String testResult = args[1];77System.out.println("Policy file = " + policyFile);78System.out.println("Expected test result = " + testResult);79JMXConnectorServer jmxcs = null;80JMXConnector jmxc = null;81try {82// Create an RMI registry83//84System.out.println("Start RMI registry...");85Registry reg = null;86int port = 5800;87while (port++ < 6000) {88try {89reg = LocateRegistry.createRegistry(port);90System.out.println("RMI registry running on port " + port);91break;92} catch (RemoteException e) {93// Failed to create RMI registry...94System.out.println("Failed to create RMI registry " +95"on port " + port);96}97}98if (reg == null) {99System.exit(1);100}101// Set the default password file102//103final String passwordFile = System.getProperty("test.src") +104File.separator + "jmxremote.password";105System.out.println("Password file = " + passwordFile);106// Set policy file107//108final String policy = System.getProperty("test.src") +109File.separator + policyFile;110System.out.println("PolicyFile = " + policy);111System.setProperty("java.security.policy", policy);112// Instantiate the MBean server113//114System.out.println("Create the MBean server");115MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();116// Register the SimpleStandardMBean117//118System.out.println("Create SimpleStandard MBean");119SimpleStandard s = new SimpleStandard("monitorRole");120mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));121// Create Properties containing the username/password entries122//123Properties props = new Properties();124props.setProperty("jmx.remote.x.password.file", passwordFile);125// Initialize environment map to be passed to the connector server126//127System.out.println("Initialize environment map");128HashMap env = new HashMap();129env.put("jmx.remote.authenticator",130new JMXPluggableAuthenticator(props));131// Set Security Manager132//133System.setSecurityManager(new SecurityManager());134// Create an RMI connector server135//136System.out.println("Create an RMI connector server");137JMXServiceURL url =138new JMXServiceURL("rmi", null, 0,139"/jndi/rmi://:" + port + "/server" + port);140jmxcs =141JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);142jmxcs.start();143// Create an RMI connector client144//145System.out.println("Create an RMI connector client");146HashMap cli_env = new HashMap();147// These credentials must match those in the default password file148//149String[] credentials = new String[] { "monitorRole" , "QED" };150cli_env.put("jmx.remote.credentials", credentials);151jmxc = JMXConnectorFactory.connect(url, cli_env);152MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();153// Get domains from MBeanServer154//155System.out.println("Domains:");156String domains[] = mbsc.getDomains();157for (int i = 0; i < domains.length; i++) {158System.out.println("\tDomain[" + i + "] = " + domains[i]);159}160// Get MBean count161//162System.out.println("MBean count = " + mbsc.getMBeanCount());163// Get State attribute164//165String oldState =166(String) mbsc.getAttribute(167new ObjectName("MBeans:type=SimpleStandard"),168"State");169System.out.println("Old State = \"" + oldState + "\"");170// Set State attribute171//172System.out.println("Set State to \"changed state\"");173mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),174new Attribute("State", "changed state"));175// Get State attribute176//177String newState =178(String) mbsc.getAttribute(179new ObjectName("MBeans:type=SimpleStandard"),180"State");181System.out.println("New State = \"" + newState + "\"");182if (!newState.equals("changed state")) {183System.out.println("Invalid State = \"" + newState + "\"");184System.exit(1);185}186// Add notification listener on SimpleStandard MBean187//188System.out.println("Add notification listener...");189mbsc.addNotificationListener(190new ObjectName("MBeans:type=SimpleStandard"),191new NotificationListener() {192public void handleNotification(Notification notification,193Object handback) {194System.out.println("Received notification: " +195notification);196}197},198null,199null);200// Unregister SimpleStandard MBean201//202System.out.println("Unregister SimpleStandard MBean...");203mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));204} catch (SecurityException e) {205if (testResult.equals("ko")) {206System.out.println("Got expected security exception = " + e);207} else {208System.out.println("Got unexpected security exception = " + e);209e.printStackTrace();210throw e;211}212} catch (Exception e) {213System.out.println("Unexpected exception caught = " + e);214e.printStackTrace();215throw e;216} finally {217// Close connector client218//219if (jmxc != null)220jmxc.close();221// Stop connector server222//223if (jmxcs != null)224jmxcs.stop();225// Say goodbye226//227System.out.println("Bye! Bye!");228}229}230}231232233