Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat: split secret rotation job into two
=37ba53f926ab
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-queue.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-queue.tsindex 59b64a56a8..b6b127e18d 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-queue.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-queue.ts@@ -49,139 +49,141 @@ export const secretRotationV2QueueServiceFactory = async ({}queueService.start(- QueueName.SecretRotationV2,+ QueueName.SecretRotationV2RotateSecrets,async (job) => {- if (job.name === QueueJobs.SecretRotationV2QueueRotations) {- try {- const rotateBy = getNextUtcRotationInterval();+ await rotateSecretsFns({+ job: {+ id: job.id || job.data?.rotationId,+ retryCount: job.attemptsMade,+ retryLimit: job.opts.attempts || 1,+ data: job.data+ },+ secretRotationV2DAL,+ secretRotationV2Service+ });+ },+ {+ persistence: true+ }+ );++ queueService.start(QueueName.SecretRotationV2, async (job) => {+ if (job.name === QueueJobs.SecretRotationV2QueueRotations) {+ try {+ const rotateBy = getNextUtcRotationInterval();- const currentTime = new Date();+ const currentTime = new Date();- const secretRotations = await secretRotationV2DAL.findSecretRotationsToQueue(rotateBy);+ const secretRotations = await secretRotationV2DAL.findSecretRotationsToQueue(rotateBy);+ logger.info(+ `secretRotationV2Queue: Queue Rotations [currentTime=${currentTime.toISOString()}] [rotateBy=${rotateBy.toISOString()}] [count=${+ secretRotations.length+ }]`+ );++ for await (const rotation of secretRotations) {logger.info(- `secretRotationV2Queue: Queue Rotations [currentTime=${currentTime.toISOString()}] [rotateBy=${rotateBy.toISOString()}] [count=${- secretRotations.length- }]`+ `secretRotationV2Queue: Queue Rotation [rotationId=${rotation.id}] [lastRotatedAt=${new Date(+ rotation.lastRotatedAt+ ).toISOString()}] [rotateAt=${new Date(rotation.nextRotationAt!).toISOString()}]`);- for await (const rotation of secretRotations) {- logger.info(- `secretRotationV2Queue: Queue Rotation [rotationId=${rotation.id}] [lastRotatedAt=${new Date(- rotation.lastRotatedAt- ).toISOString()}] [rotateAt=${new Date(rotation.nextRotationAt!).toISOString()}]`+ const data = {+ rotationId: rotation.id,+ queuedAt: currentTime+ } as TSecretRotationRotateSecretsJobPayload;++ if (appCfg.isTestMode) {+ logger.warn("secretRotationV2Queue: Manually rotating secrets for test mode");+ await rotateSecretsFns({+ job: {+ id: uuidv4(),+ data,+ retryCount: 0,+ retryLimit: 0+ },+ secretRotationV2DAL,+ secretRotationV2Service+ });+ } else {+ await queueService.queue(+ QueueName.SecretRotationV2RotateSecrets,+ QueueJobs.SecretRotationV2RotateSecrets,+ {+ rotationId: rotation.id,+ queuedAt: currentTime+ },+ getSecretRotationRotateSecretJobOptions(rotation));-- const data = {- rotationId: rotation.id,- queuedAt: currentTime- } as TSecretRotationRotateSecretsJobPayload;-- if (appCfg.isTestMode) {- logger.warn("secretRotationV2Queue: Manually rotating secrets for test mode");- await rotateSecretsFns({- job: {- id: uuidv4(),- data,- retryCount: 0,- retryLimit: 0- },- secretRotationV2DAL,- secretRotationV2Service- });- } else {- await queueService.queue(- QueueName.SecretRotationV2,- QueueJobs.SecretRotationV2RotateSecrets,- {- rotationId: rotation.id,- queuedAt: currentTime- },- getSecretRotationRotateSecretJobOptions(rotation)- );- }}- } catch (error) {- logger.error(error, "secretRotationV2Queue: Queue Rotations Error:");- throw error;}- } else if (job.name === QueueJobs.SecretRotationV2RotateSecrets) {- await rotateSecretsFns({- job: {- id: job.id || (job.data as TSecretRotationRotateSecretsJobPayload)?.rotationId,- retryCount: job.attemptsMade,- retryLimit: job.opts.attempts || 1,- data: job.data as TSecretRotationRotateSecretsJobPayload- },- secretRotationV2DAL,- secretRotationV2Service+ } catch (error) {+ logger.error(error, "secretRotationV2Queue: Queue Rotations Error:");+ throw error;+ }+ } else if (job.name === QueueJobs.SecretRotationV2SendNotification) {+ const { secretRotation } = job.data as TSecretRotationSendNotificationJobPayload;+ try {+ const {+ name: rotationName,+ type,+ projectId,+ lastRotationAttemptedAt,+ folder,+ environment,+ id: rotationId+ } = secretRotation;++ logger.info(`secretRotationV2Queue: Sending Status Notification [rotationId=${rotationId}]`);++ const projectMembers = await projectMembershipDAL.findAllProjectMembers(projectId);+ const project = await projectDAL.findById(projectId);++ const projectAdmins = projectMembers.filter((member) =>+ member.roles.some((role) => role.role === ProjectMembershipRole.Admin)+ );++ const rotationType = SECRET_ROTATION_NAME_MAP[type as SecretRotation];++ const rotationPath = `/organizations/${project.orgId}/projects/secret-management/${projectId}/secrets/${environment.slug}`;++ await notificationService.createUserNotifications(+ projectAdmins.map((admin) => ({+ userId: admin.userId,+ orgId: project.orgId,+ type: NotificationType.SECRET_ROTATION_FAILED,+ title: "Secret Rotation Failed",+ body: `Your **${rotationType}** rotation **${rotationName}** failed to rotate.`,+ link: rotationPath+ }))+ );++ await smtpService.sendMail({+ recipients: projectAdmins.map((member) => member.user.email!).filter(Boolean),+ template: SmtpTemplates.SecretRotationFailed,+ subjectLine: `Secret Rotation Failed`,+ substitutions: {+ rotationName,+ rotationType,+ content: `Your ${rotationType} Rotation failed to rotate during it's scheduled rotation. The last rotation attempt occurred at ${new Date(+ lastRotationAttemptedAt+ ).toISOString()}. Please check the rotation status in Infisical for more details.`,+ secretPath: folder.path,+ environment: environment.name,+ projectName: project.name,+ rotationUrl: encodeURI(`${appCfg.SITE_URL}${rotationPath}`)+ }});- } else if (job.name === QueueJobs.SecretRotationV2SendNotification) {- const { secretRotation } = job.data as TSecretRotationSendNotificationJobPayload;- try {- const {- name: rotationName,- type,- projectId,- lastRotationAttemptedAt,- folder,- environment,- id: rotationId- } = secretRotation;-- logger.info(`secretRotationV2Queue: Sending Status Notification [rotationId=${rotationId}]`);-- const projectMembers = await projectMembershipDAL.findAllProjectMembers(projectId);- const project = await projectDAL.findById(projectId);-- const projectAdmins = projectMembers.filter((member) =>- member.roles.some((role) => role.role === ProjectMembershipRole.Admin)- );-- const rotationType = SECRET_ROTATION_NAME_MAP[type as SecretRotation];-- const rotationPath = `/organizations/${project.orgId}/projects/secret-management/${projectId}/secrets/${environment.slug}`;-- await notificationService.createUserNotifications(- projectAdmins.map((admin) => ({- userId: admin.userId,- orgId: project.orgId,- type: NotificationType.SECRET_ROTATION_FAILED,- title: "Secret Rotation Failed",- body: `Your **${rotationType}** rotation **${rotationName}** failed to rotate.`,- link: rotationPath- }))- );-- await smtpService.sendMail({- recipients: projectAdmins.map((member) => member.user.email!).filter(Boolean),- template: SmtpTemplates.SecretRotationFailed,- subjectLine: `Secret Rotation Failed`,- substitutions: {- rotationName,- rotationType,- content: `Your ${rotationType} Rotation failed to rotate during it's scheduled rotation. The last rotation attempt occurred at ${new Date(- lastRotationAttemptedAt- ).toISOString()}. Please check the rotation status in Infisical for more details.`,- secretPath: folder.path,- environment: environment.name,- projectName: project.name,- rotationUrl: encodeURI(`${appCfg.SITE_URL}${rotationPath}`)- }- });- } catch (error) {- logger.error(- error,- `secretRotationV2Queue: Failed to Send Status Notification [rotationId=${secretRotation.id}]`- );- throw error;- }+ } catch (error) {+ logger.error(+ error,+ `secretRotationV2Queue: Failed to Send Status Notification [rotationId=${secretRotation.id}]`+ );+ throw error;}- },- {- persistence: true}- );+ });await queueService.queue(QueueName.SecretRotationV2, QueueJobs.SecretRotationV2QueueRotations, undefined, {jobId: "secret-rotation-v2-cron",diff --git a/backend/src/queue/queue-service.ts b/backend/src/queue/queue-service.tsindex 4c7c3c940b..a694d400a6 100644--- a/backend/src/queue/queue-service.ts+++ b/backend/src/queue/queue-service.ts@@ -85,6 +85,7 @@ export enum QueueName {ImportSecretsFromExternalSource = "import-secrets-from-external-source",AppConnectionSecretSync = "app-connection-secret-sync",SecretRotationV2 = "secret-rotation-v2",+ SecretRotationV2RotateSecrets = "secret-rotation-v2-rotate-secrets",FolderTreeCheckpoint = "folder-tree-checkpoint",InvalidateCache = "invalidate-cache",SecretScanningV2 = "secret-scanning-v2",@@ -352,14 +353,14 @@ export type TQueueJobTypes = {name: QueueJobs.SecretRotationV2QueueRotations;payload: undefined;}- | {- name: QueueJobs.SecretRotationV2RotateSecrets;- payload: TSecretRotationRotateSecretsJobPayload;- }| {name: QueueJobs.SecretRotationV2SendNotification;payload: TSecretRotationSendNotificationJobPayload;};+ [QueueName.SecretRotationV2RotateSecrets]: {+ name: QueueJobs.SecretRotationV2RotateSecrets;+ payload: TSecretRotationRotateSecretsJobPayload;+ };[QueueName.InvalidateCache]: {name: QueueJobs.InvalidateCache;payload: {