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(secret-sync): take the cloudflare worker lock separately and wait for it
DimitrisStaratzis3a619ae53382
diff --git a/backend/src/services/secret-sync/secret-sync-fns.test.ts b/backend/src/services/secret-sync/secret-sync-fns.test.tsindex efc383b471..742ac403c7 100644--- a/backend/src/services/secret-sync/secret-sync-fns.test.ts+++ b/backend/src/services/secret-sync/secret-sync-fns.test.ts@@ -1,33 +1,28 @@import { KeyStorePrefixes } from "@app/keystore/keystore";import { SecretSync } from "./secret-sync-enums";-import { getSecretSyncWriteLockKeys } from "./secret-sync-fns";+import { getSharedDestinationLockKey } from "./secret-sync-fns";vi.mock("@app/lib/config/env", () => ({ getConfig: () => ({}) }));-describe("getSecretSyncWriteLockKeys", () => {- test("locks a Cloudflare Workers sync on its Worker as well as on itself", () => {+describe("getSharedDestinationLockKey", () => {+ test("locks a Cloudflare Workers sync on its Worker", () => {expect(- getSecretSyncWriteLockKeys({- id: "sync-a",+ getSharedDestinationLockKey({destination: SecretSync.CloudflareWorkers,connectionId: "conn",destinationConfig: { scriptId: "my-worker" }})- ).toEqual([- KeyStorePrefixes.SecretSyncLock("sync-a"),- KeyStorePrefixes.CloudflareWorkerSyncLock("conn", "my-worker")- ]);+ ).toBe(KeyStorePrefixes.CloudflareWorkerSyncLock("conn", "my-worker"));});- test("locks other syncs on themselves only", () => {+ test("gives other syncs no shared lock", () => {expect(- getSecretSyncWriteLockKeys({- id: "sync-b",+ getSharedDestinationLockKey({destination: SecretSync.CloudflarePages,connectionId: "conn",destinationConfig: { projectName: "site", environment: "production" }})- ).toEqual([KeyStorePrefixes.SecretSyncLock("sync-b")]);+ ).toBeUndefined();});});diff --git a/backend/src/services/secret-sync/secret-sync-fns.ts b/backend/src/services/secret-sync/secret-sync-fns.tsindex ab27b1c9e1..dd792819ca 100644--- a/backend/src/services/secret-sync/secret-sync-fns.ts+++ b/backend/src/services/secret-sync/secret-sync-fns.ts@@ -755,21 +755,17 @@ const MAX_MESSAGE_LENGTH = 1024;// Cloudflare Workers variables are written as the Worker's full binding list, so two syncs writing to one// Worker at the same time would each drop what the other just wrote. Every write job on a Worker shares one-// lock; other destinations only need the per-sync lock.-export const getSecretSyncWriteLockKeys = (secretSync: {- id: string;+// lock, taken next to the per-sync lock. It is a separate lock rather than a second key on the same one,+// because Redis Cluster rejects a multi-key lock whose keys land in different slots.+export const getSharedDestinationLockKey = (secretSync: {destination: string;connectionId: string;destinationConfig?: unknown;}) => {- const keys: string[] = [KeyStorePrefixes.SecretSyncLock(secretSync.id)];+ if (secretSync.destination !== SecretSync.CloudflareWorkers) return undefined;- if (secretSync.destination === SecretSync.CloudflareWorkers) {- const { scriptId } = secretSync.destinationConfig as TCloudflareWorkersSync["destinationConfig"];- keys.push(KeyStorePrefixes.CloudflareWorkerSyncLock(secretSync.connectionId, scriptId));- }-- return keys;+ const { scriptId } = secretSync.destinationConfig as TCloudflareWorkersSync["destinationConfig"];+ return KeyStorePrefixes.CloudflareWorkerSyncLock(secretSync.connectionId, scriptId);};export const parseSyncErrorMessage = (err: unknown): string => {diff --git a/backend/src/services/secret-sync/secret-sync-queue.ts b/backend/src/services/secret-sync/secret-sync-queue.tsindex 41b87e32f6..c6ff6c564e 100644--- a/backend/src/services/secret-sync/secret-sync-queue.ts+++ b/backend/src/services/secret-sync/secret-sync-queue.ts@@ -44,7 +44,7 @@ import {import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";import {enterpriseSyncCheck,- getSecretSyncWriteLockKeys,+ getSharedDestinationLockKey,parseSyncErrorMessage,SecretSyncFns} from "@app/services/secret-sync/secret-sync-fns";@@ -161,6 +161,9 @@ const getRequeueDelay = (failureCount?: number) => {return REQUEUE_MS + jitter;};+const SECRET_SYNC_LOCK_DURATION_MS = 5 * 60 * 1000;+const SHARED_LOCK_RETRY_DELAY_MS = 2000;+export const secretSyncQueueFactory = ({queueService,cronJob,@@ -1238,12 +1241,10 @@ export const secretSyncQueueFactory = ({try {lock = await keyStore.acquireLock(- job.name === QueueJobs.SecretSyncImportSecrets- ? [KeyStorePrefixes.SecretSyncLock(syncId)]- : getSecretSyncWriteLockKeys(secretSync),+ [KeyStorePrefixes.SecretSyncLock(syncId)],// scott: not sure on this duration; syncs can take excessive amounts of time so we need to keep it locked,// but should always release below...- 5 * 60 * 1000+ SECRET_SYNC_LOCK_DURATION_MS);} catch (e) {logger.info(`SecretSync Failed to acquire lock [syncId=${syncId}] [job=${job.name}]`);@@ -1253,6 +1254,28 @@ export const secretSyncQueueFactory = ({return;}+ // Jobs that write to a destination other syncs share wait for each other, up to the lock's own lifetime, so+ // a sibling sync delays this job rather than failing it.+ const sharedLockKey =+ job.name === QueueJobs.SecretSyncImportSecrets ? undefined : getSharedDestinationLockKey(secretSync);+ let sharedLock: Awaited<ReturnType<typeof keyStore.acquireLock>> | undefined;++ if (sharedLockKey) {+ try {+ sharedLock = await keyStore.acquireLock([sharedLockKey], SECRET_SYNC_LOCK_DURATION_MS, {+ retryCount: SECRET_SYNC_LOCK_DURATION_MS / SHARED_LOCK_RETRY_DELAY_MS,+ retryDelay: SHARED_LOCK_RETRY_DELAY_MS+ });+ } catch (e) {+ logger.info(`SecretSync Failed to acquire shared destination lock [syncId=${syncId}] [job=${job.name}]`);++ await lock.release();+ await $handleAcquireLockFailure(job as SecretSyncActionJob);++ return;+ }+ }+let admittedConnectionSlot = false;try {@@ -1290,6 +1313,7 @@ export const secretSyncQueueFactory = ({await $releaseConnectionConcurrency(connectionId);}+ await sharedLock?.release();await lock.release();}});