Search in sources :

Example 81 with AccessControlContext

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) {
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) DatagramSocket(java.net.DatagramSocket) SocketPermission(java.net.SocketPermission) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test)

Example 82 with AccessControlContext

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);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext)

Example 83 with AccessControlContext

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);
}
Also used : AccessControlContext(java.security.AccessControlContext)

Example 84 with AccessControlContext

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);
}
Also used : Set(java.util.Set) AccessControlContext(java.security.AccessControlContext) JMXPrincipal(javax.management.remote.JMXPrincipal) Subject(javax.security.auth.Subject) Principal(java.security.Principal) JMXPrincipal(javax.management.remote.JMXPrincipal)

Example 85 with AccessControlContext

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;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) AccessControlContext(java.security.AccessControlContext) Row(org.apache.karaf.shell.support.table.Row) Subject(javax.security.auth.Subject) Principal(java.security.Principal)

Aggregations

AccessControlContext (java.security.AccessControlContext)100 ProtectionDomain (java.security.ProtectionDomain)24 Subject (javax.security.auth.Subject)24 PrivilegedAction (java.security.PrivilegedAction)18 Permissions (java.security.Permissions)14 PrivilegedActionException (java.security.PrivilegedActionException)13 IOException (java.io.IOException)11 SocketPermission (java.net.SocketPermission)10 Test (org.testng.annotations.Test)8 Principal (java.security.Principal)7 CodeSource (java.security.CodeSource)6 Permission (java.security.Permission)6 DatagramSocket (java.net.DatagramSocket)5 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)5 MulticastSocket (java.net.MulticastSocket)4 Set (java.util.Set)4 ExecutorService (java.util.concurrent.ExecutorService)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ReflectionException (javax.management.ReflectionException)4 Test (org.junit.Test)4