Search in sources :

Example 46 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class UserPrincipalProvider method cacheGroups.

private void cacheGroups(@Nonnull Tree authorizableNode, @Nonnull Set<Group> groupPrincipals) {
    try {
        root.refresh();
        Tree cache = authorizableNode.getChild(CacheConstants.REP_CACHE);
        if (!cache.exists()) {
            if (groupPrincipals.size() <= MEMBERSHIP_THRESHOLD) {
                log.debug("Omit cache creation for user without group membership at " + authorizableNode.getPath());
                return;
            } else {
                log.debug("Create new group membership cache at " + authorizableNode.getPath());
                cache = TreeUtil.addChild(authorizableNode, CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE);
            }
        }
        cache.setProperty(CacheConstants.REP_EXPIRATION, LongUtils.calculateExpirationTime(expiration));
        String value = (groupPrincipals.isEmpty()) ? "" : Joiner.on(",").join(Iterables.transform(groupPrincipals, new Function<Group, String>() {

            @Override
            public String apply(Group input) {
                return Text.escape(input.getName());
            }
        }));
        cache.setProperty(CacheConstants.REP_GROUP_PRINCIPAL_NAMES, value);
        root.commit(CacheValidatorProvider.asCommitAttributes());
        log.debug("Cached group membership at " + authorizableNode.getPath());
    } catch (AccessDeniedException e) {
        log.debug("Failed to cache group membership", e.getMessage());
    } catch (CommitFailedException e) {
        log.debug("Failed to cache group membership", e.getMessage(), e);
    } finally {
        root.refresh();
    }
}
Also used : Group(java.security.acl.Group) AccessDeniedException(javax.jcr.AccessDeniedException) Tree(org.apache.jackrabbit.oak.api.Tree) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 47 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class NodeStoreDiffTest method testDiff.

/**
     * This testcase demonstrates that diff logic in merge part traverses node path
     * which are not affected by the commit
     * @throws Exception
     */
@Test
public void testDiff() throws Exception {
    createNodes("/oak:index/prop-a", "/oak:index/prop-b", "/etc/workflow");
    //1. Make some other changes so as to bump root rev=3
    createNodes("/fake/a");
    createNodes("/fake/b");
    //2 - Start change
    NodeBuilder b2 = ns.getRoot().builder();
    createNodes(b2, "/etc/workflow/instance1");
    tds.reset();
    //3. Merge which does a rebase
    ns.merge(b2, new CommitHook() {

        @Nonnull
        public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
            NodeBuilder rb = after.builder();
            createNodes(rb, "/oak:index/prop-a/a1");
            //2.1 Commit some change under prop-
            //This cause diff in lastRev of base node state in ModifiedNodeState for
            //oak:index due to which when the base state is compared in ModifiedNodeState
            //then it fetches the new DocumentNodeState whose lastRev is greater than this.base.lastRev
            //but less then lastRev of the of readRevision. Where readRevision is the rev of root node when
            //rebase was performed
            // remember paths accessed so far
            List<String> paths = Lists.newArrayList(tds.paths);
            //This is not to be done in actual cases as CommitHooks are invoked in critical sections
            //and creating nodes from within CommitHooks would cause deadlock. This is done here to ensure
            //that changes are done when rebase has been performed and merge is about to happen
            createNodes("/oak:index/prop-b/b1");
            // reset accessed paths
            tds.reset();
            tds.paths.addAll(paths);
            return rb.getNodeState();
        }
    }, CommitInfo.EMPTY);
    //Assert that diff logic does not traverse to /oak:index/prop-b/b1 as
    //its not part of commit
    assertFalse(tds.paths.contains("/oak:index/prop-b/b1"));
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) List(java.util.List) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Example 48 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class AccessControlValidatorTest method testInvalidRestriction.

@Test
public void testInvalidRestriction() throws Exception {
    NodeUtil restriction = createAcl().getChild(aceName).getChild(REP_RESTRICTIONS);
    restriction.setString("invalid", "value");
    try {
        root.commit();
        fail("Creating an unsupported restriction should fail.");
    } catch (CommitFailedException e) {
        // success
        assertTrue(e.isAccessControlViolation());
        assertThat(e.getMessage(), containsString("/testRoot/rep:policy"));
    }
}
Also used : CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 49 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class AccessControlValidatorTest method testDuplicateAce.

@Test
public void testDuplicateAce() throws Exception {
    AccessControlManager acMgr = getAccessControlManager(root);
    JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acMgr, testPath);
    acl.addAccessControlEntry(testPrincipal, privilegesFromNames(PrivilegeConstants.JCR_ADD_CHILD_NODES));
    acMgr.setPolicy(testPath, acl);
    // add duplicate ac-entry on OAK-API
    NodeUtil policy = new NodeUtil(root.getTree(testPath + "/rep:policy"));
    NodeUtil ace = policy.addChild("duplicateAce", NT_REP_GRANT_ACE);
    ace.setString(REP_PRINCIPAL_NAME, testPrincipal.getName());
    ace.setNames(AccessControlConstants.REP_PRIVILEGES, PrivilegeConstants.JCR_ADD_CHILD_NODES);
    try {
        root.commit();
        fail("Creating duplicate ACE must be detected");
    } catch (CommitFailedException e) {
        assertTrue(e.isAccessControlViolation());
        assertThat(e.getMessage(), containsString("/testRoot/rep:policy/duplicateAce"));
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 50 with CommitFailedException

use of org.apache.jackrabbit.oak.api.CommitFailedException in project jackrabbit-oak by apache.

the class AccessControlValidatorTest method testAbstractPrivilege.

@Test
public void testAbstractPrivilege() throws Exception {
    PrivilegeManager pMgr = getPrivilegeManager(root);
    pMgr.registerPrivilege("abstractPrivilege", true, new String[0]);
    NodeUtil acl = createAcl();
    createACE(acl, "invalid", NT_REP_GRANT_ACE, testPrincipal.getName(), "abstractPrivilege");
    try {
        root.commit();
        fail("Creating an ACE with an abstract privilege should fail.");
    } catch (CommitFailedException e) {
        // success
        assertTrue(e.isAccessControlViolation());
        assertThat(e.getMessage(), containsString("/testRoot/rep:policy"));
    }
}
Also used : PrivilegeManager(org.apache.jackrabbit.api.security.authorization.PrivilegeManager) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Aggregations

CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)246 Test (org.junit.Test)166 Tree (org.apache.jackrabbit.oak.api.Tree)75 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)66 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)60 NodeUtil (org.apache.jackrabbit.oak.util.NodeUtil)59 Root (org.apache.jackrabbit.oak.api.Root)48 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)42 RepositoryException (javax.jcr.RepositoryException)17 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)13 EditorHook (org.apache.jackrabbit.oak.spi.commit.EditorHook)13 EmptyNodeState (org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState)12 Nonnull (javax.annotation.Nonnull)10 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)10 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)10 TokenInfo (org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo)10 CommitInfo (org.apache.jackrabbit.oak.spi.commit.CommitInfo)9 ArrayList (java.util.ArrayList)8 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)8 UserManager (org.apache.jackrabbit.api.security.user.UserManager)7