Skip to content

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

refactor(secret-sync): tidy the move warning types and component

Calvin10b4df69919e
diff --git a/frontend/src/components/secret-syncs/SecretSyncMoveWarning.tsx b/frontend/src/components/secret-syncs/SecretSyncMoveWarning.tsx
index 5502702b21..65331b4bea 100644
--- a/frontend/src/components/secret-syncs/SecretSyncMoveWarning.tsx
+++ b/frontend/src/components/secret-syncs/SecretSyncMoveWarning.tsx
@@ -6,7 +6,7 @@ import { Alert, AlertDescription, AlertTitle, Checkbox, Label } from "@app/compo
import { ROUTE_PATHS } from "@app/const/routes";
import { ProjectPermissionSub, useOrganization, useProjectPermission } from "@app/context";
import { ProjectPermissionSecretSyncActions } from "@app/context/ProjectPermissionContext/types";
-import { TMoveWarningsCheck } from "@app/helpers/secretSyncCoverage";
+import { TItemMove } from "@app/helpers/secretSyncCoverage";
import { SECRET_SYNC_MAP } from "@app/helpers/secretSyncs";
import { TSecretSync, useListSecretSyncsCoveringMove } from "@app/hooks/api/secretSyncs";
@@ -38,7 +38,7 @@ const useAcknowledgement = (warningKey: string) => {
// like the synced indicator on the dashboard, syncs the user cannot read are left out entirely
export const useSecretSyncMoveWarning = (
projectId: string,
- checks: TMoveWarningsCheck[]
+ moves: TItemMove[]
): TSecretSyncMoveWarning => {
const { permission } = useProjectPermission();
const canReadSecretSyncs = permission.can(
@@ -50,8 +50,8 @@ export const useSecretSyncMoveWarning = (
data: fetchedSecretSyncs = [],
isLoading: isChecking,
isError
- } = useListSecretSyncsCoveringMove(projectId, checks, {
- enabled: canReadSecretSyncs && checks.length > 0
+ } = useListSecretSyncsCoveringMove(projectId, moves, {
+ enabled: canReadSecretSyncs && moves.length > 0
});
// a disabled query keeps its last result, so a user whose read access is revoked mid-dialog must not
@@ -60,7 +60,7 @@ export const useSecretSyncMoveWarning = (
const hasError = canReadSecretSyncs && isError;
const needsAcknowledgement = hasError || secretSyncs.length > 0;
const { isAcknowledged, setIsAcknowledged } = useAcknowledgement(
- JSON.stringify({ checks, syncIds: secretSyncs.map(({ id }) => id), hasError })
+ JSON.stringify({ moves, syncIds: secretSyncs.map(({ id }) => id), hasError })
);
return {
@@ -82,20 +82,59 @@ type Props = {
};
const getTitle = ({
- subject,
+ noun,
+ verb,
count,
hasError
}: {
- subject: string;
+ noun: string;
+ verb: Props["verb"];
count: number;
hasError: boolean;
}) => {
if (hasError) return "Could not check the destination for secret syncs";
+
+ const subject = `${noun.charAt(0).toUpperCase()}${noun.slice(1)} ${verb}`;
return `${subject} here will be synced to ${count} external destination${count === 1 ? "" : "s"}`;
};
-export const SecretSyncMoveWarning = ({ warning, projectId, noun, verb }: Props) => {
+const SecretSyncMoveWarningItem = ({
+ sync,
+ projectId
+}: {
+ sync: TSecretSync;
+ projectId: string;
+}) => {
const { currentOrg } = useOrganization();
+
+ return (
+ <li>
+ <span className="font-medium text-foreground">{sync.name}</span>
+ {` (${SECRET_SYNC_MAP[sync.destination].name}) syncs `}
+ <code>{sync.folder?.path ?? "/"}</code>
+ {sync.syncOptions.includeAllSubFolders ? " and all its subfolders." : "."}
+ {!sync.isAutoSyncEnabled &&
+ " Auto-sync is off, so it sends them on its next manual sync."}{" "}
+ <Link
+ to={ROUTE_PATHS.SecretManager.SecretSyncDetailsByIDPage.path}
+ params={{
+ orgId: currentOrg.id,
+ projectId,
+ destination: sync.destination,
+ syncId: sync.id
+ }}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="inline-flex items-center gap-1 underline underline-offset-2"
+ >
+ View Sync
+ <ExternalLinkIcon className="size-3" />
+ </Link>
+ </li>
+ );
+};
+
+export const SecretSyncMoveWarning = ({ warning, projectId, noun, verb }: Props) => {
const { needsAcknowledgement, isChecking, hasError, secretSyncs } = warning;
if (isChecking || !needsAcknowledgement) return null;
@@ -103,41 +142,13 @@ export const SecretSyncMoveWarning = ({ warning, projectId, noun, verb }: Props)
return (
<Alert variant="warning">
<TriangleAlertIcon />
- <AlertTitle>
- {getTitle({
- subject: `${noun.charAt(0).toUpperCase()}${noun.slice(1)} ${verb}`,
- count: secretSyncs.length,
- hasError
- })}
- </AlertTitle>
+ <AlertTitle>{getTitle({ noun, verb, count: secretSyncs.length, hasError })}</AlertTitle>
<AlertDescription>
{hasError && <p>Secret syncs may send these {noun} to external destinations.</p>}
{secretSyncs.length > 0 && (
<ul className="max-h-40 list-disc overflow-y-auto pl-4">
{secretSyncs.map((sync) => (
- <li key={sync.id}>
- <span className="font-medium text-foreground">{sync.name}</span>
- {` (${SECRET_SYNC_MAP[sync.destination].name}) syncs `}
- <code>{sync.folder?.path ?? "/"}</code>
- {sync.syncOptions.includeAllSubFolders ? " and all its subfolders." : "."}
- {!sync.isAutoSyncEnabled &&
- " Auto-sync is off, so it sends them on its next manual sync."}{" "}
- <Link
- to={ROUTE_PATHS.SecretManager.SecretSyncDetailsByIDPage.path}
- params={{
- orgId: currentOrg.id,
- projectId,
- destination: sync.destination,
- syncId: sync.id
- }}
- target="_blank"
- rel="noopener noreferrer"
- className="inline-flex items-center gap-1 underline underline-offset-2"
- >
- View Sync
- <ExternalLinkIcon className="size-3" />
- </Link>
- </li>
+ <SecretSyncMoveWarningItem key={sync.id} sync={sync} projectId={projectId} />
))}
</ul>
)}
diff --git a/frontend/src/helpers/secretSyncCoverage.test.ts b/frontend/src/helpers/secretSyncCoverage.test.ts
index 1f2f3777d0..17c00afa90 100644
--- a/frontend/src/helpers/secretSyncCoverage.test.ts
+++ b/frontend/src/helpers/secretSyncCoverage.test.ts
@@ -64,11 +64,9 @@ describe("getSecretSyncsNewlyCoveringPaths", () => {
...overrides
});
- const check = (sourceSecretPath: string, destinationSecretPath: string, environment = "dev") => ({
- sourceEnvironment: environment,
- sourceSecretPath,
- destinationEnvironment: environment,
- destinationSecretPath
+ const move = (sourceSecretPath: string, destinationSecretPath: string, environment = "dev") => ({
+ source: { environment, secretPath: sourceSecretPath },
+ destination: { environment, secretPath: destinationSecretPath }
});
const ids = (syncs: { id: string }[]) => syncs.map(({ id }) => id);
@@ -76,34 +74,34 @@ describe("getSecretSyncsNewlyCoveringPaths", () => {
test("leaves out a sync that already covers the source", () => {
const syncs = [sync("shared", { folder: { path: "/" } }), sync("new")];
- expect(ids(getSecretSyncsNewlyCoveringPaths(syncs, [check("/other", "/apps")]))).toEqual([
+ expect(ids(getSecretSyncsNewlyCoveringPaths(syncs, [move("/other", "/apps")]))).toEqual([
"new"
]);
});
- test("returns nothing when no check lands under a sync", () => {
- expect(getSecretSyncsNewlyCoveringPaths([sync("a")], [check("/", "/other")])).toEqual([]);
+ test("returns nothing when no move lands under a sync", () => {
+ expect(getSecretSyncsNewlyCoveringPaths([sync("a")], [move("/", "/other")])).toEqual([]);
expect(getSecretSyncsNewlyCoveringPaths([sync("a")], [])).toEqual([]);
});
test("leaves out a sync rooted beneath the landing path", () => {
const syncs = [sync("nested", { folder: { path: "/apps/other" } })];
- expect(getSecretSyncsNewlyCoveringPaths(syncs, [check("/x", "/apps")])).toEqual([]);
- expect(getSecretSyncsNewlyCoveringPaths(syncs, [check("/x", "/")])).toEqual([]);
+ expect(getSecretSyncsNewlyCoveringPaths(syncs, [move("/x", "/apps")])).toEqual([]);
+ expect(getSecretSyncsNewlyCoveringPaths(syncs, [move("/x", "/")])).toEqual([]);
});
- test("returns a sync once even when several checks land under it", () => {
- const checks = [check("/x", "/apps"), { ...check("/y", "/apps"), sourceEnvironment: "prod" }];
+ test("returns a sync once even when several moves land under it", () => {
+ const moves = [move("/x", "/apps"), move("/y", "/apps")];
- expect(ids(getSecretSyncsNewlyCoveringPaths([sync("a")], checks))).toEqual(["a"]);
+ expect(ids(getSecretSyncsNewlyCoveringPaths([sync("a")], moves))).toEqual(["a"]);
});
test("orders syncs by name", () => {
const syncs = [sync("b", { name: "zeta" }), sync("a", { name: "alpha" })];
expect(
- getSecretSyncsNewlyCoveringPaths(syncs, [check("/other", "/apps")]).map(({ name }) => name)
+ getSecretSyncsNewlyCoveringPaths(syncs, [move("/other", "/apps")]).map(({ name }) => name)
).toEqual(["alpha", "zeta"]);
});
});
diff --git a/frontend/src/helpers/secretSyncCoverage.ts b/frontend/src/helpers/secretSyncCoverage.ts
index 1679f6d4de..fb01e42d0d 100644
--- a/frontend/src/helpers/secretSyncCoverage.ts
+++ b/frontend/src/helpers/secretSyncCoverage.ts
@@ -8,12 +8,10 @@ type TCoverageSync = {
syncOptions?: { includeAllSubFolders?: boolean } | null;
};
-export type TMoveWarningsCheck = {
- sourceEnvironment: string;
- sourceSecretPath: string;
- destinationEnvironment: string;
- destinationSecretPath: string;
-};
+export type TPathRef = { environment: string; secretPath: string };
+
+// one item moved or copied from its source path to the path it lands at
+export type TItemMove = { source: TPathRef; destination: TPathRef };
const toPathSegments = (path: string) => path.split("/").filter(Boolean);
@@ -22,7 +20,7 @@ const toPathSegments = (path: string) => path.split("/").filter(Boolean);
// moved has no folder at its landing path, and only the recursive syncs above it can cover it there.
export const isPathCoveredBySecretSync = (
sync: TCoverageSync,
- { environment, secretPath }: { environment: string; secretPath: string }
+ { environment, secretPath }: TPathRef
) => {
if (!sync.folder || sync.environment?.slug !== environment) return false;
@@ -37,27 +35,20 @@ export const isPathCoveredBySecretSync = (
);
};
-// The syncs that would start sending items to their destination if they were moved or copied as the checks
-// describe, one check per path the items land at. A sync that already covers a check's source is left out
-// for that check, since the items already reach it.
+// The syncs that would start sending items to their destination if they were moved or copied as described.
+// A sync that already covers a move's source is left out for that move, since the items already reach it.
export const getSecretSyncsNewlyCoveringPaths = <
T extends TCoverageSync & { id: string; name: string }
>(
syncs: T[],
- checks: TMoveWarningsCheck[]
+ moves: TItemMove[]
) =>
syncs
.filter((sync) =>
- checks.some(
- (check) =>
- isPathCoveredBySecretSync(sync, {
- environment: check.destinationEnvironment,
- secretPath: check.destinationSecretPath
- }) &&
- !isPathCoveredBySecretSync(sync, {
- environment: check.sourceEnvironment,
- secretPath: check.sourceSecretPath
- })
+ moves.some(
+ (move) =>
+ isPathCoveredBySecretSync(sync, move.destination) &&
+ !isPathCoveredBySecretSync(sync, move.source)
)
)
.sort((a, b) => a.name.localeCompare(b.name));
diff --git a/frontend/src/hooks/api/secretSyncs/queries.tsx b/frontend/src/hooks/api/secretSyncs/queries.tsx
index 177289bd47..3baa580010 100644
--- a/frontend/src/hooks/api/secretSyncs/queries.tsx
+++ b/frontend/src/hooks/api/secretSyncs/queries.tsx
@@ -1,10 +1,7 @@
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
-import {
- getSecretSyncsNewlyCoveringPaths,
- TMoveWarningsCheck
-} from "@app/helpers/secretSyncCoverage";
+import { getSecretSyncsNewlyCoveringPaths, TItemMove } from "@app/helpers/secretSyncCoverage";
import { SecretSync, TSecretSyncOption } from "@app/hooks/api/secretSyncs";
import {
TListSecretSyncOptions,
@@ -49,8 +46,8 @@ export const secretSyncKeys = {
secretPath,
keySchema
] as const,
- coveringMove: (projectId: string, checks: TMoveWarningsCheck[]) =>
- [...secretSyncKeys.all, "covering-move", projectId, checks] as const
+ coveringMove: (projectId: string, moves: TItemMove[]) =>
+ [...secretSyncKeys.all, "covering-move", projectId, moves] as const
};
export type TSecretSyncRecursiveConflict = {
@@ -109,17 +106,17 @@ export const useListSecretSyncs = (
});
};
-// keyed on the checks so a new destination always fetches a fresh list, never a cached one that
+// keyed on the moves so a new destination always fetches a fresh list, never a cached one that
// predates a sync created since
export const useListSecretSyncsCoveringMove = (
projectId: string,
- checks: TMoveWarningsCheck[],
+ moves: TItemMove[],
{ enabled }: { enabled: boolean }
) =>
useQuery({
- queryKey: secretSyncKeys.coveringMove(projectId, checks),
+ queryKey: secretSyncKeys.coveringMove(projectId, moves),
queryFn: () => fetchSecretSyncsByProjectId(projectId),
- select: (secretSyncs) => getSecretSyncsNewlyCoveringPaths(secretSyncs, checks),
+ select: (secretSyncs) => getSecretSyncsNewlyCoveringPaths(secretSyncs, moves),
enabled,
staleTime: 0,
gcTime: 0
diff --git a/frontend/src/pages/secret-manager/OverviewPage/components/CopySecretsSheet/CopySecretsSheet.tsx b/frontend/src/pages/secret-manager/OverviewPage/components/CopySecretsSheet/CopySecretsSheet.tsx
index b5d13d4ff5..17d2cb2173 100644
--- a/frontend/src/pages/secret-manager/OverviewPage/components/CopySecretsSheet/CopySecretsSheet.tsx
+++ b/frontend/src/pages/secret-manager/OverviewPage/components/CopySecretsSheet/CopySecretsSheet.tsx
@@ -204,10 +204,11 @@ const CopySecretsSession = ({
isSourcePathSettled &&
isDestinationPathSettled
? requestGroups.map((group) => ({
- sourceEnvironment: sourceEnvironmentSlug,
- sourceSecretPath: group.sourcePath,
- destinationEnvironment: destinationEnvironmentSlug,
- destinationSecretPath: group.destinationPath
+ source: { environment: sourceEnvironmentSlug, secretPath: group.sourcePath },
+ destination: {
+ environment: destinationEnvironmentSlug,
+ secretPath: group.destinationPath
+ }
}))
: []
);
diff --git a/frontend/src/pages/secret-manager/OverviewPage/components/SelectionPanel/components/MoveSecretsDialog/MoveSecretsDialog.tsx b/frontend/src/pages/secret-manager/OverviewPage/components/SelectionPanel/components/MoveSecretsDialog/MoveSecretsDialog.tsx
index 2e10bc92c6..d02b08744e 100644
--- a/frontend/src/pages/secret-manager/OverviewPage/components/SelectionPanel/components/MoveSecretsDialog/MoveSecretsDialog.tsx
+++ b/frontend/src/pages/secret-manager/OverviewPage/components/SelectionPanel/components/MoveSecretsDialog/MoveSecretsDialog.tsx
@@ -56,7 +56,7 @@ import {
ProjectPermissionSecretActions,
ProjectPermissionSecretRotationActions
} from "@app/context/ProjectPermissionContext/types";
-import { TMoveWarningsCheck } from "@app/helpers/secretSyncCoverage";
+import { TItemMove } from "@app/helpers/secretSyncCoverage";
import { removeTrailingSlash } from "@app/helpers/string";
import { useMoveSecrets } from "@app/hooks/api";
import {
@@ -545,9 +545,9 @@ const buildDestinationTargets = ({
return { checks: isSelfMove ? [] : checks, isSelfMove };
};
-// one check per path the items land at. a moved folder always lands at a new path (the backend rejects a
+// one move per path the items land at. a moved folder always lands at a new path (the backend rejects a
// name clash), so only recursive syncs above it can cover it or anything inside it.
-const buildMoveWarningChecks = ({
+const buildItemMoves = ({
sourceSecretPath,
destinationPath,
secretEnvironments,
@@ -557,23 +557,28 @@ const buildMoveWarningChecks = ({
destinationPath?: string;
secretEnvironments: { sourceEnv: string; destinationEnvironment: string }[];
movedFolders: MovedFolder[];
-}): TMoveWarningsCheck[] => {
+}): TItemMove[] => {
if (!destinationPath) return [];
- const secretChecks = secretEnvironments.map(({ sourceEnv, destinationEnvironment }) => ({
- sourceEnvironment: sourceEnv,
- sourceSecretPath: removeTrailingSlash(sourceSecretPath),
- destinationEnvironment,
- destinationSecretPath: removeTrailingSlash(destinationPath)
+ const secretMoves = secretEnvironments.map(({ sourceEnv, destinationEnvironment }) => ({
+ source: { environment: sourceEnv, secretPath: removeTrailingSlash(sourceSecretPath) },
+ destination: {
+ environment: destinationEnvironment,
+ secretPath: removeTrailingSlash(destinationPath)
+ }
}));
- const folderChecks = movedFolders.map(({ folderName, sourceEnv, destinationEnvironment }) => ({
- sourceEnvironment: sourceEnv,
- sourceSecretPath: `${removeTrailingSlash(sourceSecretPath)}/${folderName}`,
- destinationEnvironment,
- destinationSecretPath: `${removeTrailingSlash(destinationPath)}/${folderName}`
+ const folderMoves = movedFolders.map(({ folderName, sourceEnv, destinationEnvironment }) => ({
+ source: {
+ environment: sourceEnv,
+ secretPath: `${removeTrailingSlash(sourceSecretPath)}/${folderName}`
+ },
+ destination: {
+ environment: destinationEnvironment,
+ secretPath: `${removeTrailingSlash(destinationPath)}/${folderName}`
+ }
}));
- return [...secretChecks, ...folderChecks];
+ return [...secretMoves, ...folderMoves];
};
// memoizes the destination checks for the moved folders and runs the destination approval-policy eligibility
@@ -918,9 +923,9 @@ const SingleEnvContent = ({
Object.values(secrets).some((secretRecord) => Boolean(secretRecord[sourceEnv.slug])) ||
Object.values(rotations).some((rotationRecord) => Boolean(rotationRecord[sourceEnv.slug]));
- const moveWarningChecks = useMemo(
+ const itemMoves = useMemo(
() =>
- buildMoveWarningChecks({
+ buildItemMoves({
sourceSecretPath,
destinationPath: destinationSelected && !isSelfMove ? selectedPath?.secretPath : undefined,
secretEnvironments: hasSecretsToMove
@@ -939,7 +944,7 @@ const SingleEnvContent = ({
movedFolders
]
);
- const secretSyncWarning = useSecretSyncMoveWarning(projectId, moveWarningChecks);
+ const secretSyncWarning = useSecretSyncMoveWarning(projectId, itemMoves);
const handleFormSubmit = async (data: TSingleEnvFormSchema) => {
if (!selectedPath) {
@@ -1331,9 +1336,9 @@ const MultiEnvContent = ({
destinationPath: destinationSelected ? selectedPath?.secretPath : undefined
});
- const moveWarningChecks = useMemo(
+ const itemMoves = useMemo(
() =>
- buildMoveWarningChecks({
+ buildItemMoves({
sourceSecretPath,
destinationPath: destinationSelected && !isSelfMove ? selectedPath?.secretPath : undefined,
secretEnvironments: environments
@@ -1359,7 +1364,7 @@ const MultiEnvContent = ({
movedFolders
]
);
- const secretSyncWarning = useSecretSyncMoveWarning(projectId, moveWarningChecks);
+ const secretSyncWarning = useSecretSyncMoveWarning(projectId, itemMoves);
const environmentsToBeSkipped = useMemo(() => {
if (!destinationSelected) return [];