Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(pam): address review - ordered in-lock re-check, collapse admin loops, drop em-dash

bernie-g67fa354c850b
diff --git a/backend/src/ee/services/pam-project/pam-project-bootstrap.ts b/backend/src/ee/services/pam-project/pam-project-bootstrap.ts
index 9897ccd492..9d124c4485 100644
--- a/backend/src/ee/services/pam-project/pam-project-bootstrap.ts
+++ b/backend/src/ee/services/pam-project/pam-project-bootstrap.ts
@@ -209,60 +209,20 @@ export const bootstrapPamProject = async (
tx
);
- for (const userId of adminUserIds) {
- // eslint-disable-next-line no-await-in-loop
- const membership = await membershipDAL.create(
- {
- scope: AccessScope.Project,
- scopeOrgId: orgId,
- scopeProjectId: project.id,
- actorUserId: userId,
- isActive: true
- },
- tx
- );
-
- // eslint-disable-next-line no-await-in-loop
- await membershipRoleDAL.create(
- {
- membershipId: membership.id,
- role: ProjectMembershipRole.Admin
- },
- tx
- );
- }
-
- for (const identityId of adminIdentityIds) {
- // eslint-disable-next-line no-await-in-loop
- const membership = await membershipDAL.create(
- {
- scope: AccessScope.Project,
- scopeOrgId: orgId,
- scopeProjectId: project.id,
- actorIdentityId: identityId,
- isActive: true
- },
- tx
- );
-
- // eslint-disable-next-line no-await-in-loop
- await membershipRoleDAL.create(
- {
- membershipId: membership.id,
- role: ProjectMembershipRole.Admin
- },
- tx
- );
- }
+ const adminActors: Array<{ actorUserId: string } | { actorIdentityId: string } | { actorGroupId: string }> = [
+ ...adminUserIds.map((actorUserId) => ({ actorUserId })),
+ ...adminIdentityIds.map((actorIdentityId) => ({ actorIdentityId })),
+ ...adminGroupIds.map((actorGroupId) => ({ actorGroupId }))
+ ];
- for (const groupId of adminGroupIds) {
+ for (const actor of adminActors) {
// eslint-disable-next-line no-await-in-loop
const membership = await membershipDAL.create(
{
scope: AccessScope.Project,
scopeOrgId: orgId,
scopeProjectId: project.id,
- actorGroupId: groupId,
+ ...actor,
isActive: true
},
tx
diff --git a/backend/src/ee/services/pam-project/pam-project-resolver.ts b/backend/src/ee/services/pam-project/pam-project-resolver.ts
index ffa209a68e..1e52d29f0c 100644
--- a/backend/src/ee/services/pam-project/pam-project-resolver.ts
+++ b/backend/src/ee/services/pam-project/pam-project-resolver.ts
@@ -42,7 +42,7 @@ export const pamProjectResolverFactory = ({
// Orgs with no PAM data get no consolidated project during the migration; the first PAM request
// creates it here, seeded with the org's current admins so it is immediately usable (otherwise
- // no one could administer it — there is no org-admin fallback in the PAM permission model).
+ // no one could administer it, since the PAM permission model has no org-admin fallback).
const ensureDefaultProject = (orgId: string): Promise<string> =>
db.transaction(async (tx) => {
// Serialize concurrent first-use bootstraps for the same org. There is no DB unique
@@ -50,6 +50,17 @@ export const pamProjectResolverFactory = ({
// have several), and the advisory lock auto-releases when this transaction ends.
await tx.raw("SELECT pg_advisory_xact_lock(hashtext(?))", [`pam-bootstrap:${orgId}`]);
+ // Re-check inside the lock on the primary, with the same semantics as findDefaultProjectId
+ // (newest first, soft-deleted excluded). This resolves the race winner and, when several PAM
+ // projects exist (consolidated + old zombies), returns the consolidated one rather than an
+ // arbitrary row.
+ const existing = await tx(TableName.Project)
+ .where({ orgId, type: ProjectType.PAM })
+ .whereNull("deleteAfter")
+ .orderBy("createdAt", "desc")
+ .first("id");
+ if (existing) return existing.id;
+
const adminRows = (await tx(TableName.Membership)
.join(TableName.MembershipRole, `${TableName.MembershipRole}.membershipId`, `${TableName.Membership}.id`)
.where(`${TableName.Membership}.scope`, AccessScope.Organization)
@@ -65,8 +76,6 @@ export const pamProjectResolverFactory = ({
const uniq = (values: (string | null)[]) => [...new Set(values.filter((v): v is string => Boolean(v)))];
- // bootstrapPamProject re-checks for an existing project inside this locked transaction, so a
- // request that lost the race returns the winner's project instead of creating a duplicate.
const { project } = await bootstrapPamProject(
{
orgId,
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index aa69f2bd13..b9b8838aed 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -791,7 +791,9 @@ export const projectServiceFactory = ({
});
}
- // PAM projects are managed (one per org) and cannot be deleted
+ // PAM projects are not deletable here: the consolidated project is managed (one active project
+ // per org, recreated on demand), and any leftover pre-migration PAM projects still hold FK
+ // references from migrated accounts, so cascade-deleting them would break/erase that data.
if (project.type === ProjectType.PAM) {
throw new BadRequestError({
message: "Privileged Access Manager projects cannot be deleted."