use of java.security.AccessControlContext in project XobotOS by xamarin.
the class Subject method doAs_PrivilegedExceptionAction.
// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static <T> T doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction<T> action, final AccessControlContext context) throws PrivilegedActionException {
AccessControlContext newContext;
final SubjectDomainCombiner combiner;
if (subject == null) {
// performance optimization
// if subject is null there is nothing to combine
combiner = null;
} else {
combiner = new SubjectDomainCombiner(subject);
}
PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {
public AccessControlContext run() {
return new AccessControlContext(context, combiner);
}
};
newContext = AccessController.doPrivileged(dccAction);
return AccessController.doPrivileged(action, newContext);
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method acceptServerSocketTest.
@Test
public void acceptServerSocketTest() throws Exception {
try (ServerSocket ss = new ServerSocket(0)) {
int port = ss.getLocalPort();
String addr = "localhost:" + port;
AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,connect,resolve"), new SocketPermission("localhost:1024-", "accept"));
// Positive
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
InetAddress me = InetAddress.getLocalHost();
try (Socket client = new Socket(me, port)) {
ss.accept();
}
return null;
}, acc);
// Negative
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
InetAddress me = InetAddress.getLocalHost();
try (Socket client = new Socket(me, port)) {
ss.accept();
}
fail("Expected SecurityException");
return null;
}, RESTRICTED_ACC);
} catch (SecurityException expected) {
}
}
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method joinGroupMulticastTest.
@Test
public void joinGroupMulticastTest() throws Exception {
InetAddress group = InetAddress.getByName("229.227.226.221");
try (MulticastSocket s = new MulticastSocket(0)) {
int port = s.getLocalPort();
String addr = "localhost:" + port;
AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept"));
// Positive
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
s.joinGroup(group);
s.leaveGroup(group);
return null;
}, acc);
// Negative
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
s.joinGroup(group);
s.leaveGroup(group);
fail("Expected SecurityException");
return null;
}, RESTRICTED_ACC);
} catch (SecurityException expected) {
}
}
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method sendDatagramPacketTest.
@Test
public void sendDatagramPacketTest() throws Exception {
byte[] msg = "Hello".getBytes(UTF_8);
InetAddress group = InetAddress.getByName("229.227.226.221");
try (DatagramSocket ds = new DatagramSocket(0)) {
int port = ds.getLocalPort();
String addr = "localhost:" + port;
//test for SocketPermission "229.227.226.221", "connect,accept"
AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept"));
// Positive
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
ds.send(hi);
return null;
}, acc);
// Negative
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
ds.send(hi);
fail("Expected SecurityException");
return null;
}, RESTRICTED_ACC);
} catch (SecurityException expected) {
}
}
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class SimpleStandard method checkSubject.
/*
* ---------------
* PRIVATE METHODS
* ---------------
*/
/**
* Check that the principal contained in the Subject is of
* type JMXPrincipal and refers to the principalName identity.
*/
private void checkSubject(String op) {
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
Set principals = subject.getPrincipals();
Principal principal = (Principal) principals.iterator().next();
if (!(principal instanceof JMXPrincipal))
throw new SecurityException(op + ": Authenticated subject contains " + "invalid principal type = " + principal.getClass().getName());
String identity = principal.getName();
if (!identity.equals(principalName))
throw new SecurityException(op + ": Authenticated subject contains " + "invalid principal name = " + identity);
}
Aggregations