Skip to content

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

fix(agent-vault): read an added delegation after commit

Scott Wilson8033c607eb7e
diff --git a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
index c30e79d8b9..8bd019dfd0 100644
--- a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
+++ b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
@@ -164,10 +164,11 @@ export const agentVaultDelegationDALFactory = (db: TDbClient) => {
limit,
offset = 0
}: { projectId: string; identityIds?: string[]; limit?: number; offset?: number },
- tx?: Knex
+ tx?: Knex,
+ { readFromPrimary = false }: { readFromPrimary?: boolean } = {}
): Promise<{ delegations: TAgentVaultDelegationView[]; totalCount: number }> => {
try {
- const conn = tx || db.replicaNode();
+ const conn = tx || (readFromPrimary ? db : db.replicaNode());
const filtered = () => {
const query = conn(TableName.AgentVaultDelegation).where(
`${TableName.AgentVaultDelegation}.projectId`,
diff --git a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-service.ts b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-service.ts
index 62555cf317..9c9c0b2d2c 100644
--- a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-service.ts
+++ b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-service.ts
@@ -404,7 +404,7 @@ export const agentVaultDelegationServiceFactory = ({
}
};
- const { delegation, isChanged } = await agentVaultDelegationDAL.transaction(async (tx) => {
+ const isChanged = await agentVaultDelegationDAL.transaction(async (tx) => {
// The same order a save takes them in: memberships, then the delegation row.
const memberships = await agentVaultDelegationDAL.lockMemberships(
{ projectId, identityId: machineIdentityId, userIds, groupIds },
@@ -428,12 +428,17 @@ export const agentVaultDelegationServiceFactory = ({
{ delegationId: row.id, membershipIds: membershipIdsNaming(memberships, userIds, groupIds) },
tx
);
- // Read in the transaction because the replica may not have the write yet.
- const {
- delegations: [written]
- } = await agentVaultDelegationDAL.findViews({ projectId, identityIds: [machineIdentityId] }, tx);
- return { delegation: written, isChanged: added > 0 };
+ return added > 0;
+ });
+
+ // Read after commit so the lock isn't held for the whole list, and from the primary, which already has the write.
+ const {
+ delegations: [delegation]
+ } = await agentVaultDelegationDAL.findViews({ projectId, identityIds: [machineIdentityId] }, undefined, {
+ readFromPrimary: true
});
+ // Only a concurrent delete between the commit and this read leaves nothing to return.
+ if (!delegation) throw notFound(machineIdentityId);
// isChanged lets the caller audit only a real change: naming users and groups already in it writes nothing.
return { delegation, isChanged };