Skip to content

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

change migration to be array

adilson2ce0ad4b33f9
diff --git a/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts b/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts
index cd5ae67d80..e87966aa38 100644
--- a/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts
+++ b/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts
@@ -2,6 +2,7 @@ import { Knex } from "knex";
import { TableName } from "../schemas";
+const BLOCKED_EVENTS_COLUMN = "blockedEvents";
const SECRET_MODIFIED_EVENT_COLUMN = "isSecretModifiedEventEnabled";
const SECRET_ROTATION_FAILED_EVENT_COLUMN = "isSecretRotationFailedEventEnabled";
@@ -14,14 +15,19 @@ export async function up(knex: Knex): Promise<void> {
TableName.Webhook,
SECRET_ROTATION_FAILED_EVENT_COLUMN
);
+ const hasBlockedEventsColumn = await knex.schema.hasColumn(TableName.Webhook, BLOCKED_EVENTS_COLUMN);
await knex.schema.alterTable(TableName.Webhook, (table) => {
- if (!hasSecretModifiedEventColumn) {
- table.boolean(SECRET_MODIFIED_EVENT_COLUMN).notNullable().defaultTo(true);
+ if (hasSecretModifiedEventColumn) {
+ table.dropColumn(SECRET_MODIFIED_EVENT_COLUMN);
}
- if (!hasSecretRotationFailedEventColumn) {
- table.boolean(SECRET_ROTATION_FAILED_EVENT_COLUMN).notNullable().defaultTo(true);
+ if (hasSecretRotationFailedEventColumn) {
+ table.dropColumn(SECRET_ROTATION_FAILED_EVENT_COLUMN);
+ }
+
+ if (!hasBlockedEventsColumn) {
+ table.specificType(BLOCKED_EVENTS_COLUMN, "text[]").notNullable().defaultTo(knex.raw("'{}'"));
}
});
}
@@ -30,6 +36,7 @@ export async function down(knex: Knex): Promise<void> {
const hasWebhookTable = await knex.schema.hasTable(TableName.Webhook);
if (!hasWebhookTable) return;
+ const hasBlockedEventsColumn = await knex.schema.hasColumn(TableName.Webhook, BLOCKED_EVENTS_COLUMN);
const hasSecretModifiedEventColumn = await knex.schema.hasColumn(TableName.Webhook, SECRET_MODIFIED_EVENT_COLUMN);
const hasSecretRotationFailedEventColumn = await knex.schema.hasColumn(
TableName.Webhook,
@@ -37,12 +44,16 @@ export async function down(knex: Knex): Promise<void> {
);
await knex.schema.alterTable(TableName.Webhook, (table) => {
- if (hasSecretModifiedEventColumn) {
- table.dropColumn(SECRET_MODIFIED_EVENT_COLUMN);
+ if (hasBlockedEventsColumn) {
+ table.dropColumn(BLOCKED_EVENTS_COLUMN);
}
- if (hasSecretRotationFailedEventColumn) {
- table.dropColumn(SECRET_ROTATION_FAILED_EVENT_COLUMN);
+ if (!hasSecretModifiedEventColumn) {
+ table.boolean(SECRET_MODIFIED_EVENT_COLUMN).notNullable().defaultTo(true);
+ }
+
+ if (!hasSecretRotationFailedEventColumn) {
+ table.boolean(SECRET_ROTATION_FAILED_EVENT_COLUMN).notNullable().defaultTo(true);
}
});
}
diff --git a/backend/src/db/schemas/webhooks.ts b/backend/src/db/schemas/webhooks.ts
index c7a78beeaf..fa04412323 100644
--- a/backend/src/db/schemas/webhooks.ts
+++ b/backend/src/db/schemas/webhooks.ts
@@ -30,8 +30,7 @@ export const WebhooksSchema = z.object({
type: z.string().default("general").nullable().optional(),
encryptedPassKey: zodBuffer.nullable().optional(),
encryptedUrl: zodBuffer,
- isSecretModifiedEventEnabled: z.boolean().default(true),
- isSecretRotationFailedEventEnabled: z.boolean().default(true)
+ blockedEvents: z.string().array().default([])
});
export type TWebhooks = z.infer<typeof WebhooksSchema>;
diff --git a/backend/src/server/routes/v1/webhook-router.ts b/backend/src/server/routes/v1/webhook-router.ts
index 210a72789e..cc219da32a 100644
--- a/backend/src/server/routes/v1/webhook-router.ts
+++ b/backend/src/server/routes/v1/webhook-router.ts
@@ -19,16 +19,16 @@ export const sanitizedWebhookSchema = WebhooksSchema.pick({
createdAt: true,
updatedAt: true,
envId: true,
- type: true,
- isSecretModifiedEventEnabled: true,
- isSecretRotationFailedEventEnabled: true
+ type: true
}).extend({
projectId: z.string(),
environment: z.object({
id: z.string(),
name: z.string(),
slug: z.string()
- })
+ }),
+ isSecretModifiedEventEnabled: z.boolean(),
+ isSecretRotationFailedEventEnabled: z.boolean()
});
export const registerWebhookRouter = async (server: FastifyZodProvider) => {
diff --git a/backend/src/services/webhook/webhook-fns.ts b/backend/src/services/webhook/webhook-fns.ts
index 215335605a..2540a63c03 100644
--- a/backend/src/services/webhook/webhook-fns.ts
+++ b/backend/src/services/webhook/webhook-fns.ts
@@ -248,8 +248,7 @@ export const getWebhookPayload = (event: TWebhookPayloads) => {
}
}
- const exhaustiveCheck: never = event;
- throw new Error(`Unhandled webhook event: ${JSON.stringify(exhaustiveCheck)}`);
+ logger.warn({ event }, "Unhandled webhook event");
};
export type TFnTriggerWebhookDTO = {
@@ -278,25 +277,11 @@ export const fnTriggerWebhook = async ({
auditLogService
}: TFnTriggerWebhookDTO) => {
const webhooks = await webhookDAL.findAllWebhooks(projectId, environment);
- const toBeTriggeredHooks = webhooks.filter(
- ({ secretPath: hookSecretPath, isDisabled, isSecretModifiedEventEnabled, isSecretRotationFailedEventEnabled }) => {
- const isEventSubscribed = (() => {
- switch (event.type) {
- case WebhookEvents.SecretModified:
- return isSecretModifiedEventEnabled ?? true;
- case WebhookEvents.SecretRotationFailed:
- return isSecretRotationFailedEventEnabled ?? true;
- default:
- // all other events are subscribed by default
- return true;
- }
- })();
+ const toBeTriggeredHooks = webhooks.filter(({ secretPath: hookSecretPath, isDisabled, blockedEvents }) => {
+ const isEventSubscribed = !blockedEvents || blockedEvents.length === 0 || !blockedEvents.includes(event.type);
- return (
- !isDisabled && picomatch.isMatch(secretPath, hookSecretPath, { strictSlashes: false }) && isEventSubscribed
- );
- }
- );
+ return !isDisabled && picomatch.isMatch(secretPath, hookSecretPath, { strictSlashes: false }) && isEventSubscribed;
+ });
if (!toBeTriggeredHooks.length) return;
logger.info({ environment, secretPath, projectId }, "Secret webhook job started");
let { projectName } = event.payload;
diff --git a/backend/src/services/webhook/webhook-service.ts b/backend/src/services/webhook/webhook-service.ts
index 4005596fc4..cee8674913 100644
--- a/backend/src/services/webhook/webhook-service.ts
+++ b/backend/src/services/webhook/webhook-service.ts
@@ -38,6 +38,22 @@ export const webhookServiceFactory = ({
projectDAL,
kmsService
}: TWebhookServiceFactoryDep) => {
+ const toBlockedEvents = (
+ isSecretModifiedEventEnabled?: boolean,
+ isSecretRotationFailedEventEnabled?: boolean
+ ): string[] => {
+ const blocked: string[] = [];
+ if (isSecretModifiedEventEnabled === false) blocked.push(WebhookEvents.SecretModified);
+ if (isSecretRotationFailedEventEnabled === false) blocked.push(WebhookEvents.SecretRotationFailed);
+ return blocked;
+ };
+
+ const withEventFlags = <T extends { blockedEvents?: string[] | null }>(webhook: T) => ({
+ ...webhook,
+ isSecretModifiedEventEnabled: !webhook.blockedEvents?.includes(WebhookEvents.SecretModified),
+ isSecretRotationFailedEventEnabled: !webhook.blockedEvents?.includes(WebhookEvents.SecretRotationFailed)
+ });
+
const createWebhook = async ({
actor,
actorId,
@@ -72,14 +88,16 @@ export const webhookServiceFactory = ({
type: KmsDataKey.SecretManager,
projectId
});
+
+ const blockedEvents = toBlockedEvents(isSecretModifiedEventEnabled, isSecretRotationFailedEventEnabled);
+
const insertDoc: TWebhooksInsert = {
envId: env.id,
isDisabled: false,
secretPath: secretPath || "/",
type,
encryptedUrl: secretManagerEncryptor({ plainText: Buffer.from(webhookUrl) }).cipherTextBlob,
- ...(isSecretModifiedEventEnabled !== undefined ? { isSecretModifiedEventEnabled } : {}),
- ...(isSecretRotationFailedEventEnabled !== undefined ? { isSecretRotationFailedEventEnabled } : {})
+ blockedEvents
};
if (webhookSecretKey) {
@@ -87,7 +105,7 @@ export const webhookServiceFactory = ({
}
const webhook = await webhookDAL.create(insertDoc);
- return { ...webhook, projectId, environment: env };
+ return { ...withEventFlags(webhook), projectId, environment: env };
};
const updateWebhook = async ({
@@ -113,18 +131,40 @@ export const webhookServiceFactory = ({
});
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Webhooks);
+ const hasEventToggleUpdate =
+ isSecretModifiedEventEnabled !== undefined || isSecretRotationFailedEventEnabled !== undefined;
+
const updateData: {
isDisabled?: boolean;
- isSecretModifiedEventEnabled?: boolean;
- isSecretRotationFailedEventEnabled?: boolean;
+ blockedEvents?: string[];
} = {
- ...(isDisabled !== undefined ? { isDisabled } : {}),
- ...(isSecretModifiedEventEnabled !== undefined ? { isSecretModifiedEventEnabled } : {}),
- ...(isSecretRotationFailedEventEnabled !== undefined ? { isSecretRotationFailedEventEnabled } : {})
+ ...(isDisabled !== undefined ? { isDisabled } : {})
};
+ if (hasEventToggleUpdate) {
+ const currentBlocked = new Set(webhook.blockedEvents ?? []);
+
+ if (isSecretModifiedEventEnabled !== undefined) {
+ if (isSecretModifiedEventEnabled) {
+ currentBlocked.delete(WebhookEvents.SecretModified);
+ } else {
+ currentBlocked.add(WebhookEvents.SecretModified);
+ }
+ }
+
+ if (isSecretRotationFailedEventEnabled !== undefined) {
+ if (isSecretRotationFailedEventEnabled) {
+ currentBlocked.delete(WebhookEvents.SecretRotationFailed);
+ } else {
+ currentBlocked.add(WebhookEvents.SecretRotationFailed);
+ }
+ }
+
+ updateData.blockedEvents = Array.from(currentBlocked);
+ }
+
const updatedWebhook = await webhookDAL.updateById(id, updateData);
- return { ...webhook, ...updatedWebhook };
+ return withEventFlags({ ...webhook, ...updatedWebhook });
};
const deleteWebhook = async ({ id, actor, actorId, actorAuthMethod, actorOrgId }: TDeleteWebhookDTO) => {
@@ -142,7 +182,7 @@ export const webhookServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Webhooks);
const deletedWebhook = await webhookDAL.deleteById(id);
- return { ...webhook, ...deletedWebhook };
+ return withEventFlags({ ...webhook, ...deletedWebhook });
};
const testWebhook = async ({ id, actor, actorId, actorAuthMethod, actorOrgId }: TTestWebhookDTO) => {
@@ -189,7 +229,7 @@ export const webhookServiceFactory = ({
lastStatus: isSuccess ? "success" : "failed",
lastRunErrorMessage: isSuccess ? null : webhookError
});
- return { ...webhook, ...updatedWebhook };
+ return withEventFlags({ ...webhook, ...updatedWebhook });
};
const listWebhooks = async ({
@@ -220,7 +260,7 @@ export const webhookServiceFactory = ({
return webhooks.map((w) => {
const { url } = decryptWebhookDetails(w, (value) => secretManagerDecryptor({ cipherTextBlob: value }).toString());
return {
- ...w,
+ ...withEventFlags(w),
url
};
});
diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/WebhooksTab/EditWebhookEventsModal.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/WebhooksTab/EditWebhookEventsModal.tsx
index 1f499b7277..16d33fff81 100644
--- a/frontend/src/pages/secret-manager/SettingsPage/components/WebhooksTab/EditWebhookEventsModal.tsx
+++ b/frontend/src/pages/secret-manager/SettingsPage/components/WebhooksTab/EditWebhookEventsModal.tsx
@@ -86,7 +86,12 @@ export const EditWebhookEventsModal = ({
</div>
<div className="mt-6 flex items-center justify-end space-x-4">
- <Button variant="plain" onClick={() => onOpenChange(false)}>
+ <Button
+ type="button"
+ variant="plain"
+ colorSchema="secondary"
+ onClick={() => onOpenChange(false)}
+ >
Cancel
</Button>
<Button type="submit" isDisabled={isSubmitting} isLoading={isSubmitting}>