Skip to content

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

add audit log for adoption

Thiago Araujo da Silvaa3a73274440f
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 15f8e40b69..85a26a9bbc 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -395,6 +395,14 @@ transaction on a constraint violation, so an unscoped retry would fail with `25P
caller's remaining alias and membership writes with it. SAML and LDAP have structurally identical
branches and are deliberately not wired up.
+Because adoption rewrites an existing account's `username` and `email`, it emits an
+`OIDC_PROVISIONED_PLACEHOLDER_ADOPTED` audit event carrying the before and after, not just an
+application log: if one of the refusal checks above ever regresses, the audit trail is what makes it
+findable. That event must never fire on the unique-violation recovery path, where the returned user
+is whoever won the race rather than a rewritten placeholder. `adoptProvisionedShadowUser` draws that
+line by returning `adoptedFromUsername: null` for the yield, and the caller keys the audit log on
+it.
+
### Permission System (CASL)
Uses CASL (`@casl/ability`) with MongoDB-style rules. Permission logic lives in `src/ee/services/permission/`:
diff --git a/backend/e2e-test/routes/v1/sso-alias-tenant-isolation.spec.ts b/backend/e2e-test/routes/v1/sso-alias-tenant-isolation.spec.ts
index c8c5b3c70b..42b2b50feb 100644
--- a/backend/e2e-test/routes/v1/sso-alias-tenant-isolation.spec.ts
+++ b/backend/e2e-test/routes/v1/sso-alias-tenant-isolation.spec.ts
@@ -261,7 +261,9 @@ describe("SSO alias resolution is tenant scoped", () => {
const adopted = await adoptAs(org.id, identifier, asserted);
- expect(adopted?.id).toBe(placeholder.id);
+ expect(adopted?.user.id).toBe(placeholder.id);
+ // Non-null is what tells the caller a real rewrite happened and an audit record is owed.
+ expect(adopted?.adoptedFromUsername).toBe(identifier);
await expect(testDb(TableName.Users).where({ id: placeholder.id }).first()).resolves.toMatchObject({
username: asserted,
email: asserted
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 48c40c47e2..1b240f72c1 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -587,6 +587,7 @@ export enum EventType {
PKI_SYNC_CLEAR_DEFAULT_CERTIFICATE = "pki-sync-clear-default-certificate",
OIDC_GROUP_MEMBERSHIP_MAPPING_ASSIGN_USER = "oidc-group-membership-mapping-assign-user",
OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER = "oidc-group-membership-mapping-remove-user",
+ OIDC_PROVISIONED_PLACEHOLDER_ADOPTED = "oidc-provisioned-placeholder-adopted",
CREATE_KMIP_CLIENT = "create-kmip-client",
UPDATE_KMIP_CLIENT = "update-kmip-client",
DELETE_KMIP_CLIENT = "delete-kmip-client",
@@ -4903,6 +4904,16 @@ interface OidcGroupMembershipMappingRemoveUserEvent {
};
}
+interface OidcProvisionedPlaceholderAdoptedEvent {
+ type: EventType.OIDC_PROVISIONED_PLACEHOLDER_ADOPTED;
+ metadata: {
+ userId: string;
+ externalId: string;
+ previousUsername: string;
+ newUsername: string;
+ };
+}
+
interface CreateKmipClientEvent {
type: EventType.CREATE_KMIP_CLIENT;
metadata: {
@@ -7547,6 +7558,7 @@ export type Event =
| RemovePkiSignerMemberEvent
| OidcGroupMembershipMappingAssignUserEvent
| OidcGroupMembershipMappingRemoveUserEvent
+ | OidcProvisionedPlaceholderAdoptedEvent
| CreateKmipClientEvent
| UpdateKmipClientEvent
| DeleteKmipClientEvent
diff --git a/backend/src/ee/services/oidc/oidc-config-service.ts b/backend/src/ee/services/oidc/oidc-config-service.ts
index 4a6f32e22f..304a8f06fa 100644
--- a/backend/src/ee/services/oidc/oidc-config-service.ts
+++ b/backend/src/ee/services/oidc/oidc-config-service.ts
@@ -299,6 +299,7 @@ export const oidcConfigServiceFactory = ({
});
} else {
let isNewUser = false;
+ let adoptedFromUsername: string | null = null;
const identityLimit = getEnforcedIdentityLimit(await licenseService.getPlan(orgId));
user = await userDAL.transaction(async (tx) => {
let newUser: TUsers | undefined;
@@ -314,23 +315,20 @@ export const oidcConfigServiceFactory = ({
// identifier instead of their mailbox. Adopt it rather than creating a second account and
// stranding whatever it was granted on one nobody can log into.
if (!newUser) {
- newUser =
- (await adoptProvisionedShadowUser({
- externalId,
- assertedEmail: sanitizedEmail,
- orgId,
- rootOrgId: organization.rootOrgId,
- userDAL,
- userAliasDAL,
- orgDAL,
- tx
- })) ?? undefined;
-
- if (newUser) {
- logger.info(
- { userId: newUser.id, orgId, externalId },
- `Adopted provisioned placeholder account on first OIDC login [userId=${newUser.id}] [orgId=${orgId}] [externalId=${externalId}] [username=${sanitizedEmail}]`
- );
+ const adoption = await adoptProvisionedShadowUser({
+ externalId,
+ assertedEmail: sanitizedEmail,
+ orgId,
+ rootOrgId: organization.rootOrgId,
+ userDAL,
+ userAliasDAL,
+ orgDAL,
+ tx
+ });
+
+ if (adoption) {
+ newUser = adoption.user;
+ adoptedFromUsername = adoption.adoptedFromUsername;
}
}
@@ -409,6 +407,30 @@ export const oidcConfigServiceFactory = ({
return newUser;
});
+ if (adoptedFromUsername) {
+ logger.info(
+ { userId: user.id, orgId, externalId },
+ "Adopted provisioned placeholder account on first OIDC login"
+ );
+
+ await auditLogService.createAuditLog({
+ actor: {
+ type: ActorType.PLATFORM,
+ metadata: {}
+ },
+ orgId,
+ event: {
+ type: EventType.OIDC_PROVISIONED_PLACEHOLDER_ADOPTED,
+ metadata: {
+ userId: user.id,
+ externalId,
+ previousUsername: adoptedFromUsername,
+ newUsername: sanitizedEmail
+ }
+ }
+ });
+ }
+
if (isNewUser) {
void telemetryService.sendPostHogEvents({
event: PostHogEventTypes.UserSignedUp,
diff --git a/backend/src/services/user-alias/user-alias-fns.test.ts b/backend/src/services/user-alias/user-alias-fns.test.ts
index d7ee96c637..30dc20299b 100644
--- a/backend/src/services/user-alias/user-alias-fns.test.ts
+++ b/backend/src/services/user-alias/user-alias-fns.test.ts
@@ -151,7 +151,8 @@ describe("adoptProvisionedShadowUser", () => {
const adopted = await adopt(deps);
- expect(adopted?.id).toBe("shadow-1");
+ expect(adopted?.user.id).toBe("shadow-1");
+ expect(adopted?.adoptedFromUsername).toBe(EXTERNAL_ID);
expect(deps.userDAL.updateById).toHaveBeenCalledWith(
"shadow-1",
{ username: ASSERTED_EMAIL, email: ASSERTED_EMAIL },
@@ -231,7 +232,7 @@ describe("adoptProvisionedShadowUser", () => {
orgs: [{ id: OTHER_ORG_ID, rootOrgId: ORG_ID }]
});
- expect((await adopt(deps))?.username).toBe(ASSERTED_EMAIL);
+ expect((await adopt(deps))?.user.username).toBe(ASSERTED_EMAIL);
});
test("yields to the winner when a concurrent write takes the asserted email first", async () => {
@@ -242,7 +243,11 @@ describe("adoptProvisionedShadowUser", () => {
);
deps.userDAL.findOne.mockResolvedValueOnce(shadowUser()).mockResolvedValueOnce(winner);
- expect(await adopt(deps)).toBe(winner);
+ const yielded = await adopt(deps);
+
+ expect(yielded?.user).toBe(winner);
+ // Nothing was rewritten, so the caller must not record this as an adoption.
+ expect(yielded?.adoptedFromUsername).toBeNull();
// The write was scoped to the savepoint, and the recovery lookup runs outside it on the
// caller's transaction. Reversed, the aborted transaction would fail the lookup with 25P02.
expect(deps.tx.transaction).toHaveBeenCalledTimes(1);
diff --git a/backend/src/services/user-alias/user-alias-fns.ts b/backend/src/services/user-alias/user-alias-fns.ts
index 66bf60906e..91c7395949 100644
--- a/backend/src/services/user-alias/user-alias-fns.ts
+++ b/backend/src/services/user-alias/user-alias-fns.ts
@@ -205,6 +205,11 @@ type TAdoptProvisionedShadowUserDTO = {
tx: Knex;
};
+type TAdoptProvisionedShadowUserResult = {
+ user: TUsers;
+ adoptedFromUsername: string | null;
+};
+
/**
* Provisioning can name someone by their IdP identifier before they have ever logged in, which
* leaves a placeholder account keyed on that identifier instead of their mailbox. On first login we
@@ -212,7 +217,8 @@ type TAdoptProvisionedShadowUserDTO = {
* account and stranding whatever the placeholder was granted.
*
* Returns null if there is nothing safe to adopt. Only call this once a lookup on the asserted
- * email has missed.
+ * email has missed. A non-null result carries `adoptedFromUsername`, which is null on the yield
+ * path: the user is real either way, but only a non-null value means an account was rewritten.
*/
export const adoptProvisionedShadowUser = async ({
externalId,
@@ -223,7 +229,7 @@ export const adoptProvisionedShadowUser = async ({
userAliasDAL,
orgDAL,
tx
-}: TAdoptProvisionedShadowUserDTO): Promise<TUsers | null> => {
+}: TAdoptProvisionedShadowUserDTO): Promise<TAdoptProvisionedShadowUserResult | null> => {
// We fold case here even though resolveUsersBySsoExternalId doesn't, because this is a username
// lookup and usernames are canonically lowercase (validateEmail rejects uppercase ones). The
// placeholder's username came from lowercasing the provisioner's input, so match that form.
@@ -270,16 +276,17 @@ export const adoptProvisionedShadowUser = async ({
// savepoint: without one, the recovery lookup below and every write the caller still owes after
// this call would fail with 25P02 instead.
try {
- return await tx.transaction((savepoint) =>
+ const adopted = await tx.transaction((savepoint) =>
userDAL.updateById(candidate.id, { username: assertedEmail, email: assertedEmail }, savepoint)
);
+ return { user: adopted, adoptedFromUsername: shadowUsername };
} catch (err) {
// The caller's read showed nobody held the asserted email, but a read isn't a lock, so a
// concurrent login or invite can grab it first (users.username is globally unique). Yield to
// whoever won: same end state as the uncontended path, placeholder left alone.
if (err instanceof DatabaseError && (err.error as { code?: string })?.code === DatabaseErrorCode.UniqueViolation) {
const winner = await userDAL.findOne({ username: assertedEmail }, tx);
- if (winner) return winner;
+ if (winner) return { user: winner, adoptedFromUsername: null };
}
throw err;
}
diff --git a/frontend/src/hooks/api/auditLogs/constants.tsx b/frontend/src/hooks/api/auditLogs/constants.tsx
index 324f612eda..53f5d790df 100644
--- a/frontend/src/hooks/api/auditLogs/constants.tsx
+++ b/frontend/src/hooks/api/auditLogs/constants.tsx
@@ -195,6 +195,8 @@ export const eventToNameMap: { [K in EventType]: string } = {
"OIDC group membership mapping assigned user to groups",
[EventType.OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER]:
"OIDC group membership mapping removed user from groups",
+ [EventType.OIDC_PROVISIONED_PLACEHOLDER_ADOPTED]:
+ "OIDC login adopted provisioned placeholder account",
[EventType.SECRET_APPROVAL_REQUEST_REVIEW]: "Review Secret Approval Request",
[EventType.CREATE_KMIP_CLIENT]: "Create KMIP client",
[EventType.UPDATE_KMIP_CLIENT]: "Update KMIP client",
diff --git a/frontend/src/hooks/api/auditLogs/enums.tsx b/frontend/src/hooks/api/auditLogs/enums.tsx
index 9677f129ef..d3cedeb6ee 100644
--- a/frontend/src/hooks/api/auditLogs/enums.tsx
+++ b/frontend/src/hooks/api/auditLogs/enums.tsx
@@ -203,6 +203,7 @@ export enum EventType {
PKI_SYNC_REMOVE_CERTIFICATES = "pki-sync-remove-certificates",
OIDC_GROUP_MEMBERSHIP_MAPPING_ASSIGN_USER = "oidc-group-membership-mapping-assign-user",
OIDC_GROUP_MEMBERSHIP_MAPPING_REMOVE_USER = "oidc-group-membership-mapping-remove-user",
+ OIDC_PROVISIONED_PLACEHOLDER_ADOPTED = "oidc-provisioned-placeholder-adopted",
CREATE_KMIP_CLIENT = "create-kmip-client",
UPDATE_KMIP_CLIENT = "update-kmip-client",
DELETE_KMIP_CLIENT = "delete-kmip-client",