Search in sources :

Example 1 with DomainCombiner

use of java.security.DomainCombiner in project jdk8u_jdk by JetBrains.

the class PreserveCombinerTest method main.

public static void main(String[] args) throws Exception {
    final DomainCombiner dc = new DomainCombiner() {

        @Override
        public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, ProtectionDomain[] assignedDomains) {
            // basically a no-op
            return currentDomains;
        }
    };
    // Get an instance of the saved ACC
    AccessControlContext saved = AccessController.getContext();
    // Simulate the stack ACC with a DomainCombiner attached
    AccessControlContext stack = new AccessControlContext(AccessController.getContext(), dc);
    // Now try to run JavaSecurityAccess.doIntersectionPrivilege() and assert
    // whether the DomainCombiner from the stack ACC is preserved
    boolean ret = SharedSecrets.getJavaSecurityAccess().doIntersectionPrivilege(new PrivilegedAction<Boolean>() {

        @Override
        public Boolean run() {
            return dc == AccessController.getContext().getDomainCombiner();
        }
    }, stack, saved);
    if (!ret) {
        System.exit(1);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) DomainCombiner(java.security.DomainCombiner) AccessControlContext(java.security.AccessControlContext)

Example 2 with DomainCombiner

use of java.security.DomainCombiner in project robovm by robovm.

the class AccessControllerTest method testDoPrivilegedWithCombiner.

public void testDoPrivilegedWithCombiner() {
    final Permission permission = new RuntimePermission("do stuff");
    final DomainCombiner union = new DomainCombiner() {

        public ProtectionDomain[] combine(ProtectionDomain[] a, ProtectionDomain[] b) {
            throw new AssertionFailedError("Expected combiner to be unused");
        }
    };
    ProtectionDomain protectionDomain = new ProtectionDomain(null, new Permissions());
    AccessControlContext accessControlContext = new AccessControlContext(new AccessControlContext(new ProtectionDomain[] { protectionDomain }), union);
    final AtomicInteger actionCount = new AtomicInteger();
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        public Void run() {
            assertEquals(null, AccessController.getContext().getDomainCombiner());
            AccessController.getContext().checkPermission(permission);
            // Calling doPrivileged again would have exercised the combiner
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                public Void run() {
                    actionCount.incrementAndGet();
                    assertEquals(null, AccessController.getContext().getDomainCombiner());
                    AccessController.getContext().checkPermission(permission);
                    return null;
                }
            });
            return null;
        }
    }, accessControlContext);
    assertEquals(1, actionCount.get());
}
Also used : ProtectionDomain(java.security.ProtectionDomain) DomainCombiner(java.security.DomainCombiner) AccessControlContext(java.security.AccessControlContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrivilegedAction(java.security.PrivilegedAction) Permission(java.security.Permission) Permissions(java.security.Permissions) AssertionFailedError(junit.framework.AssertionFailedError)

Example 3 with DomainCombiner

use of java.security.DomainCombiner in project openj9 by eclipse.

the class Test_AccessController method test_doPrivilegedWithCombiner2.

/**
 *        java.security.AccessController#doPrivilegedWithCombiner(java.security
 *        .PrivilegedExceptionAction)
 */
@Test
public void test_doPrivilegedWithCombiner2() {
    class MyDomainCombiner implements DomainCombiner {

        public ProtectionDomain[] combine(ProtectionDomain[] executionDomains, ProtectionDomain[] parentDomains) {
            Permissions perms = new Permissions();
            perms.add(new RuntimePermission("checking"));
            return new ProtectionDomain[] { new ProtectionDomain(null, perms) };
        }
    }
    AccessControlContext acc1 = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
    AccessControlContext acc = new AccessControlContext(acc1, new MyDomainCombiner());
    AccessController.doPrivileged(new PrivilegedAction() {

        public Object run() {
            try {
                AccessController.doPrivilegedWithCombiner(new PrivilegedExceptionAction() {

                    public Object run() {
                        AccessController.checkPermission(new RuntimePermission("checking"));
                        return null;
                    }
                });
            } catch (PrivilegedActionException e) {
                Assert.fail("Unexpected1: " + e);
            }
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction() {

                    public Object run() {
                        AccessController.checkPermission(new RuntimePermission("checking"));
                        return null;
                    }
                });
                Assert.fail("Expected SecurityException");
            } catch (SecurityException e) {
            // expected
            } catch (PrivilegedActionException e) {
                Assert.fail("Unexpected2: " + e);
            }
            return null;
        }
    }, acc);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) DomainCombiner(java.security.DomainCombiner) AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) PrivilegedActionException(java.security.PrivilegedActionException) Permissions(java.security.Permissions) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Test(org.testng.annotations.Test)

Example 4 with DomainCombiner

use of java.security.DomainCombiner in project AsmackService by rtreffer.

the class Subject method getSubject.

/**
 * Returns the {@code Subject} that was last associated with the {@code
 * context} provided as argument.
 *
 * @param context
 *            the {@code context} that was associated with the
 *            {@code Subject}.
 * @return the {@code Subject} that was last associated with the {@code
 *         context} provided as argument.
 */
public static Subject getSubject(final AccessControlContext context) {
    checkPermission(_SUBJECT);
    if (context == null) {
        // $NON-NLS-1$
        throw new NullPointerException("auth.09");
    }
    PrivilegedAction<DomainCombiner> action = new PrivilegedAction<DomainCombiner>() {

        public DomainCombiner run() {
            return context.getDomainCombiner();
        }
    };
    DomainCombiner combiner = AccessController.doPrivileged(action);
    if ((combiner == null) || !(combiner instanceof SubjectDomainCombiner)) {
        return null;
    }
    return ((SubjectDomainCombiner) combiner).getSubject();
}
Also used : DomainCombiner(java.security.DomainCombiner) PrivilegedAction(java.security.PrivilegedAction)

Example 5 with DomainCombiner

use of java.security.DomainCombiner in project robovm by robovm.

the class Subject method getSubject.

/**
     * Returns the {@code Subject} that was last associated with the {@code
     * context} provided as argument.
     *
     * @param context
     *            the {@code context} that was associated with the
     *            {@code Subject}.
     * @return the {@code Subject} that was last associated with the {@code
     *         context} provided as argument.
     */
public static Subject getSubject(final AccessControlContext context) {
    if (context == null) {
        throw new NullPointerException("context == null");
    }
    PrivilegedAction<DomainCombiner> action = new PrivilegedAction<DomainCombiner>() {

        public DomainCombiner run() {
            return context.getDomainCombiner();
        }
    };
    DomainCombiner combiner = AccessController.doPrivileged(action);
    if ((combiner == null) || !(combiner instanceof SubjectDomainCombiner)) {
        return null;
    }
    return ((SubjectDomainCombiner) combiner).getSubject();
}
Also used : DomainCombiner(java.security.DomainCombiner) PrivilegedAction(java.security.PrivilegedAction)

Aggregations

DomainCombiner (java.security.DomainCombiner)9 PrivilegedAction (java.security.PrivilegedAction)7 AccessControlContext (java.security.AccessControlContext)6 ProtectionDomain (java.security.ProtectionDomain)5 Permissions (java.security.Permissions)4 Permission (java.security.Permission)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AssertionFailedError (junit.framework.AssertionFailedError)2 Test (org.testng.annotations.Test)2 Principal (java.security.Principal)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 Subject (javax.security.auth.Subject)1 SubjectDomainCombiner (javax.security.auth.SubjectDomainCombiner)1 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)1