Skip to content

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

apply recommendations

adilson8b7cf35266d1
diff --git a/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts b/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
index 8c863336c2..08a305576b 100644
--- a/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
+++ b/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
@@ -454,7 +454,7 @@ export const registerSecretRotationEndpoints = <
server.route({
method: "POST",
- url: "/move/:rotationId",
+ url: "/:rotationId/move",
config: {
rateLimit: writeLimit
},
@@ -468,7 +468,8 @@ export const registerSecretRotationEndpoints = <
}),
body: z.object({
destinationEnvironment: z.string().trim().min(1, "Destination environment required"),
- destinationSecretPath: z.string().trim().min(1, "Destination secret path required")
+ destinationSecretPath: z.string().trim().min(1, "Destination secret path required"),
+ overwriteDestination: z.boolean().default(false)
}),
response: {
200: z.object({ secretRotation: responseSchema })
@@ -477,11 +478,11 @@ export const registerSecretRotationEndpoints = <
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const { rotationId } = req.params;
- const { destinationEnvironment, destinationSecretPath } = req.body;
+ const { destinationEnvironment, destinationSecretPath, overwriteDestination } = req.body;
const { secretRotation, sourceEnvironment, sourceSecretPath } =
await server.services.secretRotationV2.moveSecretRotation(
- { rotationId, type, destinationEnvironment, destinationSecretPath },
+ { rotationId, type, destinationEnvironment, destinationSecretPath, overwriteDestination },
req.permission
);
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
index 9df5e46b35..2fe9aa4ffa 100644
--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
@@ -233,6 +233,26 @@ export const secretRotationV2ServiceFactory = ({
);
};
+ const $findConflictingSecrets = async ({
+ secretKeys,
+ folderId,
+ tx
+ }: {
+ secretKeys: string[];
+ folderId: string;
+ tx?: Knex;
+ }) =>
+ secretV2BridgeDAL.find(
+ {
+ $in: {
+ [`${TableName.SecretV2}.key` as "key"]: secretKeys
+ },
+ [`${TableName.SecretV2}.folderId` as "folderId"]: folderId,
+ [`${TableName.SecretV2}.type` as "type"]: SecretType.Shared
+ },
+ tx ? { tx } : undefined
+ );
+
const $throwOnConflictingSecrets = async ({
secretKeys,
folderId,
@@ -250,16 +270,7 @@ export const secretRotationV2ServiceFactory = ({
});
}
- const conflictingSecrets = await secretV2BridgeDAL.find(
- {
- $in: {
- [`${TableName.SecretV2}.key` as "key"]: secretKeys
- },
- [`${TableName.SecretV2}.folderId` as "folderId"]: folderId,
- [`${TableName.SecretV2}.type` as "type"]: SecretType.Shared
- },
- tx ? { tx } : undefined
- );
+ const conflictingSecrets = await $findConflictingSecrets({ secretKeys, folderId, tx });
if (conflictingSecrets.length) {
throw new BadRequestError({
@@ -943,7 +954,7 @@ export const secretRotationV2ServiceFactory = ({
};
const moveSecretRotation = async (
- { type, rotationId, destinationEnvironment, destinationSecretPath }: TMoveSecretRotationV2DTO,
+ { type, rotationId, destinationEnvironment, destinationSecretPath, overwriteDestination }: TMoveSecretRotationV2DTO,
actor: OrgServiceActor
) => {
const plan = await licenseService.getPlan(actor.orgId);
@@ -967,6 +978,13 @@ export const secretRotationV2ServiceFactory = ({
message: `Secret Rotation with ID "${rotationId}" is not configured for ${SECRET_ROTATION_NAME_MAP[type]}`
});
+ const isRotationOccurring = Boolean(await keyStore.getItem(KeyStorePrefixes.SecretRotationLock(secretRotation.id)));
+
+ if (isRotationOccurring)
+ throw new BadRequestError({
+ message: "A rotation is currently in progress for this secret rotation. Please try again shortly."
+ });
+
const { permission } = await permissionService.getProjectPermission({
actor: actor.type,
actorId: actor.id,
@@ -1017,12 +1035,19 @@ export const secretRotationV2ServiceFactory = ({
const mappedKeys = Object.values(secretsMapping as TSecretRotationV2["secretsMapping"]);
- await $throwOnConflictingSecrets({
+ const conflictingDestinationSecrets = await $findConflictingSecrets({
secretKeys: mappedKeys,
- folderId: destinationFolder.id,
- secretPath: destinationSecretPath
+ folderId: destinationFolder.id
});
+ if (conflictingDestinationSecrets.length && !overwriteDestination) {
+ throw new BadRequestError({
+ message: `The following secrets already exist at the destination path "${destinationSecretPath}": ${conflictingDestinationSecrets
+ .map(({ key }) => key)
+ .join(", ")}. Set "overwriteDestination" to true to replace them.`
+ });
+ }
+
const { encryptor: secretManagerEncryptor, decryptor: secretManagerDecryptor } =
await kmsService.createCipherPairWithDataKey({
type: KmsDataKey.SecretManager,
@@ -1030,6 +1055,15 @@ export const secretRotationV2ServiceFactory = ({
});
const updatedRotation = await secretRotationV2DAL.transaction(async (tx) => {
+ if (conflictingDestinationSecrets.length && overwriteDestination) {
+ await secretV2BridgeDAL.deleteMany(
+ conflictingDestinationSecrets.map((s) => ({ key: s.key, type: SecretType.Shared })),
+ destinationFolder.id,
+ actor.id,
+ tx
+ );
+ }
+
const sourceSecrets = await secretV2BridgeDAL.find(
{
$in: {
@@ -1088,6 +1122,24 @@ export const secretRotationV2ServiceFactory = ({
await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
+ await snapshotService.performSnapshot(destinationFolder.id);
+ await secretQueueService.syncSecrets({
+ orgId: connection.orgId,
+ secretPath: destinationSecretPath,
+ projectId,
+ environmentSlug: destinationEnvironment,
+ excludeReplication: true
+ });
+
+ await snapshotService.performSnapshot(sourceFolderId);
+ await secretQueueService.syncSecrets({
+ orgId: connection.orgId,
+ secretPath: folder.path,
+ projectId,
+ environmentSlug: environment.slug,
+ excludeReplication: true
+ });
+
return {
secretRotation: await expandSecretRotation(updatedRotation, kmsService),
sourceEnvironment: environment.slug,
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
index 002eba5c1b..e9a67ff84a 100644
--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
@@ -289,6 +289,7 @@ export type TMoveSecretRotationV2DTO = {
rotationId: string;
destinationEnvironment: string;
destinationSecretPath: string;
+ overwriteDestination: boolean;
};
/** Minimal rotation shape needed to build a permission subject (env, path, connectionId). */
diff --git a/frontend/src/hooks/api/secretRotationsV2/mutations.tsx b/frontend/src/hooks/api/secretRotationsV2/mutations.tsx
index 131529e8cf..2cbcc3e7e7 100644
--- a/frontend/src/hooks/api/secretRotationsV2/mutations.tsx
+++ b/frontend/src/hooks/api/secretRotationsV2/mutations.tsx
@@ -103,16 +103,25 @@ export const useMoveSecretRotation = () => {
destinationSecretPath
}: TMoveSecretRotationV2DTO) => {
const { data } = await apiRequest.post<TSecretRotationV2Response>(
- `/api/v2/secret-rotations/${type}/move/${rotationId}`,
+ `/api/v2/secret-rotations/${type}/${rotationId}/move`,
{ destinationEnvironment, destinationSecretPath }
);
return data.secretRotation;
},
- onSuccess: (_, { projectId, secretPath }) =>
+ onSuccess: (_, { projectId, secretPath, destinationSecretPath }) => {
queryClient.invalidateQueries({
queryKey: dashboardKeys.getDashboardSecrets({ projectId, secretPath })
- })
+ });
+ if (destinationSecretPath !== secretPath) {
+ queryClient.invalidateQueries({
+ queryKey: dashboardKeys.getDashboardSecrets({
+ projectId,
+ secretPath: destinationSecretPath
+ })
+ });
+ }
+ }
});
};
diff --git a/frontend/src/hooks/api/secrets/mutations.tsx b/frontend/src/hooks/api/secrets/mutations.tsx
index 3ba83c223b..c54ba599b3 100644
--- a/frontend/src/hooks/api/secrets/mutations.tsx
+++ b/frontend/src/hooks/api/secrets/mutations.tsx
@@ -422,13 +422,21 @@ export const useMoveSecrets = ({
return data;
},
- onSuccess: (_, { projectId, sourceEnvironment, sourceSecretPath }) => {
+ onSuccess: (_, { projectId, sourceEnvironment, sourceSecretPath, destinationSecretPath }) => {
queryClient.invalidateQueries({
queryKey: dashboardKeys.getDashboardSecrets({
projectId,
secretPath: sourceSecretPath
})
});
+ if (destinationSecretPath !== sourceSecretPath) {
+ queryClient.invalidateQueries({
+ queryKey: dashboardKeys.getDashboardSecrets({
+ projectId,
+ secretPath: destinationSecretPath
+ })
+ });
+ }
queryClient.invalidateQueries({
queryKey: secretKeys.getProjectSecret({
projectId,
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 fb3e1e34cd..14aee5b2c8 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
@@ -331,15 +331,6 @@ const SingleEnvContent = ({
});
}
- if (rotationFailures.length > 0) {
- createNotification({
- type: "error",
- text: `Failed to move ${rotationFailures.length} secret rotation${
- rotationFailures.length === 1 ? "" : "s"
- }: ${rotationFailures.map((f) => `${f.name} (${f.message})`).join("; ")}`
- });
- }
-
onClose();
onComplete();
};