Skip to content

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

define a proper type on the secret sync dal when the folder is not fetched

adilson60326112bbef
diff --git a/backend/e2e-test/secret-sync-dal.spec.ts b/backend/e2e-test/secret-sync-dal.spec.ts
new file mode 100644
index 0000000000..ebc54aae13
--- /dev/null
+++ b/backend/e2e-test/secret-sync-dal.spec.ts
@@ -0,0 +1,146 @@
+import { randomUUID } from "node:crypto";
+
+import { createAwsAppConnection, deleteAwsAppConnection } from "e2e-test/testUtils/app-connections";
+import { createIsolatedOrgAndProject } from "e2e-test/testUtils/fixtures";
+import { createFolder, deleteFolder } from "e2e-test/testUtils/folders";
+import { createSecretSync, deleteSecretSync } from "e2e-test/testUtils/secret-syncs";
+import { Knex } from "knex";
+import { afterAll, beforeAll, describe, expect, test } from "vitest";
+
+import { secretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
+import { secretSyncDALFactory } from "@app/services/secret-sync/secret-sync-dal";
+import { SecretSyncStatus } from "@app/services/secret-sync/secret-sync-types";
+
+declare const testDb: Knex;
+
+const secretSyncDAL = secretSyncDALFactory(testDb, secretFolderDALFactory(testDb));
+
+const ENV = "dev";
+const SOURCE_PATH = "/services/api";
+
+// Secret loading reads `folder: null` as "the source folder no longer exists" and fails the sync
+// without retrying. A read that skips path resolution must therefore leave `folder` out, not null,
+// or a healthy sync passed to secret loading is failed for a folder that is still there.
+describe("Secret sync DAL folder path resolution", () => {
+ let projectId: string;
+ let authToken: string;
+ let cleanupOrg: () => Promise<void>;
+ let connectionId: string;
+ let sourceFolderId: string;
+ let syncId: string;
+ const createdSyncIds: string[] = [];
+
+ const newSync = async (name: string, secretPath: string) => {
+ const { secretSync } = await createSecretSync({
+ name: `${name}-${randomUUID().slice(0, 8)}`,
+ projectId,
+ connectionId,
+ environmentSlug: ENV,
+ secretPath,
+ region: "us-east-1",
+ destinationPath: `/${name}/`,
+ authToken
+ });
+ createdSyncIds.push(secretSync!.id);
+ return secretSync!.id;
+ };
+
+ beforeAll(async () => {
+ ({ projectId, authToken, cleanup: cleanupOrg } = await createIsolatedOrgAndProject("secret-sync-dal-e2e"));
+
+ connectionId = await createAwsAppConnection({
+ name: `secret-sync-dal-e2e-${randomUUID().slice(0, 8)}`,
+ authToken
+ });
+
+ await createFolder({ authToken, workspaceId: projectId, environmentSlug: ENV, secretPath: "/", name: "services" });
+ ({ id: sourceFolderId } = await createFolder({
+ authToken,
+ workspaceId: projectId,
+ environmentSlug: ENV,
+ secretPath: "/services",
+ name: "api"
+ }));
+
+ syncId = await newSync("folder-path", SOURCE_PATH);
+ });
+
+ afterAll(async () => {
+ // Syncs hold a foreign key to the connection, and the org delete only soft deletes the project.
+ for (const id of createdSyncIds) {
+ // eslint-disable-next-line no-await-in-loop
+ await deleteSecretSync({ syncId: id, authToken });
+ }
+
+ await deleteAwsAppConnection({ connectionId, authToken });
+ await cleanupOrg();
+ });
+
+ test("findById resolves the source folder path by default", async () => {
+ const secretSync = await secretSyncDAL.findById(syncId);
+
+ expect(secretSync?.folder).toEqual({ id: sourceFolderId, path: SOURCE_PATH });
+ });
+
+ test("findById without the folder path omits folder instead of reporting it missing", async () => {
+ const secretSync = await secretSyncDAL.findById(syncId, undefined, { includeFolderPath: false });
+
+ expect(secretSync).toBeDefined();
+ expect(secretSync).not.toHaveProperty("folder");
+ expect(secretSync?.folderId).toBe(sourceFolderId);
+ expect(secretSync?.environment?.slug).toBe(ENV);
+ });
+
+ test("updateById without the folder path omits folder and still applies the update", async () => {
+ const updated = await secretSyncDAL.updateById(
+ syncId,
+ { syncStatus: SecretSyncStatus.Failed, lastSyncMessage: "destination refused the write" },
+ { includeFolderPath: false }
+ );
+
+ expect(updated).not.toHaveProperty("folder");
+ expect(updated.folderId).toBe(sourceFolderId);
+ expect(updated.syncStatus).toBe(SecretSyncStatus.Failed);
+ expect(updated.lastSyncMessage).toBe("destination refused the write");
+
+ const reread = await secretSyncDAL.findById(syncId);
+ expect(reread?.folder).toEqual({ id: sourceFolderId, path: SOURCE_PATH });
+ });
+
+ test("find without the folder path omits folder on every row", async () => {
+ const secretSyncs = await secretSyncDAL.find({ projectId }, undefined, { includeFolderPath: false });
+
+ expect(secretSyncs.map((secretSync) => secretSync.id)).toContain(syncId);
+ secretSyncs.forEach((secretSync) => expect(secretSync).not.toHaveProperty("folder"));
+ });
+
+ test("find resolves the source folder path by default", async () => {
+ const secretSyncs = await secretSyncDAL.find({ id: syncId });
+
+ expect(secretSyncs).toHaveLength(1);
+ expect(secretSyncs[0].folder).toEqual({ id: sourceFolderId, path: SOURCE_PATH });
+ });
+
+ test("folder is null only once the source folder is gone", async () => {
+ const { id: doomedFolderId } = await createFolder({
+ authToken,
+ workspaceId: projectId,
+ environmentSlug: ENV,
+ secretPath: "/",
+ name: "doomed"
+ });
+ const doomedSyncId = await newSync("folder-gone", "/doomed");
+
+ expect((await secretSyncDAL.findById(doomedSyncId))?.folder).toEqual({ id: doomedFolderId, path: "/doomed" });
+
+ await deleteFolder({
+ authToken,
+ workspaceId: projectId,
+ environmentSlug: ENV,
+ secretPath: "/",
+ id: doomedFolderId
+ });
+
+ expect((await secretSyncDAL.findById(doomedSyncId))?.folder).toBeNull();
+ });
+});
diff --git a/backend/src/services/secret-sync/secret-sync-dal.ts b/backend/src/services/secret-sync/secret-sync-dal.ts
index 049e800329..a3d797ac94 100644
--- a/backend/src/services/secret-sync/secret-sync-dal.ts
+++ b/backend/src/services/secret-sync/secret-sync-dal.ts
@@ -11,9 +11,12 @@ export type TSecretSyncDALFactory = ReturnType<typeof secretSyncDALFactory>;
type SecretSyncFindFilter = Parameters<typeof buildFindFilter<TSecretSyncs>>[0];
-// Resolving a folder path loads every folder in the sync's environment. Callers that don't use the
-// path, or already have it, pass false; the result then has `folder: null` even when the folder exists.
-type TFolderPathOptions = { includeFolderPath?: boolean };
+// Resolving a folder path loads every folder in the sync's environment. Callers that don't need the
+// path pass includeFolderPath: false, and the row omits `folder`. `folder: null` means the path was
+// resolved and the folder is gone, which secret loading treats as "folder no longer exists".
+type TFolderPathOptions<IncludeFolderPath extends boolean = true> = {
+ includeFolderPath?: IncludeFolderPath;
+};
const baseSecretSyncQuery = ({ filter, db, tx }: { db: TDbClient; filter?: SecretSyncFindFilter; tx?: Knex }) => {
const query = (tx || db.replicaNode())(TableName.SecretSync)
@@ -116,13 +119,31 @@ const expandSecretSync = (
};
};
+export type TSecretSyncWithFolder = ReturnType<typeof expandSecretSync>;
+type TSecretSyncWithoutFolder = Omit<TSecretSyncWithFolder, "folder">;
+type TSecretSyncForFolderOption<IncludeFolderPath extends boolean> = IncludeFolderPath extends false
+ ? TSecretSyncWithoutFolder
+ : TSecretSyncWithFolder;
+
+const withoutFolder = ({ folder, ...rest }: TSecretSyncWithFolder): TSecretSyncWithoutFolder => rest;
+
+const applyFolderOption = <IncludeFolderPath extends boolean>(
+ expanded: TSecretSyncWithFolder,
+ includeFolderPath: IncludeFolderPath
+): TSecretSyncForFolderOption<IncludeFolderPath> =>
+ (includeFolderPath ? expanded : withoutFolder(expanded)) as TSecretSyncForFolderOption<IncludeFolderPath>;
+
export const secretSyncDALFactory = (
db: TDbClient,
folderDAL: Pick<TSecretFolderDALFactory, "findSecretPathByFolderIds">
) => {
const secretSyncOrm = ormify(db, TableName.SecretSync);
- const findById = async (id: string, tx?: Knex, { includeFolderPath = true }: TFolderPathOptions = {}) => {
+ const findById = async <IncludeFolderPath extends boolean = true>(
+ id: string,
+ tx?: Knex,
+ { includeFolderPath = true as IncludeFolderPath }: TFolderPathOptions<IncludeFolderPath> = {}
+ ): Promise<TSecretSyncForFolderOption<IncludeFolderPath> | undefined> => {
try {
const secretSync = await baseSecretSyncQuery({
filter: { id },
@@ -136,7 +157,7 @@ export const secretSyncDALFactory = (
includeFolderPath && secretSync.folderId
? await folderDAL.findSecretPathByFolderIds(secretSync.projectId, [secretSync.folderId])
: [];
- return expandSecretSync(secretSync, folderWithPath);
+ return applyFolderOption(expandSecretSync(secretSync, folderWithPath), includeFolderPath);
}
} catch (error) {
throw new DatabaseError({ error, name: "Find by ID - Secret Sync" });
@@ -161,11 +182,11 @@ export const secretSyncDALFactory = (
return expandSecretSync(secretSync, folderWithPath);
};
- const updateById = async (
+ const updateById = async <IncludeFolderPath extends boolean = true>(
syncId: string,
data: Parameters<(typeof secretSyncOrm)["updateById"]>[1],
- { includeFolderPath = true }: TFolderPathOptions = {}
- ) => {
+ { includeFolderPath = true as IncludeFolderPath }: TFolderPathOptions<IncludeFolderPath> = {}
+ ): Promise<TSecretSyncForFolderOption<IncludeFolderPath>> => {
const secretSync = (await secretSyncOrm.transaction(async (tx) => {
const sync = await secretSyncOrm.updateById(syncId, data, tx);
@@ -181,7 +202,7 @@ export const secretSyncDALFactory = (
includeFolderPath && secretSync.folderId
? await folderDAL.findSecretPathByFolderIds(secretSync.projectId, [secretSync.folderId])
: [];
- return expandSecretSync(secretSync, folderWithPath);
+ return applyFolderOption(expandSecretSync(secretSync, folderWithPath), includeFolderPath);
};
const findOne = async (filter: Parameters<(typeof secretSyncOrm)["findOne"]>[0], tx?: Knex) => {
@@ -200,32 +221,38 @@ export const secretSyncDALFactory = (
}
};
- const find = async (
+ const find = async <IncludeFolderPath extends boolean = true>(
filter: Parameters<(typeof secretSyncOrm)["find"]>[0],
tx?: Knex,
- { includeFolderPath = true }: TFolderPathOptions = {}
- ) => {
+ { includeFolderPath = true as IncludeFolderPath }: TFolderPathOptions<IncludeFolderPath> = {}
+ ): Promise<TSecretSyncForFolderOption<IncludeFolderPath>[]> => {
try {
const secretSyncs = await baseSecretSyncQuery({ filter, db, tx });
if (!secretSyncs.length) return [];
- const foldersWithPath = includeFolderPath
- ? await folderDAL.findSecretPathByFolderIds(
- secretSyncs[0].projectId,
- secretSyncs.filter((sync) => Boolean(sync.folderId)).map((sync) => sync.folderId!)
- )
- : [];
+ const folderRecord: Record<
+ string,
+ Awaited<ReturnType<TSecretFolderDALFactory["findSecretPathByFolderIds"]>>[number]
+ > = {};
- // TODO (scott): replace with cached folder path once implemented
- const folderRecord: Record<string, (typeof foldersWithPath)[number]> = {};
+ if (includeFolderPath) {
+ const foldersWithPath = await folderDAL.findSecretPathByFolderIds(
+ secretSyncs[0].projectId,
+ secretSyncs.filter((sync) => Boolean(sync.folderId)).map((sync) => sync.folderId!)
+ );
- foldersWithPath.forEach((folder) => {
- if (folder) folderRecord[folder.id] = folder;
- });
+ // TODO (scott): replace with cached folder path once implemented
+ foldersWithPath.forEach((folder) => {
+ if (folder) folderRecord[folder.id] = folder;
+ });
+ }
return secretSyncs.map((secretSync) =>
- expandSecretSync(secretSync, secretSync.folderId ? folderRecord[secretSync.folderId] : undefined)
+ applyFolderOption(
+ expandSecretSync(secretSync, secretSync.folderId ? folderRecord[secretSync.folderId] : undefined),
+ includeFolderPath
+ )
);
} catch (error) {
throw new DatabaseError({ error, name: "Find - Secret Sync" });
diff --git a/backend/src/services/secret-sync/secret-sync-types.ts b/backend/src/services/secret-sync/secret-sync-types.ts
index e119a11029..91fd10269e 100644
--- a/backend/src/services/secret-sync/secret-sync-types.ts
+++ b/backend/src/services/secret-sync/secret-sync-types.ts
@@ -39,7 +39,7 @@ import {
TGitHubSyncListItem,
TGitHubSyncWithCredentials
} from "@app/services/secret-sync/github";
-import { TSecretSyncDALFactory } from "@app/services/secret-sync/secret-sync-dal";
+import { TSecretSyncWithFolder } from "@app/services/secret-sync/secret-sync-dal";
import { SecretSync, SecretSyncImportBehavior } from "@app/services/secret-sync/secret-sync-enums";
import {
TWindmillSync,
@@ -555,7 +555,9 @@ export type TSyncSecretsResult = {
deletedSecretKeys: string[];
};
-export type TSecretSyncRaw = NonNullable<Awaited<ReturnType<TSecretSyncDALFactory["findById"]>>>;
+// ReturnType<> resolves findById's includeFolderPath generic to boolean, a union whose members may
+// lack `folder`, so the default row (path resolved) is named here directly.
+export type TSecretSyncRaw = TSecretSyncWithFolder;
export type TQueueSecretSyncsByPathDTO = {
secretPath: string;