Skip to content

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

fix(webhook): render the test event for the webhook's type

InfisiCalvin855d75ef4f7a
diff --git a/backend/src/services/webhook/webhook-fns.test.ts b/backend/src/services/webhook/webhook-fns.test.ts
index 931301268a..dc6af774f9 100644
--- a/backend/src/services/webhook/webhook-fns.test.ts
+++ b/backend/src/services/webhook/webhook-fns.test.ts
@@ -1,6 +1,6 @@
import { ActorType } from "@app/services/auth/auth-type";
-import { getWebhookPayload, isWebhookPathSubscribed } from "./webhook-fns";
+import { getWebhookPayload, isWebhookPathSubscribed, TEST_EVENT_PAYLOAD_BUILDERS } from "./webhook-fns";
import { AccessRequestWebhookAction, ChangeRequestWebhookAction, WebhookEvents, WebhookType } from "./webhook-types";
const projectFields = {
@@ -405,6 +405,18 @@ describe("getWebhookPayload: secrets.access-request.modified", () => {
});
});
+describe("getWebhookPayload: test", () => {
+ test("every webhook type renders a test event body", () => {
+ expect(Object.keys(TEST_EVENT_PAYLOAD_BUILDERS).sort()).toEqual([...Object.values(WebhookType)].sort());
+ });
+
+ test("slack body carries the text field the incoming webhook API requires", () => {
+ expect(
+ getWebhookPayload({ type: WebhookEvents.TestEvent, payload: { ...projectFields, type: WebhookType.SLACK } })
+ ).toHaveProperty("text");
+ });
+});
+
describe("isWebhookPathSubscribed", () => {
describe("concrete event paths use pattern matching", () => {
test.each([
diff --git a/backend/src/services/webhook/webhook-fns.ts b/backend/src/services/webhook/webhook-fns.ts
index 35507e6ed2..cbae36ed5d 100644
--- a/backend/src/services/webhook/webhook-fns.ts
+++ b/backend/src/services/webhook/webhook-fns.ts
@@ -70,6 +70,91 @@ export const triggerWebhookRequest = async (
return req;
};
+type TTestEventFields = {
+ projectId: string;
+ projectName?: string;
+ environment: string;
+ environmentName: string;
+ secretPath?: string;
+};
+
+const TEST_EVENT_MESSAGE = "This is a test message from Infisical.";
+
+// Keyed by webhook type rather than switched on, so a new WebhookType fails the type check here
+// until it has a body its provider accepts. Slack rejects a body without `text` with 400 no_text.
+export const TEST_EVENT_PAYLOAD_BUILDERS: Record<WebhookType, (fields: TTestEventFields) => Record<string, unknown>> = {
+ [WebhookType.SLACK]: ({ projectName, environment, environmentName, secretPath }) => ({
+ text: TEST_EVENT_MESSAGE,
+ attachments: [
+ {
+ color: "#E7F256",
+ fields: [
+ {
+ title: "Project",
+ value: projectName,
+ short: false
+ },
+ {
+ title: "Environment",
+ value: environment,
+ short: false
+ },
+ {
+ title: "Environment Name",
+ value: environmentName,
+ short: false
+ },
+ {
+ title: "Secret Path",
+ value: secretPath,
+ short: false
+ }
+ ]
+ }
+ ]
+ }),
+ [WebhookType.MICROSOFT_TEAMS]: ({ projectName, environment, environmentName, secretPath }) => ({
+ type: "message",
+ attachments: [
+ {
+ contentType: "application/vnd.microsoft.card.adaptive",
+ content: {
+ type: "AdaptiveCard",
+ version: "1.2",
+ body: [
+ {
+ type: "TextBlock",
+ size: "Medium",
+ weight: "Bolder",
+ text: TEST_EVENT_MESSAGE
+ },
+ {
+ type: "FactSet",
+ facts: [
+ { title: "Project", value: projectName || "" },
+ { title: "Environment", value: environment },
+ { title: "Environment Name", value: environmentName || "" },
+ { title: "Secret Path", value: secretPath || "" }
+ ]
+ }
+ ]
+ }
+ }
+ ]
+ }),
+ [WebhookType.GENERAL]: ({ projectName, projectId, environment, environmentName, secretPath }) => ({
+ event: WebhookEvents.TestEvent,
+ project: {
+ workspaceId: projectId,
+ projectId,
+ projectName,
+ environment,
+ environmentName,
+ secretPath
+ }
+ })
+};
+
export const getWebhookPayload = (event: TWebhookPayloads) => {
if (event.type === WebhookEvents.SecretModified) {
const { projectName, projectId, environment, environmentName, secretPath, type, changedBy, changedByActorType } =
@@ -578,18 +663,11 @@ export const getWebhookPayload = (event: TWebhookPayloads) => {
}
if (event.type === WebhookEvents.TestEvent) {
- const { projectName, projectId, environment, environmentName, secretPath } = event.payload;
- return {
- event: event.type,
- project: {
- workspaceId: projectId,
- projectId,
- projectName,
- environment,
- environmentName,
- secretPath
- }
- };
+ const { type, ...fields } = event.payload;
+ const buildPayload =
+ TEST_EVENT_PAYLOAD_BUILDERS[type as WebhookType] ?? TEST_EVENT_PAYLOAD_BUILDERS[WebhookType.GENERAL];
+
+ return buildPayload(fields);
}
logger.warn({ event }, "Unhandled webhook event");