Skip to content

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

feat(secret-sync): resolve recursive sync folders and cap secret count

Calvin7b195bf23139
diff --git a/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts b/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts
new file mode 100644
index 0000000000..69762a8971
--- /dev/null
+++ b/backend/src/services/secret-sync/secret-sync-recursive-fns.test.ts
@@ -0,0 +1,51 @@
+import { assertWithinSecretLimit, resolveSyncFolders, SECRET_SYNC_MAX_SECRETS } from "./secret-sync-recursive-fns";
+
+const deps = {
+ folderDAL: {
+ find: async () => [
+ { id: "root", name: "root", parentId: null, envId: "env-1", isReserved: false },
+ { id: "api", name: "api", parentId: "root", envId: "env-1", isReserved: false }
+ ]
+ },
+ projectEnvDAL: { findOne: async () => ({ id: "env-1", slug: "dev", projectId: "proj-1" }) }
+} as unknown as Pick<Parameters<typeof resolveSyncFolders>[0], "folderDAL" | "projectEnvDAL">;
+
+describe("resolveSyncFolders", () => {
+ test("returns only the source folder when recursive is off", async () => {
+ const folders = await resolveSyncFolders({
+ ...deps,
+ projectId: "proj-1",
+ environment: "dev",
+ sourcePath: "/",
+ sourceFolderId: "root",
+ recursive: false
+ });
+
+ expect(folders).toEqual([{ folderId: "root", path: "/" }]);
+ });
+
+ test("returns the source folder and its descendants when recursive is on", async () => {
+ const folders = await resolveSyncFolders({
+ ...deps,
+ projectId: "proj-1",
+ environment: "dev",
+ sourcePath: "/",
+ sourceFolderId: "root",
+ recursive: true
+ });
+
+ expect(folders.map((folder) => folder.path).sort()).toEqual(["/", "/api"]);
+ });
+});
+
+describe("assertWithinSecretLimit", () => {
+ test("accepts a count at the limit", () => {
+ expect(() => assertWithinSecretLimit(SECRET_SYNC_MAX_SECRETS)).not.toThrow();
+ });
+
+ test("rejects a count above the limit with a message naming both numbers", () => {
+ expect(() => assertWithinSecretLimit(SECRET_SYNC_MAX_SECRETS + 1)).toThrow(
+ new RegExp(`${SECRET_SYNC_MAX_SECRETS + 1}.*${SECRET_SYNC_MAX_SECRETS}`)
+ );
+ });
+});
diff --git a/backend/src/services/secret-sync/secret-sync-recursive-fns.ts b/backend/src/services/secret-sync/secret-sync-recursive-fns.ts
new file mode 100644
index 0000000000..49a66a84dd
--- /dev/null
+++ b/backend/src/services/secret-sync/secret-sync-recursive-fns.ts
@@ -0,0 +1,52 @@
+import { TProjectEnvDALFactory } from "@app/services/project-env/project-env-dal";
+import { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
+import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
+import { recursivelyGetSecretPaths } from "@app/services/secret-v2-bridge/secret-v2-bridge-fns";
+
+export const SECRET_SYNC_MAX_SECRETS = 10_000;
+
+export const assertWithinSecretLimit = (count: number) => {
+ if (count <= SECRET_SYNC_MAX_SECRETS) return;
+
+ throw new SecretSyncError({
+ message: `This sync covers ${count} secrets, which is above the limit of ${SECRET_SYNC_MAX_SECRETS} for a single sync. Point the sync at a narrower secret path, or split it into several syncs.`,
+ shouldRetry: false
+ });
+};
+
+export const resolveSyncFolders = async ({
+ folderDAL,
+ projectEnvDAL,
+ projectId,
+ environment,
+ sourcePath,
+ sourceFolderId,
+ recursive
+}: {
+ folderDAL: Pick<TSecretFolderDALFactory, "find">;
+ projectEnvDAL: Pick<TProjectEnvDALFactory, "findOne">;
+ projectId: string;
+ environment: string;
+ sourcePath: string;
+ sourceFolderId: string;
+ recursive: boolean;
+}): Promise<{ folderId: string; path: string }[]> => {
+ if (!recursive) return [{ folderId: sourceFolderId, path: sourcePath }];
+
+ const paths = await recursivelyGetSecretPaths({
+ folderDAL,
+ projectEnvDAL,
+ projectId,
+ environment,
+ currentPath: sourcePath
+ });
+
+ if (!paths.length) {
+ throw new SecretSyncError({
+ message: `Could not find the folder "${sourcePath}" in environment "${environment}". Update the sync's source environment and secret path.`,
+ shouldRetry: false
+ });
+ }
+
+ return paths.map(({ folderId, path }) => ({ folderId, path }));
+};