diff --git a/src/prerna/semoss/web/services/local/EngineRouteResource.java b/src/prerna/semoss/web/services/local/EngineRouteResource.java index b0cc5f39..a9170e5d 100644 --- a/src/prerna/semoss/web/services/local/EngineRouteResource.java +++ b/src/prerna/semoss/web/services/local/EngineRouteResource.java @@ -27,6 +27,7 @@ *******************************************************************************/ package prerna.semoss.web.services.local; +import prerna.auth.utils.CatalogDependencyDerivation; import java.io.File; import java.io.FileFilter; import java.io.FileWriter; @@ -340,6 +341,9 @@ private static Map saveSmssFileWhileLocked(User user, SecurityAd "An error occurred re-connecting to the engine with the new credentials. Detailed message = " + e.getMessage()); } + // the smss may name a different judge model, vector database or embedder; recorded + // first, since the engine is open with it now and the steps after can fail + CatalogDependencyDerivation.syncSmss(user, engineId, engine.getCatalogType().name(), engine.getSmssProp()); syncDisplayNameToSecurityDb(user, adminUtils, engineId, oldDisplayName, newDisplayName); // the smss file is unchanged and the new values live in the secret store, so @@ -405,6 +409,9 @@ private static Map saveSmssFileWhileLocked(User user, SecurityAd throw new IllegalArgumentException( "An error occurred initializing the new engine details. Detailed message = " + e.getMessage()); } + // the smss may name a different judge model, vector database or embedder; recorded + // first, since the engine is open with it now and the steps after can fail + CatalogDependencyDerivation.syncSmss(user, engineId, engine.getCatalogType().name(), engine.getSmssProp()); syncDisplayNameToSecurityDb(user, adminUtils, engineId, oldDisplayName, newDisplayName); // push to cloud diff --git a/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java b/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java index 5bb9e283..455369f4 100644 --- a/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java +++ b/src/prerna/semoss/web/services/local/auth/ProjectAuthorizationResource.java @@ -50,8 +50,11 @@ import prerna.auth.User; import prerna.auth.access.ModelUsageLimit; import prerna.auth.utils.AbstractSecurityUtils; +import prerna.auth.utils.CatalogTypes; +import prerna.auth.utils.SecurityEngineUtils; import prerna.auth.utils.SecurityAdminUtils; import prerna.auth.utils.SecurityModelUsageLimitUtils; +import prerna.auth.utils.SecurityCatalogDependencyUtils; import prerna.auth.utils.SecurityProjectUtils; import prerna.graph.utility.MsGraphUtility; import prerna.io.connector.ms.MicrosoftGraphUserLookup; @@ -324,8 +327,11 @@ public Response addProjectUserPermission(@Context HttpServletRequest request, Mu } /** - * Propagate project dependent permissions. The person's usage limit on the - * models the project depends on is sent as {@code usageLimit} (JSON, empty for + * Gives a person access to the engines and projects an engine or project + * depends on, or asks their owners for it. The engine or project is + * {@code catalogId}, with its engine type or project type as the optional + * {@code catalogType}. The person's usage limit on the + * models it depends on is sent as {@code usageLimit} (JSON, empty for * no limit defined). Without it, a person added to a model gets no limit and a * person whose access to a model changes keeps their limit. Only a model's * owners set a limit that differs from the current one. @@ -336,39 +342,37 @@ public Response addProjectUserPermission(@Context HttpServletRequest request, Mu */ @POST @Produces("application/json") - @Path("propagateProjectDependencyPermission") - public Response propagateProjectDependencyPermission(@Context HttpServletRequest request, + @Path("propagateCatalogDependencyPermission") + public Response propagateCatalogDependencyPermission(@Context HttpServletRequest request, MultivaluedMap form) { String newUserId = WebUtility.inputSQLSanitizer(form.getFirst("id")); String newUserType = WebUtility.inputSanitizer(form.getFirst("type")); - String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String catalogId = WebUtility.inputSanitizer(form.getFirst("catalogId")); + String catalogType = WebUtility.inputSanitizer(form.getFirst("catalogType")); String requestedPermission = WebUtility.inputSanitizer(form.getFirst("permission")); String endDate = WebUtility.inputSanitizer(form.getFirst("endDate")); boolean usageLimitSent = form.containsKey(USAGE_LIMIT); String usageLimit = form.getFirst(USAGE_LIMIT); - return ResourceUtility.respond(request, "give user " + newUserId + " access to project " + projectId, user -> { - // Determine if admin right are required to add users and, if so, if requester - // has those rights. - if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) { - throw new IllegalAccessException(ADMINS_ONLY); - } - requireProjectEditor(user, projectId); + return ResourceUtility.respond(request, + "give user " + newUserId + " access to the dependencies of " + catalogId, user -> { + requireCatalogDependencyEditor(user, catalogId, catalogType); String level = AccessPermissionEnum.require(requestedPermission); if (usageLimitSent) { - return SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, level, + return SecurityCatalogDependencyUtils.propagatePermission(user, catalogId, newUserId, newUserType, level, endDate, SecurityModelUsageLimitUtils.readJson(usageLimit)); } - return SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, level, + return SecurityCatalogDependencyUtils.propagatePermission(user, catalogId, newUserId, newUserType, level, endDate); }); } /** - * Propagate project dependent permissions, in bulk. Each row of - * {@code userpermissions} can carry the person's usage limit on the models the - * project depends on as {@code usageLimit}. Without it, a person whose access + * Gives people access to the engines and projects an engine or project depends + * on, in bulk, as {@link #propagateCatalogDependencyPermission} does for one. + * Each row of {@code userpermissions} can carry the person's usage limit on the + * models it depends on as {@code usageLimit}. Without it, a person whose access * to a model changes keeps their limit. * * @param request @@ -377,20 +381,16 @@ public Response propagateProjectDependencyPermission(@Context HttpServletRequest */ @POST @Produces("application/json") - @Path("propagateProjectDependencyPermissions") - public Response propagateProjectDependencyPermissions(@Context HttpServletRequest request, + @Path("propagateCatalogDependencyPermissions") + public Response propagateCatalogDependencyPermissions(@Context HttpServletRequest request, MultivaluedMap form) { - String projectId = WebUtility.inputSanitizer(form.getFirst("projectId")); + String catalogId = WebUtility.inputSanitizer(form.getFirst("catalogId")); + String catalogType = WebUtility.inputSanitizer(form.getFirst("catalogType")); String endDate = WebUtility.inputSanitizer(form.getFirst("endDate")); - return ResourceUtility.respond(request, "give users access to project " + projectId, user -> { + return ResourceUtility.respond(request, "give users access to the dependencies of " + catalogId, user -> { List> requests = ROW_GSON.fromJson(form.getFirst("userpermissions"), List.class); - // Determine if admin right are required to add users and, if so, if requester - // has those rights. - if (AbstractSecurityUtils.adminOnlyProjectAddAccess(projectId) && !SecurityAdminUtils.userIsAdmin(user)) { - throw new IllegalAccessException(ADMINS_ONLY); - } - requireProjectEditor(user, projectId); + requireCatalogDependencyEditor(user, catalogId, catalogType); // every row is checked before the first one is propagated AccessPermissionEnum.requireEach(requests); @@ -401,10 +401,10 @@ public Response propagateProjectDependencyPermissions(@Context HttpServletReques String requestedPermission = textOf(userRequest.get("permission")); Map responses; if (userRequest.containsKey(USAGE_LIMIT)) { - responses = SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, + responses = SecurityCatalogDependencyUtils.propagatePermission(user, catalogId, newUserId, newUserType, requestedPermission, endDate, readUsageLimit(userRequest.get(USAGE_LIMIT))); } else { - responses = SecurityProjectUtils.propagateProjectPermission(user, projectId, newUserId, newUserType, + responses = SecurityCatalogDependencyUtils.propagatePermission(user, catalogId, newUserId, newUserType, requestedPermission, endDate); } userRet.put(newUserId, responses); @@ -812,6 +812,32 @@ private static String textOf(Object value) { return value == null ? null : value.toString(); } + /** + * Checks that a user can give people access to what an engine or project + * depends on: they can edit it, and they are an admin when only admins can add + * people to it. + * + * @param catalogId the engine id or project id + * @param catalogType its engine type or project type, or null to read it + * @throws IllegalAccessException when they cannot + */ + private static void requireCatalogDependencyEditor(User user, String catalogId, String catalogType) + throws IllegalAccessException { + String type = SecurityCatalogDependencyUtils.resolveCatalogType(catalogId, catalogType); + boolean isProject = CatalogTypes.isProjectType(type); + boolean adminOnly = isProject ? AbstractSecurityUtils.adminOnlyProjectAddAccess(catalogId) + : AbstractSecurityUtils.adminOnlyEngineAddAccess(catalogId); + if (adminOnly && !SecurityAdminUtils.userIsAdmin(user)) { + throw new IllegalAccessException(ADMINS_ONLY); + } + if (isProject) { + requireProjectEditor(user, catalogId); + } else if (!SecurityEngineUtils.userCanEditEngine(user, catalogId)) { + throw new IllegalAccessException( + "Engine " + catalogId + " does not exist or user does not have access to edit it"); + } + } + /** * 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/EngineRouteResourceTest.java b/test/prerna/semoss/web/services/local/EngineRouteResourceTest.java index 67a86428..5ba33c6a 100644 --- a/test/prerna/semoss/web/services/local/EngineRouteResourceTest.java +++ b/test/prerna/semoss/web/services/local/EngineRouteResourceTest.java @@ -67,6 +67,7 @@ import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.StreamingOutput; import prerna.auth.User; +import prerna.auth.utils.CatalogDependencyDerivation; import prerna.auth.utils.SecurityAdminUtils; import prerna.auth.utils.SecurityEngineUtils; import prerna.cluster.util.ClusterUtil; @@ -400,6 +401,24 @@ void rewritesTheSmssAndReopensTheEngine() throws Exception { } } + @Test + void recordsTheSmssDependenciesBeforeTheClusterPushThatCanFail() throws Exception { + try (Statics s = new Statics(); + MockedStatic derivation = Mockito + .mockStatic(CatalogDependencyDerivation.class)) { + s.security.when(() -> SecurityEngineUtils.userIsOwner(this.user, "db1")).thenReturn(true); + IEngine engine = engineWithSmss(this.tempDir.resolve("db1.smss")); + s.utilityValues.put("getEngine", engine); + s.cluster.when(() -> ClusterUtil.pushEngineSmss("db1", IEngine.CATALOG_TYPE.DATABASE)) + .thenThrow(new IllegalArgumentException("cloud is down")); + + assertBody(new EngineRouteResource().updateSmssFile(smssRequest(CURRENT_SMSS), "db1"), 400, + "cloud is down"); + derivation.verify(() -> CatalogDependencyDerivation.syncSmss(eq(this.user), eq("db1"), eq("DATABASE"), + any())); + } + } + @Test void notifiesTheUserOfAnSmssUpdate() throws Exception { try (Statics s = new Statics()) { diff --git a/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java b/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java index e7987323..b9b5e72b 100644 --- a/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java +++ b/test/prerna/semoss/web/services/local/auth/ProjectAuthorizationResourceTest.java @@ -68,6 +68,7 @@ import prerna.auth.utils.AbstractSecurityUtils; import prerna.auth.utils.SecurityAdminUtils; import prerna.auth.utils.SecurityModelUsageLimitUtils; +import prerna.auth.utils.SecurityCatalogDependencyUtils; import prerna.auth.utils.SecurityProjectUtils; import prerna.graph.utility.MsGraphUtility; import prerna.io.connector.ms.MicrosoftGraphUserLookup; @@ -289,10 +290,10 @@ void keepsEveryAccessChangeToAdminsWhenTheProjectSaysSo() throws Exception { form("projectId", "p1", "userpermissions", "[]")), resource.removeProjectUserPermission(requestFor(user), form("id", "bob", "projectId", "p1")), resource.removeProjectUserPermissions(requestFor(user), form("projectId", "p1", "ids", "[]")), - resource.propagateProjectDependencyPermission(requestFor(user), - form("id", "bob", "type", "NATIVE", "projectId", "p1", "permission", "EDIT")), - resource.propagateProjectDependencyPermissions(requestFor(user), - form("projectId", "p1", "userpermissions", "[]")), + resource.propagateCatalogDependencyPermission(requestFor(user), + form("id", "bob", "type", "NATIVE", "catalogId", "p1", "permission", "EDIT")), + resource.propagateCatalogDependencyPermissions(requestFor(user), + form("catalogId", "p1", "userpermissions", "[]")), resource.approveProjectUserAccessRequest(requestFor(user), form("projectId", "p1", "requests", "[]")), resource.denyProjectUserAccessRequest(requestFor(user), @@ -409,17 +410,17 @@ void refusesToPropagateTheAccessOfAProjectTheUserCannotEdit() throws Exception { try (Statics statics = new Statics()) { statics.project.when(() -> SecurityProjectUtils.userCanEditProject(user, "p1")).thenReturn(false); - Response one = resource.propagateProjectDependencyPermission(requestFor(user), - form("id", "bob", "type", "NATIVE", "projectId", "p1", "permission", "OWNER")); - Response many = resource.propagateProjectDependencyPermissions(requestFor(user), form("projectId", "p1", + Response one = resource.propagateCatalogDependencyPermission(requestFor(user), + form("id", "bob", "type", "NATIVE", "catalogId", "p1", "permission", "OWNER")); + Response many = resource.propagateCatalogDependencyPermissions(requestFor(user), form("catalogId", "p1", "userpermissions", "[{\"userid\":\"bob\",\"type\":\"NATIVE\",\"permission\":\"OWNER\"}]")); for (Response response : List.of(one, many)) { assertEquals(403, response.getStatus()); assertEquals("Project p1 does not exist or user does not have access to edit it", errorOf(response)); } - statics.project.verify( - () -> SecurityProjectUtils.propagateProjectPermission(any(), any(), any(), any(), any(), any()), + statics.catalog.verify( + () -> SecurityCatalogDependencyUtils.propagatePermission(any(), any(), any(), any(), any(), any()), never()); } } @@ -436,10 +437,10 @@ void refusesAnUnknownLevelWithoutSavingIt(String level) throws Exception { form("id", "bob", "projectId", "p1", "permission", level)), resource.editProjectUserPermission(requestFor(user), form("id", "bob", "type", "NATIVE", "projectId", "p1", "permission", level)), - resource.propagateProjectDependencyPermission(requestFor(user), - form("id", "bob", "type", "NATIVE", "projectId", "p1", "permission", level)), - resource.propagateProjectDependencyPermissions(requestFor(user), - form("projectId", "p1", "userpermissions", rows)), + resource.propagateCatalogDependencyPermission(requestFor(user), + form("id", "bob", "type", "NATIVE", "catalogId", "p1", "permission", level)), + resource.propagateCatalogDependencyPermissions(requestFor(user), + form("catalogId", "p1", "userpermissions", rows)), resource.addProjectUserPermissions(requestFor(user), form("projectId", "p1", "userpermissions", rows)), resource.editProjectUserPermissions(requestFor(user), @@ -452,8 +453,8 @@ void refusesAnUnknownLevelWithoutSavingIt(String level) throws Exception { assertEquals("The permission must be OWNER, EDIT or READ_ONLY. Received value = " + level, errorOf(response)); } - statics.project.verify( - () -> SecurityProjectUtils.propagateProjectPermission(any(), any(), any(), any(), any(), any()), + statics.catalog.verify( + () -> SecurityCatalogDependencyUtils.propagatePermission(any(), any(), any(), any(), any(), any()), never()); statics.project.verify(() -> SecurityProjectUtils.addProjectUser(any(), any(), any(), any(), any()), never()); @@ -483,11 +484,11 @@ void propagatesAccessToTheProjectsDependenciesWithTheLoginType() throws Exceptio User user = signedIn("alice"); Map outcome = Map.of("accessGranted", List.of("m1")); try (Statics statics = new Statics()) { - statics.project.when(() -> SecurityProjectUtils.propagateProjectPermission(user, "p1", "bob", "NATIVE", + statics.catalog.when(() -> SecurityCatalogDependencyUtils.propagatePermission(user, "p1", "bob", "NATIVE", "READ_ONLY", "2027-01-01")).thenReturn(outcome); - Response response = resource.propagateProjectDependencyPermission(requestFor(user), form("id", "bob", - "type", "NATIVE", "projectId", "p1", "permission", "READ_ONLY", "endDate", "2027-01-01")); + Response response = resource.propagateCatalogDependencyPermission(requestFor(user), form("id", "bob", + "type", "NATIVE", "catalogId", "p1", "permission", "READ_ONLY", "endDate", "2027-01-01")); assertEquals(200, response.getStatus()); assertEquals(Map.of("accessGranted", List.of("m1")), body(response)); @@ -502,11 +503,11 @@ void propagatesTheUsageLimitItIsSent() throws Exception { try (Statics statics = new Statics()) { statics.limits.when(() -> SecurityModelUsageLimitUtils.readJson("{}")).thenReturn(limit); - Response response = resource.propagateProjectDependencyPermission(requestFor(user), form("id", "bob", - "type", "NATIVE", "projectId", "p1", "permission", "READ_ONLY", "usageLimit", "{}")); + Response response = resource.propagateCatalogDependencyPermission(requestFor(user), form("id", "bob", + "type", "NATIVE", "catalogId", "p1", "permission", "READ_ONLY", "usageLimit", "{}")); assertEquals(200, response.getStatus()); - statics.project.verify(() -> SecurityProjectUtils.propagateProjectPermission(user, "p1", "bob", "NATIVE", + statics.catalog.verify(() -> SecurityCatalogDependencyUtils.propagatePermission(user, "p1", "bob", "NATIVE", "READ_ONLY", null, limit)); } } @@ -519,13 +520,13 @@ void propagatesEveryRowWithItsOwnLimitAndKeepsLimitsForRowsWithout() throws Exce + "{\"userid\":\"carol\",\"type\":\"LDAP\",\"permission\":\"READ_ONLY\"}]"; try (Statics statics = new Statics()) { statics.limits.when(() -> SecurityModelUsageLimitUtils.readJson("{\"unlimited\":true}")).thenReturn(limit); - statics.project.when(() -> SecurityProjectUtils.propagateProjectPermission(user, "p1", "bob", "NATIVE", + statics.catalog.when(() -> SecurityCatalogDependencyUtils.propagatePermission(user, "p1", "bob", "NATIVE", "EDIT", null, limit)).thenReturn(Map.of("accessGranted", List.of("m1"))); - statics.project.when(() -> SecurityProjectUtils.propagateProjectPermission(user, "p1", "carol", "LDAP", + statics.catalog.when(() -> SecurityCatalogDependencyUtils.propagatePermission(user, "p1", "carol", "LDAP", "READ_ONLY", null)).thenReturn(Map.of("newRequestAdded", List.of("m1"))); - Response response = resource.propagateProjectDependencyPermissions(requestFor(user), - form("projectId", "p1", "userpermissions", rows)); + Response response = resource.propagateCatalogDependencyPermissions(requestFor(user), + form("catalogId", "p1", "userpermissions", rows)); assertEquals(200, response.getStatus()); Map payload = (Map) body(response); @@ -543,10 +544,10 @@ void readsARowLimitSentAsJsonText() throws Exception { try (Statics statics = new Statics()) { statics.limits.when(() -> SecurityModelUsageLimitUtils.readJson("{}")).thenReturn(limit); - resource.propagateProjectDependencyPermissions(requestFor(user), - form("projectId", "p1", "userpermissions", rows)); + resource.propagateCatalogDependencyPermissions(requestFor(user), + form("catalogId", "p1", "userpermissions", rows)); - statics.project.verify(() -> SecurityProjectUtils.propagateProjectPermission(user, "p1", "bob", "NATIVE", + statics.catalog.verify(() -> SecurityCatalogDependencyUtils.propagatePermission(user, "p1", "bob", "NATIVE", "EDIT", null, limit)); } } @@ -558,11 +559,11 @@ void refusesAnUnreadableRowLimitWith400() throws Exception { try (Statics statics = new Statics()) { statics.limits.when(() -> SecurityModelUsageLimitUtils.readJson(anyString())).thenCallRealMethod(); - Response response = resource.propagateProjectDependencyPermissions(requestFor(user), - form("projectId", "p1", "userpermissions", rows)); + Response response = resource.propagateCatalogDependencyPermissions(requestFor(user), + form("catalogId", "p1", "userpermissions", rows)); assertEquals(400, response.getStatus()); - statics.project.verify(() -> SecurityProjectUtils.propagateProjectPermission(any(), any(), any(), any(), + statics.catalog.verify(() -> SecurityCatalogDependencyUtils.propagatePermission(any(), any(), any(), any(), any(), any(), any()), never()); } } @@ -822,6 +823,8 @@ private static final class Statics implements AutoCloseable { private final MockedStatic security = Mockito.mockStatic(AbstractSecurityUtils.class); private final MockedStatic admin = Mockito.mockStatic(SecurityAdminUtils.class); private final MockedStatic project = Mockito.mockStatic(SecurityProjectUtils.class); + private final MockedStatic catalog = Mockito + .mockStatic(SecurityCatalogDependencyUtils.class); private final MockedStatic limits = Mockito .mockStatic(SecurityModelUsageLimitUtils.class); private final MockedStatic directory = Mockito @@ -831,6 +834,8 @@ private static final class Statics implements AutoCloseable { private Statics() { // propagation needs edit access to the project; yes unless a test says no project.when(() -> SecurityProjectUtils.userCanEditProject(any(), anyString())).thenReturn(true); + // the dependencies propagated are a project's unless a test says otherwise + catalog.when(() -> SecurityCatalogDependencyUtils.resolveCatalogType(anyString(), any())).thenReturn("CODE"); } @Override @@ -838,6 +843,7 @@ public void close() { graph.close(); directory.close(); limits.close(); + catalog.close(); project.close(); admin.close(); security.close();