Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/subjectDelegation/SubjectDelegation1Test.java
38867 views
/*1* Copyright (c) 2003, 2005, 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 in the27* RMI connector28* @author Luis-Miguel Alventosa29* @run clean SubjectDelegation1Test SimpleStandard SimpleStandardMBean30* @run build SubjectDelegation1Test SimpleStandard SimpleStandardMBean31* @run main SubjectDelegation1Test policy11 ok32* @run main SubjectDelegation1Test policy12 ko33* @run main SubjectDelegation1Test policy13 ko34* @run main SubjectDelegation1Test policy14 ko35* @run main SubjectDelegation1Test policy15 ok36* @run main SubjectDelegation1Test policy16 ko37*/3839import com.sun.jmx.remote.security.JMXPluggableAuthenticator;40import java.io.File;41import java.lang.management.ManagementFactory;42import java.rmi.RemoteException;43import java.rmi.registry.LocateRegistry;44import java.rmi.registry.Registry;45import java.util.Collections;46import java.util.HashMap;47import java.util.Properties;48import javax.management.Attribute;49import javax.management.MBeanServer;50import javax.management.MBeanServerConnection;51import javax.management.Notification;52import javax.management.NotificationListener;53import javax.management.ObjectName;54import javax.management.remote.JMXConnector;55import javax.management.remote.JMXConnectorFactory;56import javax.management.remote.JMXConnectorServer;57import javax.management.remote.JMXConnectorServerFactory;58import javax.management.remote.JMXPrincipal;59import javax.management.remote.JMXServiceURL;60import javax.security.auth.Subject;6162public class SubjectDelegation1Test {6364public static void main(String[] args) throws Exception {65// Check for supported operating systems: Solaris66//67// This test runs only on Solaris due to CR 628591668//69String osName = System.getProperty("os.name");70System.out.println("os.name = " + osName);71if (!osName.equals("SunOS")) {72System.out.println("This test runs on Solaris only.");73System.out.println("Bye! Bye!");74return;75}76String policyFile = args[0];77String testResult = args[1];78System.out.println("Policy file = " + policyFile);79System.out.println("Expected test result = " + testResult);80JMXConnectorServer jmxcs = null;81JMXConnector jmxc = null;82try {83// Create an RMI registry84//85System.out.println("Start RMI registry...");86Registry reg = null;87int port = 5800;88while (port++ < 6000) {89try {90reg = LocateRegistry.createRegistry(port);91System.out.println("RMI registry running on port " + port);92break;93} catch (RemoteException e) {94// Failed to create RMI registry...95System.out.println("Failed to create RMI registry " +96"on port " + port);97}98}99if (reg == null) {100System.exit(1);101}102// Set the default password file103//104final String passwordFile = System.getProperty("test.src") +105File.separator + "jmxremote.password";106System.out.println("Password file = " + passwordFile);107// Set policy file108//109final String policy = System.getProperty("test.src") +110File.separator + policyFile;111System.out.println("PolicyFile = " + policy);112System.setProperty("java.security.policy", policy);113// Instantiate the MBean server114//115System.out.println("Create the MBean server");116MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();117// Register the SimpleStandardMBean118//119System.out.println("Create SimpleStandard MBean");120SimpleStandard s = new SimpleStandard("delegate");121mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));122// Create Properties containing the username/password entries123//124Properties props = new Properties();125props.setProperty("jmx.remote.x.password.file", passwordFile);126// Initialize environment map to be passed to the connector server127//128System.out.println("Initialize environment map");129HashMap env = new HashMap();130env.put("jmx.remote.authenticator",131new JMXPluggableAuthenticator(props));132// Create an RMI connector server133//134System.out.println("Create an RMI connector server");135JMXServiceURL url =136new JMXServiceURL("rmi", null, 0,137"/jndi/rmi://:" + port + "/server" + port);138jmxcs =139JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);140jmxcs.start();141// Create an RMI connector client142//143System.out.println("Create an RMI connector client");144HashMap cli_env = new HashMap();145// These credentials must match those in the default password file146//147String[] credentials = new String[] { "monitorRole" , "QED" };148cli_env.put("jmx.remote.credentials", credentials);149jmxc = JMXConnectorFactory.connect(url, cli_env);150Subject delegationSubject =151new Subject(true,152Collections.singleton(new JMXPrincipal("delegate")),153Collections.EMPTY_SET,154Collections.EMPTY_SET);155MBeanServerConnection mbsc =156jmxc.getMBeanServerConnection(delegationSubject);157// Get domains from MBeanServer158//159System.out.println("Domains:");160String domains[] = mbsc.getDomains();161for (int i = 0; i < domains.length; i++) {162System.out.println("\tDomain[" + i + "] = " + domains[i]);163}164// Get MBean count165//166System.out.println("MBean count = " + mbsc.getMBeanCount());167// Get State attribute168//169String oldState =170(String) mbsc.getAttribute(171new ObjectName("MBeans:type=SimpleStandard"),172"State");173System.out.println("Old State = \"" + oldState + "\"");174// Set State attribute175//176System.out.println("Set State to \"changed state\"");177mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),178new Attribute("State", "changed state"));179// Get State attribute180//181String newState =182(String) mbsc.getAttribute(183new ObjectName("MBeans:type=SimpleStandard"),184"State");185System.out.println("New State = \"" + newState + "\"");186if (!newState.equals("changed state")) {187System.out.println("Invalid State = \"" + newState + "\"");188System.exit(1);189}190// Add notification listener on SimpleStandard MBean191//192System.out.println("Add notification listener...");193mbsc.addNotificationListener(194new ObjectName("MBeans:type=SimpleStandard"),195new NotificationListener() {196public void handleNotification(Notification notification,197Object handback) {198System.out.println("Received notification: " +199notification);200}201},202null,203null);204// Unregister SimpleStandard MBean205//206System.out.println("Unregister SimpleStandard MBean...");207mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));208} catch (SecurityException e) {209if (testResult.equals("ko")) {210System.out.println("Got expected security exception = " + e);211} else {212System.out.println("Got unexpected security exception = " + e);213e.printStackTrace();214throw e;215}216} catch (Exception e) {217System.out.println("Unexpected exception caught = " + e);218e.printStackTrace();219throw e;220} finally {221// Close connector client222//223if (jmxc != null)224jmxc.close();225// Stop connector server226//227if (jmxcs != null)228jmxcs.stop();229// Say goodbye230//231System.out.println("Bye! Bye!");232}233}234}235236237