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
fix(agent-vault): serialize personal value cleanups within a project
Scott Wilson30c7d813d370
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.tsindex 6145feda0c..e434440c23 100644--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts@@ -2413,6 +2413,51 @@ describe("Agent Vault V1 Router", async () => {}});+ test("a revoke of an admin's last grant waits for a demotion's cleanup, then clears their values", async () => {+ const bundle = await createAccessBundle("mp-demote-revoke");+ const service = await createService(bundle.id, {+ name: "sentry",+ hostPattern: "sentry-race.example.com",+ credential: { type: "bearer", memberProvidedFields: ["credential-value"] }+ });++ const admin = await createMemberIdentity(`mp-demote-revoke-${Date.now()}`);+ try {+ expect(+ (await inject("PATCH", "/api/v1/agent-vault/members", { machineIdentityIds: [admin.id], role: "admin" }))+ .statusCode+ ).toBe(200);+ expect((await inject("POST", membersUrl(bundle.id), { machineIdentityIds: [admin.id] })).statusCode).toBe(200);+ expect((await setValues(admin.as, bundle.id, service.id, { "credential-value": "mine" })).statusCode).toBe(200);+ const membership = await testDb("memberships").where({ scope: "project", actorIdentityId: admin.id }).first();+ const projectId = membership.scopeProjectId as string;++ // A demotion, written and holding the cleanup lock: its own cleanup already ran and skipped, because the+ // grant was still there. The revoke's cleanup has to wait for it, then see the admin role gone.+ const trx = await testDb.transaction();+ let pending: ReturnType<typeof inject> | undefined;+ try {+ const { rows } = await trx.raw("select pg_backend_pid() as pid");+ await trx("membership_roles").where({ membershipId: membership.id }).delete();+ await trx("membership_roles").insert({ membershipId: membership.id, role: "member" });+ await trx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.ClearAgentVaultPersonalValues(projectId)]);++ pending = inject("DELETE", membersUrl(bundle.id), { machineIdentityIds: [admin.id] });+ await waitUntilBlockedBy(rows[0].pid);+ await trx.commit();++ expect((await pending).statusCode).toBe(200);+ } catch (err) {+ if (!trx.isCompleted()) await trx.rollback();+ await pending?.catch(() => undefined);+ throw err;+ }+ expect(await testDb("agent_vault_personal_values").where({ identityId: admin.id })).toHaveLength(0);+ } finally {+ await admin.cleanup();+ }+ });+test("an admin is asked for values only on the bundles granted to them", async () => {const ungranted = await createAccessBundle("mp-mine-ungranted");const service = await createService(ungranted.id, {diff --git a/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-fns.ts b/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-fns.tsindex f93be7223a..ee3af70774 100644--- a/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-fns.ts+++ b/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-fns.ts@@ -2,6 +2,7 @@ import { Knex } from "knex";import { TIdentityGroupMembershipDALFactory } from "@app/ee/services/group/identity-group-membership-dal";import { TUserGroupMembershipDALFactory } from "@app/ee/services/group/user-group-membership-dal";+import { PgSqlLock } from "@app/keystore/keystore";import { TAgentVaultPersonalValueDALFactory } from "./agent-vault-personal-value-dal";@@ -15,6 +16,10 @@ export type TClearPersonalValuesDeps = {* Losing a bundle clears the values an actor saved for its services, so a later grant starts from nothing. A* group's revoke reaches its members, and anyone who still holds the bundle another way keeps theirs. Without* `accessBundleId` this covers every bundle in the project, for an actor leaving Agent Vault.+ *+ * Call it after the change is written in `tx`. Cleanups in one project run one at a time, so of two changes that+ * each leave the other's access in place (a demotion and the revoke of the last grant), the second checks against+ * the first once it has committed instead of both skipping.*/export const clearPersonalValuesAfterRevoke = async ({ agentVaultPersonalValueDAL, userGroupMembershipDAL, identityGroupMembershipDAL }: TClearPersonalValuesDeps,@@ -27,6 +32,8 @@ export const clearPersonalValuesAfterRevoke = async (}: { projectId: string; accessBundleId?: string; userIds: string[]; identityIds: string[]; groupIds: string[] },tx: Knex) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.ClearAgentVaultPersonalValues(projectId)]);+const [groupUsers, groupIdentities] = groupIds.length? await Promise.all([userGroupMembershipDAL.find({ $in: { groupId: groupIds } }, { tx }),diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.tsindex e3cdf20090..5a4d2f4ec2 100644--- a/backend/src/keystore/keystore.ts+++ b/backend/src/keystore/keystore.ts@@ -34,6 +34,8 @@ export const PgSqlLock = {CreateAgentVaultVariable: (projectId: string) => pgAdvisoryLockHashText(`create-agent-vault-variable:${projectId}`),CreateAgentVaultPersonalVariable: (projectId: string, ownerId: string) =>pgAdvisoryLockHashText(`create-agent-vault-personal-variable:${projectId}:${ownerId}`),+ ClearAgentVaultPersonalValues: (projectId: string) =>+ pgAdvisoryLockHashText(`clear-agent-vault-personal-values:${projectId}`),AccessSharedSecret: (sharedSecretId: string) => pgAdvisoryLockHashText(`access-shared-secret:${sharedSecretId}`),KmsOrgKeyCreation: (orgId: string) => pgAdvisoryLockHashText(`kms-org-key:${orgId}`),KmsOrgDataKeyCreation: (orgId: string) => pgAdvisoryLockHashText(`kms-org-data-key:${orgId}`),