Skip to content

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

fix(secrets): keep viewed environment when batch-creating in multiple environments

Claudedf5b77b3219a
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.test.ts b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.test.ts
new file mode 100644
index 0000000000..bdd2562188
--- /dev/null
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.test.ts
@@ -0,0 +1,60 @@
+import assert from "node:assert/strict";
+import { describe, it } from "vitest";
+import { createStore } from "zustand";
+
+import { PendingAction } from "@app/hooks/api/secretFolders/types";
+
+import {
+ BatchContext,
+ BatchModeState,
+ createBatchModeStore,
+ PendingSecretCreate
+} from "./SecretMainPage.store";
+
+const createTestStore = () =>
+ createStore<BatchModeState>()((...args) =>
+ createBatchModeStore(...(args as unknown as Parameters<typeof createBatchModeStore>))
+ );
+
+const contextFor = (environment: string): BatchContext => ({
+ projectId: "project-1",
+ environment,
+ secretPath: "/test"
+});
+
+const makeCreate = (id: string): PendingSecretCreate => ({
+ id,
+ resourceType: "secret",
+ type: PendingAction.Create,
+ secretKey: "NEW_SECRET",
+ secretValue: "value",
+ timestamp: 1
+});
+
+describe("batch mode store", () => {
+ it("keeps the viewed environment when staging a create across several environments", () => {
+ const store = createTestStore();
+ const { batchActions } = store.getState();
+
+ batchActions.loadPendingChanges(contextFor("prod"));
+ ["prod", "dev", "staging"].forEach((env) =>
+ batchActions.addPendingChange(makeCreate(`create-${env}`), contextFor(env))
+ );
+
+ const state = store.getState();
+ assert.equal(state.currentContext?.environment, "prod");
+ assert.deepEqual(
+ state.pendingChanges.secrets.map((s) => s.id),
+ ["create-prod"]
+ );
+
+ batchActions.clearAllPendingChanges(contextFor("prod"));
+ assert.equal(store.getState().pendingChanges.secrets.length, 0);
+
+ batchActions.loadPendingChanges(contextFor("dev"));
+ assert.deepEqual(
+ store.getState().pendingChanges.secrets.map((s) => s.id),
+ ["create-dev"]
+ );
+ });
+});
diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
index f3d13a7862..5ef6cc47e0 100644
--- a/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/SecretMainPage.store.tsx
@@ -168,7 +168,7 @@ const createSelectedSecretStore: StateCreator<CombinedState, [], [], SelectedSec
}
});
-type BatchModeState = {
+export type BatchModeState = {
isBatchMode: boolean;
pendingChanges: PendingChanges;
pendingChangesByContext: Map<string, PendingChanges>;
@@ -194,7 +194,10 @@ const generateContextKey = (projectId: string, environment: string, secretPath:
return `${projectId}_${environment}_${secretPath}`;
};
-const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState> = (set, get) => ({
+export const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState> = (
+ set,
+ get
+) => ({
isBatchMode: true, // Always enabled by default
pendingChanges: { secrets: [], folders: [] },
pendingChangesByContext: new Map<string, PendingChanges>(),
@@ -227,7 +230,7 @@ const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState>
text: "A folder with this name already exists",
type: "error"
});
- return { pendingChanges: newChanges };
+ return state;
}
if (
change.type === PendingAction.Update &&
@@ -238,7 +241,7 @@ const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState>
text: "A folder with this name already exists",
type: "error"
});
- return { pendingChanges: newChanges };
+ return state;
}
}
@@ -269,7 +272,7 @@ const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState>
text: "A secret with this name already exists",
type: "error"
});
- return { pendingChanges: newChanges };
+ return state;
}
const existingNewSecretName =
@@ -287,7 +290,7 @@ const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState>
text: "A secret with this name already exists",
type: "error"
});
- return { pendingChanges: newChanges };
+ return state;
}
}
@@ -378,20 +381,21 @@ const createBatchModeStore: StateCreator<CombinedState, [], [], BatchModeState>
const updatedContextMap = new Map(state.pendingChangesByContext);
updatedContextMap.set(contextKey, newChanges);
- const currentChanges =
+ // A change can target another environment (e.g. creating a secret in several
+ // environments at once), so it must not move the viewed context away from the page.
+ const currentContext = state.currentContext ?? context;
+ const isCurrentContext =
contextKey ===
generateContextKey(
- state.currentContext?.projectId || context.projectId,
- state.currentContext?.environment || context.environment,
- state.currentContext?.secretPath || context.secretPath
- )
- ? newChanges
- : state.pendingChanges;
+ currentContext.projectId,
+ currentContext.environment,
+ currentContext.secretPath
+ );
return {
pendingChangesByContext: updatedContextMap,
- pendingChanges: currentChanges,
- currentContext: context
+ pendingChanges: isCurrentContext ? newChanges : state.pendingChanges,
+ currentContext
};
}),