if (creds.length != 2) throw new SecurityException("Bad credentials");
String user = creds[0];
String password = creds[1];
if (!"test".equals(user)) throw new SecurityException("Unknown user");
if (!"test".equals(password)) throw new SecurityException("Wrong password");
Principal principal = new JMXPrincipal(user);
Set principals = new HashSet();
principals.add(principal);
Subject subject = new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
return subject;
}
});
JMXConnectorServer cntorServer = null;
JMXConnector cntor = null;
try
{
cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, serverEnv, newMBeanServer());
// Required by the server
addPermission(new SocketPermission("localhost:" + url.getPort(), "listen"));
cntorServer.start();
policy.setSeparateClientServerPermissions(true);
Map clientEnv = createClientEnvironment();
clientEnv.put(JMXConnector.CREDENTIALS, new String[]{"test", "test"});
policy.addClientPermission(new SocketPermission("localhost:" + url.getPort(), "connect"));
policy.addServerPermission(new JMXPrincipal("test"), new AuthPermission("doAsPrivileged"));
policy.addServerPermission(new JMXPrincipal("test"), new SocketPermission("localhost:" + url.getPort(), "listen"));
policy.addServerPermission(new JMXPrincipal("test"), new SocketPermission("*:1024-" + url.getPort(), "accept"));
cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), clientEnv);
addPermission(new MBeanTrustPermission("*"));
policy.addServerPermission(new JMXPrincipal("delegate"), new MBeanPermission("*", "instantiate, registerMBean, getAttribute"));
policy.addServerPermission(new JMXPrincipal("test"), new SubjectDelegationPermission(JMXPrincipal.class.getName() + ".delegate"));
Set delegates = new HashSet();
delegates.add(new JMXPrincipal("delegate"));
Subject delegate = new Subject(true, delegates, Collections.EMPTY_SET, Collections.EMPTY_SET);
MBeanServerConnection cntion = cntor.getMBeanServerConnection(delegate);
ObjectName name = ObjectName.getInstance(":name=subject");
cntion.createMBean(SubjectCheck.class.getName(), name, null);
policy.addServerPermission(new JMXPrincipal("delegate"), new AuthPermission("getSubject"));
Subject subject = (Subject)cntion.getAttribute(name, "Subject");
Set principals = subject.getPrincipals();
assertNotNull(principals);
assertEquals(principals.size(), 1);
Principal principal = (Principal)principals.iterator().next();
assertTrue(principal instanceof JMXPrincipal);
assertEquals(principal.getName(), "delegate");
}
finally
{
if (cntor != null) cntor.close();
if (cntorServer != null) cntorServer.stop();