Author: angela
Date: Thu Jul 18 09:19:00 2019
New Revision: 1863261

URL: http://svn.apache.org/viewvc?rev=1863261&view=rev
Log:
OAK-8490 : FilterProviderImpl.getFilter may fail if principal cache enabled

Added:
    
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
      - copied, changed from r1863260, 
jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
Removed:
    
jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
Modified:
    
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationTest.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ImmutableRoot.java

Modified: 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationTest.java?rev=1863261&r1=1863260&r2=1863261&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationTest.java
 Thu Jul 18 09:19:00 2019
@@ -171,6 +171,18 @@ public class PrincipalBasedAuthorization
     }
 
     @Test
+    public void testGetPermissionProvider2() throws Exception {
+        PrincipalBasedAuthorizationConfiguration pbac = new 
PrincipalBasedAuthorizationConfiguration();
+        pbac.bindFilterProvider(getFilterProvider());
+        pbac.setSecurityProvider(getSecurityProvider());
+        pbac.setRootProvider(getRootProvider());
+
+        Set<Principal> principals = 
ImmutableSet.of(getTestSystemUser().getPrincipal());
+        PermissionProvider pp = pbac.getPermissionProvider(root, "wspName", 
principals);
+        assertTrue(pp instanceof PrincipalBasedPermissionProvider);
+    }
+
+    @Test
     public void testGetAccessControlManager() {
         PrincipalBasedAuthorizationConfiguration pbac = new 
PrincipalBasedAuthorizationConfiguration();
         pbac.setSecurityProvider(getSecurityProvider());

Added: 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java?rev=1863261&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java
 Thu Jul 18 09:19:00 2019
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package 
org.apache.jackrabbit.oak.spi.security.authorization.principalbased.impl;
+
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
+
+public class PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest 
extends PrincipalBasedAuthorizationConfigurationTest {
+
+    @Override
+    protected ConfigurationParameters getSecurityConfigParameters() {
+        return ConfigurationParameters.of(UserConfiguration.NAME, 
ConfigurationParameters.of("cacheExpiration", 500));
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAuthorizationConfigurationWithPrincipalCacheTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ImmutableRoot.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ImmutableRoot.java?rev=1863261&r1=1863260&r2=1863261&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ImmutableRoot.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/core/ImmutableRoot.java
 Thu Jul 18 09:19:00 2019
@@ -24,6 +24,7 @@ import static org.apache.jackrabbit.oak.
 import java.io.InputStream;
 import java.util.Map;
 
+import org.apache.jackrabbit.oak.api.AuthInfo;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.QueryEngine;
@@ -47,6 +48,8 @@ import org.jetbrains.annotations.NotNull
 public final class ImmutableRoot implements Root, ReadOnly {
 
     private final ImmutableTree rootTree;
+    private final AuthInfo authInfo;
+    private final String wspName;
 
     public ImmutableRoot(@NotNull NodeState rootState) {
         this(new ImmutableTree(rootState));
@@ -55,8 +58,13 @@ public final class ImmutableRoot impleme
     public ImmutableRoot(@NotNull Root root) {
         if (root instanceof MutableRoot) {
             rootTree = new ImmutableTree(((MutableRoot) root).getBaseState());
+            authInfo = root.getContentSession().getAuthInfo();
+            wspName = root.getContentSession().getWorkspaceName();
         } else if (root instanceof ImmutableRoot) {
-            rootTree = ((ImmutableRoot) root).getTree("/");
+            ImmutableRoot ir = (ImmutableRoot) root;
+            rootTree = ir.getTree(PathUtils.ROOT_PATH);
+            authInfo = ir.authInfo;
+            wspName = ir.wspName;
         } else {
             throw new IllegalArgumentException("Unsupported Root 
implementation: " + root.getClass());
         }
@@ -65,6 +73,8 @@ public final class ImmutableRoot impleme
     public ImmutableRoot(@NotNull ImmutableTree rootTree) {
         checkArgument(rootTree.isRoot());
         this.rootTree = rootTree;
+        this.authInfo = AuthInfo.EMPTY;
+        this.wspName = null;
     }
 
     public static ImmutableRoot getInstance(@NotNull Root root) {
@@ -145,7 +155,26 @@ public final class ImmutableRoot impleme
     @NotNull
     @Override
     public ContentSession getContentSession() {
-        throw new UnsupportedOperationException();
+        return new ContentSession() {
+            @Override
+            public @NotNull AuthInfo getAuthInfo() {
+                return authInfo;
+            }
+
+            @Override
+            public String getWorkspaceName() {
+                return wspName;
+            }
+
+            @Override
+            public @NotNull Root getLatestRoot() {
+                return ImmutableRoot.this;
+            }
+
+            @Override
+            public void close() {
+            }
+        };
     }
 
 }

Copied: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
 (from r1863260, 
jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java)
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java?p2=jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java&p1=jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java&r1=1863260&r2=1863261&rev=1863261&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/ImmutableRootTest.java
 Thu Jul 18 09:19:00 2019
@@ -16,87 +16,121 @@
  */
 package org.apache.jackrabbit.oak.core;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-
-import java.io.ByteArrayInputStream;
-
-import org.apache.jackrabbit.oak.OakBaseTest;
-import org.apache.jackrabbit.oak.api.CommitFailedException;
+import com.google.common.collect.ImmutableMap;
+import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.apache.jackrabbit.oak.api.AuthInfo;
 import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.fixture.NodeStoreFixture;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
+import org.apache.jackrabbit.oak.spi.nodetype.NodeTypeConstants;
 import org.junit.Before;
 import org.junit.Test;
 
-public class ImmutableRootTest extends OakBaseTest {
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+public class ImmutableRootTest extends AbstractSecurityTest {
 
     private ImmutableRoot root;
 
-    public ImmutableRootTest(NodeStoreFixture fixture) {
-        super(fixture);
-    }
-
     @Before
-    public void setUp() throws CommitFailedException {
-        ContentSession session = createContentSession();
+    public void before() throws Exception {
+        super.before();
 
         // Add test content
-        Root root = session.getLatestRoot();
-        Tree tree = root.getTree("/");
-        Tree x = tree.addChild("x");
-        Tree y = x.addChild("y");
-        Tree z = y.addChild("z");
+        Root root = adminSession.getLatestRoot();
+        Tree tree = root.getTree(PathUtils.ROOT_PATH);
+        Tree x = TreeUtil.addChild(tree, "x", 
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+        Tree y = TreeUtil.addChild(x, "y", 
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
+        Tree z = TreeUtil.addChild(y, "z", 
NodeTypeConstants.NT_OAK_UNSTRUCTURED);
         root.commit();
 
         // Acquire a fresh new root to avoid problems from lingering state
-        this.root = new ImmutableRoot(session.getLatestRoot());
+        this.root = new ImmutableRoot(adminSession.getLatestRoot());
     }
 
-    // TODO: add more tests
-
     @Test
     public void testHasPendingChanges() {
         assertFalse(root.hasPendingChanges());
     }
 
-    @Test
-    public void testImmutable() {
+    @Test(expected = UnsupportedOperationException.class)
+    public void testCommit() {
+        root.commit();
+    }
 
-        try {
-            root.commit();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // success
-        }
+    @Test(expected = UnsupportedOperationException.class)
+    public void testCommitWithMap() {
+        root.commit(ImmutableMap.of());
+    }
 
-        try {
-            root.rebase();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // success
-        }
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRebase() {
+        root.rebase();
+    }
 
-        try {
-            root.refresh();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // success
-        }
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRefresh() {
+        root.refresh();
+    }
 
-        try {
-            root.createBlob(new ByteArrayInputStream(new byte[0]));
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // success
-        }
+    @Test(expected = UnsupportedOperationException.class)
+    public void testCreateBlob() {
+        root.createBlob(new ByteArrayInputStream(new byte[0]));
+    }
 
+    @Test(expected = UnsupportedOperationException.class)
+    public void testMove() {
+        root.move("/x", "/b");
+    }
+
+    @Test
+    public void testGetBlob() {
+        assertNull(root.getBlob("reference"));
+    }
+
+    @Test
+    public void testGetContentSession() {
+        ContentSession cs = root.getContentSession();
+        assertNotEquals(adminSession, cs);
+        assertSame(adminSession.getAuthInfo(), cs.getAuthInfo());
+        assertEquals(adminSession.getWorkspaceName(), cs.getWorkspaceName());
+        assertSame(root, cs.getLatestRoot());
+    }
+
+    @Test
+    public void testGetContentSessionCreatedFromImmutableRoot() {
+        ImmutableRoot ir = new ImmutableRoot(root);
+        ContentSession cs = ir.getContentSession();
+        assertSame(root.getContentSession().getAuthInfo(), cs.getAuthInfo());
+        assertEquals(root.getContentSession().getWorkspaceName(), 
cs.getWorkspaceName());
         try {
-            root.move("/x", "/b");
+            cs.close();
+        } catch (IOException e) {
             fail();
-        } catch (UnsupportedOperationException e) {
-            // success
         }
     }
+
+    @Test
+    public void testGetContentSessionCreatedFromRootTree() {
+        ImmutableRoot ir = new 
ImmutableRoot(root.getTree(PathUtils.ROOT_PATH));
+        ContentSession cs = ir.getContentSession();
+        assertSame(AuthInfo.EMPTY, cs.getAuthInfo());
+        assertNull(cs.getWorkspaceName());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFromInvalidRoot() {
+        new ImmutableRoot(mock(Root.class));
+    }
 }


Reply via email to