Skip to content

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

fix(agent-vault): lock a bundle's services before its grants on delete

Scott Wilson16dc5d554022
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.ts
index 98a5fe52d5..efe3c0aa92 100644
--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts
@@ -2497,6 +2497,52 @@ describe("Agent Vault V1 Router", async () => {
}
});
+ test("deleting a bundle while a member saves their personal values doesn't deadlock", async () => {
+ const bundle = await createAccessBundle("mp-delete-vs-save");
+ const service = await createService(bundle.id, {
+ name: "notion",
+ hostPattern: "notion-deadlock.example.com",
+ credential: { type: "bearer", memberProvidedFields: ["credential-value"] }
+ });
+
+ const member = await createMemberIdentity(`mp-delete-vs-save-${Date.now()}`);
+ try {
+ expect((await inject("POST", membersUrl(bundle.id), { machineIdentityIds: [member.id] })).statusCode).toBe(200);
+
+ // Holding the service row queues the save on it and the delete behind the save, so once this lets go the
+ // save holds the service while the delete waits for it.
+ const trx = await testDb.transaction();
+ let save: ReturnType<typeof inject> | undefined;
+ let remove: ReturnType<typeof inject> | undefined;
+ try {
+ const { rows } = await trx.raw("select pg_backend_pid() as pid");
+ await trx("agent_vault_services").where({ id: service.id }).forUpdate().first();
+
+ save = setValues(member.as, bundle.id, service.id, { "credential-value": "mine" });
+ await waitUntilBlockedBy(rows[0].pid);
+ const { rows: queued } = await testDb.raw(
+ "select pid from pg_stat_activity where ? = any(pg_blocking_pids(pid))",
+ [rows[0].pid]
+ );
+ expect(queued).toHaveLength(1);
+ remove = inject("DELETE", `/api/v1/agent-vault/access-bundles/${bundle.id}`);
+ await waitUntilBlockedBy(queued[0].pid);
+ await trx.rollback();
+
+ expect((await save).statusCode).toBe(200);
+ expect((await remove).statusCode).toBe(200);
+ } catch (err) {
+ if (!trx.isCompleted()) await trx.rollback();
+ await Promise.all([save?.catch(() => undefined), remove?.catch(() => undefined)]);
+ throw err;
+ }
+ // The values went with the service, which the API can no longer name.
+ expect(await testDb("agent_vault_personal_values").where({ identityId: member.id })).toHaveLength(0);
+ } finally {
+ await member.cleanup();
+ }
+ });
+
test("until a member sets up their half, resolve sends no credential, not the admin's half", async () => {
const bundle = await createAccessBundle("mp-half");
await createService(bundle.id, {
@@ -2627,7 +2673,7 @@ describe("Agent Vault V1 Router", async () => {
}
});
- test("an admin is asked for values on a bundle they reach only by role, flagged as not granted", async () => {
+ test("an admin is asked for values on a bundle they reach only by role, and granted narrows the list", async () => {
const ungranted = await createAccessBundle("mp-mine-ungranted");
const service = await createService(ungranted.id, {
name: "openai",
@@ -2637,23 +2683,32 @@ describe("Agent Vault V1 Router", async () => {
const granted = await createAccessBundle("mp-mine-granted");
expect((await inject("DELETE", membersUrl(ungranted.id), { userIds: [seedData1.id] })).statusCode).toBe(200);
+ const listOf = async (query: string) => {
+ const res = await inject("GET", `/api/v1/agent-vault/access-bundles?search=mp-mine&${query}`);
+ expect(res.statusCode).toBe(200);
+ const { accessBundles, totalCount } = JSON.parse(res.payload) as {
+ accessBundles: { id: string }[];
+ totalCount: number;
+ };
+ return { ids: accessBundles.map((bundle) => bundle.id), totalCount };
+ };
+
const listed = await inject("GET", "/api/v1/agent-vault/access-bundles?search=mp-mine&limit=100");
expect(JSON.parse(listed.payload).accessBundles).toEqual(
expect.arrayContaining([
- expect.objectContaining({
- id: ungranted.id,
- isGranted: false,
- servicesRequiringSetup: [{ id: service.id, name: "openai" }]
- }),
- expect.objectContaining({ id: granted.id, isGranted: true, servicesRequiringSetup: [] })
+ expect.objectContaining({ id: ungranted.id, servicesRequiringSetup: [{ id: service.id, name: "openai" }] }),
+ expect.objectContaining({ id: granted.id, servicesRequiringSetup: [] })
])
);
expect((await pendingOf(inject)).get(ungranted.id)).toEqual(["openai"]);
- const setUp = await inject("GET", "/api/v1/agent-vault/access-bundles?requiresSetup=false&search=mp-mine");
- expect(
- (JSON.parse(setUp.payload) as { accessBundles: { id: string }[] }).accessBundles.map((bundle) => bundle.id)
- ).toEqual([granted.id]);
+ expect(await listOf("requiresSetup=false")).toEqual({ ids: [granted.id], totalCount: 1 });
+ expect(await listOf("granted=true")).toEqual({ ids: [granted.id], totalCount: 1 });
+ expect(await listOf("granted=false")).toEqual({ ids: [ungranted.id], totalCount: 1 });
+ expect(await listOf("requiresSetup=true&granted=true")).toEqual({ ids: [], totalCount: 0 });
+ expect(await listOf("requiresSetup=true&granted=false")).toEqual({ ids: [ungranted.id], totalCount: 1 });
+ expect(await listOf("requiresSetup=false&granted=false")).toEqual({ ids: [], totalCount: 0 });
expect((await inject("GET", "/api/v1/agent-vault/access-bundles?requiresSetup=maybe")).statusCode).toBe(422);
+ expect((await inject("GET", "/api/v1/agent-vault/access-bundles?granted=maybe")).statusCode).toBe(422);
expect((await statusesOf(inject, ungranted.id)).get(service.id)?.isConfigured).toBe(false);
});
diff --git a/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-access-bundle-router.ts b/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-access-bundle-router.ts
index e944bc1f28..11e38e5522 100644
--- a/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-access-bundle-router.ts
+++ b/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-access-bundle-router.ts
@@ -76,6 +76,11 @@ export const registerAgentVaultAccessBundleRouter = async (server: FastifyZodPro
.transform((value) => value === "true")
.optional()
.describe(AGENT_VAULT.ACCESS_BUNDLE.requiresSetup),
+ granted: z
+ .enum(["true", "false"])
+ .transform((value) => value === "true")
+ .optional()
+ .describe(AGENT_VAULT.ACCESS_BUNDLE.granted),
orderBy: z
.enum(["name", "serviceCount", "createdAt"])
.default("createdAt")
@@ -89,7 +94,6 @@ export const registerAgentVaultAccessBundleRouter = async (server: FastifyZodPro
serviceCount: z.number().describe(AGENT_VAULT.ACCESS_BUNDLE.serviceCount),
memberCount: z.number().describe(AGENT_VAULT.ACCESS_BUNDLE.memberCount),
hostPatterns: z.string().array().describe(AGENT_VAULT.ACCESS_BUNDLE.hostPatterns),
- isGranted: z.boolean().describe(AGENT_VAULT.ACCESS_BUNDLE.isGranted),
servicesRequiringSetup: z
.object({
id: z.string().uuid().describe(AGENT_VAULT.ACCESS_BUNDLE.servicesRequiringSetupId),
diff --git a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts
index f1fd5aa825..f3d7768573 100644
--- a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts
+++ b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts
@@ -591,8 +591,8 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
};
// Setup is counted over every bundle the caller reaches, since an admin can start a session on one they were never
- // granted. isGranted lets the sidebar nag only about the bundles they take on.
- const listAccessBundles = async ({ projectId, ctx, requiresSetup, ...page }: TListAccessBundlesDTO) => {
+ // granted.
+ const listAccessBundles = async ({ projectId, ctx, requiresSetup, granted, ...page }: TListAccessBundlesDTO) => {
const { permission, accessBundleIds, grantedAccessBundleIds } = await getAgentVaultGrantedReachability(
{ permissionService, membershipDAL },
{ projectId, ctx }
@@ -602,8 +602,12 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
ProjectPermissionSub.AgentVaultAccessBundles
);
+ // null is every bundle in the project, which only an admin reaches.
+ const scopeIds = granted ? grantedAccessBundleIds : accessBundleIds;
+ const excludeIds = granted === false ? grantedAccessBundleIds : [];
+ const excluded = new Set(excludeIds);
+
const owner = ownerOrNullOf(ctx);
- const granted = new Set(grantedAccessBundleIds);
const pendingByBundleOf = async (bundleIds: string[] | null) => {
const byBundle = new Map<string, { id: string; name: string }[]>();
if (!owner || bundleIds?.length === 0) return byBundle;
@@ -614,7 +618,7 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
withVariableReferences: false
});
statuses
- .filter((status) => !status.isConfigured)
+ .filter((status) => !status.isConfigured && !excluded.has(status.accessBundleId))
.forEach((status) =>
byBundle.set(status.accessBundleId, [
...(byBundle.get(status.accessBundleId) ?? []),
@@ -629,17 +633,18 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
) =>
accessBundles.map((bundle) => ({
...bundle,
- isGranted: granted.has(bundle.id),
servicesRequiringSetup: pendingByBundle.get(bundle.id) ?? []
}));
- // Either filter needs every reachable bundle's status before it can page; the plain list only needs the page's.
+ // Either setup filter needs every bundle's status before it can page; the plain list only needs the page's.
if (requiresSetup !== undefined) {
- const pendingByBundle = await pendingByBundleOf(accessBundleIds);
+ const pendingByBundle = await pendingByBundleOf(scopeIds);
const pendingIds = [...pendingByBundle.keys()];
const { accessBundles, totalCount } = await agentVaultAccessBundleDAL.findForList({
projectId,
- ...(requiresSetup ? { accessBundleIds: pendingIds } : { accessBundleIds, excludeAccessBundleIds: pendingIds }),
+ ...(requiresSetup
+ ? { accessBundleIds: pendingIds }
+ : { accessBundleIds: scopeIds, excludeAccessBundleIds: [...excludeIds, ...pendingIds] }),
...page
});
return { accessBundles: withSetup(accessBundles, pendingByBundle), totalCount };
@@ -647,7 +652,8 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
const { accessBundles, totalCount } = await agentVaultAccessBundleDAL.findForList({
projectId,
- accessBundleIds,
+ accessBundleIds: scopeIds,
+ excludeAccessBundleIds: excludeIds,
...page
});
const pendingByBundle = await pendingByBundleOf(accessBundles.map((bundle) => bundle.id));
@@ -786,11 +792,12 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
ProjectPermissionSub.AgentVaultAccessBundles
);
- // Bundle row first, for the row lock addMember takes. Then the grants before the bundle's own delete, which
- // cascades to its services' personal values: a removal from Agent Vault deletes grants and then those values,
- // and taking them in the same order is what keeps the two from deadlocking. Roles cascade with the grants.
+ // Locks in the order every path takes them, bundle row, services, grants, values, so this can't deadlock with
+ // a grant, a personal values save or a removal. The bundle's delete cascades to its services and their values,
+ // and roles go with the grants.
return agentVaultAccessBundleDAL.transaction(async (tx) => {
await agentVaultAccessBundleDAL.lockByIdInProject({ id: bundle.id, projectId: rest.projectId }, tx);
+ await agentVaultServiceDAL.lockByAccessBundleId(bundle.id, tx);
await membershipDAL.delete(bundleScope(rest.projectId, bundle.id), tx);
return agentVaultAccessBundleDAL.deleteById(bundle.id, tx);
});
diff --git a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-types.ts b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-types.ts
index ae4e3a527b..6c27bf0733 100644
--- a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-types.ts
+++ b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-types.ts
@@ -52,6 +52,7 @@ export type TAgentVaultProjectScoped = { projectId: string; ctx: TGenericPermiss
export type TListAccessBundlesDTO = TAgentVaultProjectScoped & {
requiresSetup?: boolean;
+ granted?: boolean;
search?: string;
orderBy: TAgentVaultAccessBundleOrderBy;
orderDirection: "asc" | "desc";
diff --git a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-service-dal.ts b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-service-dal.ts
index 3f28831d3b..a1b3e32d05 100644
--- a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-service-dal.ts
+++ b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-service-dal.ts
@@ -20,6 +20,19 @@ export const agentVaultServiceDALFactory = (db: TDbClient) => {
}
};
+ // In id order, so two transactions locking the same services queue for them the same way.
+ const lockByAccessBundleId = async (accessBundleId: string, tx: Knex): Promise<{ id: string }[]> => {
+ try {
+ return (await tx(TableName.AgentVaultService)
+ .where({ accessBundleId })
+ .orderBy("id", "asc")
+ .forUpdate()
+ .select("id")) as { id: string }[];
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Lock agent vault services" });
+ }
+ };
+
// Reads the primary even without a tx, so the row is never older than the last committed save.
const findByIdInAccessBundle = async (
{ id, accessBundleId }: { id: string; accessBundleId: string },
@@ -73,5 +86,12 @@ export const agentVaultServiceDALFactory = (db: TDbClient) => {
}
};
- return { ...orm, findByAccessBundleId, findByIdInAccessBundle, findByIdInProject, lockForPersonalValues };
+ return {
+ ...orm,
+ findByAccessBundleId,
+ lockByAccessBundleId,
+ findByIdInAccessBundle,
+ findByIdInProject,
+ lockForPersonalValues
+ };
};
diff --git a/backend/src/ee/services/agent-vault/CLAUDE.md b/backend/src/ee/services/agent-vault/CLAUDE.md
index a2e9d4dde5..ad3a02d09b 100644
--- a/backend/src/ee/services/agent-vault/CLAUDE.md
+++ b/backend/src/ee/services/agent-vault/CLAUDE.md
@@ -55,10 +55,14 @@ and `packages/agentvault/` in the CLI repo. Frontend: `frontend/src/pages/agent-
`scopeResourceId = <bundle id>`) with one `membership_roles` row, slug `consumer`. The slug is a label;
bundle access is decided in SQL, not CASL. Because it is the platform's table, the platform reaps grants
on project removal, org removal, SCIM and every FK cascade. Do not add an Agent Vault reaper.
-- `scopeResourceId` has no FK, so `deleteAccessBundle` deletes the bundle row first and then the grants in one
- transaction, and `addMembers` locks the bundle row (`lockByIdInProject`) before inserting. Keep that order.
-- Granting is batch: one POST carries `{ userIds, machineIdentityIds, groupIds }`. That lock is what makes
- the dedupe read inside it authoritative, so an actor who already holds the bundle is returned in
+- `scopeResourceId` has no FK, so `deleteAccessBundle` deletes the grants itself, in the bundle delete's transaction,
+ and `addMembers` locks the bundle row (`lockByIdInProject`) before inserting. Every path takes these rows in one
+ order, bundle, services, grants, values, and a new one must too: the delete locks the bundle row and then its
+ services before deleting the grants and then the bundle, whose cascade takes the services and their values. A
+ personal values save takes its service and then the caller's grant, and a removal deletes grants before values,
+ so a delete that reached the grants before the services would deadlock with a save.
+- Granting is batch: one POST carries `{ userIds, machineIdentityIds, groupIds }`. The bundle row lock is what
+ makes the dedupe read inside it authoritative, so an actor who already holds the bundle is returned in
`skipped` rather than erroring; the unique index is only the backstop.
- Member responses, for bundles and for the product, are the actor itself, flat (`type`, the actor's `id`,
its profile) plus what the membership adds (`role`, `addedAt`, `grantedAt`). There is no membership id:
@@ -308,9 +312,10 @@ reference.
Reads ride along: the bundle read puts the caller's status on each service (`personalValues`, null when nothing
is member-provided), and the bundle list gives `servicesRequiringSetup` (`{ id, name }`) per bundle plus a
`requiresSetup` filter for the sidebar and banners (`false` is the rest of what the caller reaches). Both count
- every bundle the caller reaches, because an admin can start a session on one they were never granted. Each
- list item also carries `isGranted`, and the sidebar warns only about granted bundles, so an admin isn't nagged
- in the nav about bundles they reach only by role.
+ every bundle the caller reaches, because an admin can start a session on one they were never granted. A
+ `granted` filter keeps only the bundles granted to the caller (`false`, only the ones an admin reaches by role).
+ The sidebar asks for `requiresSetup=true&granted=true` and reads the count, so an admin isn't nagged in the nav
+ about bundles they reach only by role, while the page's banner and badges still show them.
## Credentials at rest
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 239c7972ff..43df09c34b 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -4485,8 +4485,8 @@ export const AGENT_VAULT = {
hostPatterns: "Every host pattern the access bundle's services cover.",
requiresSetup:
"true lists only the access bundles you can reach that have services still requiring your personal values. false lists the rest of them. Omit to list them all.",
- isGranted:
- "Whether the access bundle is granted to you, directly or through a group. false for one you reach only as an admin.",
+ granted:
+ "true lists only the access bundles granted to you, directly or through a group. false lists the ones you reach only as an admin. Omit to list them all.",
servicesRequiringSetup: "The services in this access bundle that still require your personal values.",
servicesRequiringSetupId: "The ID of the service, for setting your personal values on it.",
servicesRequiringSetupName: "The name of the service."
diff --git a/frontend/src/hooks/api/agentVault/queries.tsx b/frontend/src/hooks/api/agentVault/queries.tsx
index 06ec33ce95..0f03793835 100644
--- a/frontend/src/hooks/api/agentVault/queries.tsx
+++ b/frontend/src/hooks/api/agentVault/queries.tsx
@@ -219,11 +219,14 @@ export const useListAgentVaultAccessBundles = (params: TListAgentVaultAccessBund
});
};
-// The sidebar and the access bundle list ask the same question, so they share one cached answer. One page holds them
-// all in practice: it is only the bundles the caller reaches that still require their values.
+// One page holds them all in practice: it is only the bundles the caller reaches that still require their values.
export const useListAgentVaultAccessBundlesRequiringSetup = () =>
useListAgentVaultAccessBundles({ requiresSetup: true, limit: 100 });
+// Only the count is read, so one row is enough.
+export const useListGrantedAgentVaultAccessBundlesRequiringSetup = () =>
+ useListAgentVaultAccessBundles({ requiresSetup: true, granted: true, limit: 1 });
+
export const useGetAgentVaultAccessBundle = (accessBundleId: string) => {
const { currentOrg } = useOrganization();
diff --git a/frontend/src/hooks/api/agentVault/types.ts b/frontend/src/hooks/api/agentVault/types.ts
index bed4699bb2..847b615638 100644
--- a/frontend/src/hooks/api/agentVault/types.ts
+++ b/frontend/src/hooks/api/agentVault/types.ts
@@ -151,8 +151,10 @@ export type TListAgentVaultProxiesDTO = {
export type TListAgentVaultAccessBundlesDTO = Omit<TListAgentVaultProxiesDTO, "orderBy"> & {
orderBy?: "name" | "serviceCount" | "createdAt";
- /** Only the bundles granted to the caller that still require their personal values. */
+ /** Only the bundles the caller reaches that still require their personal values. */
requiresSetup?: boolean;
+ /** Only the bundles granted to the caller, or with false, only those an admin reaches by role. */
+ granted?: boolean;
};
export type TListAgentVaultVariablesDTO = {
@@ -207,8 +209,6 @@ export type TAgentVaultAccessBundleListItem = TAgentVaultAccessBundle & {
serviceCount: number;
memberCount: number;
hostPatterns: string[];
- // False on a bundle an admin reaches only by role.
- isGranted: boolean;
servicesRequiringSetup: { id: string; name: string }[];
};
diff --git a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/AgentVaultNav.tsx b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/AgentVaultNav.tsx
index 149fe4c85c..a69233057f 100644
--- a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/AgentVaultNav.tsx
+++ b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/AgentVaultNav.tsx
@@ -15,7 +15,7 @@ import {
import {
useGetAgentVaultSessionLogHealth,
useGetAgentVaultSessionLogSettings,
- useListAgentVaultAccessBundlesRequiringSetup
+ useListGrantedAgentVaultAccessBundlesRequiringSetup
} from "@app/hooks/api/agentVault";
import { ProjectMembershipRole } from "@app/hooks/api/roles/types";
@@ -29,9 +29,9 @@ export const AgentVaultNav = ({ onSubmenuOpen }: { onSubmenuOpen: (submenu: Subm
const { data: sessionLogSettings } = useGetAgentVaultSessionLogSettings(isAdmin);
const { data: sessionLogHealth } = useGetAgentVaultSessionLogHealth(isAdmin);
const { subscription } = useSubscription();
- const { data: requiringSetup } = useListAgentVaultAccessBundlesRequiringSetup();
// An admin can use any bundle, but the nav only nags about the ones granted to them.
- const needsSetup = Boolean(requiringSetup?.accessBundles.some((bundle) => bundle.isGranted));
+ const { data: grantedRequiringSetup } = useListGrantedAgentVaultAccessBundlesRequiringSetup();
+ const needsSetup = Boolean(grantedRequiringSetup?.totalCount);
const accessItems: NavItem[] = [
{ label: "Sessions", icon: IdCard, pathSuffix: "sessions" },