Skip to content

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

fix(agent-vault): read the service from the primary before a credential merge

Scott Wilson9a00baca64bc
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 55ca2f3eec..15655ee9a3 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
@@ -952,7 +952,7 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
ProjectPermissionSub.AgentVaultAccessBundles
);
- const service = await agentVaultServiceDAL.findOne({ id: serviceId, accessBundleId: bundle.id });
+ const service = await agentVaultServiceDAL.findByIdInAccessBundle({ id: serviceId, accessBundleId: bundle.id });
if (!service) throw new NotFoundError({ message: `Service with ID '${serviceId}' not found` });
if (name && name !== service.name) {
@@ -1089,8 +1089,8 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund
}
}
- // Both halves read under the lock and on the primary. The service read above the transaction came off
- // a replica, and stale it would admit the very collision this check exists to stop.
+ // Both halves read under the lock. The service read above the transaction came before it, and stale it
+ // would admit the very collision this check exists to stop.
if (credential || customHeaders) {
const effectiveCustomHeaders =
customHeaders ??
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 3f0ad4be22..9ecd120f9e 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,5 +20,19 @@ export const agentVaultServiceDALFactory = (db: TDbClient) => {
}
};
- return { ...orm, findByAccessBundleId };
+ // Reads the primary even without a tx. An update merges its credential against this row before the lock and
+ // compares it with the row under the lock, so a replica still behind the last save would turn the next one
+ // into a conflict with nobody else editing.
+ const findByIdInAccessBundle = async (
+ { id, accessBundleId }: { id: string; accessBundleId: string },
+ tx?: Knex
+ ): Promise<TAgentVaultServices | undefined> => {
+ try {
+ return await (tx || db)(TableName.AgentVaultService).where({ id, accessBundleId }).first();
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Find agent vault service" });
+ }
+ };
+
+ return { ...orm, findByAccessBundleId, findByIdInAccessBundle };
};
diff --git a/backend/src/ee/services/agent-vault/CLAUDE.md b/backend/src/ee/services/agent-vault/CLAUDE.md
index 1d1dc49d18..be656b772e 100644
--- a/backend/src/ee/services/agent-vault/CLAUDE.md
+++ b/backend/src/ee/services/agent-vault/CLAUDE.md
@@ -190,7 +190,8 @@ path decides what a service can reach, so it stays literal.
before variables existed has to keep reaching the host as it was, braces included.
- A credential update merges against the service read before the lock, so under the lock it re-reads the row
and returns a 409 if the type changed, or, for a partial basic update, the sealed secret did. Otherwise the
- half left out is written back stale, and its rows describe text that is no longer sealed.
+ half left out is written back stale, and its rows describe text that is no longer sealed. That first read is
+ on the primary (`findByIdInAccessBundle`): a replica still behind the previous save would 409 the next one.
- The `variableId` key is `DEFERRABLE INITIALLY DEFERRED`. A bundle delete cascades to its services and its
variables in one statement, and an immediate check can fire before the service cascade has removed the
rows, depending on which constraint was created first. The refusal that matters is the check under the