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 2023/05/16 14:55:06 UTC
[jackrabbit-oak] branch trunk updated: OAK-10246 : Minor cleanup of unused imports, indention and whitespace
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 8c73d8217e OAK-10246 : Minor cleanup of unused imports, indention and whitespace
8c73d8217e is described below
commit 8c73d8217e649d28cf1c3825a095b038c0b434e9
Author: angela <an...@adobe.com>
AuthorDate: Tue May 16 16:54:56 2023 +0200
OAK-10246 : Minor cleanup of unused imports, indention and whitespace
---
.../oak/security/user/ImpersonationImpl.java | 9 ++-
.../apache/jackrabbit/oak/security/user/Utils.java | 38 ++++++------
.../oak/security/user/ImpersonationTestUtil.java | 24 ++++----
.../jackrabbit/oak/security/user/UtilsTest.java | 70 +++++++++++++++++-----
4 files changed, 92 insertions(+), 49 deletions(-)
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ImpersonationImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ImpersonationImpl.java
index 11b704fed4..cedbe6f7f3 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ImpersonationImpl.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ImpersonationImpl.java
@@ -19,7 +19,6 @@ package org.apache.jackrabbit.oak.security.user;
import org.apache.jackrabbit.api.security.principal.PrincipalIterator;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.api.security.user.Authorizable;
-import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.Impersonation;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Tree;
@@ -37,7 +36,6 @@ import javax.jcr.RepositoryException;
import javax.security.auth.Subject;
import java.security.Principal;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
import static org.apache.jackrabbit.oak.api.Type.STRINGS;
@@ -131,9 +129,10 @@ class ImpersonationImpl implements Impersonation, UserConstants {
Set<Principal> principals = subject.getPrincipals();
Set<String> principalNames = new HashSet<>();
for (Principal principal : principals) {
- principalNames.add(principal.getName());
+ principalNames.add(principal.getName());
}
+ // OAK-10173 : short-cut if the subject contains any of the configured principal names that can impersonate all users
if (isImpersonator(principalNames)){
return true;
}
@@ -158,7 +157,7 @@ class ImpersonationImpl implements Impersonation, UserConstants {
}
@NotNull
- private Set<String> getImpersonatorNames(@NotNull Tree userTree) {
+ private static Set<String> getImpersonatorNames(@NotNull Tree userTree) {
Set<String> princNames = new HashSet<>();
PropertyState impersonators = userTree.getProperty(REP_IMPERSONATORS);
if (impersonators != null) {
@@ -169,7 +168,7 @@ class ImpersonationImpl implements Impersonation, UserConstants {
return princNames;
}
- private void updateImpersonatorNames(@NotNull Tree userTree, @NotNull Set<String> principalNames) {
+ private static void updateImpersonatorNames(@NotNull Tree userTree, @NotNull Set<String> principalNames) {
if (principalNames.isEmpty()) {
userTree.removeProperty(REP_IMPERSONATORS);
} else {
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/Utils.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/Utils.java
index bad5b562c9..f0b4af1ac2 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/Utils.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/Utils.java
@@ -58,10 +58,10 @@ public final class Utils {
*
* @param relativePath A relative OAK path that may contain parent and
* current elements.
- * @param primaryTypeName A oak name of a primary node type that is used
+ * @param primaryTypeName An oak name of a primary node type that is used
* to create the missing trees.
* @return The node util of the tree at the specified {@code relativePath}.
- * @throws AccessDeniedException If the any intermediate tree does not exist
+ * @throws AccessDeniedException If the intermediate tree does not exist
* and cannot be created.
*/
@NotNull
@@ -99,7 +99,7 @@ public final class Utils {
/**
* Return {@code true} if the given principal can impersonate all users.
* The implementation tests if the given principal refers to an existing {@code User} for which {@link User#isAdmin()}
- * returns {@code true} OR if the user contains a principal name configured to impersonate all users.
+ * returns {@code true} OR if the user's principal name or any of its membership is configured to impersonate all users.
*
* @param principal A non-null principal instance.
* @param userManager The user manager used for the lookup calling {@link UserManager#getAuthorizable(Principal))}
@@ -113,7 +113,8 @@ public final class Utils {
return false;
}
- return ((User)authorizable).isAdmin() || Utils.isImpersonator(authorizable, userManager);
+ User user = (User) authorizable;
+ return user.isAdmin() || Utils.isImpersonator(user, userManager);
} catch (RepositoryException e) {
log.debug(e.getMessage());
return false;
@@ -121,35 +122,38 @@ public final class Utils {
}
/**
- * Return {@code true} if the given authorizable has the right to impersonate.
- * The implementation tests if the given authorizable refers to an existing {@code Principal} that is either member
+ * Return {@code true} if the given user has the right to impersonate.
+ * The implementation tests if the given user refers to an existing {@code Principal} that is either member
* of a configured impersonator group or is has its name amongst configured impersonators. Both those configurations
- * are under the PARAM_IMPERSONATOR_PRINCIPAL_NAMES configuration value.
+ * are under the {@code PARAM_IMPERSONATOR_PRINCIPAL_NAMES} configuration value.
*
- * @param authorizable A non-null authorizable instance.
- * @param userMng The user manager used for the lookup calling {@link UserManager#getAuthorizable(Principal))}
- * @return {@code true} if the given authorizable is an impersonator; {@code false} if that condition is not met
+ * @param user A non-null user instance.
+ * @param userManager The user manager implementation to retrieve the configuration and principal manager.
+ * @return {@code true} if the given user is an impersonator; {@code false} if that condition is not met
* or if the evaluation failed.
*/
- public static boolean isImpersonator(@NotNull Authorizable authorizable, @NotNull UserManager userMng) throws RepositoryException {
- UserManagerImpl userManager = (UserManagerImpl)userMng;
- Set<String> impersonatorPrincipals = Set.of(userManager.getConfig().getConfigValue(
+ private static boolean isImpersonator(@NotNull User user, @NotNull UserManager userManager) throws RepositoryException {
+ if (!(userManager instanceof UserManagerImpl)) {
+ return false;
+ }
+ UserManagerImpl umImpl = (UserManagerImpl) userManager;
+ Set<String> impersonatorPrincipals = Set.of(umImpl.getConfig().getConfigValue(
PARAM_IMPERSONATOR_PRINCIPAL_NAMES,
new String[]{}));
if (impersonatorPrincipals.isEmpty()) {
return false;
}
- Principal userPrincipal = authorizable.getPrincipal();
- PrincipalManager principalManager = userManager.getPrincipalManager();
- for (String impersonatorPrincipalName: impersonatorPrincipals) {
+ Principal userPrincipal = user.getPrincipal();
+ PrincipalManager principalManager = umImpl.getPrincipalManager();
+ for (String impersonatorPrincipalName : impersonatorPrincipals) {
Principal impersonatorPrincipal = principalManager.getPrincipal(impersonatorPrincipalName);
if (impersonatorPrincipal == null) {
continue;
}
if (GroupPrincipals.isGroup(impersonatorPrincipal)) {
- if (((GroupPrincipal)impersonatorPrincipal).isMember(userPrincipal)) {
+ if (((GroupPrincipal) impersonatorPrincipal).isMember(userPrincipal)) {
return true;
}
} else if (impersonatorPrincipalName.equals(userPrincipal.getName())) {
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationTestUtil.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationTestUtil.java
index 45fa27e4e9..8de1ac9693 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationTestUtil.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/ImpersonationTestUtil.java
@@ -17,14 +17,14 @@
package org.apache.jackrabbit.oak.security.user;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
-import org.apache.jackrabbit.api.security.user.Authorizable;
-import org.apache.jackrabbit.api.security.user.Group;
+import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import javax.jcr.RepositoryException;
import java.security.Principal;
-import java.util.Iterator;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -42,10 +42,10 @@ public interface ImpersonationTestUtil {
* @param configs The configuration object to be spied. If null, a new mock object will be created.
* @param impersonatorGroupId The impersonator group ID to be returned when searching in the config for
- * <code>PARAM_IMPERSONATOR_GROUPS_ID</code>.
+ * {@code PARAM_IMPERSONATOR_GROUPS_ID}.
* @return A mocked configuration object with the specified impersonator group ID.
*/
- static ConfigurationParameters getMockedConfigs(ConfigurationParameters configs, String impersonatorGroupId) {
+ @NotNull static ConfigurationParameters getMockedConfigs(@Nullable ConfigurationParameters configs, @NotNull String impersonatorGroupId) {
ConfigurationParameters configMock;
if (configs != null) {
configMock = spy(configs);
@@ -58,7 +58,7 @@ public interface ImpersonationTestUtil {
return configMock;
}
- static ConfigurationParameters getMockedConfigs(String impersonatorGroupId) {
+ @NotNull static ConfigurationParameters getMockedConfigs(@NotNull String impersonatorGroupId) {
return getMockedConfigs(null, impersonatorGroupId);
}
@@ -71,7 +71,7 @@ public interface ImpersonationTestUtil {
* @param user the existing user object to attach a config to
* @return a mock user object with mocked configuration parameters
*/
- static UserImpl getUserWithMockedConfigs(String impersonatorGroupName, UserImpl user) {
+ @NotNull static UserImpl getUserWithMockedConfigs(@NotNull String impersonatorGroupName, @NotNull UserImpl user) {
ConfigurationParameters configMock = getMockedConfigs(impersonatorGroupName);
UserManagerImpl userManagerMock = spy(user.getUserManager());
@@ -83,14 +83,14 @@ public interface ImpersonationTestUtil {
return userMock;
}
- static Authorizable getAuthorizableWithPrincipal(Principal principal) throws RepositoryException {
- Authorizable authorizable = mock(Authorizable.class);
- when(authorizable.getPrincipal()).thenReturn(principal);
+ @NotNull static User getUserWithPrincipal(@NotNull Principal principal) throws RepositoryException {
+ User user = mock(User.class);
+ when(user.getPrincipal()).thenReturn(principal);
- return authorizable;
+ return user;
}
- static PrincipalManager getMockedPrincipalManager(String impersonatorName, Principal principal) {
+ @NotNull static PrincipalManager getMockedPrincipalManager(@NotNull String impersonatorName, @NotNull Principal principal) {
PrincipalManager principalManagerMocked = mock(PrincipalManager.class);
when(principalManagerMocked.getPrincipal(impersonatorName)).thenReturn(principal);
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UtilsTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UtilsTest.java
index 904c24183a..5163753118 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UtilsTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UtilsTest.java
@@ -17,6 +17,7 @@
package org.apache.jackrabbit.oak.security.user;
import java.security.Principal;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
@@ -45,7 +46,6 @@ import javax.jcr.AccessDeniedException;
import javax.jcr.RepositoryException;
import static org.apache.jackrabbit.oak.security.user.Utils.canImpersonateAllUsers;
-import static org.apache.jackrabbit.oak.security.user.Utils.isImpersonator;
import static org.apache.jackrabbit.oak.spi.security.user.UserConstants.DEFAULT_ADMIN_ID;
import static org.apache.jackrabbit.oak.spi.security.user.UserConstants.PARAM_ADMIN_ID;
import static org.junit.Assert.assertEquals;
@@ -244,14 +244,43 @@ public class UtilsTest extends AbstractSecurityTest {
Group impersonatorGroup = userManagerSpy.createGroup(impersonatorGroupId);
Authorizable authorizable = userManagerSpy.getAuthorizable(getTestUser().getID());
+ assertNotNull(authorizable);
+
+ assertFalse(canImpersonateAllUsers(authorizable.getPrincipal(), userManagerSpy));
+
impersonatorGroup.addMember(authorizable);
root.commit();
- assertTrue(canImpersonateAllUsers(new PrincipalImpl(getTestUser().getID()), userManagerSpy));
+ assertTrue(canImpersonateAllUsers(authorizable.getPrincipal(), userManagerSpy));
+ }
+
+ @Test
+ public void testCanImpersonateAllConfiguredNonExistingPrincipal() throws Exception {
+ String impersonatorName = "nonExisting";
+
+ UserManagerImpl userManagerSpy = spy((UserManagerImpl)getUserManager(root));
+ ConfigurationParameters configs = ImpersonationTestUtil.getMockedConfigs(userManagerSpy.getConfig(), impersonatorName);
+ when(userManagerSpy.getConfig()).thenReturn(configs);
+
+ Authorizable authorizable = userManagerSpy.getAuthorizable(getTestUser().getID());
+ assertNotNull(authorizable);
+ assertFalse(canImpersonateAllUsers(authorizable.getPrincipal(), userManagerSpy));
}
@Test
public void testIsImpersonatorByPrincipal() throws Exception {
+ Principal impersonatorPrincipal = getTestUser().getPrincipal();
+
+ UserManagerImpl userManagerSpy = spy((UserManagerImpl)getUserManager(root));
+ ConfigurationParameters configs = ImpersonationTestUtil.getMockedConfigs(userManagerSpy.getConfig(), impersonatorPrincipal.getName());
+ when(userManagerSpy.getConfig()).thenReturn(configs);
+
+ User user = userManagerSpy.getAuthorizable(getTestUser().getID(), User.class);
+ assertNotNull(user);
+ assertTrue(canImpersonateAllUsers(user.getPrincipal(), userManagerSpy));
+ }
+ @Test
+ public void testIsImpersonatorByNonMatchingPrincipal() throws Exception {
String impersonatorName = "impersonator";
Principal impersonatorPrincipal = new PrincipalImpl(impersonatorName);
@@ -261,26 +290,39 @@ public class UtilsTest extends AbstractSecurityTest {
PrincipalManager principalManagerMock = ImpersonationTestUtil.getMockedPrincipalManager(impersonatorName, impersonatorPrincipal);
when(userManagerSpy.getPrincipalManager()).thenReturn(principalManagerMock);
- root.commit();
- Authorizable authorizableMock = ImpersonationTestUtil.getAuthorizableWithPrincipal(impersonatorPrincipal);
- assertTrue(isImpersonator(authorizableMock, userManagerSpy));
+ User user = userManagerSpy.getAuthorizable(getTestUser().getID(), User.class);
+ assertNotNull(user);
+ assertFalse(canImpersonateAllUsers(user.getPrincipal(), userManagerSpy));
+ }
+
+ @Test
+ public void testIsImpersonatorOtherMgrImpl() throws Exception {
+ Principal p = new PrincipalImpl("mockPrincipal");
+
+ User userMock = when(mock(User.class).getPrincipal()).thenReturn(p).getMock();
+ UserManager umMock = when(mock(UserManager.class).getAuthorizable(p)).thenReturn(userMock).getMock();
+
+ assertFalse(canImpersonateAllUsers(p, umMock));
+ verify(umMock).getAuthorizable(p);
+ verify(userMock).isGroup();
+ verify(userMock).isAdmin();
+ verifyNoMoreInteractions(userMock, umMock);
}
@Test
public void testIsImpersonatorByGroup() throws Exception {
- String userName = "user";
String groupName = "impersonator_group";
- Principal impersonatorPrincipal = new PrincipalImpl(userName);
+ Principal impersonatorPrincipal = getTestUser().getPrincipal();
Principal impersonatorGroupPrincipal = new GroupPrincipal() {
@Override
public boolean isMember(@NotNull Principal member) {
- return member.getName().equals(userName);
+ return member.getName().equals(impersonatorPrincipal.getName());
}
@Override
- public Enumeration<? extends Principal> members() {
- return null;
+ public @NotNull Enumeration<? extends Principal> members() {
+ return Collections.emptyEnumeration();
}
@Override
@@ -292,13 +334,11 @@ public class UtilsTest extends AbstractSecurityTest {
UserManagerImpl userManagerSpy = spy((UserManagerImpl)getUserManager(root));
ConfigurationParameters configs = ImpersonationTestUtil.getMockedConfigs(userManagerSpy.getConfig(), groupName);
when(userManagerSpy.getConfig()).thenReturn(configs);
-
+
PrincipalManager principalManagerMock = ImpersonationTestUtil.getMockedPrincipalManager(groupName, impersonatorGroupPrincipal);
when(userManagerSpy.getPrincipalManager()).thenReturn(principalManagerMock);
- root.commit();
- Authorizable authorizableMock = ImpersonationTestUtil.getAuthorizableWithPrincipal(impersonatorPrincipal);
-
- assertTrue(isImpersonator(authorizableMock, userManagerSpy));
+
+ assertTrue(canImpersonateAllUsers(impersonatorPrincipal, userManagerSpy));
}
@Test