You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by an...@apache.org on 2024/12/12 18:48:13 UTC
(jackrabbit-oak) branch trunk updated: OAK-10466: Allow to configure anonymous user disablement (#1882)
This is an automated email from the ASF dual-hosted git repository.
angela pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new 9bc52a54e0 OAK-10466: Allow to configure anonymous user disablement (#1882)
9bc52a54e0 is described below
commit 9bc52a54e0ac2da3a6f7205ef7b1d3a3da65b9e1
Author: Alejandro Moratinos <Am...@users.noreply.github.com>
AuthorDate: Thu Dec 12 19:48:08 2024 +0100
OAK-10466: Allow to configure anonymous user disablement (#1882)
* OAK-10466: Allow to configure anonymous user disablement
* OAK-10466: Apply PR comments
* OAK-10466: Change anonymous evaluation
Configuration check moved to be performed when the disable method is called
---
.../oak/security/user/UserConfigurationImpl.java | 6 ++
.../jackrabbit/oak/security/user/UserImpl.java | 22 ++++--
.../jackrabbit/oak/AbstractSecurityTest.java | 7 ++
.../jackrabbit/oak/security/user/UserImplTest.java | 80 ++++++++++++++++++----
.../oak/spi/security/user/UserConstants.java | 7 ++
.../oak/spi/security/user/package-info.java | 2 +-
6 files changed, 105 insertions(+), 19 deletions(-)
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImpl.java
index 8aca078081..b01b11d45f 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImpl.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserConfigurationImpl.java
@@ -195,6 +195,12 @@ public class UserConfigurationImpl extends ConfigurationBase implements UserConf
name = "RFC7613 Username Comparison Profile",
description = "Enable the UsercaseMappedProfile defined in RFC7613 for username comparison.")
boolean enableRFC7613UsercaseMappedProfile() default false;
+
+ @AttributeDefinition(
+ name = "Allow Disable Anonymous User",
+ description = "By default the anonymous user can be disabled. By changing this option to false trying "
+ + "to disable the anonymous user will throw an exception.")
+ boolean allowDisableAnonymous() default true;
}
private static final UserAuthenticationFactory DEFAULT_AUTH_FACTORY = new UserAuthenticationFactoryImpl();
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
index ac4dbb036e..d237458722 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
@@ -25,6 +25,7 @@ import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.security.user.UserIdCredentials;
@@ -47,8 +48,9 @@ class UserImpl extends AuthorizableImpl implements User {
UserImpl(String id, Tree tree, UserManagerImpl userManager) throws RepositoryException {
super(id, tree, userManager);
- isAdmin = UserUtil.isAdmin(userManager.getConfig(), id);
- pwHistory = new PasswordHistory(userManager.getConfig());
+ ConfigurationParameters configurationParameters = userManager.getConfig();
+ isAdmin = UserUtil.isAdmin(configurationParameters, id);
+ pwHistory = new PasswordHistory(configurationParameters);
}
//---------------------------------------------------< AuthorizableImpl >---
@@ -131,9 +133,7 @@ class UserImpl extends AuthorizableImpl implements User {
@Override
public void disable(@Nullable String reason) throws RepositoryException {
- if (isAdmin) {
- throw new RepositoryException("The administrator user cannot be disabled.");
- }
+ validateDisableUser();
getUserManager().onDisable(this, reason);
@@ -148,6 +148,18 @@ class UserImpl extends AuthorizableImpl implements User {
}
}
+ private void validateDisableUser() throws RepositoryException {
+ if (isAdmin) {
+ throw new RepositoryException("The administrator user cannot be disabled.");
+ }
+
+ boolean isAnonymous = UserUtil.getAnonymousId(getUserManager().getConfig()).equals(getID());
+ boolean allowDisableAnonymous = getUserManager().getConfig().getConfigValue(UserConstants.PARAM_ALLOW_DISABLE_ANONYMOUS, true);
+ if (isAnonymous && !allowDisableAnonymous) {
+ throw new RepositoryException("The anonymous user cannot be disabled.");
+ }
+ }
+
@Override
public boolean isDisabled() {
return getTree().hasProperty(REP_DISABLED);
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractSecurityTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractSecurityTest.java
index 0e1e93dcda..53f406d4bb 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractSecurityTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractSecurityTest.java
@@ -204,6 +204,13 @@ public abstract class AbstractSecurityTest {
}
}
+ /*
+ * Useful when needing to re-create the user manager instance while updating configuration
+ */
+ protected void cleanUserManager() {
+ userManager = null;
+ }
+
protected PrincipalManager getPrincipalManager(Root root) {
return getConfig(PrincipalConfiguration.class).getPrincipalManager(root, getNamePathMapper());
}
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
index 5bc11b38a5..791e78d156 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserImplTest.java
@@ -16,15 +16,27 @@
*/
package org.apache.jackrabbit.oak.security.user;
+import static javax.jcr.Property.JCR_PRIMARY_TYPE;
+import static org.apache.jackrabbit.oak.spi.security.user.UserConstants.NT_REP_GROUP;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
import javax.jcr.Credentials;
import javax.jcr.RepositoryException;
-
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.AbstractSecurityTest;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.UUIDUtils;
import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
import org.apache.jackrabbit.oak.spi.security.user.UserIdCredentials;
import org.apache.jackrabbit.oak.spi.security.user.util.PasswordUtil;
@@ -33,19 +45,10 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import static javax.jcr.Property.JCR_PRIMARY_TYPE;
-import static org.apache.jackrabbit.oak.spi.security.user.UserConstants.NT_REP_GROUP;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
public class UserImplTest extends AbstractSecurityTest {
private User user;
+ private boolean allowDisableAnonymous = true;
@Override
@Before
@@ -71,9 +74,19 @@ public class UserImplTest extends AbstractSecurityTest {
return admin;
}
+ @NotNull
+ private User getAnonymousUser() throws Exception {
+ User anonymous = getUserManager(root).getAuthorizable(UserConstants.DEFAULT_ANONYMOUS_ID, User.class);
+ assertNotNull(anonymous);
+ return anonymous;
+ }
+
@Test(expected = IllegalArgumentException.class)
public void testCreateFromInvalidTree() throws Exception {
- Tree t = when(mock(Tree.class).getProperty(JCR_PRIMARY_TYPE)).thenReturn(PropertyStates.createProperty(JCR_PRIMARY_TYPE, NT_REP_GROUP, Type.NAME)).getMock();
+ Tree t = when(mock(Tree.class).getProperty(JCR_PRIMARY_TYPE)).thenReturn(PropertyStates.createProperty(
+ JCR_PRIMARY_TYPE,
+ NT_REP_GROUP,
+ Type.NAME)).getMock();
User u = new UserImpl("uid", t, (UserManagerImpl) getUserManager(root));
}
@@ -165,6 +178,36 @@ public class UserImplTest extends AbstractSecurityTest {
assertFalse(user.isDisabled());
}
+ @Test
+ public void testDisableAnonymous() throws Exception {
+ User anonymous = getAnonymousUser();
+ assertFalse(anonymous.isDisabled());
+
+ anonymous.disable("Test anonymous disable");
+
+ assertNotNull(anonymous.getDisabledReason());
+ assertEquals("Test anonymous disable", anonymous.getDisabledReason());
+ assertTrue(anonymous.isDisabled());
+ }
+
+ @Test(expected = RepositoryException.class)
+ public void testDisableAnonymousNotAllowed() throws Exception {
+ // Dirty hack to prevent anonymous user from being disabled
+ // and initialize the repo again
+ allowDisableAnonymous = false;
+ securityProvider = null;
+
+ cleanUserManager();
+ before();
+
+ User anonymous = getAnonymousUser();
+ assertFalse(anonymous.isDisabled());
+
+ anonymous.disable("Test anonymous disable");
+
+ fail("Shouldn't have reached this point");
+ }
+
@Test(expected = RepositoryException.class)
public void testDisableAdministrator() throws Exception {
getAdminUser().disable("reason");
@@ -182,10 +225,21 @@ public class UserImplTest extends AbstractSecurityTest {
@Test
public void testGetCredentialsUserWithoutPassword() throws Exception {
- User u = getUserManager(root).createUser("u"+ UUIDUtils.generateUUID(), null);
+ User u = getUserManager(root).createUser("u" + UUIDUtils.generateUUID(), null);
Credentials creds = u.getCredentials();
assertTrue(creds instanceof UserIdCredentials);
assertEquals(u.getID(), ((UserIdCredentials) creds).getUserId());
}
+
+ @Override
+ protected ConfigurationParameters getSecurityConfigParameters() {
+ if (allowDisableAnonymous) {
+ return super.getSecurityConfigParameters();
+ } else {
+ ConfigurationParameters userConfig = ConfigurationParameters.of(
+ UserConstants.PARAM_ALLOW_DISABLE_ANONYMOUS, false);
+ return ConfigurationParameters.of(UserConfiguration.NAME, userConfig);
+ }
+ }
}
\ No newline at end of file
diff --git a/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/UserConstants.java b/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/UserConstants.java
index 50c34aba0c..1abb3ae5c9 100644
--- a/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/UserConstants.java
+++ b/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/UserConstants.java
@@ -265,4 +265,11 @@ public interface UserConstants {
* @since Oak 1.3.3
*/
int PASSWORD_HISTORY_DISABLED_SIZE = 0;
+
+ /**
+ * Optional configuration parameter indicating if the anonymous user can be disabled or not.
+ * By default, the anonymous user can be disabled.
+ *
+ */
+ String PARAM_ALLOW_DISABLE_ANONYMOUS = "allowDisableAnonymous";
}
diff --git a/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/package-info.java b/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/package-info.java
index 3ff6e090bf..8522e754e8 100644
--- a/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/package-info.java
+++ b/oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/user/package-info.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-@Version("2.7.0")
+@Version("2.8.0")
package org.apache.jackrabbit.oak.spi.security.user;
import org.osgi.annotation.versioning.Version;