Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix n+1
adilson4413f21a47ea
diff --git a/backend/src/ee/services/group/identity-group-membership-dal.ts b/backend/src/ee/services/group/identity-group-membership-dal.tsindex 69d4768de7..20ec1de02f 100644--- a/backend/src/ee/services/group/identity-group-membership-dal.ts+++ b/backend/src/ee/services/group/identity-group-membership-dal.ts@@ -11,37 +11,63 @@ export const identityGroupMembershipDALFactory = (db: TDbClient) => {const identityGroupMembershipOrm = ormify(db, TableName.IdentityGroupMembership);/**- * Returns a sub-set of projectIds fed into this function corresponding to projects where either:- * - The identity is a direct member of the project.- * - The identity is a member of a group that is a member of the project, excluding projects that they are part of- * through the group with id [groupId].+ * For the given identities, returns the subset of [projectIds] each still reaches via either:+ * - a direct project membership, or+ * - membership in a group other than [groupId] that is itself a member of the project.*/const filterProjectsByIdentityMembership = async (- identityId: string,+ identityIds: string[],groupId: string,projectIds: string[],tx?: Knex) => {+ const stillReach = new Map<string, Set<string>>();+ if (!identityIds.length || !projectIds.length) return stillReach;+try {- const identityProjectMemberships: string[] = await (tx || db.replicaNode())(TableName.Membership)- .where(`${TableName.Membership}.actorIdentityId`, identityId)- .where(`${TableName.Membership}.scope`, AccessScope.Project)- .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)- .pluck(`${TableName.Membership}.scopeProjectId`);-- const identityGroupMemberships: string[] = await (tx || db.replicaNode())(TableName.IdentityGroupMembership)- .where(`${TableName.IdentityGroupMembership}.identityId`, identityId)- .whereNot(`${TableName.IdentityGroupMembership}.groupId`, groupId)- .join(- TableName.Membership,- `${TableName.IdentityGroupMembership}.groupId`,- `${TableName.Membership}.actorGroupId`- )- .where(`${TableName.Membership}.scope`, AccessScope.Project)- .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)- .pluck(`${TableName.Membership}.scopeProjectId`);-- return new Set(identityProjectMemberships.concat(identityGroupMemberships));+ const knex = tx || db.replicaNode();++ const rows = (await knex.queryBuilder().union([+ (qb) => {+ void qb+ .from(TableName.Membership)+ .where(`${TableName.Membership}.scope`, AccessScope.Project)+ .whereIn(`${TableName.Membership}.actorIdentityId`, identityIds)+ .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)+ .select(+ db.ref("actorIdentityId").withSchema(TableName.Membership).as("identityId"),+ db.ref("scopeProjectId").withSchema(TableName.Membership).as("projectId")+ );+ },+ (qb) => {+ void qb+ .from(TableName.IdentityGroupMembership)+ .whereIn(`${TableName.IdentityGroupMembership}.identityId`, identityIds)+ .whereNot(`${TableName.IdentityGroupMembership}.groupId`, groupId)+ .join(+ TableName.Membership,+ `${TableName.IdentityGroupMembership}.groupId`,+ `${TableName.Membership}.actorGroupId`+ )+ .where(`${TableName.Membership}.scope`, AccessScope.Project)+ .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)+ .select(+ db.ref("identityId").withSchema(TableName.IdentityGroupMembership),+ db.ref("scopeProjectId").withSchema(TableName.Membership).as("projectId")+ );+ }+ ])) as { identityId: string; projectId: string }[];++ for (const { identityId, projectId } of rows) {+ let projects = stillReach.get(identityId);+ if (!projects) {+ projects = new Set();+ stillReach.set(identityId, projects);+ }+ projects.add(projectId);+ }++ return stillReach;} catch (error) {throw new DatabaseError({ error, name: "Filter projects by identity membership" });}diff --git a/backend/src/ee/services/group/user-group-membership-dal.ts b/backend/src/ee/services/group/user-group-membership-dal.tsindex 368f15ab1a..aa2e29b57b 100644--- a/backend/src/ee/services/group/user-group-membership-dal.ts+++ b/backend/src/ee/services/group/user-group-membership-dal.ts@@ -11,28 +11,63 @@ export const userGroupMembershipDALFactory = (db: TDbClient) => {const userGroupMembershipOrm = ormify(db, TableName.UserGroupMembership);/**- * Returns a sub-set of projectIds fed into this function corresponding to projects where either:- * - The user is a direct member of the project.- * - The user is a member of a group that is a member of the project, excluding projects that they are part of- * through the group with id [groupId].+ * For the given users, returns the subset of [projectIds] each still reaches via either:+ * - a direct project membership, or+ * - membership in a group other than [groupId] that is itself a member of the project.*/- const filterProjectsByUserMembership = async (userId: string, groupId: string, projectIds: string[], tx?: Knex) => {+ const filterProjectsByUserMembership = async (+ userIds: string[],+ groupId: string,+ projectIds: string[],+ tx?: Knex+ ) => {+ const stillReach = new Map<string, Set<string>>();+ if (!userIds.length || !projectIds.length) return stillReach;+try {- const userProjectMemberships: string[] = await (tx || db.replicaNode())(TableName.Membership)- .where(`${TableName.Membership}.actorUserId`, userId)- .where(`${TableName.Membership}.scope`, AccessScope.Project)- .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)- .pluck(`${TableName.Membership}.scopeProjectId`);+ const knex = tx || db.replicaNode();- const userGroupMemberships: string[] = await (tx || db.replicaNode())(TableName.UserGroupMembership)- .where(`${TableName.UserGroupMembership}.userId`, userId)- .whereNot(`${TableName.UserGroupMembership}.groupId`, groupId)- .join(TableName.Membership, `${TableName.UserGroupMembership}.groupId`, `${TableName.Membership}.actorGroupId`)- .where(`${TableName.Membership}.scope`, AccessScope.Project)- .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)- .pluck(`${TableName.Membership}.scopeProjectId`);+ const rows = (await knex.queryBuilder().union([+ (qb) => {+ void qb+ .from(TableName.Membership)+ .where(`${TableName.Membership}.scope`, AccessScope.Project)+ .whereIn(`${TableName.Membership}.actorUserId`, userIds)+ .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)+ .select(+ db.ref("actorUserId").withSchema(TableName.Membership).as("userId"),+ db.ref("scopeProjectId").withSchema(TableName.Membership).as("projectId")+ );+ },+ (qb) => {+ void qb+ .from(TableName.UserGroupMembership)+ .whereIn(`${TableName.UserGroupMembership}.userId`, userIds)+ .whereNot(`${TableName.UserGroupMembership}.groupId`, groupId)+ .join(+ TableName.Membership,+ `${TableName.UserGroupMembership}.groupId`,+ `${TableName.Membership}.actorGroupId`+ )+ .where(`${TableName.Membership}.scope`, AccessScope.Project)+ .whereIn(`${TableName.Membership}.scopeProjectId`, projectIds)+ .select(+ db.ref("userId").withSchema(TableName.UserGroupMembership),+ db.ref("scopeProjectId").withSchema(TableName.Membership).as("projectId")+ );+ }+ ])) as { userId: string; projectId: string }[];++ for (const { userId, projectId } of rows) {+ let projects = stillReach.get(userId);+ if (!projects) {+ projects = new Set();+ stillReach.set(userId, projects);+ }+ projects.add(projectId);+ }- return new Set(userProjectMemberships.concat(userGroupMemberships));+ return stillReach;} catch (error) {throw new DatabaseError({ error, name: "Filter projects by user membership" });}diff --git a/backend/src/services/membership-group/membership-group-service.ts b/backend/src/services/membership-group/membership-group-service.tsindex b40988a693..0eb7e5b9be 100644--- a/backend/src/services/membership-group/membership-group-service.ts+++ b/backend/src/services/membership-group/membership-group-service.ts@@ -431,32 +431,37 @@ export const membershipGroupServiceFactory = ({);const projectId = existingMembership.scopeProjectId;- const projectIds = [projectId];const { groupId } = dto.selector;const identityMembers = await identityGroupMembershipDAL.find({ groupId }, { tx });- for await (const { identityId } of identityMembers) {- const projectsIdentityStillMemberOf = await identityGroupMembershipDAL.filterProjectsByIdentityMembership(- identityId,+ if (identityMembers.length) {+ const identityIds = identityMembers.map(({ identityId }) => identityId);+ const identitiesStillInProject = await identityGroupMembershipDAL.filterProjectsByIdentityMembership(+ identityIds,groupId,- projectIds,+ [projectId],tx);- if (!projectsIdentityStillMemberOf.has(projectId)) {- await additionalPrivilegeDAL.delete({ actorIdentityId: identityId, projectId }, tx);+ const identityIdsToDelete = identityIds.filter(+ (identityId) => !identitiesStillInProject.get(identityId)?.has(projectId)+ );+ if (identityIdsToDelete.length) {+ await additionalPrivilegeDAL.delete({ projectId, $in: { actorIdentityId: identityIdsToDelete } }, tx);}}const userMembers = await userGroupMembershipDAL.find({ groupId }, { tx });- for await (const { userId } of userMembers) {- const projectsUserStillMemberOf = await userGroupMembershipDAL.filterProjectsByUserMembership(- userId,+ if (userMembers.length) {+ const userIds = userMembers.map(({ userId }) => userId);+ const usersStillInProject = await userGroupMembershipDAL.filterProjectsByUserMembership(+ userIds,groupId,- projectIds,+ [projectId],tx);- if (!projectsUserStillMemberOf.has(projectId)) {- await additionalPrivilegeDAL.delete({ actorUserId: userId, projectId }, tx);+ const userIdsToDelete = userIds.filter((userId) => !usersStillInProject.get(userId)?.has(projectId));+ if (userIdsToDelete.length) {+ await additionalPrivilegeDAL.delete({ projectId, $in: { actorUserId: userIdsToDelete } }, tx);}}}