Search in sources :

Example 1 with PrivilegeDefinition

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

the class PrivilegeDefinitionWriter method getBuiltInDefinitions.

private static Collection<PrivilegeDefinition> getBuiltInDefinitions() {
    Map<String, PrivilegeDefinition> definitions = new LinkedHashMap<String, PrivilegeDefinition>();
    for (String privilegeName : NON_AGGREGATE_PRIVILEGES) {
        PrivilegeDefinition def = new ImmutablePrivilegeDefinition(privilegeName, false, null);
        definitions.put(privilegeName, def);
    }
    for (String privilegeName : AGGREGATE_PRIVILEGES.keySet()) {
        PrivilegeDefinition def = new ImmutablePrivilegeDefinition(privilegeName, false, asList(AGGREGATE_PRIVILEGES.get(privilegeName)));
        definitions.put(privilegeName, def);
    }
    PrivilegeDefinition all = new ImmutablePrivilegeDefinition(JCR_ALL, false, definitions.keySet());
    definitions.put(JCR_ALL, all);
    return definitions.values();
}
Also used : PrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition) ImmutablePrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.ImmutablePrivilegeDefinition) ImmutablePrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.ImmutablePrivilegeDefinition) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with PrivilegeDefinition

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

the class PrivilegeDefinitionWriter method writeDefinitions.

/**
     * @param definitions
     * @throws RepositoryException
     */
private void writeDefinitions(Iterable<PrivilegeDefinition> definitions) throws RepositoryException {
    try {
        // make sure the privileges path is defined
        Tree privilegesTree = root.getTree(PRIVILEGES_PATH);
        if (!privilegesTree.exists()) {
            throw new RepositoryException("Privilege store does not exist.");
        }
        for (PrivilegeDefinition definition : definitions) {
            if (privilegesTree.hasChild(definition.getName())) {
                throw new RepositoryException("Privilege definition with name '" + definition.getName() + "' already exists.");
            }
            writePrivilegeNode(privilegesTree, definition);
        }
        /*
            update the property storing the next privilege bits with the
            privileges root tree. this is a cheap way to detect collisions that
            may arise from concurrent registration of custom privileges.
            */
        getNext().writeTo(privilegesTree);
        // delegate validation to the commit validation (see above)
        root.commit();
    } catch (CommitFailedException e) {
        throw e.asRepositoryException();
    }
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) PrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition) ImmutablePrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.ImmutablePrivilegeDefinition) RepositoryException(javax.jcr.RepositoryException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 3 with PrivilegeDefinition

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

the class PrivilegeValidator method resolveAggregates.

private static Set<String> resolveAggregates(Set<String> declared, Map<String, PrivilegeDefinition> definitions) throws CommitFailedException {
    Set<String> aggregateNames = new HashSet<String>();
    for (String name : declared) {
        PrivilegeDefinition d = definitions.get(name);
        if (d == null) {
            throw new CommitFailedException(CONSTRAINT, 47, "Invalid declared aggregate name " + name + ": Unknown privilege.");
        }
        Set<String> names = d.getDeclaredAggregateNames();
        if (names.isEmpty()) {
            aggregateNames.add(name);
        } else {
            aggregateNames.addAll(resolveAggregates(names, definitions));
        }
    }
    return aggregateNames;
}
Also used : PrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) HashSet(java.util.HashSet)

Example 4 with PrivilegeDefinition

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

the class PrivilegeValidator method validateDefinition.

/**
     * Validation of the privilege definition including the following steps:
     * <p>
     * - privilege bits must not collide with an existing privilege
     * - next bits must have been adjusted in case of a non-aggregate privilege
     * - all aggregates must have been registered before
     * - no existing privilege defines the same aggregation
     * - no cyclic aggregation
     *
     * @param definitionTree The new privilege definition tree to validate.
     * @throws org.apache.jackrabbit.oak.api.CommitFailedException
     *          If any of
     *          the checks listed above fails.
     */
private void validateDefinition(Tree definitionTree) throws CommitFailedException {
    PrivilegeBits newBits = PrivilegeBits.getInstance(definitionTree);
    if (newBits.isEmpty()) {
        throw new CommitFailedException(CONSTRAINT, 48, "PrivilegeBits are missing.");
    }
    Set<String> privNames = bitsProvider.getPrivilegeNames(newBits);
    PrivilegeDefinition definition = PrivilegeUtil.readDefinition(definitionTree);
    Set<String> declaredNames = definition.getDeclaredAggregateNames();
    // non-aggregate privilege
    if (declaredNames.isEmpty()) {
        if (!privNames.isEmpty()) {
            throw new CommitFailedException(CONSTRAINT, 49, "PrivilegeBits already in used.");
        }
        validateNext(newBits);
        return;
    }
    // aggregation of a single privilege
    if (declaredNames.size() == 1) {
        throw new CommitFailedException(CONSTRAINT, 50, "Singular aggregation is equivalent to existing privilege.");
    }
    // aggregation of >1 privileges
    Map<String, PrivilegeDefinition> definitions = new PrivilegeDefinitionReader(rootBefore).readDefinitions();
    for (String aggrName : declaredNames) {
        // aggregated privilege not registered
        if (!definitions.containsKey(aggrName)) {
            throw new CommitFailedException(CONSTRAINT, 51, "Declared aggregate '" + aggrName + "' is not a registered privilege.");
        }
        // check for circular aggregation
        if (isCircularAggregation(definition.getName(), aggrName, definitions)) {
            String msg = "Detected circular aggregation within custom privilege caused by " + aggrName;
            throw new CommitFailedException(CONSTRAINT, 52, msg);
        }
    }
    Set<String> aggregateNames = resolveAggregates(declaredNames, definitions);
    for (PrivilegeDefinition existing : definitions.values()) {
        Set<String> existingDeclared = existing.getDeclaredAggregateNames();
        if (existingDeclared.isEmpty()) {
            continue;
        }
        // test for exact same aggregation or aggregation with the same net effect
        if (declaredNames.equals(existingDeclared) || aggregateNames.equals(resolveAggregates(existingDeclared, definitions))) {
            String msg = "Custom aggregate privilege '" + definition.getName() + "' is already covered by '" + existing.getName() + '\'';
            throw new CommitFailedException(CONSTRAINT, 53, msg);
        }
    }
    PrivilegeBits aggrBits = bitsProvider.getBits(declaredNames.toArray(new String[declaredNames.size()]));
    if (!newBits.equals(aggrBits)) {
        throw new CommitFailedException(CONSTRAINT, 53, "Invalid privilege bits for aggregated privilege definition.");
    }
}
Also used : PrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition) PrivilegeBits(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeBits) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 5 with PrivilegeDefinition

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

the class PrivilegeImplTest method testToString.

@Test
public void testToString() {
    PrivilegeDefinition def = new PrivilegeDefinitionReader(root).readDefinition(privilege.getName());
    assertEquals(def.getName(), privilege.toString());
}
Also used : PrivilegeDefinition(org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Aggregations

PrivilegeDefinition (org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition)9 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)3 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)3 Tree (org.apache.jackrabbit.oak.api.Tree)3 ImmutablePrivilegeDefinition (org.apache.jackrabbit.oak.spi.security.privilege.ImmutablePrivilegeDefinition)3 Test (org.junit.Test)3 RepositoryException (javax.jcr.RepositoryException)2 Privilege (javax.jcr.security.Privilege)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Nonnull (javax.annotation.Nonnull)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 L4_PrivilegesAndPermissionsTest (org.apache.jackrabbit.oak.exercise.security.authorization.permission.L4_PrivilegesAndPermissionsTest)1 L7_PermissionContentTest (org.apache.jackrabbit.oak.exercise.security.authorization.permission.L7_PermissionContentTest)1 PrivilegeBits (org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeBits)1