Skip to content

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

feat(secret-value-tracking): add the pure walk logic

Calvin6056d2373173
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts
new file mode 100644
index 0000000000..3b2e4d588c
--- /dev/null
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.test.ts
@@ -0,0 +1,151 @@
+import { advanceCursor, needsBackfill } from "./secret-value-tracking-fns";
+
+const PROJECTS = ["p1", "p2"];
+const FOLDERS = { p1: ["f1", "f2"], p2: ["f3"] };
+
+describe("advanceCursor", () => {
+ test("a null cursor starts at the first folder of the first project", () => {
+ expect(
+ advanceCursor({
+ cursor: null,
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: false
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p1", folderId: "f1", key: "", id: "" }, completedProjectId: null });
+ });
+
+ test("a partly read folder advances within itself", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "f1", key: "A", id: "s1" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: { key: "B", id: "s2" },
+ folderExhausted: false
+ })
+ ).toEqual({
+ done: false,
+ cursor: { projectId: "p1", folderId: "f1", key: "B", id: "s2" },
+ completedProjectId: null
+ });
+ });
+
+ test("an exhausted folder moves to the next folder of the same project", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "f1", key: "Z", id: "s9" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: true
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p1", folderId: "f2", key: "", id: "" }, completedProjectId: null });
+ });
+
+ test("the last folder of a project reports the project complete and moves to the next project", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "f2", key: "Z", id: "s9" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: true
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p2", folderId: "f3", key: "", id: "" }, completedProjectId: "p1" });
+ });
+
+ test("the last folder of the last project ends the walk", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p2", folderId: "f3", key: "Z", id: "s9" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: true
+ })
+ ).toEqual({ done: true });
+ });
+
+ // An empty folder must not leave the cursor where it was, or the walk spins on it forever.
+ test("an empty folder advances rather than repeating itself", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "f1", key: "", id: "" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: true
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p1", folderId: "f2", key: "", id: "" }, completedProjectId: null });
+ });
+
+ test("a cursor on a deleted folder resumes at the next folder of that project", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "gone", key: "M", id: "s5" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: false
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p1", folderId: "f1", key: "", id: "" }, completedProjectId: null });
+ });
+
+ test("a cursor on a deleted project resumes at the next project", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "gone", folderId: "gone", key: "M", id: "s5" },
+ projectIds: PROJECTS,
+ folderIdsByProject: FOLDERS,
+ lastRow: null,
+ folderExhausted: false
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p1", folderId: "f1", key: "", id: "" }, completedProjectId: null });
+ });
+
+ test("a project with no folders is skipped", () => {
+ expect(
+ advanceCursor({
+ cursor: { projectId: "p1", folderId: "f2", key: "Z", id: "s9" },
+ projectIds: ["p1", "empty", "p2"],
+ folderIdsByProject: { ...FOLDERS, empty: [] },
+ lastRow: null,
+ folderExhausted: true
+ })
+ ).toEqual({ done: false, cursor: { projectId: "p2", folderId: "f3", key: "", id: "" }, completedProjectId: "p1" });
+ });
+
+ test("an organization with no projects is immediately done", () => {
+ expect(
+ advanceCursor({ cursor: null, projectIds: [], folderIdsByProject: {}, lastRow: null, folderExhausted: false })
+ ).toEqual({ done: true });
+ });
+});
+
+describe("needsBackfill", () => {
+ test("a row missing the org digest needs work", () => {
+ expect(
+ needsBackfill({ secretValueBlindIndex: "abc", secretValueOrgBlindIndex: null, encryptedValue: Buffer.from("x") })
+ ).toBe(true);
+ });
+
+ test("a row missing the project digest needs work", () => {
+ expect(
+ needsBackfill({ secretValueBlindIndex: null, secretValueOrgBlindIndex: "abc", encryptedValue: Buffer.from("x") })
+ ).toBe(true);
+ });
+
+ test("a row with both digests is skipped", () => {
+ expect(
+ needsBackfill({ secretValueBlindIndex: "a", secretValueOrgBlindIndex: "b", encryptedValue: Buffer.from("x") })
+ ).toBe(false);
+ });
+
+ test("a row with no encrypted value is skipped even when digests are missing", () => {
+ expect(needsBackfill({ secretValueBlindIndex: null, secretValueOrgBlindIndex: null, encryptedValue: null })).toBe(
+ false
+ );
+ });
+});
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts
new file mode 100644
index 0000000000..c3d7aab93e
--- /dev/null
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-fns.ts
@@ -0,0 +1,83 @@
+import { TAdvanceCursorInput, TAdvanceCursorResult, TBackfillCursor } from "./secret-value-tracking-types";
+
+const startOfFolder = (projectId: string, folderId: string): TBackfillCursor => ({
+ projectId,
+ folderId,
+ key: "",
+ id: ""
+});
+
+// Resolves to the first project from `fromIndex` that still has folders, so a project deleted or
+// emptied between chunks is stepped over rather than stalling the walk on something that is no
+// longer there.
+const firstProjectWithFolders = (
+ projectIds: string[],
+ folderIdsByProject: Record<string, string[]>,
+ fromIndex: number
+) => {
+ for (let i = fromIndex; i < projectIds.length; i += 1) {
+ const projectId = projectIds[i];
+ const folderIds = folderIdsByProject[projectId] ?? [];
+ if (folderIds.length) return { projectId, folderId: folderIds[0] };
+ }
+ return null;
+};
+
+export const advanceCursor = ({
+ cursor,
+ projectIds,
+ folderIdsByProject,
+ lastRow,
+ folderExhausted
+}: TAdvanceCursorInput): TAdvanceCursorResult => {
+ if (!cursor) {
+ const next = firstProjectWithFolders(projectIds, folderIdsByProject, 0);
+ if (!next) return { done: true };
+ return { done: false, cursor: startOfFolder(next.projectId, next.folderId), completedProjectId: null };
+ }
+
+ const projectIndex = projectIds.indexOf(cursor.projectId);
+ if (projectIndex === -1) {
+ const next = firstProjectWithFolders(projectIds, folderIdsByProject, 0);
+ if (!next) return { done: true };
+ return { done: false, cursor: startOfFolder(next.projectId, next.folderId), completedProjectId: null };
+ }
+
+ const folderIds = folderIdsByProject[cursor.projectId] ?? [];
+ const folderIndex = folderIds.indexOf(cursor.folderId);
+
+ if (folderIndex === -1) {
+ if (folderIds.length) {
+ return { done: false, cursor: startOfFolder(cursor.projectId, folderIds[0]), completedProjectId: null };
+ }
+ const next = firstProjectWithFolders(projectIds, folderIdsByProject, projectIndex + 1);
+ if (!next) return { done: true };
+ return { done: false, cursor: startOfFolder(next.projectId, next.folderId), completedProjectId: null };
+ }
+
+ if (!folderExhausted && lastRow) {
+ return {
+ done: false,
+ cursor: { projectId: cursor.projectId, folderId: cursor.folderId, key: lastRow.key, id: lastRow.id },
+ completedProjectId: null
+ };
+ }
+
+ if (folderIndex + 1 < folderIds.length) {
+ return {
+ done: false,
+ cursor: startOfFolder(cursor.projectId, folderIds[folderIndex + 1]),
+ completedProjectId: null
+ };
+ }
+
+ const next = firstProjectWithFolders(projectIds, folderIdsByProject, projectIndex + 1);
+ if (!next) return { done: true };
+ return { done: false, cursor: startOfFolder(next.projectId, next.folderId), completedProjectId: cursor.projectId };
+};
+
+export const needsBackfill = (row: {
+ secretValueBlindIndex?: string | null;
+ secretValueOrgBlindIndex?: string | null;
+ encryptedValue?: Buffer | null;
+}) => Boolean(row.encryptedValue) && (!row.secretValueBlindIndex || !row.secretValueOrgBlindIndex);
diff --git a/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts b/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts
new file mode 100644
index 0000000000..a1871c7446
--- /dev/null
+++ b/backend/src/services/secret-value-tracking/secret-value-tracking-types.ts
@@ -0,0 +1,32 @@
+export type TBackfillCursor = {
+ projectId: string;
+ folderId: string;
+ key: string;
+ id: string;
+};
+
+export type TBackfillScope = { scope: "org"; orgId: string } | { scope: "project"; projectId: string };
+
+export type TBackfillRunState = {
+ status: "running" | "failed";
+ cursor: TBackfillCursor | null;
+ projectsTotal: number;
+ projectsDone: number;
+ secretsProcessed: number;
+ lastProgressAt: string;
+ error?: string;
+};
+
+export type TAdvanceCursorInput = {
+ cursor: TBackfillCursor | null;
+ projectIds: string[];
+ folderIdsByProject: Record<string, string[]>;
+ lastRow: { key: string; id: string } | null;
+ folderExhausted: boolean;
+};
+
+// `completedProjectId` is how the caller learns the walk just left a project behind, which is the
+// moment that project's own blind index flag can be set.
+export type TAdvanceCursorResult =
+ | { done: true }
+ | { done: false; cursor: TBackfillCursor; completedProjectId: string | null };