Skip to content

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

add webhook to secret rotation when event fails

adilson9365458a3527
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-fns.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-fns.ts
index ba8b18c858..85e5b3b5a2 100644
--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-fns.ts
@@ -370,7 +370,8 @@ export const rotateSecretsFns = async ({
jobId: job.id,
shouldSendNotification: true,
isFinalAttempt: retryCount === retryLimit,
- isManualRotation
+ isManualRotation,
+ retryCount
});
logger.info(`secretRotationV2Queue: Secrets Rotated ${logDetails}`);
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 2f668af019..48999dc284 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
@@ -83,6 +83,7 @@ import {
} from "@app/services/secret-v2-bridge/secret-v2-bridge-fns";
import { TSecretVersionV2DALFactory } from "@app/services/secret-v2-bridge/secret-version-dal";
import { TSecretVersionV2TagDALFactory } from "@app/services/secret-v2-bridge/secret-version-tag-dal";
+import { WebhookEvents } from "@app/services/webhook/webhook-types";
import { TGatewayV2ServiceFactory } from "../gateway-v2/gateway-v2-service";
import { awsIamUserSecretRotationFactory } from "./aws-iam-user-secret/aws-iam-user-secret-rotation-fns";
@@ -931,7 +932,8 @@ export const secretRotationV2ServiceFactory = ({
jobId,
shouldSendNotification,
isFinalAttempt = true,
- isManualRotation = false
+ isManualRotation = false,
+ retryCount
}: TSecretRotationRotateGeneratedCredentials = {}
) => {
const {
@@ -1098,6 +1100,36 @@ export const secretRotationV2ServiceFactory = ({
} catch (error) {
const errorMessage = parseRotationErrorMessage(error);
+ await queueService.queue(
+ QueueName.SecretWebhook,
+ QueueJobs.SecWebhook,
+ {
+ type: WebhookEvents.SecretSecretRotationFailed,
+ payload: {
+ projectId,
+ environment: environment.slug,
+ secretPath: folder.path,
+ rotationName: secretRotation.name,
+ triggeredManually: isManualRotation,
+ errorMessage,
+ nextRetryTime: isFinalAttempt
+ ? "Maximum retry attempts reached"
+ : new Date(Date.now() + 1000 * 2 ** ((retryCount ?? 1) - 1)).toISOString()
+ }
+ },
+ {
+ jobId: `secret-rotation-webhook-${secretRotation.id}-${Date.now()}`,
+ removeOnFail: { count: 5 },
+ removeOnComplete: true,
+ delay: 1000,
+ attempts: 5,
+ backoff: {
+ type: "exponential",
+ delay: 3000
+ }
+ }
+ );
+
if (isFinalAttempt) {
const { encryptor } = await kmsService.createCipherPairWithDataKey({
type: KmsDataKey.SecretManager,
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 028aa39d76..258c24776e 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
@@ -317,6 +317,7 @@ export type TSecretRotationRotateGeneratedCredentials = {
shouldSendNotification?: boolean;
isFinalAttempt?: boolean;
isManualRotation?: boolean;
+ retryCount?: number;
};
export type TSecretRotationRotateSecretsJobPayload = { rotationId: string; queuedAt: Date; isManualRotation?: boolean };
diff --git a/backend/src/services/webhook/webhook-fns.ts b/backend/src/services/webhook/webhook-fns.ts
index a5b6a086a6..471ff7ce04 100644
--- a/backend/src/services/webhook/webhook-fns.ts
+++ b/backend/src/services/webhook/webhook-fns.ts
@@ -105,6 +105,89 @@ export const getWebhookPayload = (event: TWebhookPayloads) => {
}
}
+ if (event.type === WebhookEvents.SecretSecretRotationFailed) {
+ const {
+ projectName,
+ projectId,
+ environment,
+ secretPath,
+ type,
+ rotationName,
+ errorMessage,
+ nextRetryTime,
+ triggeredManually
+ } = event.payload;
+
+ switch (type) {
+ case WebhookType.SLACK:
+ return {
+ text: "A secret rotation has failed.",
+ attachments: [
+ {
+ color: "#E7F256",
+ fields: [
+ {
+ title: "Rotation Name",
+ value: rotationName,
+ short: false
+ },
+ {
+ title: "Project",
+ value: projectName,
+ short: false
+ },
+ {
+ title: "Environment",
+ value: environment,
+ short: false
+ },
+ {
+ title: "Secret Path",
+ value: secretPath,
+ short: false
+ },
+ {
+ title: "Error Message",
+ value: errorMessage,
+ short: false
+ },
+ {
+ title: "Triggered Manually",
+ value: triggeredManually ? "Yes" : "No",
+ short: false
+ },
+ ...(nextRetryTime
+ ? [
+ {
+ title: "Next Retry",
+ value: nextRetryTime,
+ short: false
+ }
+ ]
+ : [])
+ ]
+ }
+ ]
+ };
+ case WebhookType.GENERAL:
+ default:
+ return {
+ event: event.type,
+ project: {
+ workspaceId: projectId,
+ projectId,
+ projectName,
+ environment,
+ secretPath,
+ rotationName,
+ errorMessage,
+ nextRetryTime,
+ triggeredManually
+ }
+ };
+ }
+ }
+
const { projectName, projectId, environment, secretPath, type, reminderNote, secretName } = event.payload;
switch (type) {
diff --git a/backend/src/services/webhook/webhook-types.ts b/backend/src/services/webhook/webhook-types.ts
index 8ce2c8d8e6..8d5fba6142 100644
--- a/backend/src/services/webhook/webhook-types.ts
+++ b/backend/src/services/webhook/webhook-types.ts
@@ -34,6 +34,7 @@ export enum WebhookType {
export enum WebhookEvents {
SecretModified = "secrets.modified",
SecretReminderExpired = "secrets.reminder-expired",
+ SecretSecretRotationFailed = "secrets.rotation-failed",
TestEvent = "test"
}
@@ -62,4 +63,23 @@ type TWebhookSecretReminderEventPayload = {
};
};
-export type TWebhookPayloads = TWebhookSecretModifiedEventPayload | TWebhookSecretReminderEventPayload;
+type TWebhookSecretRotationFailedEventPayload = {
+ type: WebhookEvents.SecretSecretRotationFailed;
+
+ payload: {
+ rotationName?: string;
+ projectName?: string;
+ projectId: string;
+ environment: string;
+ secretPath?: string;
+ nextRetryTime?: string;
+ triggeredManually?: boolean;
+ errorMessage?: string;
+ type?: string | null;
+ };
+};
+
+export type TWebhookPayloads =
+ | TWebhookSecretModifiedEventPayload
+ | TWebhookSecretReminderEventPayload
+ | TWebhookSecretRotationFailedEventPayload;