Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/remote/mandatory/subjectDelegation/SubjectDelegation1Test.java
41159 views
1
/*
2
* Copyright (c) 2003, 2017, 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 6261831
27
* @summary Tests the use of the subject delegation feature in the
28
* RMI connector
29
* @author Luis-Miguel Alventosa
30
* @modules java.management.rmi
31
* java.management/com.sun.jmx.remote.security
32
* @run clean SubjectDelegation1Test SimpleStandard SimpleStandardMBean
33
* @run build SubjectDelegation1Test SimpleStandard SimpleStandardMBean
34
* @run main SubjectDelegation1Test policy11 ok
35
* @run main SubjectDelegation1Test policy12 ko
36
* @run main SubjectDelegation1Test policy13 ko
37
* @run main SubjectDelegation1Test policy14 ko
38
* @run main SubjectDelegation1Test policy15 ok
39
* @run main SubjectDelegation1Test policy16 ko
40
*/
41
42
import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
43
import java.io.File;
44
import java.lang.management.ManagementFactory;
45
import java.rmi.RemoteException;
46
import java.rmi.registry.LocateRegistry;
47
import java.rmi.registry.Registry;
48
import java.util.Collections;
49
import java.util.HashMap;
50
import java.util.Properties;
51
import javax.management.Attribute;
52
import javax.management.MBeanServer;
53
import javax.management.MBeanServerConnection;
54
import javax.management.Notification;
55
import javax.management.NotificationListener;
56
import javax.management.ObjectName;
57
import javax.management.remote.JMXConnector;
58
import javax.management.remote.JMXConnectorFactory;
59
import javax.management.remote.JMXConnectorServer;
60
import javax.management.remote.JMXConnectorServerFactory;
61
import javax.management.remote.JMXPrincipal;
62
import javax.management.remote.JMXServiceURL;
63
import javax.security.auth.Subject;
64
65
public class SubjectDelegation1Test {
66
67
public static void main(String[] args) throws Exception {
68
String policyFile = args[0];
69
String testResult = args[1];
70
System.out.println("Policy file = " + policyFile);
71
System.out.println("Expected test result = " + testResult);
72
JMXConnectorServer jmxcs = null;
73
JMXConnector jmxc = null;
74
try {
75
// Create an RMI registry
76
//
77
System.out.println("Start RMI registry...");
78
Registry reg = null;
79
int port = 5800;
80
while (port++ < 6000) {
81
try {
82
reg = LocateRegistry.createRegistry(port);
83
System.out.println("RMI registry running on port " + port);
84
break;
85
} catch (RemoteException e) {
86
// Failed to create RMI registry...
87
System.out.println("Failed to create RMI registry " +
88
"on port " + port);
89
}
90
}
91
if (reg == null) {
92
System.exit(1);
93
}
94
// Set the default password file
95
//
96
final String passwordFile = System.getProperty("test.src") +
97
File.separator + "jmxremote.password";
98
System.out.println("Password file = " + passwordFile);
99
// Set policy file
100
//
101
final String policy = System.getProperty("test.src") +
102
File.separator + policyFile;
103
System.out.println("PolicyFile = " + policy);
104
System.setProperty("java.security.policy", policy);
105
// Instantiate the MBean server
106
//
107
System.out.println("Create the MBean server");
108
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
109
// Register the SimpleStandardMBean
110
//
111
System.out.println("Create SimpleStandard MBean");
112
SimpleStandard s = new SimpleStandard("delegate");
113
mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
114
// Create Properties containing the username/password entries
115
//
116
Properties props = new Properties();
117
props.setProperty("jmx.remote.x.password.file", passwordFile);
118
// Initialize environment map to be passed to the connector server
119
//
120
System.out.println("Initialize environment map");
121
HashMap env = new HashMap();
122
env.put("jmx.remote.authenticator",
123
new JMXPluggableAuthenticator(props));
124
// Create an RMI connector server
125
//
126
System.out.println("Create an RMI connector server");
127
JMXServiceURL url =
128
new JMXServiceURL("rmi", null, 0,
129
"/jndi/rmi://:" + port + "/server" + port);
130
jmxcs =
131
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
132
jmxcs.start();
133
// Create an RMI connector client
134
//
135
System.out.println("Create an RMI connector client");
136
HashMap cli_env = new HashMap();
137
// These credentials must match those in the default password file
138
//
139
String[] credentials = new String[] { "monitorRole" , "QED" };
140
cli_env.put("jmx.remote.credentials", credentials);
141
jmxc = JMXConnectorFactory.connect(url, cli_env);
142
Subject delegationSubject =
143
new Subject(true,
144
Collections.singleton(new JMXPrincipal("delegate")),
145
Collections.EMPTY_SET,
146
Collections.EMPTY_SET);
147
MBeanServerConnection mbsc =
148
jmxc.getMBeanServerConnection(delegationSubject);
149
// Get domains from MBeanServer
150
//
151
System.out.println("Domains:");
152
String domains[] = mbsc.getDomains();
153
for (int i = 0; i < domains.length; i++) {
154
System.out.println("\tDomain[" + i + "] = " + domains[i]);
155
}
156
// Get MBean count
157
//
158
System.out.println("MBean count = " + mbsc.getMBeanCount());
159
// Get State attribute
160
//
161
String oldState =
162
(String) mbsc.getAttribute(
163
new ObjectName("MBeans:type=SimpleStandard"),
164
"State");
165
System.out.println("Old State = \"" + oldState + "\"");
166
// Set State attribute
167
//
168
System.out.println("Set State to \"changed state\"");
169
mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"),
170
new Attribute("State", "changed state"));
171
// Get State attribute
172
//
173
String newState =
174
(String) mbsc.getAttribute(
175
new ObjectName("MBeans:type=SimpleStandard"),
176
"State");
177
System.out.println("New State = \"" + newState + "\"");
178
if (!newState.equals("changed state")) {
179
System.out.println("Invalid State = \"" + newState + "\"");
180
System.exit(1);
181
}
182
// Add notification listener on SimpleStandard MBean
183
//
184
System.out.println("Add notification listener...");
185
mbsc.addNotificationListener(
186
new ObjectName("MBeans:type=SimpleStandard"),
187
new NotificationListener() {
188
public void handleNotification(Notification notification,
189
Object handback) {
190
System.out.println("Received notification: " +
191
notification);
192
}
193
},
194
null,
195
null);
196
// Unregister SimpleStandard MBean
197
//
198
System.out.println("Unregister SimpleStandard MBean...");
199
mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
200
} catch (SecurityException e) {
201
if (testResult.equals("ko")) {
202
System.out.println("Got expected security exception = " + e);
203
} else {
204
System.out.println("Got unexpected security exception = " + e);
205
e.printStackTrace();
206
throw e;
207
}
208
} catch (Exception e) {
209
System.out.println("Unexpected exception caught = " + e);
210
e.printStackTrace();
211
throw e;
212
} finally {
213
// Close connector client
214
//
215
if (jmxc != null)
216
jmxc.close();
217
// Stop connector server
218
//
219
if (jmxcs != null)
220
jmxcs.stop();
221
// Say goodbye
222
//
223
System.out.println("Bye! Bye!");
224
}
225
}
226
}
227
228