Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/subjectDelegation/SubjectDelegation3Test.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 with28* subject delegation.29* @author Luis-Miguel Alventosa30* @run clean SubjectDelegation3Test SimpleStandard SimpleStandardMBean31* @run build SubjectDelegation3Test SimpleStandard SimpleStandardMBean32* @run main SubjectDelegation3Test policy31 ok33* @run main SubjectDelegation3Test policy32 ko34* @run main SubjectDelegation3Test policy33 ko35* @run main SubjectDelegation3Test policy34 ok36* @run main SubjectDelegation3Test policy35 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 SubjectDelegation3Test {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// Set Security Manager133//134System.setSecurityManager(new SecurityManager());135// Create an RMI connector server136//137System.out.println("Create an RMI connector server");138JMXServiceURL url =139new JMXServiceURL("rmi", null, 0,140"/jndi/rmi://:" + port + "/server" + port);141jmxcs =142JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);143jmxcs.start();144// Create an RMI connector client145//146System.out.println("Create an RMI connector client");147HashMap cli_env = new HashMap();148// These credentials must match those in the default password file149//150String[] credentials = new String[] { "monitorRole" , "QED" };151cli_env.put("jmx.remote.credentials", credentials);152jmxc = JMXConnectorFactory.connect(url, cli_env);153Subject delegationSubject =154new Subject(true,155Collections.singleton(new JMXPrincipal("delegate")),156Collections.EMPTY_SET,157Collections.EMPTY_SET);158MBeanServerConnection mbsc =159jmxc.getMBeanServerConnection(delegationSubject);160// Get domains from MBeanServer161//162System.out.println("Domains:");163String domains[] = mbsc.getDomains();164for (int i = 0; i < domains.length; i++) {165System.out.println("\tDomain[" + i + "] = " + domains[i]);166}167// Get MBean count168//169System.out.println("MBean count = " + mbsc.getMBeanCount());170// Get State attribute171//172String oldState =173(String) mbsc.getAttribute(174new ObjectName("MBeans:type=SimpleStandard"),175"State");176System.out.println("Old State = \"" + oldState + "\"");177// Set State attribute178//179System.out.println("Set State to \"changed state\"");180mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),181new Attribute("State", "changed state"));182// Get State attribute183//184String newState =185(String) mbsc.getAttribute(186new ObjectName("MBeans:type=SimpleStandard"),187"State");188System.out.println("New State = \"" + newState + "\"");189if (!newState.equals("changed state")) {190System.out.println("Invalid State = \"" + newState + "\"");191System.exit(1);192}193// Add notification listener on SimpleStandard MBean194//195System.out.println("Add notification listener...");196mbsc.addNotificationListener(197new ObjectName("MBeans:type=SimpleStandard"),198new NotificationListener() {199public void handleNotification(Notification notification,200Object handback) {201System.out.println("Received notification: " +202notification);203}204},205null,206null);207// Unregister SimpleStandard MBean208//209System.out.println("Unregister SimpleStandard MBean...");210mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));211} catch (SecurityException e) {212if (testResult.equals("ko")) {213System.out.println("Got expected security exception = " + e);214} else {215System.out.println("Got unexpected security exception = " + e);216e.printStackTrace();217throw e;218}219} catch (Exception e) {220System.out.println("Unexpected exception caught = " + e);221e.printStackTrace();222throw e;223} finally {224// Close connector client225//226if (jmxc != null)227jmxc.close();228// Stop connector server229//230if (jmxcs != null)231jmxcs.stop();232// Say goodbye233//234System.out.println("Bye! Bye!");235}236}237}238239240