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
feat(external-group-org-role-mapping): implement SCIM group-org role mapping tests and update DAL
Victor Hugo dos Santos7d5bffc7a0bb
diff --git a/backend/e2e-test/routes/v1/external-group-org-role-mapping.spec.ts b/backend/e2e-test/routes/v1/external-group-org-role-mapping.spec.tsnew file mode 100644index 0000000000..33f3eea988--- /dev/null+++ b/backend/e2e-test/routes/v1/external-group-org-role-mapping.spec.ts@@ -0,0 +1,234 @@+import { randomUUID } from "node:crypto";++import jwt from "jsonwebtoken";+import { MockInstance, vi } from "vitest";++import { AccessScope, OrgMembershipRole, OrgMembershipStatus, TableName } from "@app/db/schemas";+import { seedData1 } from "@app/db/seed-data";+import { EventType } from "@app/ee/services/audit-log/audit-log-types";+import { PgSqlLock } from "@app/keystore/keystore";+import { getConfig, initEnvConfig } from "@app/lib/config/env";+import { initLogger, logger } from "@app/lib/logger";+import { alphaNumericNanoId } from "@app/lib/nanoid";+import { AuthMethod, AuthTokenType } from "@app/services/auth/auth-type";++// SCIM applies a stored mapping to newly provisioned members with no user in the loop, so saving a+// mapping is the grant. These cases pin the route-level contract the service tests cannot see: the+// audit log records only what the service accepted, and a save cannot land on a mapping set other+// than the one it was authorized against.++const orgId = seedData1.organization.id;+const URL = "/api/v1/scim/group-org-role-mappings";++const adminHeaders = () => ({ authorization: `Bearer ${jwtAuthToken}` });++const createOrgUser = async (label: string, role: { role: string; customRoleId?: string }) => {+ const username = `scim-mapping-${label}-${alphaNumericNanoId(8)}@example.com`;+ const [user] = await testDb(TableName.Users)+ .insert({ username, email: username, isGhost: false, isAccepted: true, authMethods: [AuthMethod.EMAIL] })+ .returning("*");++ const [membership] = await testDb(TableName.Membership)+ .insert({+ scope: AccessScope.Organization,+ scopeOrgId: orgId,+ actorUserId: user.id,+ status: OrgMembershipStatus.Accepted,+ isActive: true+ })+ .returning("*");+ await testDb(TableName.MembershipRole).insert({ membershipId: membership.id, ...role });++ const sessionId = randomUUID();+ await testDb(TableName.AuthTokenSession).insert({+ id: sessionId,+ userId: user.id,+ ip: "127.0.0.1",+ userAgent: "e2e-scim-mapping",+ accessVersion: 1,+ refreshVersion: 1,+ lastUsed: new Date()+ } as never);++ const token = jwt.sign(+ {+ authTokenType: AuthTokenType.ACCESS_TOKEN,+ userId: user.id,+ tokenVersionId: sessionId,+ authMethod: AuthMethod.EMAIL,+ organizationId: orgId,+ accessVersion: 1+ },+ getConfig().AUTH_SECRET,+ { expiresIn: 3600 }+ );++ return { userId: user.id, sessionId, headers: { authorization: `Bearer ${token}` } };+};++const storedMappings = async () =>+ (+ await testDb(TableName.ExternalGroupOrgRoleMapping)+ .where({ orgId })+ .select("groupName", "role")+ .orderBy("groupName")+ ).map(({ groupName, role }: { groupName: string; role: string }) => `${groupName}=${role}`);++const putMappings = (headers: Record<string, string>, mappings: [string, string][]) =>+ testServer.inject({+ method: "PUT",+ url: URL,+ headers,+ body: { mappings: mappings.map(([groupName, roleSlug]) => ({ groupName, roleSlug })) }+ });++type TAuditSpy = MockInstance<(typeof testServer.services.auditLog)["createAuditLog"]>;++const auditEventsOfType = (spy: TAuditSpy, type: EventType) =>+ spy.mock.calls.filter(([arg]) => (arg as { event?: { type?: string } })?.event?.type === type);++const waitForLockWaiter = async (lockKey: number, deadline = Date.now() + 10_000): Promise<void> => {+ const { rows } = await testDb.raw(+ "SELECT count(*)::int AS waiting FROM pg_locks WHERE locktype = 'advisory' AND NOT granted AND objid = ?",+ [lockKey]+ );+ if (rows[0].waiting > 0) return;+ if (Date.now() > deadline) throw new Error("the save never reached the write lock");+ await new Promise((resolve) => {+ setTimeout(resolve, 50);+ });+ return waitForLockWaiter(lockKey, deadline);+};++describe("SCIM group to org role mappings", () => {+ let editor: Awaited<ReturnType<typeof createOrgUser>>;+ let member: Awaited<ReturnType<typeof createOrgUser>>;+ let editorRoleId: string;+ let auditSpy: TAuditSpy;++ beforeAll(async () => {+ initLogger();+ await initEnvConfig(testHsmService, testKmsRootConfigDAL, testSuperAdminDAL, logger);++ // `scim` edit alone: enough for the route's own check, not enough to hand out org roles.+ const [role] = await testDb(TableName.Role)+ .insert({+ name: "e2e-scim-mapping-editor",+ slug: `e2e-scim-mapping-editor-${alphaNumericNanoId(8)}`,+ orgId,+ permissions: JSON.stringify([{ subject: "scim", action: ["read", "edit"] }])+ })+ .returning("*");+ editorRoleId = role.id;++ editor = await createOrgUser("editor", { role: OrgMembershipRole.Custom, customRoleId: editorRoleId });+ member = await createOrgUser("member", { role: OrgMembershipRole.Member });+ });++ beforeEach(async () => {+ await testDb(TableName.ExternalGroupOrgRoleMapping).where({ orgId }).del();+ auditSpy = vi.spyOn(testServer.services.auditLog, "createAuditLog");+ });++ afterEach(() => {+ auditSpy.mockRestore();+ });++ afterAll(async () => {+ await testDb(TableName.ExternalGroupOrgRoleMapping).where({ orgId }).del();+ const actors = [editor, member].filter(Boolean);+ await testDb(TableName.AuthTokenSession)+ .whereIn(+ "id",+ actors.map((actor) => actor.sessionId)+ )+ .del();+ await testDb(TableName.Membership)+ .whereIn(+ "actorUserId",+ actors.map((actor) => actor.userId)+ )+ .del();+ await testDb(TableName.Users)+ .whereIn(+ "id",+ actors.map((actor) => actor.userId)+ )+ .del();+ await testDb(TableName.Role).where({ id: editorRoleId }).del();+ });++ test("a mapping the editor cannot grant is refused and not audited as an update", async () => {+ const res = await putMappings(editor.headers, [["g-escalate", OrgMembershipRole.Admin]]);++ expect(res.statusCode).toBe(403);+ expect(await storedMappings()).toEqual([]);+ expect(auditEventsOfType(auditSpy, EventType.UPDATE_EXTERNAL_GROUP_ORG_ROLE_MAPPINGS)).toHaveLength(0);+ });++ test("an accepted update is audited exactly once", async () => {+ const res = await putMappings(editor.headers, [["g-none", OrgMembershipRole.NoAccess]]);++ expect(res.statusCode).toBe(200);+ expect(await storedMappings()).toEqual([`g-none=${OrgMembershipRole.NoAccess}`]);+ expect(auditEventsOfType(auditSpy, EventType.UPDATE_EXTERNAL_GROUP_ORG_ROLE_MAPPINGS)).toHaveLength(1);+ });++ test("a forbidden read is refused and not audited", async () => {+ const res = await testServer.inject({ method: "GET", url: URL, headers: member.headers });++ expect(res.statusCode).toBe(403);+ expect(auditEventsOfType(auditSpy, EventType.GET_EXTERNAL_GROUP_ORG_ROLE_MAPPINGS)).toHaveLength(0);+ });++ test("a group named twice is a 400, not a unique-index 500", async () => {+ const res = await putMappings(adminHeaders(), [+ ["g-twice", OrgMembershipRole.NoAccess],+ ["g-twice", OrgMembershipRole.NoAccess]+ ]);++ expect(res.statusCode).toBe(400);+ expect(await storedMappings()).toEqual([]);+ });++ // The editor resubmits an admin mapping unchanged, which needs no grant. If an admin removes it+ // after the editor's checks ran but before the write, the write must not put it back.+ test("a save authorized against a stale snapshot is refused instead of restoring a removed mapping", async () => {+ const seed = await putMappings(adminHeaders(), [["g-admin", OrgMembershipRole.Admin]]);+ expect(seed.statusCode).toBe(200);++ const lockKey = PgSqlLock.ExternalGroupOrgRoleMappingUpdate(orgId);+ let releaseLock!: () => void;+ const lockReleased = new Promise<void>((resolve) => {+ releaseLock = resolve;+ });+ let lockTaken!: () => void;+ const lockHeld = new Promise<void>((resolve) => {+ lockTaken = resolve;+ });++ const concurrentAdminSave = testDb.transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [lockKey]);+ lockTaken();+ await lockReleased;+ await tx(TableName.ExternalGroupOrgRoleMapping).where({ orgId, groupName: "g-admin" }).del();+ });+ await lockHeld;++ const editorSave = putMappings(editor.headers, [+ ["g-admin", OrgMembershipRole.Admin],+ ["g-none", OrgMembershipRole.NoAccess]+ ]).then((res) => res);++ // wait until the editor's request has passed its checks and is queued on the write lock+ await waitForLockWaiter(lockKey);++ releaseLock();+ await concurrentAdminSave;+ const res = await editorSave;++ expect(res.statusCode).toBe(409);+ expect(await storedMappings()).toEqual([]);+ expect(auditEventsOfType(auditSpy, EventType.UPDATE_EXTERNAL_GROUP_ORG_ROLE_MAPPINGS)).toHaveLength(1);+ });+});diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.tsindex 8c4791b6db..1aa49cb42a 100644--- a/backend/src/keystore/keystore.ts+++ b/backend/src/keystore/keystore.ts@@ -40,7 +40,9 @@ export const PgSqlLock = {pgAdvisoryLockHashText(`last-admin-guard:${scope}:${scopeId}`),AuditReportRequest: (projectId: string) => pgAdvisoryLockHashText(`audit-report-request:${projectId}`),OrgAuditReportRequest: (orgId: string) => pgAdvisoryLockHashText(`audit-report-request:org:${orgId}`),- OrgAgentProxyConfigInit: (orgId: string) => pgAdvisoryLockHashText(`org-agent-proxy-config-init:${orgId}`)+ OrgAgentProxyConfigInit: (orgId: string) => pgAdvisoryLockHashText(`org-agent-proxy-config-init:${orgId}`),+ ExternalGroupOrgRoleMappingUpdate: (orgId: string) =>+ pgAdvisoryLockHashText(`external-group-org-role-mapping-update:${orgId}`)} as const;// all the key prefixes used must be set here to avoid conflictdiff --git a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-boundary.test.ts b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-boundary.test.tsindex 8f8f184281..9225e72056 100644--- a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-boundary.test.ts+++ b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-boundary.test.ts@@ -233,4 +233,29 @@ describe("updateExternalGroupOrgRoleMappings privilege boundary", () => {expect(persist).toHaveBeenCalledOnce();});++ // The write re-reads under a lock and refuses when the rows moved, so the snapshot it receives+ // must be the one the checks above ran against, not a second read.+ test("hands the write the exact snapshot the boundary was checked against", async () => {+ const currentMappings = [+ { groupName: "admins", role: OrgMembershipRole.Admin },+ { groupName: "highs", role: OrgMembershipRole.Custom, roleId: HIGH_ROLE.id }+ ];+ const { run, persist } = createService({+ actorPermission: scimEditor,+ shouldUseNewPrivilegeSystem: true,+ currentMappings+ });++ await run([+ { groupName: "admins", roleSlug: OrgMembershipRole.Admin },+ { groupName: "highs", roleSlug: HIGH_ROLE.slug }+ ]);++ expect(persist).toHaveBeenCalledWith(+ ORG_ID,+ expect.any(Array),+ currentMappings.map((mapping): unknown => expect.objectContaining(mapping))+ );+ });});diff --git a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-dal.ts b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-dal.tsindex 6f8f5973c2..02cf9042e1 100644--- a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-dal.ts+++ b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-dal.ts@@ -3,29 +3,49 @@ import { Tables } from "knex/types/tables";import { TDbClient } from "@app/db";import { TableName } from "@app/db/schemas";import { TExternalGroupOrgRoleMappings } from "@app/db/schemas/external-group-org-role-mappings";+import { PgSqlLock } from "@app/keystore/keystore";+import { ConflictError } from "@app/lib/errors";import { ormify } from "@app/lib/knex";+type TMappingRole = Pick<TExternalGroupOrgRoleMappings, "groupName" | "role" | "roleId">;++const toMappingSetKey = (mappings: readonly TMappingRole[]) =>+ mappings+ .map(({ groupName, role, roleId }) => JSON.stringify([groupName, role, roleId ?? null]))+ .sort()+ .join(",");+export type TExternalGroupOrgRoleMappingDALFactory = ReturnType<typeof externalGroupOrgRoleMappingDALFactory>;export const externalGroupOrgRoleMappingDALFactory = (db: TDbClient) => {const externalGroupOrgRoleMappingOrm = ormify(db, TableName.ExternalGroupOrgRoleMapping);+ // The caller authorizes the change against `expectedMappings`, so the write must land on exactly+ // that set: a concurrent save in between would otherwise be overwritten (or restored) unchecked.const updateExternalGroupOrgRoleMappingForOrg = async (orgId: string,- newMappings: readonly Tables[TableName.ExternalGroupOrgRoleMapping]["insert"][]+ newMappings: readonly Tables[TableName.ExternalGroupOrgRoleMapping]["insert"][],+ expectedMappings: readonly TMappingRole[]) => {- const currentMappings = await externalGroupOrgRoleMappingOrm.find({ orgId });+ const mappings = await externalGroupOrgRoleMappingOrm.transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.ExternalGroupOrgRoleMappingUpdate(orgId)]);- const newMap = new Map(newMappings.map((mapping) => [mapping.groupName, mapping]));- const currentMap = new Map(currentMappings.map((mapping) => [mapping.groupName, mapping]));+ const currentMappings = await externalGroupOrgRoleMappingOrm.find({ orgId }, { tx });+ if (toMappingSetKey(currentMappings) !== toMappingSetKey(expectedMappings))+ throw new ConflictError({+ message:+ "The group to organization role mappings were changed by another user while you were saving. Reload the mappings and try again."+ });- const mappingsToDelete = currentMappings.filter((mapping) => !newMap.has(mapping.groupName));- const mappingsToUpdate = currentMappings- .filter((mapping) => newMap.has(mapping.groupName))- .map((mapping) => ({ id: mapping.id, ...newMap.get(mapping.groupName) }));- const mappingsToInsert = newMappings.filter((mapping) => !currentMap.has(mapping.groupName));+ const newMap = new Map(newMappings.map((mapping) => [mapping.groupName, mapping]));+ const currentMap = new Map(currentMappings.map((mapping) => [mapping.groupName, mapping]));++ const mappingsToDelete = currentMappings.filter((mapping) => !newMap.has(mapping.groupName));+ const mappingsToUpdate = currentMappings+ .filter((mapping) => newMap.has(mapping.groupName))+ .map((mapping) => ({ id: mapping.id, ...newMap.get(mapping.groupName) }));+ const mappingsToInsert = newMappings.filter((mapping) => !currentMap.has(mapping.groupName));- const mappings = await externalGroupOrgRoleMappingOrm.transaction(async (tx) => {await externalGroupOrgRoleMappingOrm.delete({ $in: { id: mappingsToDelete.map((mapping) => mapping.id) } }, tx);const updatedMappings: TExternalGroupOrgRoleMappings[] = [];diff --git a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-service.ts b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-service.tsindex 2c87ae5a44..9d8a11cc50 100644--- a/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-service.ts+++ b/backend/src/services/external-group-org-role-mapping/external-group-org-role-mapping-service.ts@@ -159,7 +159,11 @@ export const externalGroupOrgRoleMappingServiceFactory = ({}}- const data = await externalGroupOrgRoleMappingDAL.updateExternalGroupOrgRoleMappingForOrg(actor.orgId, mappings);+ const data = await externalGroupOrgRoleMappingDAL.updateExternalGroupOrgRoleMappingForOrg(+ actor.orgId,+ mappings,+ currentMappings+ );return data;};