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
Merge branch 'main' into ENG-5826
Thiago Araujo da Silva9e6fbc7718de
diff --cc backend/e2e-test/routes/v1/org-access-reconcile.spec.tsindex 2ea6cbf3b0,0000000000..5dd31ee696mode 100644,000000..100644--- a/backend/e2e-test/routes/v1/org-access-reconcile.spec.ts+++ b/backend/e2e-test/routes/v1/org-access-reconcile.spec.ts@@@ -1,472 -1,0 +1,476 @@@+import { randomUUID } from "node:crypto";++import { eventOutbox } from "e2e-test/fakes/event-outbox-queue";++import {+ AccessScope,+ OrgMembershipRole,+ ProjectMembershipRole,+ RESOURCE_SCOPE,+ ResourceType,+ TableName+} from "@app/db/schemas";+import { EventOutboxStatus } from "@app/services/event-outbox/event-outbox-types";+import { ORG_ACCESS_RECONCILE_CONSUMER } from "@app/services/org/org-access-reconcile-consumer";+import { orgDALFactory } from "@app/services/org/org-dal";++import { createIsolatedOrgAndProject, createSubOrgAndProject } from "../../testUtils/fixtures";+import {+ createOrgGroup,+ deleteGroups,+ linkGroupToOrg,+ removeIdentityFromGroup,+ removeUserFromGroup,+ unlinkGroupFromOrg+} from "../../testUtils/groups";+import { addIdentityProjectMembership, addIdentityToProject, createIdentityActor } from "../../testUtils/identities";+import { pollUntil } from "../../testUtils/poll";+import {+ inviteUsersToProject,+ listAvailableProjectIdentities,+ listAvailableProjectUsers+} from "../../testUtils/project-members";+import { addUserMembership, createUser, deleteUsers } from "../../testUtils/users";++// A user reaches the sub-org only through a linked root group. Losing that path reaps their sub-org+// project rows and nothing else: a second path keeps them and the root org is never touched.+describe("Org access reconcile", () => {+ let rootOrgId: string;+ let rootProjectId: string;+ let rootToken: string;+ let cleanupRootOrg: () => Promise<void>;+ let subOrgId: string;+ let subOrgProjectId: string;+ let subOrgToken: string;+ const createdUserIds: string[] = [];+ const createdGroupIds: string[] = [];+ const createdIdentityIds: string[] = [];++ const projectRows = (projectId: string, actor: { actorUserId?: string; actorIdentityId?: string }) =>+ testDb(TableName.Membership).where({ scope: AccessScope.Project, scopeProjectId: projectId, ...actor });++ // Drain, then wait out anything the relay claimed first, so "rows survived" checks run after the consumer.+ const reconcile = async () => {+ await eventOutbox.drain(ORG_ACCESS_RECONCILE_CONSUMER);+ await pollUntil({+ describe: "the org access reconcile consumer to settle every pending event",+ read: () =>+ testDb(TableName.EventOutbox)+ .where({ consumer: ORG_ACCESS_RECONCILE_CONSUMER })+ .whereIn("status", [EventOutboxStatus.Pending, EventOutboxStatus.Processing, EventOutboxStatus.Retry])+ .count<{ count: string }[]>("id as count")+ .then((rows) => Number(rows[0].count)),+ done: (pending) => pending === 0+ });+ };++ const createLinkedGroup = async (label: string) => {- const { groupId } = await createOrgGroup({ orgId: rootOrgId, label });++ const groupId = await createOrgGroup({++ orgId: rootOrgId,++ name: `${label}-${randomUUID().slice(0, 8)}`,++ role: OrgMembershipRole.NoAccess++ });+ createdGroupIds.push(groupId);+ await linkGroupToOrg({ groupId, authToken: subOrgToken });+ return groupId;+ };++ const createRootUserWithProjects = async (label: string, groupIds: string[]) => {+ const { userId, username } = await createUser(label);+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });+ await addUserMembership({ userId, orgId: rootOrgId, projectId: rootProjectId, role: ProjectMembershipRole.Member });+ await addUserMembership({+ userId,+ orgId: subOrgId,+ projectId: subOrgProjectId,+ role: ProjectMembershipRole.Member+ });+ await testDb(TableName.UserGroupMembership).insert(groupIds.map((groupId) => ({ userId, groupId })));+ return { userId, username };+ };++ beforeAll(async () => {+ const root = await createIsolatedOrgAndProject("org-access-reconcile");+ rootOrgId = root.orgId;+ rootProjectId = root.projectId;+ rootToken = root.authToken;+ cleanupRootOrg = root.cleanup;++ const sub = await createSubOrgAndProject({ rootOrgId, namePrefix: "org-access-reconcile-sub" });+ subOrgId = sub.subOrgId;+ subOrgProjectId = sub.projectId;+ subOrgToken = sub.authToken;+ });++ afterAll(async () => {+ for (const identityId of createdIdentityIds) {+ // eslint-disable-next-line no-await-in-loop+ await testServer.inject({+ method: "DELETE",+ url: `/api/v1/identities/${identityId}`,+ headers: { authorization: `Bearer ${rootToken}` }+ });+ }+ await deleteGroups(createdGroupIds);+ await deleteUsers(createdUserIds);+ await cleanupRootOrg();+ });++ test("a user removed from the group that reached the sub-org loses their sub-org project rows and keeps the root ones", async () => {+ const groupId = await createLinkedGroup("reconcile-grp");+ const { userId, username } = await createRootUserWithProjects("reconcile-user", [groupId]);++ await removeUserFromGroup({ groupId, username, authToken: rootToken });+ await reconcile();++ await pollUntil({+ describe: "the user's sub-org project membership to be revoked",+ read: () => projectRows(subOrgProjectId, { actorUserId: userId }),+ done: (rows) => rows.length === 0+ });+ expect(await projectRows(rootProjectId, { actorUserId: userId })).toHaveLength(1);+ expect(+ await testDb(TableName.Membership).where({+ scope: AccessScope.Organization,+ scopeOrgId: rootOrgId,+ actorUserId: userId+ })+ ).toHaveLength(1);+ });++ test("a user who still reaches the sub-org through another linked group keeps their project rows", async () => {+ const leftGroupId = await createLinkedGroup("reconcile-left");+ const keptGroupId = await createLinkedGroup("reconcile-kept");+ const { userId, username } = await createRootUserWithProjects("reconcile-two-paths", [leftGroupId, keptGroupId]);++ await removeUserFromGroup({ groupId: leftGroupId, username, authToken: rootToken });+ await reconcile();++ expect(await projectRows(subOrgProjectId, { actorUserId: userId })).toHaveLength(1);+ expect(await projectRows(rootProjectId, { actorUserId: userId })).toHaveLength(1);+ });++ test("unlinking the group from the sub-org revokes the members' direct project rows there", async () => {+ const groupId = await createLinkedGroup("reconcile-unlink");+ const { userId } = await createRootUserWithProjects("reconcile-unlink-user", [groupId]);++ await unlinkGroupFromOrg({ groupId, authToken: subOrgToken });+ await reconcile();++ await pollUntil({+ describe: "the unlinked group's member to lose their sub-org project membership",+ read: () => projectRows(subOrgProjectId, { actorUserId: userId }),+ done: (rows) => rows.length === 0+ });+ expect(await projectRows(rootProjectId, { actorUserId: userId })).toHaveLength(1);+ });++ test("a machine identity removed from the group loses its sub-org project row", async () => {+ const groupId = await createLinkedGroup("reconcile-identity-grp");+ const { identityId } = await createIdentityActor({ orgId: rootOrgId, authToken: rootToken });+ createdIdentityIds.push(identityId);+ await testDb(TableName.IdentityGroupMembership).insert({ identityId, groupId });+ await addIdentityProjectMembership({ identityId, orgId: subOrgId, projectId: subOrgProjectId });+ await addIdentityProjectMembership({ identityId, orgId: rootOrgId, projectId: rootProjectId });++ await removeIdentityFromGroup({ groupId, identityId, authToken: rootToken });+ await reconcile();++ await pollUntil({+ describe: "the identity's sub-org project membership to be revoked",+ read: () => projectRows(subOrgProjectId, { actorIdentityId: identityId }),+ done: (rows) => rows.length === 0+ });+ expect(await projectRows(rootProjectId, { actorIdentityId: identityId })).toHaveLength(1);+ });++ // Indirect members can be added to a sub-org project without becoming direct sub-org members, and lose it+ // once the group path goes.+ test("a user who reaches the sub-org only through a group can be added to its project", async () => {+ const groupId = await createLinkedGroup("assign-grp");+ const { userId, username } = await createUser("assign-user");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.UserGroupMembership).insert({ userId, groupId });++ const available = await listAvailableProjectUsers({ projectId: subOrgProjectId, authToken: subOrgToken });+ expect(available.map((user) => user.id)).toContain(userId);++ await inviteUsersToProject({ projectId: subOrgProjectId, usernames: [username], authToken: subOrgToken }).expect(+ (res) => expect(res.statusCode).toBe(200)+ );++ expect(await projectRows(subOrgProjectId, { actorUserId: userId })).toHaveLength(1);+ expect(+ await testDb(TableName.Membership).where({+ scope: AccessScope.Organization,+ scopeOrgId: subOrgId,+ actorUserId: userId+ })+ ).toHaveLength(0);+ const afterAdd = await listAvailableProjectUsers({ projectId: subOrgProjectId, authToken: subOrgToken });+ expect(afterAdd.map((user) => user.id)).not.toContain(userId);++ await removeUserFromGroup({ groupId, username, authToken: rootToken });+ await reconcile();++ await pollUntil({+ describe: "the indirect member's project row to be revoked once the group path is gone",+ read: () => projectRows(subOrgProjectId, { actorUserId: userId }),+ done: (rows) => rows.length === 0+ });+ });++ test("a deactivated member is refused for a project by name, not as a non-member", async () => {+ const { userId, username } = await createUser("reconcile-deactivated");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: subOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.Membership)+ .where({ scope: AccessScope.Organization, scopeOrgId: subOrgId, actorUserId: userId })+ .update({ isActive: false });++ const available = await listAvailableProjectUsers({ projectId: subOrgProjectId, authToken: subOrgToken });+ expect(available.map((user) => user.id)).not.toContain(userId);++ await inviteUsersToProject({ projectId: subOrgProjectId, usernames: [username], authToken: subOrgToken }).expect(+ (res) => {+ expect(res.statusCode).toBe(400);+ expect(res.json().message).toBe(+ `'${username}' is deactivated in this organization. Reactivate them before adding them to this project.`+ );+ }+ );+ expect(await projectRows(subOrgProjectId, { actorUserId: userId })).toHaveLength(0);+ });++ test("a user with no path into the sub-org is not offered for its project", async () => {+ const { userId } = await createUser("assign-outsider");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });++ const available = await listAvailableProjectUsers({ projectId: subOrgProjectId, authToken: subOrgToken });+ expect(available.map((user) => user.id)).not.toContain(userId);+ });++ test("a machine identity that reaches the sub-org only through a group can be added to its project", async () => {+ const groupId = await createLinkedGroup("assign-identity-grp");+ const { identityId } = await createIdentityActor({ orgId: rootOrgId, authToken: rootToken });+ createdIdentityIds.push(identityId);+ await testDb(TableName.IdentityGroupMembership).insert({ identityId, groupId });++ const available = await listAvailableProjectIdentities({ projectId: subOrgProjectId, authToken: subOrgToken });+ expect(available.map((identity) => identity.id)).toContain(identityId);++ await addIdentityToProject({+ projectId: subOrgProjectId,+ identityId,+ role: ProjectMembershipRole.Member,+ authToken: subOrgToken+ });+ expect(await projectRows(subOrgProjectId, { actorIdentityId: identityId })).toHaveLength(1);+ });++ test("the available lists answer 404 for a project outside the caller's org", async () => {+ await listAvailableProjectUsers({ projectId: rootProjectId, authToken: subOrgToken }).expect((res) =>+ expect(res.statusCode).toBe(404)+ );+ await listAvailableProjectIdentities({ projectId: rootProjectId, authToken: subOrgToken }).expect((res) =>+ expect(res.statusCode).toBe(404)+ );+ });++ // PAM and Agent Vault have one implicit project per org and their own member routes.+ const asSubOrg = (method: "GET" | "POST", url: string, body?: Record<string, unknown>) =>+ testServer.inject({ method, url, headers: { authorization: `Bearer ${subOrgToken}` }, ...(body ? { body } : {}) });++ const implicitProjectId = async (product: "pam" | "agent-vault") => {+ const res = await asSubOrg("GET", `/api/v1/${product}/project`);+ expect(res.statusCode).toBe(200);+ return res.json().projectId as string;+ };++ test("PAM takes a group-only user and identity of the sub-org, and a root group linked into it", async () => {+ const groupId = await createLinkedGroup("pam-grp");+ const { userId, username } = await createUser("pam-user");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.UserGroupMembership).insert({ userId, groupId });+ const { identityId } = await createIdentityActor({ orgId: rootOrgId, authToken: rootToken });+ createdIdentityIds.push(identityId);+ await testDb(TableName.IdentityGroupMembership).insert({ identityId, groupId });+ const pamProjectId = await implicitProjectId("pam");++ const available = await listAvailableProjectUsers({ projectId: pamProjectId, authToken: subOrgToken });+ expect(available.map((user) => user.id)).toContain(userId);++ const userRes = await asSubOrg("POST", "/api/v1/pam/memberships/users", { userIds: [userId], role: "member" });+ expect(userRes.statusCode).toBe(200);+ const identityRes = await asSubOrg("POST", `/api/v1/pam/memberships/identities/${identityId}`, { role: "member" });+ expect(identityRes.statusCode).toBe(200);+ const groupRes = await asSubOrg("POST", `/api/v1/pam/memberships/groups/${groupId}`, { role: "member" });+ expect(groupRes.statusCode).toBe(200);++ expect(await projectRows(pamProjectId, { actorUserId: userId })).toHaveLength(1);+ expect(await projectRows(pamProjectId, { actorIdentityId: identityId })).toHaveLength(1);++ await removeUserFromGroup({ groupId, username, authToken: rootToken });+ await reconcile();+ await pollUntil({+ describe: "the group-only PAM member to lose their membership once the group path is gone",+ read: () => projectRows(pamProjectId, { actorUserId: userId }),+ done: (rows) => rows.length === 0+ });+ });++ // Resource reads match a group's folder rows without checking the group still reaches the org, so the+ // unlink itself has to drop them; the consumer only handles users and identities.+ test("unlinking a group from the sub-org drops its PAM membership and folder grant there", async () => {+ const groupId = await createLinkedGroup("pam-unlink-grp");+ const pamProjectId = await implicitProjectId("pam");+ const join = await asSubOrg("POST", `/api/v1/organization-admin/projects/${pamProjectId}/grant-admin-access`);+ expect(join.statusCode, join.payload).toBe(200);++ const memberRes = await asSubOrg("POST", `/api/v1/pam/memberships/groups/${groupId}`, { role: "member" });+ expect(memberRes.statusCode, memberRes.payload).toBe(200);+ const folderRes = await asSubOrg("POST", "/api/v1/pam/folders", { name: `pam-unlink-${randomUUID().slice(0, 8)}` });+ expect(folderRes.statusCode, folderRes.payload).toBe(200);+ const folderId = folderRes.json().folder.id as string;+ const grantRes = await asSubOrg("POST", `/api/v1/pam/folders/${folderId}/groups/${groupId}`, { role: "operator" });+ expect(grantRes.statusCode, grantRes.payload).toBe(200);++ const groupRows = () => testDb(TableName.Membership).where({ scopeOrgId: subOrgId, actorGroupId: groupId });+ expect(await groupRows()).toHaveLength(3);++ await unlinkGroupFromOrg({ groupId, authToken: subOrgToken });++ expect(await groupRows()).toHaveLength(0);+ const listRes = await asSubOrg("GET", `/api/v1/pam/folders/${folderId}/groups`);+ expect(listRes.statusCode, listRes.payload).toBe(200);+ expect((listRes.json().members as { groupId?: string }[]).map((m) => m.groupId)).not.toContain(groupId);+ });++ // A lazily created Agent Vault project starts empty, so an org admin has to join it first.+ test("Agent Vault offers and takes a user who reaches the sub-org only through a group", async () => {+ const groupId = await createLinkedGroup("av-grp");+ const { userId } = await createUser("av-user");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.UserGroupMembership).insert({ userId, groupId });+ const agentVaultProjectId = await implicitProjectId("agent-vault");+ const join = await asSubOrg(+ "POST",+ `/api/v1/organization-admin/projects/${agentVaultProjectId}/grant-admin-access`+ );+ expect(join.statusCode, join.payload).toBe(200);++ const availableRes = await asSubOrg("GET", "/api/v1/agent-vault/members/available?actorType=user&limit=100");+ expect(availableRes.statusCode, availableRes.payload).toBe(200);+ expect((availableRes.json().actors as { id: string }[]).map((actor) => actor.id)).toContain(userId);++ const addRes = await asSubOrg("POST", "/api/v1/agent-vault/members", { userIds: [userId], role: "member" });+ expect(addRes.statusCode, addRes.payload).toBe(200);+ expect(await projectRows(agentVaultProjectId, { actorUserId: userId })).toHaveLength(1);+ });++ test("removing a member from the instance admin panel tears down their access like org member removal", async () => {+ const groupId = await createLinkedGroup("reconcile-admin-panel");+ const { userId } = await createRootUserWithProjects("reconcile-admin-panel-user", [groupId]);+ await addUserMembership({ userId, orgId: subOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.AdditionalPrivilege).insert({+ name: "reconcile-admin-panel-priv",+ actorUserId: userId,+ projectId: subOrgProjectId,+ permissions: JSON.stringify([])+ });+ const orgMembership = (await testDb(TableName.Membership)+ .where({ scope: AccessScope.Organization, scopeOrgId: rootOrgId, actorUserId: userId })+ .first("id")) as { id: string };++ // The seed user is the instance admin.+ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/admin/organization-management/organizations/${rootOrgId}/memberships/${orgMembership.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);++ // Read from the DB because the API can't list a removed member's leftover rows: the teardown is synchronous,+ // so nothing may survive in either org, and the group no longer gives them a way back in.+ expect(await testDb(TableName.Membership).where({ actorUserId: userId })).toHaveLength(0);+ expect(await testDb(TableName.UserGroupMembership).where({ userId })).toHaveLength(0);+ expect(await testDb(TableName.AdditionalPrivilege).where({ actorUserId: userId })).toHaveLength(0);+ expect(+ await testDb(TableName.EventOutbox)+ .where({ consumer: ORG_ACCESS_RECONCILE_CONSUMER })+ .whereRaw(`payload->>'actorId' = ?`, [userId])+ .whereRaw(`payload->>'orgId' = ?`, [rootOrgId])+ ).toHaveLength(1);+ });++ // The sweep query, against real rows: a project or resource row with no path into the org is an orphan,+ // any path clears it. Resource-only rows (like a group-derived PAM admin's folder row) count too.+ test("the sweep query finds an actor with a project or resource row and no path into the org, and nothing else", async () => {+ const orgDAL = orgDALFactory(testDb);+ const groupId = await createLinkedGroup("reconcile-sweep-grp");++ const { userId: orphanUserId } = await createUser("reconcile-orphan");+ createdUserIds.push(orphanUserId);+ await addUserMembership({+ userId: orphanUserId,+ orgId: subOrgId,+ projectId: subOrgProjectId,+ role: ProjectMembershipRole.Member+ });++ const { userId: directUserId } = await createUser("reconcile-direct");+ createdUserIds.push(directUserId);+ await addUserMembership({ userId: directUserId, orgId: subOrgId, role: OrgMembershipRole.Member });+ await addUserMembership({+ userId: directUserId,+ orgId: subOrgId,+ projectId: subOrgProjectId,+ role: ProjectMembershipRole.Member+ });++ const { userId: viaGroupUserId } = await createRootUserWithProjects("reconcile-via-group", [groupId]);++ const resourceRow = (actor: { actorUserId?: string; actorIdentityId?: string }, resourceType: ResourceType) => ({+ scope: RESOURCE_SCOPE,+ scopeOrgId: subOrgId,+ scopeProjectId: subOrgProjectId,+ scopeResourceType: resourceType,+ scopeResourceId: randomUUID(),+ ...actor+ });+ const { userId: resourceOnlyUserId } = await createUser("reconcile-resource-only");+ createdUserIds.push(resourceOnlyUserId);+ const { identityId: resourceOnlyIdentityId } = await createIdentityActor({+ orgId: rootOrgId,+ authToken: rootToken+ });+ createdIdentityIds.push(resourceOnlyIdentityId);+ await testDb(TableName.Membership).insert([+ resourceRow({ actorUserId: resourceOnlyUserId }, ResourceType.PamFolder),+ resourceRow({ actorIdentityId: resourceOnlyIdentityId }, ResourceType.AgentVaultAccessBundle),+ resourceRow({ actorUserId: viaGroupUserId }, ResourceType.PamFolder)+ ]);++ const orphans = await orgDAL.findOrphanedProjectActors({ limit: 100_000 });+ const orphanPairs = orphans.map((row) => `${row.actorUserId ?? row.actorIdentityId}:${row.scopeOrgId}`);++ expect(orphanPairs).toContain(`${orphanUserId}:${subOrgId}`);+ expect(orphanPairs).toContain(`${resourceOnlyUserId}:${subOrgId}`);+ expect(orphanPairs).toContain(`${resourceOnlyIdentityId}:${subOrgId}`);+ expect(orphanPairs).not.toContain(`${directUserId}:${subOrgId}`);+ expect(orphanPairs).not.toContain(`${viaGroupUserId}:${subOrgId}`);+ expect(orphanPairs).not.toContain(`${viaGroupUserId}:${rootOrgId}`);++ const withAccess = await orgDAL.findActorsWithOrgAccess({+ orgId: subOrgId,+ userIds: [orphanUserId, directUserId, viaGroupUserId],+ identityIds: []+ });+ expect([...withAccess.userIds].sort()).toEqual([directUserId, viaGroupUserId].sort());+ });+});diff --cc backend/e2e-test/routes/v1/scim.spec.tsindex b8b5b5734e,519d283573..9cd7dcefe5--- a/backend/e2e-test/routes/v1/scim.spec.ts+++ b/backend/e2e-test/routes/v1/scim.spec.ts@@@ -4,7 -4,7 +4,7 @@@ import crypto from "node:crypto"import jwt from "jsonwebtoken";import { Knex } from "knex";- import { AccessScope, ProjectMembershipRole, SecretFolderRole, TableName } from "@app/db/schemas";-import { AccessScope, TableName } from "@app/db/schemas";++import { AccessScope, OrgMembershipRole, ProjectMembershipRole, SecretFolderRole, TableName } from "@app/db/schemas";import { seedData1 } from "@app/db/seed-data";import { AuthTokenType } from "@app/services/auth/auth-type";@@@ -1245,62 -1241,4 +1245,66 @@@ describe("SCIM v1 Router", () =>);});});++ describe("DELETE /Groups/:groupId - Group in another org", () => {+ test("should leave a group owned by another org, and its members' folder grants, untouched", async () => {+ const db = getDb();+ const otherOrg = await createIsolatedOrgAndProject("scim-foreign-group");- const { groupId } = await createOrgGroup({ orgId: otherOrg.orgId, label: "scim-foreign" });++ const groupId = await createOrgGroup({++ orgId: otherOrg.orgId,++ name: `scim-foreign-${crypto.randomUUID().slice(0, 8)}`,++ role: OrgMembershipRole.NoAccess++ });+ const { userId } = await createUser("scim-foreign-member");++ try {+ await db(TableName.UserGroupMembership).insert({ userId, groupId, isPending: false });+ const [groupProjectRow] = await db(TableName.Membership)+ .insert({+ scope: AccessScope.Project,+ scopeOrgId: otherOrg.orgId,+ scopeProjectId: otherOrg.projectId,+ actorGroupId: groupId+ })+ .returning("id");+ await db(TableName.MembershipRole).insert({+ membershipId: (groupProjectRow as { id: string }).id,+ role: ProjectMembershipRole.Viewer+ });+ const folder = await createFolder({+ workspaceId: otherOrg.projectId,+ environmentSlug: "dev",+ secretPath: "/",+ name: "scim-foreign",+ authToken: otherOrg.authToken+ });+ const [grant] = await db(TableName.AdditionalPrivilege)+ .insert({+ name: `scim-foreign-${crypto.randomUUID()}`,+ actorUserId: userId,+ projectId: otherOrg.projectId,+ folderId: folder.id,+ role: SecretFolderRole.Read+ })+ .returning("id");++ const deleteRes = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/scim/Groups/${groupId}`,+ headers: { authorization: `Bearer ${scimToken}` }+ });+ // SCIM answers a miss with 204 too, so the IdP stops retrying a stale id.+ expect(deleteRes.statusCode).toBe(204);++ expect(await db(TableName.Groups).where({ id: groupId })).toHaveLength(1);+ expect(await db(TableName.AdditionalPrivilege).where({ id: (grant as { id: string }).id })).toHaveLength(1);+ const events = await db(TableName.EventOutbox).whereRaw(`payload->>'groupId' = ?`, [groupId]);+ expect(events).toHaveLength(0);+ } finally {+ await deleteGroups([groupId]);+ await deleteUsers([userId]);+ await otherOrg.cleanup();+ }+ });+ });});diff --cc backend/e2e-test/testUtils/groups.tsindex d8c0b44c27,5063771281..3597616180--- a/backend/e2e-test/testUtils/groups.ts+++ b/backend/e2e-test/testUtils/groups.ts@@@ -1,66 -1,43 +1,91 @@@- import { randomUUID } from "node:crypto";-import { AccessScope, OrgMembershipRole, TableName } from "@app/db/schemas";- // Group creation sits behind a plan flag the license fake turns off, so the group and its org row are- // written directly.- export const createOrgGroup = async (dto: { orgId: string; label: string }) => {- const slug = `${dto.label}-${randomUUID().slice(0, 8)}`;- const [group] = await testDb(TableName.Groups).insert({ orgId: dto.orgId, name: slug, slug }).returning("*");+ // Inserts rows directly: the group API refuses users with no org membership, which is exactly who these specs test.+ // `ownerOrgId` puts the group row in another org while its membership stays in `orgId`, the shape a parent+ // org's group takes when it is linked into a sub-org.+ export const createOrgGroup = async (dto: {+ orgId: string;+ ownerOrgId?: string;+ name: string;+ role: OrgMembershipRole;+ temporaryRole?: { role: OrgMembershipRole; endsAt: Date };+ }) => {+ const [group] = await testDb(TableName.Groups)+ .insert({ orgId: dto.ownerOrgId ?? dto.orgId, name: dto.name, slug: dto.name })+ .returning("id");+ const groupId = (group as { id: string }).id;+const [membership] = await testDb(TableName.Membership)- .insert({ scope: AccessScope.Organization, scopeOrgId: dto.orgId, actorGroupId: group.id })- .returning("*");- await testDb(TableName.MembershipRole).insert({- membershipId: membership.id,- role: OrgMembershipRole.NoAccess- });- return { groupId: group.id as string };+ .insert({ scope: AccessScope.Organization, scopeOrgId: dto.orgId, actorGroupId: groupId, isActive: true })+ .returning("id");+ const membershipId = (membership as { id: string }).id;++ await testDb(TableName.MembershipRole).insert([+ { membershipId, role: dto.role },+ ...(dto.temporaryRole+ ? [+ {+ membershipId,+ role: dto.temporaryRole.role,+ isTemporary: true,+ temporaryAccessEndTime: dto.temporaryRole.endsAt+ }+ ]+ : [])+ ]);++ return groupId;+ };++ export const addUsersToGroup = async (groupId: string, userIds: string[]) => {+ if (userIds.length === 0) return;+ await testDb(TableName.UserGroupMembership).insert(userIds.map((userId) => ({ userId, groupId })));};++export const deleteGroups = async (groupIds: string[]) => {+ if (groupIds.length === 0) return;+ await testDb(TableName.UserGroupMembership).whereIn("groupId", groupIds).delete();+ await testDb(TableName.IdentityGroupMembership).whereIn("groupId", groupIds).delete();+ const membershipIds = await testDb(TableName.Membership).whereIn("actorGroupId", groupIds).pluck("id");+ await testDb(TableName.MembershipRole).whereIn("membershipId", membershipIds).delete();+ await testDb(TableName.Membership).whereIn("id", membershipIds).delete();+ await testDb(TableName.Groups).whereIn("id", groupIds).delete();+};++// `authToken` has to be scoped to the sub-org, the route reads the org from it.+export const linkGroupToOrg = async (dto: { groupId: string; authToken: string }) => {+ const res = await testServer.inject({+ method: "POST",+ url: `/api/v1/organizations/memberships/groups/${dto.groupId}`,+ headers: { authorization: `Bearer ${dto.authToken}` },+ body: { roles: [{ role: OrgMembershipRole.Member }] }+ });+ expect(res.statusCode).toBe(200);+};++export const unlinkGroupFromOrg = async (dto: { groupId: string; authToken: string }) => {+ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/organizations/memberships/groups/${dto.groupId}`,+ headers: { authorization: `Bearer ${dto.authToken}` }+ });+ expect(res.statusCode).toBe(200);+};++export const removeUserFromGroup = async (dto: { groupId: string; username: string; authToken: string }) => {+ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/groups/${dto.groupId}/users/${encodeURIComponent(dto.username)}`,+ headers: { authorization: `Bearer ${dto.authToken}` }+ });+ expect(res.statusCode).toBe(200);+};++export const removeIdentityFromGroup = async (dto: { groupId: string; identityId: string; authToken: string }) => {+ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/groups/${dto.groupId}/machine-identities/${dto.identityId}`,+ headers: { authorization: `Bearer ${dto.authToken}` }+ });+ expect(res.statusCode).toBe(200);+};