Skip to content

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

apply PR comments

Matheus Nogueira552735f64a20
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index f1ca593af2..302509ae62 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -69,6 +69,8 @@ export const KeyStorePrefixes = {
SyncSecretIntegrationLastRunTimestamp: (projectId: string, environmentSlug: string, secretPath: string) =>
`sync-integration-last-run-${projectId}-${environmentSlug}-${secretPath}` as const,
SecretSyncLock: (syncId: string) => `secret-sync-mutex-${syncId}` as const,
+ CloudflareWorkerVersionLock: (accountId: string, scriptId: string) =>
+ `cloudflare-worker-version-mutex-${accountId}-${scriptId}` as const,
PkiSyncLock: (syncId: string) => `pki-sync-mutex-${syncId}` as const,
PkiSyncFilterLock: (syncId: string) => `pki-sync-filter-mutex-${syncId}` as const,
AppConnectionConcurrentJobs: (connectionId: string, targetHost?: string) =>
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-errors.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-errors.ts
index e8b796fbea..9eaf996758 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-errors.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-errors.ts
@@ -6,14 +6,6 @@ import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
// it isn't the deployed one.
const CLOUDFLARE_PENDING_VERSION_ERROR_CODES = new Set([10214, 10215]);
-const CLOUDFLARE_ERROR_MESSAGES: Record<number, string> = {
- 10021: "Cloudflare rejected the Worker code in the new version",
- 10068: "Cloudflare rejected the Worker code in the new version",
- 10057: "a binding the new version keeps from its base version no longer exists there",
- 10027: "the Worker is over Cloudflare's 64 MiB size limit",
- 10339: "Cloudflare doesn't allow deleting the latest Worker version"
-};
-
export type TCloudflareApiResponse = {
result?: unknown;
success?: boolean;
@@ -23,7 +15,7 @@ export type TCloudflareApiResponse = {
export class CloudflareWorkersPendingVersionError extends SecretSyncError {}
-const $getCloudflareErrors = (data: unknown): Array<{ code: number; message: string }> => {
+export const getCloudflareErrors = (data: unknown): Array<{ code: number; message: string }> => {
if (typeof data !== "object" || data === null) return [];
const { errors } = data as TCloudflareApiResponse;
if (!Array.isArray(errors)) return [];
@@ -46,7 +38,7 @@ const $throwCloudflareError = (
}
throw new SecretSyncError({
- message: errors.map((e) => CLOUDFLARE_ERROR_MESSAGES[e.code] ?? e.message).join(". "),
+ message: errors.map((e) => e.message).join(". "),
error: cause,
secretKey,
shouldRetry: false
@@ -55,7 +47,7 @@ const $throwCloudflareError = (
export const throwOnCloudflareRequestError = (err: unknown, secretKey?: string): never => {
if (err instanceof AxiosError) {
- const errors = $getCloudflareErrors(err.response?.data);
+ const errors = getCloudflareErrors(err.response?.data);
if (errors.length > 0) {
$throwCloudflareError(errors, err, secretKey);
}
@@ -67,7 +59,7 @@ export const throwOnCloudflareRequestError = (err: unknown, secretKey?: string):
export const validateCloudflareResponse = (data: unknown) => {
if (typeof data !== "object" || data === null) return;
if ((data as TCloudflareApiResponse).success === false) {
- const errors = $getCloudflareErrors(data);
+ const errors = getCloudflareErrors(data);
if (errors.length > 0) {
$throwCloudflareError(errors, undefined);
}
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
index 0d18124994..7e59257ce9 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.test.ts
@@ -1,5 +1,7 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
+import { TKeyStoreFactory } from "@app/keystore/keystore";
+
import { TSecretSyncPayload } from "../secret-sync-payload";
import { CloudflareWorkersVariableType } from "./cloudflare-workers-constants";
import { CloudflareWorkersSyncFns } from "./cloudflare-workers-fns";
@@ -27,6 +29,12 @@ const syncWith = (variableType?: CloudflareWorkersVariableType, syncOptions: Rec
connection: { credentials: { apiToken: "token", accountId: "account-id" } }
}) as unknown as TCloudflareWorkersSyncWithCredentials;
+// These cases never reach the version fallback, which is the only user of the key store.
+const syncSecrets = (secretSync: TCloudflareWorkersSyncWithCredentials, payload: TSecretSyncPayload) =>
+ CloudflareWorkersSyncFns.syncSecrets(secretSync, payload, {
+ keyStore: { acquireLock: vi.fn() } as unknown as Pick<TKeyStoreFactory, "acquireLock">
+ });
+
const payloadOf = (secrets: Record<string, string>) =>
({
flatten: () => Object.fromEntries(Object.entries(secrets).map(([key, value]) => [key, { value }]))
@@ -57,10 +65,7 @@ describe("CloudflareWorkersSyncFns", () => {
{ type: "kv_namespace", name: "CACHE" }
]);
- await CloudflareWorkersSyncFns.syncSecrets(
- syncWith(CloudflareWorkersVariableType.Text),
- payloadOf({ API_KEY: "key", NEW_VAR: "new" })
- );
+ await syncSecrets(syncWith(CloudflareWorkersVariableType.Text), payloadOf({ API_KEY: "key", NEW_VAR: "new" }));
expect(patch).toHaveBeenCalledTimes(1);
expect(patch.mock.calls[0][0]).toMatch(/\/workers\/scripts\/my-worker\/settings$/);
@@ -74,10 +79,7 @@ describe("CloudflareWorkersSyncFns", () => {
it("rejects values that are not JSON before calling Cloudflare", async () => {
await expect(
- CloudflareWorkersSyncFns.syncSecrets(
- syncWith(CloudflareWorkersVariableType.Json),
- payloadOf({ VALID: '{"a":1}', BROKEN: "not json" })
- )
+ syncSecrets(syncWith(CloudflareWorkersVariableType.Json), payloadOf({ VALID: '{"a":1}', BROKEN: "not json" }))
).rejects.toThrow("cannot be synced as Cloudflare Workers JSON variables: BROKEN.");
expect(get).not.toHaveBeenCalled();
@@ -87,7 +89,7 @@ describe("CloudflareWorkersSyncFns", () => {
stubWorker([{ type: "kv_namespace", name: "CACHE" }]);
await expect(
- CloudflareWorkersSyncFns.syncSecrets(syncWith(CloudflareWorkersVariableType.Text), payloadOf({ CACHE: "value" }))
+ syncSecrets(syncWith(CloudflareWorkersVariableType.Text), payloadOf({ CACHE: "value" }))
).rejects.toThrow(
"same name as another binding on the Cloudflare Worker, such as a KV namespace or a service: CACHE."
);
@@ -103,9 +105,9 @@ describe("CloudflareWorkersSyncFns", () => {
])("fails a %s sync before writing when a key exists as another type", async (variableType, binding, listed) => {
stubWorker([binding]);
- await expect(
- CloudflareWorkersSyncFns.syncSecrets(syncWith(variableType), payloadOf({ CONFIG: '{"a":2}', NEW_KEY: "{}" }))
- ).rejects.toThrow(`already exist on the Cloudflare Worker as a different type: ${listed}.`);
+ await expect(syncSecrets(syncWith(variableType), payloadOf({ CONFIG: '{"a":2}', NEW_KEY: "{}" }))).rejects.toThrow(
+ `already exist on the Cloudflare Worker as a different type: ${listed}.`
+ );
expect(patch).not.toHaveBeenCalled();
});
@@ -116,7 +118,7 @@ describe("CloudflareWorkersSyncFns", () => {
{ type: "json", name: "CONFIG", json: { a: 1 } }
]);
- await CloudflareWorkersSyncFns.syncSecrets(syncWith(), payloadOf({ API_URL: "new", CONFIG: "{}", NEW_KEY: "n" }));
+ await syncSecrets(syncWith(), payloadOf({ API_URL: "new", CONFIG: "{}", NEW_KEY: "n" }));
expect(patch).toHaveBeenCalledTimes(1);
expect(patch.mock.calls[0][0]).toMatch(/\/secrets-bulk$/);
@@ -128,7 +130,7 @@ describe("CloudflareWorkersSyncFns", () => {
it("updates same-name text variables as they are when the toggle is on", async () => {
stubWorker([{ type: "plain_text", name: "API_URL", text: "old" }]);
- await CloudflareWorkersSyncFns.syncSecrets(
+ await syncSecrets(
syncWith(CloudflareWorkersVariableType.Secret, { syncNonSecretBindings: true }),
payloadOf({ API_URL: "new" })
);
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
index aecc96220d..7f429ad9fc 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-fns.ts
@@ -1,3 +1,4 @@
+import { TKeyStoreFactory } from "@app/keystore/keystore";
import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
import { chunkArray } from "@app/lib/fn";
@@ -313,26 +314,6 @@ const $planSyncBindings = (
return { written, dropped };
};
-const $planRemoveBindings = (
- secretSync: TCloudflareWorkersSyncWithCredentials,
- secretMap: TSecretMap,
- baseBindings: Array<{ type: string; name: string }>
-): TBindingPlan => {
- const managedTypes = $getManagedTypes(secretSync);
-
- return {
- written: [],
- dropped: baseBindings
- .filter(
- (binding) =>
- managedTypes.has(binding.type) &&
- secretMap[binding.name] !== undefined &&
- $isManagedBySchema(secretSync, binding.name)
- )
- .map(({ name }) => name)
- };
-};
-
const $inPlace = {
syncSecrets: async (secretSync: TCloudflareWorkersSyncWithCredentials, payload: TSecretSyncPayload) => {
const secretMap = payload.flatten();
@@ -638,7 +619,8 @@ const $inPlace = {
export const CloudflareWorkersSyncFns = {
syncSecrets: async (
secretSync: TCloudflareWorkersSyncWithCredentials,
- payload: TSecretSyncPayload
+ payload: TSecretSyncPayload,
+ { keyStore }: { keyStore: Pick<TKeyStoreFactory, "acquireLock"> }
): Promise<TSyncSecretsResult | undefined> => {
try {
await $inPlace.syncSecrets(secretSync, payload);
@@ -647,7 +629,7 @@ export const CloudflareWorkersSyncFns = {
if (!(err instanceof CloudflareWorkersPendingVersionError)) throw err;
const secretMap = payload.flatten();
- return applyBindingsWithVersionFallback(secretSync, (baseBindings) =>
+ return applyBindingsWithVersionFallback(secretSync, keyStore, (baseBindings) =>
$planSyncBindings(secretSync, secretMap, baseBindings)
);
}
@@ -657,16 +639,7 @@ export const CloudflareWorkersSyncFns = {
throw new Error(`${SECRET_SYNC_NAME_MAP[secretSync.destination]} does not support importing secrets.`);
},
- removeSecrets: async (secretSync: TCloudflareWorkersSyncWithCredentials, payload: TSecretSyncPayload) => {
- try {
- await $inPlace.removeSecrets(secretSync, payload);
- } catch (err) {
- if (!(err instanceof CloudflareWorkersPendingVersionError)) throw err;
-
- const secretMap = payload.flatten();
- await applyBindingsWithVersionFallback(secretSync, (baseBindings) =>
- $planRemoveBindings(secretSync, secretMap, baseBindings)
- );
- }
- }
+ // No version fallback: it never deploys, so the secrets would stay on the live Worker while the removal, and with
+ // it a sync deletion, reported success.
+ removeSecrets: $inPlace.removeSecrets
};
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts
index 7ec0b02452..6915698533 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.test.ts
@@ -1,6 +1,9 @@
import { AxiosError, AxiosResponse } from "axios";
import { beforeEach, describe, expect, it, vi } from "vitest";
+import { TKeyStoreFactory } from "@app/keystore/keystore";
+
+import { SecretSyncError } from "../secret-sync-errors";
import { TSecretSyncPayload } from "../secret-sync-payload";
import { CloudflareWorkersVariableType } from "./cloudflare-workers-constants";
import { CloudflareWorkersSyncFns } from "./cloudflare-workers-fns";
@@ -47,7 +50,10 @@ const pendingVersion = (extra: Record<string, unknown> = {}): TVersion => ({
});
let base: TVersion;
-let created: TVersion | undefined;
+let created: Map<string, TVersion>;
+let uploadGate: Promise<void> | undefined;
+// Lets a test make Cloudflare store something other than what was uploaded.
+let alterStoredVersion: ((version: TVersion) => TVersion) | undefined;
// The secrets endpoint refuses because v21 isn't deployed. Version reads and creates behave like Cloudflare's,
// including the two rejections seen against the live API.
@@ -57,7 +63,7 @@ const installFakeCloudflare = (secretsEndpointRefuses = true) => {
return { data: { result: base.bindings.filter(({ type }) => type === "secret_text") } };
}
if (url.endsWith("/settings")) return { data: { result: { bindings: base.bindings } } };
- const version = url.endsWith(`/versions/${created?.id}`) ? created : base;
+ const version = created.get(url.split("/versions/")[1]) ?? base;
return { data: { success: true, result: structuredClone(version) } };
});
@@ -73,25 +79,50 @@ const installFakeCloudflare = (secretsEndpointRefuses = true) => {
const bindings = body.bindings.map((binding) =>
binding.type === "inherit" ? base.bindings.find(({ name }) => name === binding.name)! : binding
);
- created = { ...body, id: "new-1", bindings };
- return { data: { success: true, result: { id: created.id } } };
+ // Holds only the upload that is in flight when the gate is set.
+ const gate = uploadGate;
+ uploadGate = undefined;
+ await gate;
+ const uploaded = { ...body, id: `new-${created.size + 1}`, bindings };
+ const version = alterStoredVersion ? alterStoredVersion(uploaded) : uploaded;
+ created.set(version.id, version);
+ base = version;
+ return { data: { success: true, result: { id: version.id } } };
});
};
-const sync = (secrets: Record<string, string>) =>
- CloudflareWorkersSyncFns.syncSecrets(
- {
- id: "sync-id",
- destination: "cloudflare-workers",
- destinationConfig: { scriptId: "my-worker", variableType: CloudflareWorkersVariableType.Secret },
- environment: { slug: "prod" },
- syncOptions: { disableSecretDeletion: false },
- connection: { credentials: { apiToken: "token", accountId: "account-id" } }
- } as unknown as TCloudflareWorkersSyncWithCredentials,
- {
- flatten: () => Object.fromEntries(Object.entries(secrets).map(([key, value]) => [key, { value }]))
- } as unknown as TSecretSyncPayload
- );
+const syncOf = (syncOptions: Record<string, unknown> = {}, id = "sync-id") =>
+ ({
+ id,
+ destination: "cloudflare-workers",
+ destinationConfig: { scriptId: "my-worker", variableType: CloudflareWorkersVariableType.Secret },
+ environment: { slug: "prod" },
+ syncOptions: { disableSecretDeletion: false, ...syncOptions },
+ connection: { credentials: { apiToken: "token", accountId: "account-id" } }
+ }) as unknown as TCloudflareWorkersSyncWithCredentials;
+const secretSync = syncOf();
+
+// Like Redlock: acquiring a held lock fails rather than waits.
+const heldLocks = new Set<string>();
+const keyStore = {
+ acquireLock: async ([key]: string[]) => {
+ if (heldLocks.has(key)) throw new Error("lock is held");
+ heldLocks.add(key);
+ return {
+ release: async () => {
+ heldLocks.delete(key);
+ }
+ };
+ }
+} as unknown as Pick<TKeyStoreFactory, "acquireLock">;
+
+const payloadOf = (secrets: Record<string, string>) =>
+ ({
+ flatten: () => Object.fromEntries(Object.entries(secrets).map(([key, value]) => [key, { value }]))
+ }) as unknown as TSecretSyncPayload;
+
+const sync = (secrets: Record<string, string>, target = secretSync) =>
+ CloudflareWorkersSyncFns.syncSecrets(target, payloadOf(secrets), { keyStore });
const versionCreates = () =>
post.mock.calls.filter(([url]) => (url as string).endsWith("/versions")) as Array<
@@ -102,7 +133,10 @@ describe("Cloudflare Workers version fallback", () => {
beforeEach(() => {
vi.clearAllMocks();
base = pendingVersion();
- created = undefined;
+ created = new Map();
+ uploadGate = undefined;
+ alterStoredVersion = undefined;
+ heldLocks.clear();
});
it("doesn't create a version when the secrets endpoint succeeds", async () => {
@@ -152,4 +186,78 @@ describe("Cloudflare Workers version fallback", () => {
await expect(sync({ API_KEY: "new" })).rejects.toThrow("static assets");
expect(versionCreates()).toEqual([]);
});
+
+ it("fails a secret removal instead of leaving the secrets on the deployed Worker", async () => {
+ installFakeCloudflare();
+
+ await expect(CloudflareWorkersSyncFns.removeSecrets(secretSync, payloadOf({ API_KEY: "x" }))).rejects.toThrow(
+ "deploy the latest Worker version"
+ );
+ expect(versionCreates()).toEqual([]);
+ });
+
+ it("explains a timed-out upload, and doesn't retry it", async () => {
+ installFakeCloudflare();
+ post.mockRejectedValueOnce(new AxiosError("timeout of 300000ms exceeded", "ECONNABORTED"));
+
+ const error = (await sync({ API_KEY: "new" }).catch((err: unknown) => err)) as SecretSyncError;
+
+ expect(error).toBeInstanceOf(SecretSyncError);
+ expect(error.message).toContain("took longer than 5 minutes");
+ expect(error.shouldRetry).toBe(false);
+ });
+
+ it("makes a second sync to the same Worker wait, so neither loses the other's changes", async () => {
+ installFakeCloudflare();
+ // Two different syncs, so only a lock on the Worker itself serializes them.
+ const firstSync = syncOf({ disableSecretDeletion: true }, "first-sync");
+ const secondSync = syncOf({ disableSecretDeletion: true }, "second-sync");
+ let openUploadGate = () => {};
+ uploadGate = new Promise((resolve) => {
+ openUploadGate = resolve;
+ });
+
+ const first = sync({ API_KEY: "a" }, firstSync);
+ try {
+ await vi.waitFor(() => expect(versionCreates()).toHaveLength(1));
+
+ // The first sync is mid-upload and holds the Worker's lock.
+ const second = (await sync({ STALE: "b" }, secondSync).catch((err: unknown) => err)) as SecretSyncError;
+ expect(second).toBeInstanceOf(SecretSyncError);
+ expect(second.shouldRetry).toBe(true);
+ } finally {
+ openUploadGate();
+ }
+ await first;
+
+ // The queue's retry builds on the first sync's version.
+ await sync({ STALE: "b" }, secondSync);
+ const latest = Object.fromEntries(base.bindings.map((binding) => [binding.name, binding]));
+ expect(latest.API_KEY).toMatchObject({ text: "a" });
+ expect(latest.STALE).toMatchObject({ text: "b" });
+ });
+
+ it("releases the Worker's lock when the fallback fails", async () => {
+ base = pendingVersion({ assets: { config: {} } });
+ installFakeCloudflare();
+
+ await expect(sync({ API_KEY: "new" })).rejects.toThrow("static assets");
+ expect(heldLocks.size).toBe(0);
+ });
+
+ it("fails, without exposing secret values, when the stored version doesn't match the upload", async () => {
+ installFakeCloudflare();
+ alterStoredVersion = (version) => ({
+ ...version,
+ bindings: version.bindings.filter(({ name }) => name !== "CACHE"),
+ modules: [{ name: "index.js", content_type: "application/javascript+module", content_base64: "Y2hhbmdlZA==" }]
+ });
+
+ const error = (await sync({ API_KEY: "s3cr3t-value" }).catch((err: unknown) => err)) as Error;
+
+ expect(error).toBeInstanceOf(SecretSyncError);
+ expect(error.message).toContain("CACHE");
+ expect(error.message).toContain("index.js");
+ expect(error.message).not.toContain("s3cr3t-value");
+ });
});
diff --git a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts
index 100d512faf..8e7fe69795 100644
--- a/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts
+++ b/backend/src/services/secret-sync/cloudflare-workers/cloudflare-workers-version-fns.ts
@@ -1,5 +1,7 @@
+import { isAxiosError } from "axios";
import pLimit from "p-limit";
+import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { request } from "@app/lib/config/request";
import { crypto } from "@app/lib/crypto/cryptography";
import { logger } from "@app/lib/logger";
@@ -7,13 +9,23 @@ import { IntegrationUrls } from "@app/services/integration-auth/integration-list
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
import { TSyncSecretsResult } from "@app/services/secret-sync/secret-sync-types";
-import { throwOnCloudflareRequestError, validateCloudflareResponse } from "./cloudflare-workers-errors";
+import { getCloudflareErrors, validateCloudflareResponse } from "./cloudflare-workers-errors";
import { TCloudflareWorkersSyncWithCredentials } from "./cloudflare-workers-types";
// A fallback moves the Worker's whole bundle each way (up to ~85 MB of base64), so only a few run at once.
const fallbackLimit = pLimit(2);
const VERSION_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
const MAX_VERSION_PAYLOAD_BYTES = 128 * 1024 * 1024;
+// Outlives the three version requests made under the lock (read, upload, read back) at their longest.
+const WORKER_LOCK_DURATION_MS = 3 * VERSION_REQUEST_TIMEOUT_MS + 60 * 1000;
+
+// Read as the reason in $fail's message, for the errors a copied version can run into.
+const CLOUDFLARE_ERROR_REASONS: Record<number, string> = {
+ 10021: "Cloudflare rejected the Worker code copied from the latest version",
+ 10068: "Cloudflare rejected the Worker code copied from the latest version",
+ 10057: "a binding kept from the latest version no longer exists there",
+ 10027: "the Worker is over Cloudflare's 64 MiB size limit"
+};
type TVersionModule = {
name: string;
@@ -54,13 +66,54 @@ type TExpectedVersion = {
const $fail = (
scriptId: string,
reason: string,
- fix = "Deploy the latest version of the Worker in Cloudflare, then sync again."
+ fix = "Deploy the latest version of the Worker in Cloudflare, then sync again.",
+ { error, shouldRetry = false }: { error?: unknown; shouldRetry?: boolean } = {}
) =>
new SecretSyncError({
message: `The latest version of Cloudflare Worker '${scriptId}' is not deployed, so Infisical tried to add the secrets to a new version of the Worker instead, but ${reason}. ${fix}`,
- shouldRetry: false
+ error,
+ shouldRetry
});
+// Every failure of a fallback request reaches the user as a $fail message, including timeouts and the payload
+// limits, which carry no Cloudflare error body.
+const $throwRequestError = (scriptId: string, err: unknown, { canRetry }: { canRetry: boolean }): never => {
+ if (!isAxiosError(err)) throw err;
+
+ const cloudflareErrors = getCloudflareErrors(err.response?.data);
+ if (cloudflareErrors.length) {
+ const known = cloudflareErrors.find(({ code }) => CLOUDFLARE_ERROR_REASONS[code]);
+ throw $fail(
+ scriptId,
+ known
+ ? CLOUDFLARE_ERROR_REASONS[known.code]
+ : `Cloudflare rejected the request: ${cloudflareErrors.map(({ message }) => message).join(". ")}`,
+ undefined,
+ { error: err }
+ );
+ }
+
+ if (err.message.includes("maxContentLength") || err.message.includes("maxBodyLength")) {
+ throw $fail(
+ scriptId,
+ `the Worker is larger than the ${MAX_VERSION_PAYLOAD_BYTES / 1024 / 1024} MiB Infisical can copy`,
+ undefined,
+ { error: err }
+ );
+ }
+
+ const isTimeout = err.code === "ECONNABORTED" || err.code === "ETIMEDOUT";
+ throw $fail(
+ scriptId,
+ isTimeout
+ ? `the request to Cloudflare took longer than ${VERSION_REQUEST_TIMEOUT_MS / 60000} minutes`
+ : "Infisical couldn't reach Cloudflare",
+ "Sync again.",
+ // A retried upload can't be told apart from a new one, so only reads are retried.
+ { error: err, shouldRetry: canRetry }
+ );
+};
+
const $bindingLabel = ({ name, type }: { name: string; type: string }) => `${name} (${type})`;
const $hashModule = ({ content_type: contentType, content_base64: contentBase64 }: TVersionModule) =>
@@ -87,7 +140,7 @@ const $getVersion = async ({ scriptId, accountUrl, headers }: TFallbackContext,
);
return data.result;
} catch (err) {
- return throwOnCloudflareRequestError(err);
+ return $throwRequestError(scriptId, err, { canRetry: true });
}
};
@@ -196,7 +249,8 @@ const $createVersion = async (
}
newVersionId = data.result.id;
} catch (err) {
- return throwOnCloudflareRequestError(err);
+ if (err instanceof SecretSyncError) throw err;
+ return $throwRequestError(ctx.scriptId, err, { canRetry: false });
}
logger.info(
@@ -248,6 +302,7 @@ const $verifyVersion = async (ctx: TFallbackContext, newVersionId: string, expec
// and checks it. Deploying it is left to the user, because it also ships the pending changes it was built on.
export const applyBindingsWithVersionFallback = (
secretSync: TCloudflareWorkersSyncWithCredentials,
+ keyStore: Pick<TKeyStoreFactory, "acquireLock">,
planBindings: (baseBindings: TVersionBinding[]) => TBindingPlan
) =>
fallbackLimit(async (): Promise<TSyncSecretsResult | undefined> => {
@@ -273,12 +328,39 @@ export const applyBindingsWithVersionFallback = (
`SecretSync Cloudflare Workers latest version not deployed, using fallback [syncId=${syncId}] [scriptId=${scriptId}]`
);
- const created = await $createVersion(ctx, "latest", planBindings);
- if (!created) return undefined;
+ // Each sync builds on the latest version, so two syncs to one Worker reading it at once would each upload a
+ // copy missing the other's changes. The lock is per Worker because syncs only lock themselves.
+ let lock: Awaited<ReturnType<typeof keyStore.acquireLock>>;
+ try {
+ lock = await keyStore.acquireLock(
+ [KeyStorePrefixes.CloudflareWorkerVersionLock(accountId, scriptId)],
+ WORKER_LOCK_DURATION_MS
+ );
+ } catch {
+ throw $fail(
+ scriptId,
+ "another sync was adding secrets to a new version of the same Worker",
+ "Infisical retries the sync automatically. If it keeps failing, sync again later.",
+ { shouldRetry: true }
+ );
+ }
- await $verifyVersion(ctx, created.newVersionId, created.expected);
- return {
- ...created.result,
- message: `Worker has undeployed changes; created version ${created.newVersionId} with updated secrets. Deploy it for the secrets to take effect.`
- };
+ try {
+ const created = await $createVersion(ctx, "latest", planBindings);
+ if (!created) return undefined;
+
+ await $verifyVersion(ctx, created.newVersionId, created.expected);
+ return {
+ ...created.result,
+ message: `Worker has undeployed changes; created version ${created.newVersionId} with updated secrets. Deploy it for the secrets to take effect.`
+ };
+ } finally {
+ // A release that fails (the lock already expired) mustn't turn a finished sync into a failed one.
+ await lock.release().catch((err: unknown) => {
+ logger.warn(
+ err,
+ `SecretSync Cloudflare Workers failed to release Worker lock [syncId=${syncId}] [scriptId=${scriptId}]`
+ );
+ });
+ }
});
diff --git a/backend/src/services/secret-sync/secret-sync-fns.ts b/backend/src/services/secret-sync/secret-sync-fns.ts
index 0614a7fc09..fe0c8ba7a6 100644
--- a/backend/src/services/secret-sync/secret-sync-fns.ts
+++ b/backend/src/services/secret-sync/secret-sync-fns.ts
@@ -7,6 +7,7 @@ import { TLicenseServiceFactory } from "@app/ee/services/license/license-service
import { CHEF_SYNC_LIST_OPTION } from "@app/ee/services/secret-sync/chef";
import { chefPreSaveTransformDestinationConfig, ChefSyncFns } from "@app/ee/services/secret-sync/chef/chef-sync-fns";
import { OCI_VAULT_SYNC_LIST_OPTION, OCIVaultSyncFns } from "@app/ee/services/secret-sync/oci-vault";
+import { TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError } from "@app/lib/errors";
import {
AWS_PARAMETER_STORE_SYNC_LIST_OPTION,
@@ -297,8 +298,9 @@ export const SecretSyncFns = {
gitHubAppDAL,
gatewayV2Service,
gatewayPoolService,
- oidcIssuerService
- }: TSyncSecretDeps
+ oidcIssuerService,
+ keyStore
+ }: TSyncSecretDeps & { keyStore: Pick<TKeyStoreFactory, "acquireLock"> }
): Promise<TSyncSecretsResult | void> => {
switch (secretSync.destination) {
case SecretSync.AWSParameterStore:
@@ -370,7 +372,7 @@ export const SecretSyncFns = {
case SecretSync.CloudflarePages:
return CloudflarePagesSyncFns.syncSecrets(secretSync, payload);
case SecretSync.CloudflareWorkers:
- return CloudflareWorkersSyncFns.syncSecrets(secretSync, payload);
+ return CloudflareWorkersSyncFns.syncSecrets(secretSync, payload, { keyStore });
case SecretSync.CloudflareSecretsStore:
return CloudflareSecretsStoreSyncFns.syncSecrets(secretSync, payload);
case SecretSync.Zabbix:
diff --git a/backend/src/services/secret-sync/secret-sync-queue.ts b/backend/src/services/secret-sync/secret-sync-queue.ts
index bc064ca516..07bda454b3 100644
--- a/backend/src/services/secret-sync/secret-sync-queue.ts
+++ b/backend/src/services/secret-sync/secret-sync-queue.ts
@@ -581,7 +581,8 @@ export const secretSyncQueueFactory = ({
kmsService,
gatewayV2Service,
gatewayPoolService,
- oidcIssuerService
+ oidcIssuerService,
+ keyStore
});
syncResult = result ?? undefined;
diff --git a/docs/integrations/secret-syncs/cloudflare-workers.mdx b/docs/integrations/secret-syncs/cloudflare-workers.mdx
index cf0f603329..dec9acd28d 100644
--- a/docs/integrations/secret-syncs/cloudflare-workers.mdx
+++ b/docs/integrations/secret-syncs/cloudflare-workers.mdx
@@ -138,8 +138,12 @@ description: "Learn how to configure a Cloudflare Workers Sync for Infisical."
If someone uploads a version of the Worker without deploying it, for example with `wrangler versions upload`, that version is pending, and Cloudflare doesn't let Infisical change the Worker's secrets directly. Instead, Infisical uploads a new version with the pending version's code and settings and the updated secrets, and labels the new version with the `infisical-sync` tag in Cloudflare.
-Infisical doesn't deploy the new version, because deploying it also deploys the pending changes. The sync's status shows the ID of the new version, and the secrets take effect when you deploy that version in Cloudflare.
+Infisical doesn't deploy the new version, because deploying it also deploys the pending changes. The sync's status shows the ID of the new version, and the secrets take effect when you [deploy that version](https://developers.cloudflare.com/workers/versions-and-deployments/) in Cloudflare.
### When Infisical can't upload a new version
If the pending version serves static assets, the sync fails without changing the Worker. To sync the secrets, deploy the pending version in Cloudflare, then sync again.
+
+### Removing secrets while a version is pending
+
+Infisical can only remove secrets while the Worker's latest version is the deployed one, so if a version is pending, removing secrets fails and nothing on the Worker changes. If you delete the sync with **Remove Synced Secrets** turned on, Infisical keeps the sync. To remove the secrets, deploy the pending version in Cloudflare, then remove the secrets again.
diff --git a/frontend/src/pages/secret-manager/SecretSyncDetailsByIDPage/components/SecretSyncDetailsSection.tsx b/frontend/src/pages/secret-manager/SecretSyncDetailsByIDPage/components/SecretSyncDetailsSection.tsx
index 7285d8069e..b6ce205d9b 100644
--- a/frontend/src/pages/secret-manager/SecretSyncDetailsByIDPage/components/SecretSyncDetailsSection.tsx
+++ b/frontend/src/pages/secret-manager/SecretSyncDetailsByIDPage/components/SecretSyncDetailsSection.tsx
@@ -67,6 +67,16 @@ export const SecretSyncDetailsSection = ({ secretSync }: Props) => {
</DetailValue>
</Detail>
)}
+ {syncStatus === SecretSyncStatus.Succeeded && lastSyncMessage && (
+ <Detail>
+ <DetailLabel className="text-info">Last Sync Note</DetailLabel>
+ <DetailValue>
+ <p className="rounded-sm bg-surface-active p-2 text-xs break-words">
+ {lastSyncMessage}
+ </p>
+ </DetailValue>
+ </Detail>
+ )}
</>
);
};