use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class SocketPermissionTest method connectDatagramSocketTest.
@Test
public void connectDatagramSocketTest() throws Exception {
byte[] msg = "Hello".getBytes(UTF_8);
InetAddress lh = InetAddress.getLocalHost();
try (DatagramSocket ds = new DatagramSocket(0)) {
int port = ds.getLocalPort();
String addr = lh.getHostAddress() + ":" + port;
AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "connect,resolve"));
// Positive
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
ds.send(dp);
return null;
}, acc);
// Negative
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
ds.send(dp);
fail("Expected SecurityException");
return null;
}, RESTRICTED_ACC);
} catch (SecurityException expected) {
}
}
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class CheckCtor method main.
public static void main(String[] args) throws Exception {
// check that null PD array throws NPE
try {
new AccessControlContext(null);
throw new Exception("Expected NullPointerException not thrown");
} catch (Exception e) {
if (!(e instanceof NullPointerException)) {
throw new Exception("Expected NullPointerException not thrown");
}
}
// check that empty PD array equals PD array of one or more nulls
ProtectionDomain[] zero = {};
ProtectionDomain[] null1 = { null };
ProtectionDomain[] null2 = { null, null };
AccessControlContext accZero = new AccessControlContext(zero);
AccessControlContext accNull1 = new AccessControlContext(null1);
AccessControlContext accNull2 = new AccessControlContext(null2);
testEquals(accZero, accNull1);
testEquals(accZero, accNull2);
testEquals(accNull1, accNull2);
testEquals(accNull1, accZero);
testEquals(accNull2, accZero);
testEquals(accNull2, accNull1);
}
use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.
the class ChildThread method run.
@Override
public void run() {
//Verified that child thread has permission p1,
runTest(null, P1, false, 1);
//Verified that child thread inherits parent thread's access control context
AccessControlContext childAcc = AccessController.getContext();
runTest(childAcc, P1, true, 2);
//Verified that we can give permision p2 to limit the "privilege" of the
//class calling doprivileged action, stack walk will continue
runTest(null, P2, true, 3);
}
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 "monitorRole" identity.
*/
private void checkSubject() {
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("Authenticated subject contains " + "invalid principal type = " + principal.getClass().getName());
String identity = principal.getName();
if (!identity.equals("monitorRole"))
throw new SecurityException("Authenticated subject contains " + "invalid principal name = " + identity);
}
use of java.security.AccessControlContext in project karaf by apache.
the class WhoamiCommand method execute.
@Override
public Object execute() throws Exception {
ShellTable table = new ShellTable();
// Get the currently-active JAAS Subject.
AccessControlContext acc = AccessController.getContext();
Subject subj = Subject.getSubject(acc);
String classString = USER_CLASS;
if (groups) {
classString = GROUP_CLASS;
} else if (roles) {
classString = ROLE_CLASS;
} else if (all) {
classString = ALL_CLASS;
}
Class c = Class.forName(classString);
Set<Principal> principals = subj.getPrincipals(c);
table.column("Name");
if (all) {
table.column("Class");
}
for (Principal p : principals) {
Row row = table.addRow();
row.addContent(p.getName());
if (all) {
row.addContent(p.getClass().getCanonicalName());
}
}
table.print(System.out, !noFormat);
return null;
}
Aggregations