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
emit event on identity removal
Thiago Araujo da Silvad2e5975f8f1f
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.mdindex 2c404cb254..567ae2621a 100644--- a/backend/CLAUDE.md+++ b/backend/CLAUDE.md@@ -894,7 +894,8 @@ itself whether the actor still reaches the org.- `group`: `groupId`, `orgIds`, all `members`. From owned and SCIM group deletes, read with`groupDAL.findGroupActorsAndOrgs` before the cascade.- `membership`: `orgId` + actor. From `deleteOrgMembershipsFn` (users), the org branch of- `membershipIdentityService.deleteMembership` (identities) and the sweep.+ `membershipIdentityService.deleteMembership` and the invited-identity branch of `identityService.deleteIdentity`+ (identities), and the sweep.Group events snapshot orgs and members at removal time, since a later removal can wipe those rows beforethe consumer runs. The org list always comes from `groupDAL.findGroupOrgIds` (the owning org plus every@@ -908,11 +909,13 @@ itself whether the actor still reaches the org.live PAM sessions, across every project in the given orgs since those can exist in group-only projects.It runs in the caller's tx and hands back the PAM cancellation sender for after commit. Don't add anothercopy of that list.-- **Removing a user from an org tears down synchronously, an identity doesn't.** `deleteOrgMembershipsFn`+- **Removing a user from an org tears down synchronously; for an identity it depends on the path.** `deleteOrgMembershipsFn`(org member removal, bulk removal, SCIM deprovision) drops the user's org rows in the sub-orgs, theirgroups there and calls `revokeActorsInOrgs` for the org plus its sub-orgs in the same tx; its event only- lets the consumer confirm nothing's left. Identity removal just writes the revocation marker and emits, so- the consumer does all the project cleanup.+ lets the consumer confirm nothing's left. `membershipIdentityService.deleteMembership` just writes the+ revocation marker and emits, so the consumer does all the project cleanup there. `identityService.deleteIdentity`+ (the UI's "Remove From Sub-Organization") deletes the identity's rows in the org itself and emits anyway, like+ `deleteOrgMembershipsFn`.- **The consumer (`org-access-reconcile`) rechecks against current state.** Orgs and actors come from thepayload for group events, and from the org plus its sub-orgs for a membership removal. Per org it works inbatches of 10 actors, each its own tx with `SET LOCAL statement_timeout` (30s) that locks the actors,diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex 11f9a501d9..08fcc7c071 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -2814,7 +2814,8 @@ export const registerRoutes = async (membershipRoleDAL,usageMeteringService,alertService,- identityAccessTokenService+ identityAccessTokenService,+ eventEmitter: eventOutboxService});const identityV2Service = identityV2ServiceFactory({diff --git a/backend/src/services/identity/identity-service.test.ts b/backend/src/services/identity/identity-service.test.tsindex 1a23e0f5e1..5927483c88 100644--- a/backend/src/services/identity/identity-service.test.ts+++ b/backend/src/services/identity/identity-service.test.ts@@ -45,6 +45,7 @@ const createService = ({}: { identityOrgId?: string; hasDeleteProtection?: boolean } = {}) => {const deleteAlertsForResource = vi.fn().mockResolvedValue(0);const deleteAlertsForDeletedResource = vi.fn().mockResolvedValue(0);+ const emit = vi.fn().mockResolvedValue(undefined);const identityDAL = {transaction: vi.fn(async (cb: (tx: Knex) => Promise<unknown>) => cb(TX)),@@ -90,10 +91,11 @@ const createService = ({insertIdentityWideRevocationMarker: vi.fn().mockResolvedValue(undefined),insertOrgMembershipRevocationMarker: vi.fn().mockResolvedValue(undefined),bumpIdentityRevocationVersion: vi.fn().mockResolvedValue(undefined)- } as never+ } as never,+ eventEmitter: { emit }});- return { service, identityDAL, membershipIdentityDAL, deleteAlertsForResource, deleteAlertsForDeletedResource };+ return { service, identityDAL, membershipIdentityDAL, deleteAlertsForResource, deleteAlertsForDeletedResource, emit };};describe("deleteIdentity alert cleanup", () => {@@ -149,3 +151,30 @@ describe("deleteIdentity alert cleanup", () => {expect(deleteAlertsForResource).not.toHaveBeenCalled();});});++describe("deleteIdentity org access reconcile", () => {+ beforeEach(() => vi.clearAllMocks());++ test("removing an identity from an org it was invited into emits the removal in the same transaction", async () => {+ const { service, emit } = createService({ identityOrgId: ROOT_ORG_ID });++ await service.deleteIdentity(buildDto());++ expect(emit).toHaveBeenCalledTimes(1);+ expect(emit).toHaveBeenCalledWith(+ {+ eventType: "com.infisical.organization.membership.removed.v1",+ payload: { orgId: ORG_ID, actorType: "identity", actorId: IDENTITY_ID }+ },+ TX+ );+ });++ test("deleting the identity row emits nothing, every membership goes with it", async () => {+ const { service, emit } = createService();++ await service.deleteIdentity(buildDto());++ expect(emit).not.toHaveBeenCalled();+ });+});diff --git a/backend/src/services/identity/identity-service.ts b/backend/src/services/identity/identity-service.tsindex 6d4f354ff5..dd8ce5d047 100644--- a/backend/src/services/identity/identity-service.ts+++ b/backend/src/services/identity/identity-service.ts@@ -22,11 +22,13 @@ import { TUsageMeteringServiceFactory } from "@app/services/license-client/usageimport { TAdditionalPrivilegeDALFactory } from "../additional-privilege/additional-privilege-dal";import { ActorType } from "../auth/auth-type";+import { TEventEmitter } from "../event-outbox/event-outbox-types";import { TIdentityAccessTokenServiceFactory } from "../identity-access-token/identity-access-token-service";import { resolveMembershipRoleSlugs } from "../membership/membership-fns";import { TMembershipRoleDALFactory } from "../membership/membership-role-dal";import { TMembershipIdentityDALFactory } from "../membership-identity/membership-identity-dal";import { TOrgDALFactory } from "../org/org-dal";+import { emitOrgMembershipRemoved, toOrgMembershipActors } from "../org/org-membership-events";import { validateIdentityUpdateForSuperAdminPrivileges } from "../super-admin/super-admin-fns";import { TIdentityDALFactory } from "./identity-dal";import { getIdentityActiveLockoutAuthMethods } from "./identity-fns";@@ -60,6 +62,7 @@ type TIdentityServiceFactoryDep = {TIdentityAccessTokenServiceFactory,"insertIdentityWideRevocationMarker" | "insertOrgMembershipRevocationMarker" | "bumpIdentityRevocationVersion">;+ eventEmitter: TEventEmitter;};export type TIdentityServiceFactory = ReturnType<typeof identityServiceFactory>;@@ -78,7 +81,8 @@ export const identityServiceFactory = ({additionalPrivilegeDAL,usageMeteringService,alertService,- identityAccessTokenService+ identityAccessTokenService,+ eventEmitter}: TIdentityServiceFactoryDep) => {const createIdentity = async ({name,@@ -468,6 +472,11 @@ export const identityServiceFactory = ({const doc = await membershipIdentityDAL.delete({ actorIdentityId: id, scopeOrgId: actorOrgId }, tx);await identityAccessTokenService.insertOrgMembershipRevocationMarker({ identityId: id, orgId: actorOrgId, tx });+ await emitOrgMembershipRemoved(+ eventEmitter,+ { orgId: actorOrgId, actors: toOrgMembershipActors({ identityIds: [id] }) },+ tx+ );await alertService.deleteAlertsForResource({