Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -340,6 +341,9 @@ private static Map<String, Object> 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
Expand Down Expand Up @@ -405,6 +409,9 @@ private static Map<String, Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<String, String> 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
Expand All @@ -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<String, String> 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<Map<String, Object>> 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);

Expand All @@ -401,10 +401,10 @@ public Response propagateProjectDependencyPermissions(@Context HttpServletReques
String requestedPermission = textOf(userRequest.get("permission"));
Map<String, Object> 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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -400,6 +401,24 @@ void rewritesTheSmssAndReopensTheEngine() throws Exception {
}
}

@Test
void recordsTheSmssDependenciesBeforeTheClusterPushThatCanFail() throws Exception {
try (Statics s = new Statics();
MockedStatic<CatalogDependencyDerivation> 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()) {
Expand Down
Loading
Loading