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
Revert "fix(secret-sync): take the cloudflare worker lock separately and wait for it"
DimitrisStaratzis55aa8d6c3e9a
diff --git a/backend/src/services/secret-sync/secret-sync-fns.test.ts b/backend/src/services/secret-sync/secret-sync-fns.test.tsindex 742ac403c7..efc383b471 100644--- a/backend/src/services/secret-sync/secret-sync-fns.test.ts+++ b/backend/src/services/secret-sync/secret-sync-fns.test.ts@@ -1,28 +1,33 @@import { KeyStorePrefixes } from "@app/keystore/keystore";import { SecretSync } from "./secret-sync-enums";-import { getSharedDestinationLockKey } from "./secret-sync-fns";+import { getSecretSyncWriteLockKeys } from "./secret-sync-fns";vi.mock("@app/lib/config/env", () => ({ getConfig: () => ({}) }));-describe("getSharedDestinationLockKey", () => {- test("locks a Cloudflare Workers sync on its Worker", () => {+describe("getSecretSyncWriteLockKeys", () => {+ test("locks a Cloudflare Workers sync on its Worker as well as on itself", () => {expect(- getSharedDestinationLockKey({+ getSecretSyncWriteLockKeys({+ id: "sync-a",destination: SecretSync.CloudflareWorkers,connectionId: "conn",destinationConfig: { scriptId: "my-worker" }})- ).toBe(KeyStorePrefixes.CloudflareWorkerSyncLock("conn", "my-worker"));+ ).toEqual([+ KeyStorePrefixes.SecretSyncLock("sync-a"),+ KeyStorePrefixes.CloudflareWorkerSyncLock("conn", "my-worker")+ ]);});- test("gives other syncs no shared lock", () => {+ test("locks other syncs on themselves only", () => {expect(- getSharedDestinationLockKey({+ getSecretSyncWriteLockKeys({+ id: "sync-b",destination: SecretSync.CloudflarePages,connectionId: "conn",destinationConfig: { projectName: "site", environment: "production" }})- ).toBeUndefined();+ ).toEqual([KeyStorePrefixes.SecretSyncLock("sync-b")]);});});diff --git a/backend/src/services/secret-sync/secret-sync-fns.ts b/backend/src/services/secret-sync/secret-sync-fns.tsindex dd792819ca..ab27b1c9e1 100644--- a/backend/src/services/secret-sync/secret-sync-fns.ts+++ b/backend/src/services/secret-sync/secret-sync-fns.ts@@ -755,17 +755,21 @@ 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, 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: {+// lock; other destinations only need the per-sync lock.+export const getSecretSyncWriteLockKeys = (secretSync: {+ id: string;destination: string;connectionId: string;destinationConfig?: unknown;}) => {- if (secretSync.destination !== SecretSync.CloudflareWorkers) return undefined;+ const keys: string[] = [KeyStorePrefixes.SecretSyncLock(secretSync.id)];- const { scriptId } = secretSync.destinationConfig as TCloudflareWorkersSync["destinationConfig"];- return KeyStorePrefixes.CloudflareWorkerSyncLock(secretSync.connectionId, scriptId);+ if (secretSync.destination === SecretSync.CloudflareWorkers) {+ const { scriptId } = secretSync.destinationConfig as TCloudflareWorkersSync["destinationConfig"];+ keys.push(KeyStorePrefixes.CloudflareWorkerSyncLock(secretSync.connectionId, scriptId));+ }++ return keys;};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 c6ff6c564e..41b87e32f6 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,- getSharedDestinationLockKey,+ getSecretSyncWriteLockKeys,parseSyncErrorMessage,SecretSyncFns} from "@app/services/secret-sync/secret-sync-fns";@@ -161,9 +161,6 @@ 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,@@ -1241,10 +1238,12 @@ export const secretSyncQueueFactory = ({try {lock = await keyStore.acquireLock(- [KeyStorePrefixes.SecretSyncLock(syncId)],+ job.name === QueueJobs.SecretSyncImportSecrets+ ? [KeyStorePrefixes.SecretSyncLock(syncId)]+ : getSecretSyncWriteLockKeys(secretSync),// 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...- SECRET_SYNC_LOCK_DURATION_MS+ 5 * 60 * 1000);} catch (e) {logger.info(`SecretSync Failed to acquire lock [syncId=${syncId}] [job=${job.name}]`);@@ -1254,28 +1253,6 @@ 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 {@@ -1313,7 +1290,6 @@ export const secretSyncQueueFactory = ({await $releaseConnectionConcurrency(connectionId);}- await sharedLock?.release();await lock.release();}});