From 02b59ca833dd9297846352aaf41e4a4149c63fac Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Wed, 26 Jun 2019 17:26:10 +0200 Subject: [PATCH 1/9] Bug 5080: added convenience method to the SecurityService which calls hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification for each RoleDefinition in a set of RoleDefinitions. --- .../src/com/sap/sse/security/SecurityService.java | 3 +++ .../sap/sse/security/impl/SecurityServiceImpl.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/java/com.sap.sse.security/src/com/sap/sse/security/SecurityService.java b/java/com.sap.sse.security/src/com/sap/sse/security/SecurityService.java index 2321be2544e..d9715484f17 100644 --- a/java/com.sap.sse.security/src/com/sap/sse/security/SecurityService.java +++ b/java/com.sap.sse.security/src/com/sap/sse/security/SecurityService.java @@ -569,6 +569,9 @@ public interface SecurityService extends ReplicableWithObjectInputStream roleDefinitions, + Ownership qualificationForGrantedPermissions); + /** * @return {@code true} if the {@link UserStore} is initial or permission vertical migration is necessary. */ diff --git a/java/com.sap.sse.security/src/com/sap/sse/security/impl/SecurityServiceImpl.java b/java/com.sap.sse.security/src/com/sap/sse/security/impl/SecurityServiceImpl.java index 3ada13aefaf..aa48006c8de 100644 --- a/java/com.sap.sse.security/src/com/sap/sse/security/impl/SecurityServiceImpl.java +++ b/java/com.sap.sse.security/src/com/sap/sse/security/impl/SecurityServiceImpl.java @@ -1992,6 +1992,20 @@ public class SecurityServiceImpl implements ReplicableSecurityService, ClearStat return result; } + @Override + public boolean hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification(Set roleDefinitions, + Ownership qualificationForGrantedPermissions) { + boolean result = true; + for (final RoleDefinition roleDefinition : roleDefinitions) { + result &= hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification(roleDefinition, + qualificationForGrantedPermissions); + if (!result) { + break; + } + } + return result; + } + @Override public RoleDefinition getOrCreateRoleDefinitionFromPrototype(final RolePrototype rolePrototype) { final RoleDefinition potentiallyExistingRoleDefinition = store.getRoleDefinition(rolePrototype.getId()); From aa56707ba64e6c9221c5a0b4fa7e31a43356e83a Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Wed, 26 Jun 2019 17:26:48 +0200 Subject: [PATCH 2/9] Bug 5080: added meta permission check if the current user has all roles implicated by a user group before allowing him to add a user (possibly himself) to a user group --- .../sap/sse/security/jaxrs/api/UserGroupResource.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java index f364e7cd6a5..769a349725b 100644 --- a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java +++ b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java @@ -198,8 +198,13 @@ public class UserGroupResource extends AbstractSecurityResource { .build(); } else { if (getService().hasCurrentUserUpdatePermission(usergroup)) { - getService().addUserToUserGroup(usergroup, user); - response = Response.ok().build(); + if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( + usergroup.getRoleDefinitionMap().keySet(), new Ownership(null, usergroup))) { + response = Response.status(Status.UNAUTHORIZED).build(); + } else { + getService().addUserToUserGroup(usergroup, user); + response = Response.ok().build(); + } } else { response = Response.status(Status.UNAUTHORIZED).build(); } From 647f001ea14fe76324071f5941ff5802fc624c22 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Wed, 26 Jun 2019 17:30:07 +0200 Subject: [PATCH 3/9] Bug 5080: added meta permission check when adding a user to a user group to UserManagementServiceImpl --- .../sse/security/ui/server/UserManagementServiceImpl.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java index d0e73b848a4..bac9132c002 100644 --- a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java +++ b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java @@ -391,7 +391,13 @@ public class UserManagementServiceImpl extends RemoteServiceServlet implements U if (userByName == null) { throw new UserManagementException("user '" + username + "' not found."); } - getSecurityService().addUserToUserGroup(tenant, userByName); + if (getSecurityService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( + tenant.getRoleDefinitionMap().keySet(), new Ownership(null, tenant))) { + getSecurityService().addUserToUserGroup(tenant, userByName); + } else { + throw new UnauthorizedException( + "Current user does not have all the meta permissions of the user group the user would be added to"); + } } else { throw new UnauthorizedException("Not permitted to add user to group"); } From fac96ac24710c4104588ccb1e5dbf0d6394e17d2 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 13:54:50 +0200 Subject: [PATCH 4/9] Bug 5080: adjusted meta permission checks when editing a user group to check against the granted permissions of the user instead the ones of the group to edit --- .../com/sap/sse/security/jaxrs/api/UserGroupResource.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java index 769a349725b..f6d11ad86ae 100644 --- a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java +++ b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java @@ -180,6 +180,7 @@ public class UserGroupResource extends AbstractSecurityResource { @PathParam(KEY_USERNAME) String username) { Response response; try { + final UUID groupId = UUID.fromString(userGroupId); final UserGroup usergroup = getService().getUserGroup(groupId); @@ -199,7 +200,8 @@ public class UserGroupResource extends AbstractSecurityResource { } else { if (getService().hasCurrentUserUpdatePermission(usergroup)) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( - usergroup.getRoleDefinitionMap().keySet(), new Ownership(null, usergroup))) { + usergroup.getRoleDefinitionMap().keySet(), + new Ownership(getService().getCurrentUser(), null))) { response = Response.status(Status.UNAUTHORIZED).build(); } else { getService().addUserToUserGroup(usergroup, user); @@ -289,7 +291,7 @@ public class UserGroupResource extends AbstractSecurityResource { boolean roleForAll = Boolean.valueOf((String) jsonBody.get(KEY_FOR_ALL)); if (forAll == null || forAll != roleForAll) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification(role, - new Ownership(null, usergroup))) { + new Ownership(getService().getCurrentUser(), null))) { response = Response.status(Status.UNAUTHORIZED) .entity(String.format("Not permitted to add role %s to group", role.getName())) .build(); @@ -335,7 +337,7 @@ public class UserGroupResource extends AbstractSecurityResource { && getService().hasCurrentUserReadPermission(role)) { if (usergroup.getRoleAssociation(role) != null) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification(role, - new Ownership(null, usergroup))) { + new Ownership(getService().getCurrentUser(), null))) { response = Response .status(Status.UNAUTHORIZED).entity(String .format("Not permitted to remove role %s from group", role.getName())) From 1c7a48025c86c7a73c44d3f1e89ffccf45fe013c Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 13:57:10 +0200 Subject: [PATCH 5/9] Bug 5080: adjusted meta permission checks when editing a user group to check against the granted permissions of the user instead the ones of the group to edit --- .../sap/sse/security/ui/server/UserManagementServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java index bac9132c002..e2b4339d5c3 100644 --- a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java +++ b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java @@ -392,7 +392,7 @@ public class UserManagementServiceImpl extends RemoteServiceServlet implements U throw new UserManagementException("user '" + username + "' not found."); } if (getSecurityService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( - tenant.getRoleDefinitionMap().keySet(), new Ownership(null, tenant))) { + tenant.getRoleDefinitionMap().keySet(), new Ownership(userByName, null))) { getSecurityService().addUserToUserGroup(tenant, userByName); } else { throw new UnauthorizedException( From 464430f469dc2e1f4fb47cb8325017b786f91537 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 13:57:23 +0200 Subject: [PATCH 6/9] Bug 5080: added selenium test to test meta permission check when adding a user to a usergroup --- .../selenium/api/test/UserGroupApiTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java b/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java index 6292a749fa5..96f0822effa 100644 --- a/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java +++ b/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java @@ -7,6 +7,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.Arrays; + import org.junit.Before; import org.junit.Test; @@ -22,6 +24,7 @@ import com.sap.sse.common.Util; public class UserGroupApiTest extends AbstractSeleniumTest { private final UserGroupApi userGroupApi = new UserGroupApi(); + private final RoleApi roleApi = new RoleApi(); @Before public void setUp() { @@ -68,6 +71,41 @@ public class UserGroupApiTest extends AbstractSeleniumTest { } } + @Test + public void testMetaPermissionCheck() { + final ApiContext adminCtx = createAdminApiContext(getContextRoot(), SECURITY_CONTEXT); + + // add test users + final SecurityApi securityApi = new SecurityApi(); + final String addingUsername = "adding user"; + final String addedUsername = "added user"; + + securityApi.createUser(adminCtx, addingUsername, "test", "company", "password"); + securityApi.createUser(adminCtx, addedUsername, "test", "company", "password"); + + final ApiContext userCtx = ApiContext.createApiContext(getContextRoot(), SECURITY_CONTEXT, addingUsername, + "password"); + + // create user group + final UserGroup userGroupCreated = userGroupApi.createUserGroup(userCtx, "test-group-01"); + assertNotNull("GroupId is missing in reponse!", userGroupCreated.getGroupId()); + + // add new role to group with admin user + final Role createdRole = roleApi.createRole(adminCtx, "My-Epic-Role"); + roleApi.updateRole(adminCtx, createdRole.getId(), Arrays.asList("*"), createdRole.getName()); + userGroupApi.addRoleToGroup(adminCtx, userGroupCreated.getGroupId(), createdRole.getId(), false); + + try { + + // add user to group with a user who does not have one of the roles implicated by the group + userGroupApi.addUserToGroup(userCtx, userGroupCreated.getGroupId(), addedUsername); + + fail("Expected unauthorized exception."); + } catch (RuntimeException e) { + assertTrue("Expected unauthorized exception", e.getMessage().contains("failed (rc=401)")); + } + } + @Test public void testChangeUsersInUserGroup() { final ApiContext adminCtx = createAdminApiContext(getContextRoot(), SECURITY_CONTEXT); From b9c668cf9563ed222823a208f608c50522091ad5 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 15:01:18 +0200 Subject: [PATCH 7/9] Revert "Bug 5080: adjusted meta permission checks when editing a user group to check against the granted permissions of the user instead the ones of the group to edit" This reverts commit 1c7a48025c86c7a73c44d3f1e89ffccf45fe013c. --- .../sap/sse/security/ui/server/UserManagementServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java index e2b4339d5c3..bac9132c002 100644 --- a/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java +++ b/java/com.sap.sse.security.ui/src/main/java/com/sap/sse/security/ui/server/UserManagementServiceImpl.java @@ -392,7 +392,7 @@ public class UserManagementServiceImpl extends RemoteServiceServlet implements U throw new UserManagementException("user '" + username + "' not found."); } if (getSecurityService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( - tenant.getRoleDefinitionMap().keySet(), new Ownership(userByName, null))) { + tenant.getRoleDefinitionMap().keySet(), new Ownership(null, tenant))) { getSecurityService().addUserToUserGroup(tenant, userByName); } else { throw new UnauthorizedException( From c3f9bfc6039c12c70a490daa267c25beb6416ee0 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 15:01:23 +0200 Subject: [PATCH 8/9] Revert "Bug 5080: adjusted meta permission checks when editing a user group to check against the granted permissions of the user instead the ones of the group to edit" This reverts commit fac96ac24710c4104588ccb1e5dbf0d6394e17d2. --- .../com/sap/sse/security/jaxrs/api/UserGroupResource.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java index f6d11ad86ae..769a349725b 100644 --- a/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java +++ b/java/com.sap.sse.security/src/com/sap/sse/security/jaxrs/api/UserGroupResource.java @@ -180,7 +180,6 @@ public class UserGroupResource extends AbstractSecurityResource { @PathParam(KEY_USERNAME) String username) { Response response; try { - final UUID groupId = UUID.fromString(userGroupId); final UserGroup usergroup = getService().getUserGroup(groupId); @@ -200,8 +199,7 @@ public class UserGroupResource extends AbstractSecurityResource { } else { if (getService().hasCurrentUserUpdatePermission(usergroup)) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionsWithQualification( - usergroup.getRoleDefinitionMap().keySet(), - new Ownership(getService().getCurrentUser(), null))) { + usergroup.getRoleDefinitionMap().keySet(), new Ownership(null, usergroup))) { response = Response.status(Status.UNAUTHORIZED).build(); } else { getService().addUserToUserGroup(usergroup, user); @@ -291,7 +289,7 @@ public class UserGroupResource extends AbstractSecurityResource { boolean roleForAll = Boolean.valueOf((String) jsonBody.get(KEY_FOR_ALL)); if (forAll == null || forAll != roleForAll) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification(role, - new Ownership(getService().getCurrentUser(), null))) { + new Ownership(null, usergroup))) { response = Response.status(Status.UNAUTHORIZED) .entity(String.format("Not permitted to add role %s to group", role.getName())) .build(); @@ -337,7 +335,7 @@ public class UserGroupResource extends AbstractSecurityResource { && getService().hasCurrentUserReadPermission(role)) { if (usergroup.getRoleAssociation(role) != null) { if (!getService().hasCurrentUserMetaPermissionsOfRoleDefinitionWithQualification(role, - new Ownership(getService().getCurrentUser(), null))) { + new Ownership(null, usergroup))) { response = Response .status(Status.UNAUTHORIZED).entity(String .format("Not permitted to remove role %s from group", role.getName())) From 47992af58cd6503afcc4dc848e7762f071d87b97 Mon Sep 17 00:00:00 2001 From: Steffen Jacobs Date: Fri, 5 Jul 2019 15:52:14 +0200 Subject: [PATCH 9/9] Bug 5080: fixed meta permission check test in UserGroupApiTest --- .../src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java b/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java index 96f0822effa..8f4e88c7cdb 100644 --- a/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java +++ b/java/com.sap.sailing.selenium.test/src/com/sap/sailing/selenium/api/test/UserGroupApiTest.java @@ -87,7 +87,7 @@ public class UserGroupApiTest extends AbstractSeleniumTest { "password"); // create user group - final UserGroup userGroupCreated = userGroupApi.createUserGroup(userCtx, "test-group-01"); + final UserGroup userGroupCreated = userGroupApi.createUserGroup(adminCtx, "test-group-01"); assertNotNull("GroupId is missing in reponse!", userGroupCreated.getGroupId()); // add new role to group with admin user