diff --git a/src/prerna/semoss/web/services/config/ServerConfig.java b/src/prerna/semoss/web/services/config/ServerConfig.java index 862cf0c4..41851da5 100644 --- a/src/prerna/semoss/web/services/config/ServerConfig.java +++ b/src/prerna/semoss/web/services/config/ServerConfig.java @@ -89,6 +89,11 @@ public record ServerConfig(@Schema(description = "Whether R is enabled", require @Schema(description = "Whether only admins may give access to functions", requiredMode = REQUIRED) boolean adminOnlyFunctionAddAccess, @Schema(description = "Whether only admins may make functions public", requiredMode = REQUIRED) boolean adminOnlyFunctionSetPublic, @Schema(description = "Whether only admins may make functions discoverable", requiredMode = REQUIRED) boolean adminOnlyFunctionSetDiscoverable, + @Schema(description = "Whether only admins may add guardrails", requiredMode = REQUIRED) boolean adminOnlyGuardrailAdd, + @Schema(description = "Whether only admins may delete guardrails", requiredMode = REQUIRED) boolean adminOnlyGuardrailDelete, + @Schema(description = "Whether only admins may give access to guardrails", requiredMode = REQUIRED) boolean adminOnlyGuardrailAddAccess, + @Schema(description = "Whether only admins may make guardrails public", requiredMode = REQUIRED) boolean adminOnlyGuardrailSetPublic, + @Schema(description = "Whether only admins may make guardrails discoverable", requiredMode = REQUIRED) boolean adminOnlyGuardrailSetDiscoverable, @Schema(description = "Whether only admins may give access to insights", requiredMode = REQUIRED) boolean adminOnlyInsightAddAccess, @Schema(description = "Whether only admins may make insights public", requiredMode = REQUIRED) boolean adminOnlyInsightSetPublic, @Schema(description = "Whether only admins may share insights", requiredMode = REQUIRED) boolean adminOnlyInsightShare, diff --git a/src/prerna/semoss/web/services/config/ServerConfigurationResource.java b/src/prerna/semoss/web/services/config/ServerConfigurationResource.java index ee5750f1..579ca963 100644 --- a/src/prerna/semoss/web/services/config/ServerConfigurationResource.java +++ b/src/prerna/semoss/web/services/config/ServerConfigurationResource.java @@ -176,6 +176,11 @@ private static void loadConfig() { loadConfig.put("adminOnlyFunctionAddAccess", AbstractSecurityUtils.adminOnlyFunctionAddAccess()); loadConfig.put("adminOnlyFunctionSetPublic", AbstractSecurityUtils.adminOnlyFunctionSetPublic()); loadConfig.put("adminOnlyFunctionSetDiscoverable", AbstractSecurityUtils.adminOnlyFunctionSetDiscoverable()); + loadConfig.put("adminOnlyGuardrailAdd", AbstractSecurityUtils.adminOnlyGuardrailAdd()); + loadConfig.put("adminOnlyGuardrailDelete", AbstractSecurityUtils.adminOnlyGuardrailDelete()); + loadConfig.put("adminOnlyGuardrailAddAccess", AbstractSecurityUtils.adminOnlyGuardrailAddAccess()); + loadConfig.put("adminOnlyGuardrailSetPublic", AbstractSecurityUtils.adminOnlyGuardrailSetPublic()); + loadConfig.put("adminOnlyGuardrailSetDiscoverable", AbstractSecurityUtils.adminOnlyGuardrailSetDiscoverable()); // admin only for insight actions loadConfig.put("adminOnlyInsightAddAccess", AbstractSecurityUtils.adminOnlyInsightAddAccess()); loadConfig.put("adminOnlyInsightSetPublic", AbstractSecurityUtils.adminOnlyInsightSetPublic()); diff --git a/src/prerna/semoss/web/services/local/auth/AdminPermissionBatchResource.java b/src/prerna/semoss/web/services/local/auth/AdminPermissionBatchResource.java index fb8331ea..16d3f3f8 100644 --- a/src/prerna/semoss/web/services/local/auth/AdminPermissionBatchResource.java +++ b/src/prerna/semoss/web/services/local/auth/AdminPermissionBatchResource.java @@ -36,6 +36,7 @@ import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; +import prerna.auth.access.GranteePermissionQuery; import prerna.auth.utils.EffectivePermissionUtils; import prerna.auth.utils.SecurityAccessPermissionBatchUtils; import prerna.auth.utils.SecurityAdminUtils; @@ -81,6 +82,41 @@ public Response savePermissionBatches(@Context HttpServletRequest request) { .saveBatches(adminUtils, user, SecurityAccessPermissionBatchUtils.readBatches(batches))); } + /** + * Every permission any engine or project gives an app, an agent or an engine, + * in admin mode, each one changeable. + * + * @param search text the name or id of the target or grantee contains + * @param targetKind PROJECT or ENGINE, what gives the permission, or none for + * either + * @param targetType the target's type: a project's type, such as SKILL, or an + * engine's catalog type, such as MODEL + * @param granteeKind PROJECT or ENGINE, what is given the permission, or none + * for either + * @param granteeType the grantee's type in the same way, such as WORKSPACE + * @param limit how many to return; none or 0 for all + * @param offset how many to skip + * @return the page and how many match; 400 for a kind that is neither + */ + @GET + @Produces("application/json") + @Path("/getAllGranteePermissions") + public Response getAllGranteePermissions(@Context HttpServletRequest request, @QueryParam("search") String search, + @QueryParam("targetKind") String targetKind, @QueryParam("targetType") String targetType, + @QueryParam("granteeKind") String granteeKind, @QueryParam("granteeType") String granteeType, + @QueryParam("limit") int limit, @QueryParam("offset") int offset) { + String searchTerm = WebUtility.inputSanitizer(search); + String targetKindName = WebUtility.inputSanitizer(targetKind); + String target = WebUtility.inputSanitizer(targetType); + String granteeKindName = WebUtility.inputSanitizer(granteeKind); + String grantee = WebUtility.inputSanitizer(granteeType); + return ResourceUtility.respondAsAdmin(request, "list every permission engines and projects give as an admin", + SecurityAdminUtils::getInstance, + (user, adminUtils) -> SecurityAccessPermissionBatchUtils.getAllGranteePermissions(adminUtils, user, + new GranteePermissionQuery(searchTerm, targetKindName, target, granteeKindName, grantee, limit, + offset))); + } + /** * Who can use a project or an engine now, or who is in or manages a custom * group. diff --git a/src/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResource.java b/src/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResource.java index 3de19bb0..de207502 100644 --- a/src/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResource.java +++ b/src/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResource.java @@ -775,4 +775,86 @@ private static Map success() { return ret; } + /** + * Lists the apps, agents and engines a project gives a permission to, in admin + * mode. + * + * @param request the request, whose session holds the admin + * @param projectId the project + * @return one entry for each app and engine, with its id, catalog type + * ({@code PROJECT} for an app or an agent), name and subtype, and its + * level and end date; 403 for a user who is not an admin + */ + @GET + @Produces("application/json") + @Path("getProjectGranteePermissions") + public Response getProjectGranteePermissions(@Context HttpServletRequest request, + @QueryParam("projectId") String projectId) { + String id = WebUtility.inputSanitizer(projectId); + return ResourceUtility.respondAsAdmin(request, + "list the apps and engines project " + id + " gives a permission to", SecurityAdminUtils::getInstance, + (user, adminUtils) -> adminUtils.getProjectGranteePermissions(id)); + } + + /** + * Gives an app or an agent a permission on any project, or changes the one it + * has, in admin mode. A project gives access to apps and agents only. + * + * @param request the request, whose session holds the admin + * @param form {@code projectId}, {@code granteeEngineId}, + * {@code granteeEngineType} ({@code PROJECT}), + * {@code permission} (READ_ONLY or EDIT), and optionally + * {@code endDate} + * @return 200 when the permission is saved, 400 with the reason when it cannot + * be, and 403 for a user who is not an admin + */ + @POST + @Produces("application/json") + @Path("setProjectGranteePermission") + public Response setProjectGranteePermission(@Context HttpServletRequest request, + MultivaluedMap form) { + String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId")); + String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType")); + String permission = WebUtility.inputSanitizer(form.getFirst("permission")); + String endDate = WebUtility.inputSanitizer(form.getFirst("endDate")); + String action = "give " + granteeEngineType + " " + granteeEngineId + " " + permission + " access to project " + + projectId; + return ResourceUtility.respondAsAdmin(request, action, SecurityAdminUtils::getInstance, (user, adminUtils) -> { + String level = AccessPermissionEnum.require(permission); + adminUtils.setProjectGranteePermission(granteeEngineId, granteeEngineType, projectId, level, user, endDate); + Map ret = new HashMap<>(); + ret.put("success", true); + return ret; + }); + } + + /** + * Takes away the permission a project gives an app, an agent or an engine, in + * admin mode. + * + * @param request the request, whose session holds the admin + * @param form {@code projectId}, {@code granteeEngineId} and + * {@code granteeEngineType} + * @return 200 when the permission is taken away, 400 with the reason when it + * cannot be, and 403 for a user who is not an admin + */ + @POST + @Produces("application/json") + @Path("removeProjectGranteePermission") + public Response removeProjectGranteePermission(@Context HttpServletRequest request, + MultivaluedMap form) { + String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId")); + String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType")); + String action = "take away the access of " + granteeEngineType + " " + granteeEngineId + " to project " + + projectId; + return ResourceUtility.respondAsAdmin(request, action, SecurityAdminUtils::getInstance, (user, adminUtils) -> { + adminUtils.removeProjectGranteePermission(granteeEngineId, granteeEngineType, projectId, user); + Map ret = new HashMap<>(); + ret.put("success", true); + return ret; + }); + } + } diff --git a/src/prerna/semoss/web/services/local/auth/PermissionBatchResource.java b/src/prerna/semoss/web/services/local/auth/PermissionBatchResource.java index 2844e904..2d8ec4e9 100644 --- a/src/prerna/semoss/web/services/local/auth/PermissionBatchResource.java +++ b/src/prerna/semoss/web/services/local/auth/PermissionBatchResource.java @@ -38,6 +38,7 @@ import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; +import prerna.auth.access.GranteePermissionQuery; import prerna.auth.access.PermissionTargetResource; import prerna.auth.utils.EffectivePermissionUtils; import prerna.auth.utils.SecurityAccessPermissionBatchUtils; @@ -104,6 +105,43 @@ public Response getPermissions(@Context HttpServletRequest request, @QueryParam( user -> SecurityAccessPermissionBatchUtils.getPermissions(user, toTarget(resourceType, resource))); } + /** + * Every permission an engine or a project gives an app, an agent or an engine, + * on the engines and projects the signed in user can edit, for the page that + * lists them all. Each entry has its {@code target}, {@code subject}, + * {@code access} and {@code canChange}, true for the target's owners. + * + * @param search text the name or id of the target or grantee contains + * @param targetKind PROJECT or ENGINE, what gives the permission, or none for + * either + * @param targetType the target's type: a project's type, such as SKILL, or an + * engine's catalog type, such as MODEL + * @param granteeKind PROJECT or ENGINE, what is given the permission, or none + * for either + * @param granteeType the grantee's type in the same way, such as WORKSPACE + * @param limit how many to return; none or 0 for all + * @param offset how many to skip + * @return the page and how many match; 400 for a kind that is neither + */ + @GET + @Produces("application/json") + @Path("/getAllGranteePermissions") + public Response getAllGranteePermissions(@Context HttpServletRequest request, @QueryParam("search") String search, + @QueryParam("targetKind") String targetKind, @QueryParam("targetType") String targetType, + @QueryParam("granteeKind") String granteeKind, @QueryParam("granteeType") String granteeType, + @QueryParam("limit") int limit, @QueryParam("offset") int offset) { + String searchTerm = WebUtility.inputSanitizer(search); + String targetKindName = WebUtility.inputSanitizer(targetKind); + String target = WebUtility.inputSanitizer(targetType); + String granteeKindName = WebUtility.inputSanitizer(granteeKind); + String grantee = WebUtility.inputSanitizer(granteeType); + return ResourceUtility + .respond(request, "list the permissions engines and projects give", + user -> SecurityAccessPermissionBatchUtils.getAllGranteePermissions(user, + new GranteePermissionQuery(searchTerm, targetKindName, target, granteeKindName, grantee, + limit, offset))); + } + /** * The people to copy from a project, engine or custom group into another, with * the access each has on both. diff --git a/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java b/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java index 5bb9e283..d3cd3336 100644 --- a/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java +++ b/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java @@ -812,6 +812,92 @@ private static String textOf(Object value) { return value == null ? null : value.toString(); } + /** + * Lists the apps, agents and engines a project gives a permission to. An app's + * custom reactors, an agent's runs, or an engine's own code, use the permission + * in place of the signed in user's own. The project's editors and owners list + * them. + * + * @param request the request, whose session holds the user + * @param projectId the project + * @return one entry for each app and engine, with its id, catalog type + * ({@code PROJECT} for an app or an agent), name and subtype, and its + * level and end date; 403 for a user who cannot edit the project + */ + @GET + @Produces("application/json") + @Path("getProjectGranteePermissions") + public Response getProjectGranteePermissions(@Context HttpServletRequest request, + @QueryParam("projectId") String projectId) { + String id = WebUtility.inputSanitizer(projectId); + return ResourceUtility.respond(request, "list the apps and engines project " + id + " gives a permission to", + user -> SecurityProjectUtils.getProjectGranteePermissions(user, id)); + } + + /** + * Gives an app or an agent a permission on a project, or changes the one it + * has, so an app's custom reactors or an agent's runs can use the project for + * people who have no access to it. A project gives access to apps and agents + * only. Only the project's owners can. + * + * @param request the request, whose session holds the user + * @param form {@code projectId}, {@code granteeEngineId}, + * {@code granteeEngineType} ({@code PROJECT}), + * {@code permission} (READ_ONLY or EDIT), and optionally + * {@code endDate} + * @return 200 when the permission is saved, 400 with the reason when it cannot + * be, and 403 for a user who is not an owner of the project + */ + @POST + @Produces("application/json") + @Path("setProjectGranteePermission") + public Response setProjectGranteePermission(@Context HttpServletRequest request, + MultivaluedMap form) { + String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId")); + String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType")); + String permission = WebUtility.inputSanitizer(form.getFirst("permission")); + String endDate = WebUtility.inputSanitizer(form.getFirst("endDate")); + String action = "give " + granteeEngineType + " " + granteeEngineId + " " + permission + " access to project " + + projectId; + return ResourceUtility.respond(request, action, user -> { + String level = AccessPermissionEnum.require(permission); + SecurityProjectUtils.setProjectGranteePermission(user, granteeEngineId, granteeEngineType, projectId, level, + endDate); + Map ret = new HashMap<>(); + ret.put("success", true); + return ret; + }); + } + + /** + * Takes away the permission a project gives an app, an agent or an engine. Only + * the project's owners can. + * + * @param request the request, whose session holds the user + * @param form {@code projectId}, {@code granteeEngineId} and + * {@code granteeEngineType} + * @return 200 when the permission is taken away, 400 with the reason when it + * cannot be, and 403 for a user who is not an owner of the project + */ + @POST + @Produces("application/json") + @Path("removeProjectGranteePermission") + public Response removeProjectGranteePermission(@Context HttpServletRequest request, + MultivaluedMap form) { + String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String granteeEngineId = WebUtility.inputSanitizer(form.getFirst("granteeEngineId")); + String granteeEngineType = WebUtility.inputSanitizer(form.getFirst("granteeEngineType")); + String action = "take away the access of " + granteeEngineType + " " + granteeEngineId + " to project " + + projectId; + return ResourceUtility.respond(request, action, user -> { + SecurityProjectUtils.removeProjectGranteePermission(user, granteeEngineId, granteeEngineType, projectId); + Map ret = new HashMap<>(); + ret.put("success", true); + return ret; + }); + } + /** * Refuses a user who cannot edit the project. Propagating a project's * permissions gives people access to everything it depends on, so it is limited diff --git a/test/prerna/semoss/web/services/local/auth/AdminPermissionBatchResourceTest.java b/test/prerna/semoss/web/services/local/auth/AdminPermissionBatchResourceTest.java index 8f9a5353..f18a1b82 100644 --- a/test/prerna/semoss/web/services/local/auth/AdminPermissionBatchResourceTest.java +++ b/test/prerna/semoss/web/services/local/auth/AdminPermissionBatchResourceTest.java @@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.when; @@ -157,7 +158,9 @@ static Stream> endpoints() { endpoint("getPermissions", (r, q) -> r.getPermissions(q, "PROJECT", "p1")), endpoint("getPermissionsToCopy", (r, q) -> r.getPermissionsToCopy(q, "PROJECT", "p1", "PROJECT", "p2")), endpoint("getEffectivePermissions", (r, q) -> r.getEffectivePermissions(q, "ENGINE", "e1")), - endpoint("getUsageLimitPath", (r, q) -> r.getUsageLimitPath(q, "e1", "u1", "NATIVE"))); + endpoint("getUsageLimitPath", (r, q) -> r.getUsageLimitPath(q, "e1", "u1", "NATIVE")), + endpoint("getAllGranteePermissions", + (r, q) -> r.getAllGranteePermissions(q, null, null, null, null, null, 0, 0))); } @ParameterizedTest @@ -201,8 +204,9 @@ void validatesTheBatchesInAdminModeWithoutSavingThem() throws Exception { assertEquals(200, response.getStatus()); assertEquals(true, body(response).getAsJsonArray().get(0).getAsJsonObject().get("valid").getAsBoolean()); - f.batches.verify(() -> SecurityAccessPermissionBatchUtils.saveBatches(any(SecurityAdminUtils.class), - any(), any()), never()); + f.batches.verify( + () -> SecurityAccessPermissionBatchUtils.saveBatches(any(SecurityAdminUtils.class), any(), any()), + never()); } } @@ -232,8 +236,9 @@ void answersMissingBatchesWith400WithoutSaving() throws Exception { assertEquals(400, response.getStatus()); assertEquals("Must define the permission batches", errorMessage(response)); - f.batches.verify(() -> SecurityAccessPermissionBatchUtils.saveBatches(any(SecurityAdminUtils.class), - any(), any()), never()); + f.batches.verify( + () -> SecurityAccessPermissionBatchUtils.saveBatches(any(SecurityAdminUtils.class), any(), any()), + never()); } } @@ -258,11 +263,12 @@ void answersAFailedSaveWith500AndTheTechnicalMessage() throws Exception { @ParameterizedTest @CsvSource({ "project, PROJECT", "ENGINE, ENGINE", " team , TEAM", "Team_Managers, TEAM_MANAGERS" }) - void readsThePermissionsOnATargetOfAnyKnownType(String resourceType, TargetResourceType expected) - throws Exception { + void readsThePermissionsOnATargetOfAnyKnownType(String resourceType, TargetResourceType expected) throws Exception { try (Fixture f = Fixture.asAdmin()) { - f.batches.when(() -> SecurityAccessPermissionBatchUtils.getPermissions(Mockito.eq(f.utils), - Mockito.eq(f.user), argThat(target(expected, "x1")))).thenReturn(Map.of("people", List.of())); + f.batches + .when(() -> SecurityAccessPermissionBatchUtils.getPermissions(Mockito.eq(f.utils), + Mockito.eq(f.user), argThat(target(expected, "x1")))) + .thenReturn(Map.of("people", List.of())); Response response = new AdminPermissionBatchResource().getPermissions(f.request, resourceType, "x1"); @@ -271,6 +277,24 @@ void readsThePermissionsOnATargetOfAnyKnownType(String resourceType, TargetResou } } + @Test + void listsEveryPermissionEnginesAndProjectsGiveInAdminMode() throws Exception { + try (Fixture f = Fixture.asAdmin()) { + f.batches + .when(() -> SecurityAccessPermissionBatchUtils.getAllGranteePermissions(eq(f.utils), eq(f.user), + argThat(query -> "claims".equals(query.search()) && "PROJECT".equals(query.targetKind()) + && "SKILL".equals(query.targetType()) && "PROJECT".equals(query.granteeKind()) + && query.granteeType() == null && query.limit() == 25 && query.offset() == 50))) + .thenReturn(Map.of("grants", List.of(), "total", 0)); + + Response response = new AdminPermissionBatchResource().getAllGranteePermissions(f.request, "claims", + "PROJECT", "SKILL", "PROJECT", null, 25, 50); + + assertEquals(200, response.getStatus()); + assertEquals(0, body(response).getAsJsonObject().get("total").getAsInt()); + } + } + @Test void rejectsAnUnknownOrMissingResourceType() throws Exception { try (Fixture f = Fixture.asAdmin()) { @@ -306,8 +330,9 @@ void readsThePeopleToCopyFromOneTargetToAnother() throws Exception { @Test void readsTheEffectivePermissionsOnAProject() throws Exception { try (Fixture f = Fixture.asAdmin()) { - f.effective.when(() -> EffectivePermissionUtils.getEffectivePermissions(Mockito.eq(f.utils), - Mockito.eq(f.user), argThat(target(TargetResourceType.PROJECT, "p1")))) + f.effective + .when(() -> EffectivePermissionUtils.getEffectivePermissions(Mockito.eq(f.utils), + Mockito.eq(f.user), argThat(target(TargetResourceType.PROJECT, "p1")))) .thenReturn(Map.of("total", 2)); Response response = new AdminPermissionBatchResource().getEffectivePermissions(f.request, "PROJECT", "p1"); @@ -333,8 +358,9 @@ void readsHowUsageLimitsApplyToAPersonByIdAndLoginType() throws Exception { @Test void passesAMissingLoginTypeOnForTheUsageLimitPathToReject() throws Exception { try (Fixture f = Fixture.asAdmin()) { - f.batches.when(() -> SecurityAccessPermissionBatchUtils.getUsageLimitPath(f.utils, f.user, "e1", "u1", - null)).thenThrow(new IllegalArgumentException("Must define the user's login type")); + f.batches + .when(() -> SecurityAccessPermissionBatchUtils.getUsageLimitPath(f.utils, f.user, "e1", "u1", null)) + .thenThrow(new IllegalArgumentException("Must define the user's login type")); Response response = new AdminPermissionBatchResource().getUsageLimitPath(f.request, "e1", "u1", null); diff --git a/test/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResourceTest.java b/test/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResourceTest.java index 590cb572..cecd240e 100644 --- a/test/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResourceTest.java +++ b/test/prerna/semoss/web/services/local/auth/AdminProjectAuthorizationResourceTest.java @@ -290,7 +290,45 @@ static Stream> endpoints() { endpoint("addProjectUserPermissions", (r, q) -> r.addProjectUserPermissions(q, form("projectId", "p1"))), endpoint("removeProjectUserPermissions", - (r, q) -> r.removeProjectUserPermissions(q, form("projectId", "p1")))); + (r, q) -> r.removeProjectUserPermissions(q, form("projectId", "p1"))), + endpoint("getProjectGranteePermissions", (r, q) -> r.getProjectGranteePermissions(q, "p1")), + endpoint("setProjectGranteePermission", + (r, q) -> r.setProjectGranteePermission(q, + form("projectId", "p1", "granteeEngineId", "agent1", "granteeEngineType", "PROJECT", + "permission", "READ_ONLY"))), + endpoint("removeProjectGranteePermission", (r, q) -> r.removeProjectGranteePermission(q, + form("projectId", "p1", "granteeEngineId", "agent1", "granteeEngineType", "PROJECT")))); + } + + @Test + void listsTheAppsAgentsAndEnginesAProjectGivesAPermissionTo() throws Exception { + try (Fixture f = Fixture.asAdmin()) { + List> grantees = List + .of(Map.of("subject", Map.of("id", "agent1", "type", "PROJECT", "name", "Claims Agent"))); + when(f.utils.getProjectGranteePermissions("p1")).thenReturn(grantees); + + Response response = new AdminProjectAuthorizationResource().getProjectGranteePermissions(f.request, "p1"); + + assertEquals(200, response.getStatus()); + verify(f.utils).getProjectGranteePermissions("p1"); + } + } + + @Test + void givesAndTakesAwayAProjectsPermissionInAdminMode() throws Exception { + try (Fixture f = Fixture.asAdmin()) { + AdminProjectAuthorizationResource resource = new AdminProjectAuthorizationResource(); + + Response given = resource.setProjectGranteePermission(f.request, form("projectId", "skill1", + "granteeEngineId", "agent1", "granteeEngineType", "PROJECT", "permission", "edit")); + Response taken = resource.removeProjectGranteePermission(f.request, + form("projectId", "skill1", "granteeEngineId", "agent1", "granteeEngineType", "PROJECT")); + + assertEquals(200, given.getStatus()); + assertEquals(200, taken.getStatus()); + verify(f.utils).setProjectGranteePermission("agent1", "PROJECT", "skill1", "EDIT", f.user, null); + verify(f.utils).removeProjectGranteePermission("agent1", "PROJECT", "skill1", f.user); + } } @ParameterizedTest diff --git a/test/prerna/semoss/web/services/local/auth/PermissionBatchResourceTest.java b/test/prerna/semoss/web/services/local/auth/PermissionBatchResourceTest.java index c318af51..fcebf7a5 100644 --- a/test/prerna/semoss/web/services/local/auth/PermissionBatchResourceTest.java +++ b/test/prerna/semoss/web/services/local/auth/PermissionBatchResourceTest.java @@ -86,8 +86,8 @@ void validatesTheBatchesItReadsWithTheCallersOwnPermissions() throws Exception { assertEquals(List.of(Map.of("valid", true)), body(response)); statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.validateBatches(eq(user), argThat(PermissionBatchResourceTest::isTheOneProjectBatch))); - statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.validateBatches(any(SecurityAdminUtils.class), - any(), anyList()), never()); + statics.batches.verify(() -> SecurityAccessPermissionBatchUtils + .validateBatches(any(SecurityAdminUtils.class), any(), anyList()), never()); } } @@ -125,8 +125,8 @@ void refusesMissingEmptyOrUnreadableBatchesWith400() throws Exception { assertTrue(errorOf(unreadable).startsWith("The permission batches are not valid"), errorOf(unreadable)); statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.saveBatches(any(User.class), anyList()), never()); - statics.batches.verify( - () -> SecurityAccessPermissionBatchUtils.validateBatches(any(User.class), anyList()), never()); + statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.validateBatches(any(User.class), anyList()), + never()); } } @@ -161,6 +161,36 @@ void readsThePermissionsOfATargetNamedInAnyCase() throws Exception { } } + @Test + void listsThePermissionsOnTheResourcesTheUserCanEdit() throws Exception { + User user = signedIn("alice"); + Map grants = Map.of("grants", List.of()); + try (Statics statics = new Statics()) { + statics.batches.when(() -> SecurityAccessPermissionBatchUtils.getAllGranteePermissions(eq(user), + argThat(query -> "claims".equals(query.search()) && "ENGINE".equals(query.targetKind()) + && "MODEL".equals(query.targetType()) && "PROJECT".equals(query.granteeKind()) + && "WORKSPACE".equals(query.granteeType()) && query.limit() == 10 && query.offset() == 0))) + .thenReturn(grants); + + Response response = resource.getAllGranteePermissions(requestFor(user), "claims", "engine", "MODEL", + "PROJECT", "WORKSPACE", 10, 0); + + assertEquals(200, response.getStatus()); + assertEquals(grants, body(response)); + } + } + + @Test + void refusesAKindThatIsNeitherAProjectNorAnEngineWith400() throws Exception { + User user = signedIn("alice"); + try (Statics statics = new Statics()) { + Response response = resource.getAllGranteePermissions(requestFor(user), null, "FOLDER", null, null, null, 0, + 0); + + assertEquals(400, response.getStatus()); + } + } + @Test void refusesAnUnknownOrMissingResourceTypeWith400() throws Exception { User user = signedIn("alice"); @@ -201,9 +231,8 @@ void readsThePeopleToCopyBetweenTwoTargets() throws Exception { "Sales"); assertEquals(List.of(Map.of("id", "bob")), body(response)); - statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.getPermissionsToCopy(eq(user), - argThat(source -> source.getResourceType() == TargetResourceType.TEAM - && "Sales".equals(source.getId())), + statics.batches.verify(() -> SecurityAccessPermissionBatchUtils.getPermissionsToCopy(eq(user), argThat( + source -> source.getResourceType() == TargetResourceType.TEAM && "Sales".equals(source.getId())), argThat(target -> target.getResourceType() == TargetResourceType.TEAM_MANAGERS && "Sales".equals(target.getId())))); } @@ -231,9 +260,8 @@ void readsTheEffectivePermissionsOfATarget() throws Exception { Response response = resource.getEffectivePermissions(requestFor(user), "project", "p1"); assertEquals(Map.of("rows", List.of()), body(response)); - statics.effective.verify(() -> EffectivePermissionUtils.getEffectivePermissions(eq(user), - argThat(target -> target.getResourceType() == TargetResourceType.PROJECT - && "p1".equals(target.getId())))); + statics.effective.verify(() -> EffectivePermissionUtils.getEffectivePermissions(eq(user), argThat( + target -> target.getResourceType() == TargetResourceType.PROJECT && "p1".equals(target.getId())))); } } @@ -255,7 +283,8 @@ void readsAPersonsUsageLimitPathWithTheirLoginType() throws Exception { void refusesAUsageLimitPathToSomeoneWhoCannotEditTheModel() throws Exception { User user = signedIn("mallory"); try (Statics statics = new Statics()) { - statics.batches.when(() -> SecurityAccessPermissionBatchUtils.getUsageLimitPath(user, "m1", "bob", "NATIVE")) + statics.batches + .when(() -> SecurityAccessPermissionBatchUtils.getUsageLimitPath(user, "m1", "bob", "NATIVE")) .thenThrow(new IllegalAccessException("Only the model's editors and owners can see this")); Response response = resource.getUsageLimitPath(requestFor(user), "m1", "bob", "NATIVE"); @@ -357,8 +386,8 @@ private static String errorOf(Response response) throws Exception { } /** - * The Semoss statics these endpoints call, mocked for one test. Reading - * batches stays real, so the endpoints are tested with what they parse. + * The Semoss statics these endpoints call, mocked for one test. Reading batches + * stays real, so the endpoints are tested with what they parse. */ private static final class Statics implements AutoCloseable { diff --git a/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java b/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java index e7987323..d15cf93e 100644 --- a/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java +++ b/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java @@ -769,6 +769,93 @@ private static List values(NounStore store, ReactorKeysEnum key) { return struct == null ? null : struct.getAllValues(); } + @Test + void listsTheAppsAgentsAndEnginesAProjectGivesAPermissionTo() throws Exception { + User user = signedIn("alice"); + List> grantees = List.of(Map.of("subject", Map.of("id", "agent1", "type", "PROJECT"))); + try (Statics statics = new Statics()) { + statics.project.when(() -> SecurityProjectUtils.getProjectGranteePermissions(user, "skill1")) + .thenReturn(grantees); + + Response response = resource.getProjectGranteePermissions(requestFor(user), "skill1"); + + assertEquals(200, response.getStatus()); + assertEquals(grantees, body(response)); + } + } + + @Test + void refusesListingAProjectsGranteesToSomeoneWhoCannotEditItWith403() throws Exception { + User user = signedIn("mallory"); + String message = "Only this project's editors and owners can see which apps and engines can use it"; + try (Statics statics = new Statics()) { + statics.project.when(() -> SecurityProjectUtils.getProjectGranteePermissions(user, "skill1")) + .thenThrow(new IllegalAccessException(message)); + + Response response = resource.getProjectGranteePermissions(requestFor(user), "skill1"); + + assertEquals(403, response.getStatus()); + assertEquals(message, errorOf(response)); + } + } + + @Test + void givesAnAgentAPermissionOnASkill() throws Exception { + User user = signedIn("alice"); + try (Statics statics = new Statics()) { + Response response = resource.setProjectGranteePermission(requestFor(user), form("projectId", "skill1", + "granteeEngineId", "agent1", "granteeEngineType", "PROJECT", "permission", " read_only ")); + + assertEquals(200, response.getStatus()); + assertEquals(Map.of("success", true), body(response)); + statics.project.verify(() -> SecurityProjectUtils.setProjectGranteePermission(user, "agent1", "PROJECT", + "skill1", "READ_ONLY", null)); + } + } + + @Test + void refusesSomeoneWhoIsNotAnOwnerGivingAProjectsPermissionWith403() throws Exception { + User user = signedIn("mallory"); + String message = "Only this project's owners can change which apps and engines can use it."; + try (Statics statics = new Statics()) { + statics.project.when(() -> SecurityProjectUtils.setProjectGranteePermission(user, "agent1", "PROJECT", + "skill1", "EDIT", null)).thenThrow(new IllegalAccessException(message)); + + Response response = resource.setProjectGranteePermission(requestFor(user), form("projectId", "skill1", + "granteeEngineId", "agent1", "granteeEngineType", "PROJECT", "permission", "EDIT")); + + assertEquals(403, response.getStatus()); + assertEquals(message, errorOf(response)); + } + } + + @Test + void refusesALevelThatIsNotOneForAProjectGranteeWith400() throws Exception { + User user = signedIn("alice"); + try (Statics statics = new Statics()) { + Response response = resource.setProjectGranteePermission(requestFor(user), form("projectId", "skill1", + "granteeEngineId", "agent1", "granteeEngineType", "PROJECT", "permission", "OWNR")); + + assertEquals(400, response.getStatus()); + statics.project.verify(() -> SecurityProjectUtils.setProjectGranteePermission(any(), anyString(), + anyString(), anyString(), anyString(), any()), Mockito.never()); + } + } + + @Test + void takesAwayTheAccessOfAnAgentToASkill() throws Exception { + User user = signedIn("alice"); + try (Statics statics = new Statics()) { + Response response = resource.removeProjectGranteePermission(requestFor(user), + form("projectId", "skill1", "granteeEngineId", "agent1", "granteeEngineType", "PROJECT")); + + assertEquals(200, response.getStatus()); + assertEquals(Map.of("success", true), body(response)); + statics.project.verify( + () -> SecurityProjectUtils.removeProjectGranteePermission(user, "agent1", "PROJECT", "skill1")); + } + } + private static MultivaluedMap form(String... pairs) { MultivaluedMap form = new MultivaluedHashMap<>(); for (int i = 0; i < pairs.length; i += 2) {