Skip to content

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

add new endpoint to move secret rotation

adilsone99e0717db62
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 de8d27e2ec..8c863336c2 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
@@ -452,6 +452,61 @@ export const registerSecretRotationEndpoints = <
}
});
+ server.route({
+ method: "POST",
+ url: "/move/:rotationId",
+ config: {
+ rateLimit: writeLimit
+ },
+ schema: {
+ hide: false,
+ operationId: `move${rotationTypeId}Rotation`,
+ tags: [ApiDocsTags.SecretRotations],
+ description: `Move the specified ${rotationType} Rotation to a different secret path or environment.`,
+ params: z.object({
+ rotationId: z.string().uuid().describe(SecretRotations.UPDATE(type).rotationId)
+ }),
+ body: z.object({
+ destinationEnvironment: z.string().trim().min(1, "Destination environment required"),
+ destinationSecretPath: z.string().trim().min(1, "Destination secret path required")
+ }),
+ response: {
+ 200: z.object({ secretRotation: responseSchema })
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
+ handler: async (req) => {
+ const { rotationId } = req.params;
+ const { destinationEnvironment, destinationSecretPath } = req.body;
+
+ const { secretRotation, sourceEnvironment, sourceSecretPath } =
+ await server.services.secretRotationV2.moveSecretRotation(
+ { rotationId, type, destinationEnvironment, destinationSecretPath },
+ req.permission
+ );
+
+ const movedRotation = secretRotation as T;
+
+ await server.services.auditLog.createAuditLog({
+ ...req.auditLogInfo,
+ projectId: movedRotation.projectId,
+ event: {
+ type: EventType.MOVE_SECRET_ROTATION,
+ metadata: {
+ type,
+ rotationId,
+ sourceEnvironment,
+ sourceSecretPath,
+ destinationEnvironment,
+ destinationSecretPath
+ }
+ }
+ });
+
+ return { secretRotation: movedRotation };
+ }
+ });
+
server.route({
method: "POST",
url: "/:rotationId/rotate-secrets",
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 8dd84ceaba..b987897d3b 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -544,6 +544,7 @@ export enum EventType {
CREATE_SECRET_ROTATION = "create-secret-rotation",
UPDATE_SECRET_ROTATION = "update-secret-rotation",
DELETE_SECRET_ROTATION = "delete-secret-rotation",
+ MOVE_SECRET_ROTATION = "move-secret-rotation",
SECRET_ROTATION_ROTATE_SECRETS = "secret-rotation-rotate-secrets",
RECONCILE_SECRET_ROTATION = "reconcile-secret-rotation",
@@ -4374,6 +4375,18 @@ interface DeleteSecretRotationEvent {
metadata: TDeleteSecretRotationV2DTO;
}
+interface MoveSecretRotationEvent {
+ type: EventType.MOVE_SECRET_ROTATION;
+ metadata: {
+ type: string;
+ rotationId: string;
+ sourceEnvironment: string;
+ sourceSecretPath: string;
+ destinationEnvironment: string;
+ destinationSecretPath: string;
+ };
+}
+
interface RotateSecretRotationEvent {
type: EventType.SECRET_ROTATION_ROTATE_SECRETS;
metadata: Pick<TSecretRotationV2Raw, "parameters" | "secretsMapping" | "type" | "connectionId" | "folderId"> & {
@@ -6546,6 +6559,7 @@ export type Event =
| CreateSecretRotationEvent
| UpdateSecretRotationEvent
| DeleteSecretRotationEvent
+ | MoveSecretRotationEvent
| RotateSecretRotationEvent
| ReconcileSecretRotationEvent
| MicrosoftTeamsWorkflowIntegrationCreateEvent
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 1bee6996ca..334d2a33cf 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
@@ -42,6 +42,7 @@ import {
TGetDashboardSecretRotationsV2,
TGetDashboardSecretRotationV2Count,
TListSecretRotationsV2ByProjectId,
+ TMoveSecretRotationV2DTO,
TQuickSearchSecretRotationsV2,
TRotateSecretRotationV2,
TRotationFactory,
@@ -928,6 +929,106 @@ export const secretRotationV2ServiceFactory = ({
return expandSecretRotation(secretRotation, kmsService);
};
+ const moveSecretRotation = async (
+ { type, rotationId, destinationEnvironment, destinationSecretPath }: TMoveSecretRotationV2DTO,
+ actor: OrgServiceActor
+ ) => {
+ const plan = await licenseService.getPlan(actor.orgId);
+
+ if (!plan.secretRotation)
+ throw new BadRequestError({
+ message: "Failed to move secret rotation due to plan restriction. Upgrade plan to manage secret rotations."
+ });
+
+ const secretRotation = await secretRotationV2DAL.findById(rotationId);
+
+ if (!secretRotation)
+ throw new NotFoundError({
+ message: `Could not find ${SECRET_ROTATION_NAME_MAP[type]} Rotation with ID "${rotationId}"`
+ });
+
+ const { projectId, folderId: sourceFolderId, secretsMapping, folder, environment } = secretRotation;
+
+ const { permission } = await permissionService.getProjectPermission({
+ actor: actor.type,
+ actorId: actor.id,
+ actorAuthMethod: actor.authMethod,
+ actorOrgId: actor.orgId,
+ actionProjectType: ActionProjectType.SecretManager,
+ projectId
+ });
+
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionSecretRotationActions.Delete,
+ getSecretRotationSubject(secretRotation)
+ );
+
+ const destinationFolder = await folderDAL.findBySecretPath(
+ projectId,
+ destinationEnvironment,
+ destinationSecretPath
+ );
+
+ if (!destinationFolder)
+ throw new NotFoundError({
+ message: `Destination folder with path "${destinationSecretPath}" in environment "${destinationEnvironment}" not found`
+ });
+
+ if (destinationFolder.id === sourceFolderId)
+ throw new BadRequestError({
+ message: "Source and destination locations are the same"
+ });
+
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionSecretRotationActions.Create,
+ getSecretRotationSubject(secretRotation, {
+ environment: destinationEnvironment,
+ secretPath: destinationSecretPath
+ })
+ );
+
+ const mappedKeys = Object.values(secretsMapping as TSecretRotationV2["secretsMapping"]);
+
+ await $throwOnConflictingSecrets({
+ secretKeys: mappedKeys,
+ folderId: destinationFolder.id,
+ secretPath: destinationSecretPath
+ });
+
+ const updatedRotation = await secretRotationV2DAL.transaction(async (tx) => {
+ const sourceSecrets = await secretV2BridgeDAL.find(
+ {
+ $in: {
+ [`${TableName.SecretV2}.key` as "key"]: mappedKeys
+ },
+ [`${TableName.SecretV2}.folderId` as "folderId"]: sourceFolderId,
+ [`${TableName.SecretV2}.type` as "type"]: SecretType.Shared
+ },
+ { tx }
+ );
+
+ if (sourceSecrets.length) {
+ await secretV2BridgeDAL.bulkUpdate(
+ sourceSecrets.map((s) => ({
+ filter: { id: s.id },
+ data: { folderId: destinationFolder.id }
+ })),
+ tx
+ );
+ }
+
+ return secretRotationV2DAL.updateById(rotationId, { folderId: destinationFolder.id }, tx);
+ });
+
+ await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
+
+ return {
+ secretRotation: await expandSecretRotation(updatedRotation, kmsService),
+ sourceEnvironment: environment.slug,
+ sourceSecretPath: folder.path
+ };
+ };
+
const triggerFailedWebhook = async (
projectId: string,
environment: { slug: string; name: string; id: string },
@@ -1686,6 +1787,7 @@ export const secretRotationV2ServiceFactory = ({
findSecretRotationById,
findSecretRotationByName,
deleteSecretRotation,
+ moveSecretRotation,
findSecretRotationGeneratedCredentialsById,
rotateSecretRotation,
rotateGeneratedCredentials,
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 feaa79a175..002eba5c1b 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
@@ -284,6 +284,13 @@ export type TDeleteSecretRotationV2DTO = {
revokeGeneratedCredentials: boolean;
};
+export type TMoveSecretRotationV2DTO = {
+ type: SecretRotation;
+ rotationId: string;
+ destinationEnvironment: string;
+ destinationSecretPath: string;
+};
+
/** Minimal rotation shape needed to build a permission subject (env, path, connectionId). */
export type TSecretRotationV2PermissionContext = {
environment: { slug: string };