Search in sources :

Example 1 with Context

use of org.apache.jackrabbit.oak.spi.security.Context in project jackrabbit-oak by apache.

the class SecurityProviderRegistrationTest method testAuthorizationContext.

@Test
public void testAuthorizationContext() throws Exception {
    Tree t = root.getTree("/");
    Field f = registration.getClass().getDeclaredField("authorizationConfiguration");
    f.setAccessible(true);
    AuthorizationConfiguration ac = new AuthorizationConfigurationImpl();
    registration.bindAuthorizationConfiguration(ac, PROPS);
    CompositeAuthorizationConfiguration cac = (CompositeAuthorizationConfiguration) f.get(registration);
    Context ctx = cac.getContext();
    assertContext(ctx, 1, t, false);
    AuthorizationConfiguration ac1 = mockConfiguration(AuthorizationConfiguration.class);
    registration.bindAuthorizationConfiguration(ac1, PROPS);
    cac = (CompositeAuthorizationConfiguration) f.get(registration);
    ctx = cac.getContext();
    assertContext(ctx, 2, t, true);
    AuthorizationConfiguration ac2 = mockConfiguration(AuthorizationConfiguration.class);
    registration.bindAuthorizationConfiguration(ac2, PROPS);
    cac = (CompositeAuthorizationConfiguration) f.get(registration);
    ctx = cac.getContext();
    assertContext(ctx, 3, t, true);
    // unbind again:
    registration.unbindAuthorizationConfiguration(ac1, PROPS);
    cac = (CompositeAuthorizationConfiguration) f.get(registration);
    ctx = cac.getContext();
    assertContext(ctx, 2, t, true);
    registration.unbindAuthorizationConfiguration(ac, PROPS);
    cac = (CompositeAuthorizationConfiguration) f.get(registration);
    ctx = cac.getContext();
    assertContext(ctx, 1, t, true);
    registration.unbindAuthorizationConfiguration(ac2, PROPS);
    cac = (CompositeAuthorizationConfiguration) f.get(registration);
    ctx = cac.getContext();
    assertContext(ctx, 0, t, false);
}
Also used : OsgiContext(org.apache.sling.testing.mock.osgi.junit.OsgiContext) Context(org.apache.jackrabbit.oak.spi.security.Context) Field(java.lang.reflect.Field) CompositeAuthorizationConfiguration(org.apache.jackrabbit.oak.security.authorization.composite.CompositeAuthorizationConfiguration) AuthorizationConfiguration(org.apache.jackrabbit.oak.spi.security.authorization.AuthorizationConfiguration) AuthorizationConfigurationImpl(org.apache.jackrabbit.oak.security.authorization.AuthorizationConfigurationImpl) Tree(org.apache.jackrabbit.oak.api.Tree) CompositeAuthorizationConfiguration(org.apache.jackrabbit.oak.security.authorization.composite.CompositeAuthorizationConfiguration) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 2 with Context

use of org.apache.jackrabbit.oak.spi.security.Context in project jackrabbit-oak by apache.

the class AuthorizationContextTest method testLocation.

@Test
public void testLocation() throws Exception {
    createAcl("/", PrivilegeConstants.JCR_READ);
    Context ctx = AuthorizationContext.getInstance();
    String policyPath = "/rep:policy";
    assertTrue(ctx.definesLocation(TreeLocation.create(root, policyPath + "/allow")));
    assertTrue(ctx.definesLocation(TreeLocation.create(root, policyPath + "/allow/" + AccessControlConstants.REP_PRINCIPAL_NAME)));
    assertTrue(ctx.definesLocation(TreeLocation.create(root, policyPath + "/allow/" + AccessControlConstants.REP_PRIVILEGES)));
    List<String> existingRegular = ImmutableList.of("/", "/jcr:system");
    for (String path : existingRegular) {
        assertFalse(path, ctx.definesLocation(TreeLocation.create(root, path)));
        assertFalse(path, ctx.definesLocation(TreeLocation.create(root, PathUtils.concat(path, JcrConstants.JCR_PRIMARYTYPE))));
    }
    List<String> nonExistingItem = ImmutableList.of('/' + AccessControlConstants.REP_REPO_POLICY, "/content/" + AccessControlConstants.REP_POLICY, "/content/" + AccessControlConstants.REP_PRIVILEGES, "/content/" + AccessControlConstants.REP_REPO_POLICY, "/jcr:system/" + AccessControlConstants.REP_POLICY, PermissionConstants.PERMISSIONS_STORE_PATH + "/nonexisting");
    for (String path : nonExistingItem) {
        assertTrue(path, ctx.definesLocation(TreeLocation.create(root, path)));
        assertTrue(path, ctx.definesLocation(TreeLocation.create(root, PathUtils.concat(path, AccessControlConstants.REP_PRIVILEGES))));
    }
}
Also used : Context(org.apache.jackrabbit.oak.spi.security.Context) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 3 with Context

use of org.apache.jackrabbit.oak.spi.security.Context in project jackrabbit-oak by apache.

the class SecurityProviderRegistrationTest method assertContext.

private static void assertContext(@Nonnull Context context, int expectedSize, @Nonnull Tree tree, boolean isDefined) throws Exception {
    Class<?> c = context.getClass();
    assertTrue(c.getName().endsWith("CompositeContext"));
    Field f = c.getDeclaredField("delegatees");
    f.setAccessible(true);
    if (expectedSize == 0) {
        assertNull(f.get(context));
    } else {
        assertEquals(expectedSize, ((Context[]) f.get(context)).length);
    }
    assertEquals(isDefined, context.definesContextRoot(tree));
    assertEquals(isDefined, context.definesTree(tree));
    assertEquals(isDefined, context.definesProperty(tree, PropertyStates.createProperty("abc", "abc")));
    assertEquals(isDefined, context.definesLocation(TreeLocation.create(tree)));
}
Also used : OsgiContext(org.apache.sling.testing.mock.osgi.junit.OsgiContext) Context(org.apache.jackrabbit.oak.spi.security.Context) Field(java.lang.reflect.Field)

Example 4 with Context

use of org.apache.jackrabbit.oak.spi.security.Context in project jackrabbit-oak by apache.

the class SecurityProviderRegistrationTest method testPrincipalContext.

@Test
public void testPrincipalContext() throws Exception {
    Tree t = root.getTree("/");
    Field f = registration.getClass().getDeclaredField("principalConfiguration");
    f.setAccessible(true);
    PrincipalConfiguration pc = new PrincipalConfigurationImpl();
    registration.bindPrincipalConfiguration(pc, PROPS);
    CompositePrincipalConfiguration cpc = (CompositePrincipalConfiguration) f.get(registration);
    Context ctx = cpc.getContext();
    // expected size = 0 because PrincipalConfigurationImpl comes with the default ctx
    assertContext(ctx, 0, t, false);
    PrincipalConfiguration pc1 = mockConfiguration(PrincipalConfiguration.class);
    registration.bindPrincipalConfiguration(pc1, PROPS);
    cpc = (CompositePrincipalConfiguration) f.get(registration);
    ctx = cpc.getContext();
    // expected size 1 because the PrincipalConfigurationImpl comes with the default ctx
    assertContext(ctx, 1, t, true);
    PrincipalConfiguration pc2 = mockConfiguration(PrincipalConfiguration.class);
    registration.bindPrincipalConfiguration(pc2, PROPS);
    cpc = (CompositePrincipalConfiguration) f.get(registration);
    ctx = cpc.getContext();
    assertContext(ctx, 2, t, true);
    // unbind again:
    registration.unbindPrincipalConfiguration(pc, PROPS);
    cpc = (CompositePrincipalConfiguration) f.get(registration);
    ctx = cpc.getContext();
    assertContext(ctx, 2, t, true);
    registration.unbindPrincipalConfiguration(pc1, PROPS);
    cpc = (CompositePrincipalConfiguration) f.get(registration);
    ctx = cpc.getContext();
    assertContext(ctx, 1, t, true);
    registration.unbindPrincipalConfiguration(pc2, PROPS);
    cpc = (CompositePrincipalConfiguration) f.get(registration);
    ctx = cpc.getContext();
    assertContext(ctx, 0, t, false);
}
Also used : OsgiContext(org.apache.sling.testing.mock.osgi.junit.OsgiContext) Context(org.apache.jackrabbit.oak.spi.security.Context) Field(java.lang.reflect.Field) CompositePrincipalConfiguration(org.apache.jackrabbit.oak.spi.security.principal.CompositePrincipalConfiguration) CompositePrincipalConfiguration(org.apache.jackrabbit.oak.spi.security.principal.CompositePrincipalConfiguration) PrincipalConfiguration(org.apache.jackrabbit.oak.spi.security.principal.PrincipalConfiguration) Tree(org.apache.jackrabbit.oak.api.Tree) PrincipalConfigurationImpl(org.apache.jackrabbit.oak.security.principal.PrincipalConfigurationImpl) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 5 with Context

use of org.apache.jackrabbit.oak.spi.security.Context in project jackrabbit-oak by apache.

the class AbstractAccessControlManagerTest method testGetTreeDefinesAcContent.

@Test(expected = AccessControlException.class)
public void testGetTreeDefinesAcContent() throws Exception {
    Context ctx = new Context.Default() {

        @Override
        public boolean definesTree(@Nonnull Tree tree) {
            return true;
        }
    };
    when(authorizationConfiguration.getContext()).thenReturn(ctx);
    acMgr.getTree(testPath, Permissions.NO_PERMISSION, true);
}
Also used : Context(org.apache.jackrabbit.oak.spi.security.Context) Nonnull(javax.annotation.Nonnull) Tree(org.apache.jackrabbit.oak.api.Tree) Test(org.junit.Test)

Aggregations

Context (org.apache.jackrabbit.oak.spi.security.Context)5 Test (org.junit.Test)4 Field (java.lang.reflect.Field)3 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)3 Tree (org.apache.jackrabbit.oak.api.Tree)3 OsgiContext (org.apache.sling.testing.mock.osgi.junit.OsgiContext)3 Nonnull (javax.annotation.Nonnull)1 AuthorizationConfigurationImpl (org.apache.jackrabbit.oak.security.authorization.AuthorizationConfigurationImpl)1 CompositeAuthorizationConfiguration (org.apache.jackrabbit.oak.security.authorization.composite.CompositeAuthorizationConfiguration)1 PrincipalConfigurationImpl (org.apache.jackrabbit.oak.security.principal.PrincipalConfigurationImpl)1 AuthorizationConfiguration (org.apache.jackrabbit.oak.spi.security.authorization.AuthorizationConfiguration)1 CompositePrincipalConfiguration (org.apache.jackrabbit.oak.spi.security.principal.CompositePrincipalConfiguration)1 PrincipalConfiguration (org.apache.jackrabbit.oak.spi.security.principal.PrincipalConfiguration)1