Skip to content

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

fix(audit-log-stream): emit the delivery-failed event without a resourceId so org-wide alerts match

Calvinde5842de86d3
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 1597a6fe18..4b6a5ed914 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -733,7 +733,7 @@ Adding a new alertable resource type:
1. Implement `IResourceAlertProvider` (`alert-types.ts`) in `src/services/alert/providers/<name>-alert-provider.ts`, with its DAL alongside it. You supply: a dot-namespaced `resourceType` (e.g. `identity.authentication`), an `events` list (each event carries its own `conditionSchema` for the "when", so a scheduled expiry and an event-triggered change on the same resource type don't share one shape), `buildViewUrl` / `buildPayload` / `targetId`, one discovery method per trigger type you declare (below), and the two authorization hooks `assertPermission` + `assertResourceInScope`. Test sends need nothing from you: they go through the generic `buildTestAlertPayload` (`alert-test-payload-fns.ts`).
2. Register it on the singleton registry in `src/server/routes/index.ts` (`alertProviderRegistry.register(...)`).
-That's it — CRUD routes, channel creation/rotation, recipient resolution, KMS encryption, dedup, history, retention pruning, test sends, and dispatch metrics all come for free, because the cron tick enumerates `alertProviderRegistry.resourceTypes()`. See `src/services/alert/providers/identity-credential-alert-provider.ts` for a complete example that declares both trigger types (`identity.authentication.expiry` scheduled, `identity.authentication.auth-method-changed` event-triggered). `audit-log-stream-alert-provider.ts` is the smallest event-only example: the stream delivery path emits `audit-log.stream.delivery-failed` (contract in `ee/services/audit-log-stream/audit-log-stream-events.ts`) only on the healthy to failing edge, in the transaction that drops the rows, and `auditLogStreamService.deleteById` reaps the stream's alerts.
+That's it — CRUD routes, channel creation/rotation, recipient resolution, KMS encryption, dedup, history, retention pruning, test sends, and dispatch metrics all come for free, because the cron tick enumerates `alertProviderRegistry.resourceTypes()`. See `src/services/alert/providers/identity-credential-alert-provider.ts` for a complete example that declares both trigger types (`identity.authentication.expiry` scheduled, `identity.authentication.auth-method-changed` event-triggered). `audit-log-stream-alert-provider.ts` is the smallest event-only example: the stream delivery path emits `audit-log.stream.delivery-failed` (contract in `ee/services/audit-log-stream/audit-log-stream-events.ts`) only on the healthy to failing edge, in the transaction that drops the rows. Its alerts are org-wide only: the event carries no `resourceId`, because the consumer matches alerts by the event's `resourceId` and resource-less alerts only hear resource-less events.
**Each event declares how it fires**, and the trigger decides which discovery method the provider owes:
diff --git a/backend/src/ee/services/audit-log-stream/audit-log-stream-events.ts b/backend/src/ee/services/audit-log-stream/audit-log-stream-events.ts
index 7e6bd0b948..efc6f5fc42 100644
--- a/backend/src/ee/services/audit-log-stream/audit-log-stream-events.ts
+++ b/backend/src/ee/services/audit-log-stream/audit-log-stream-events.ts
@@ -26,7 +26,7 @@ export type TAuditLogStreamDeliveryFailedPayload = {
orgId: string;
projectId: null;
resourceType: typeof AUDIT_LOG_STREAM_RESOURCE_TYPE;
- resourceId: string;
+ resourceId: null;
targetIds: string[];
provider: string;
errorMessage: string;
@@ -43,7 +43,7 @@ export const emitAuditLogStreamDeliveryFailed = (
orgId,
projectId: null,
resourceType: AUDIT_LOG_STREAM_RESOURCE_TYPE,
- resourceId: streamId,
+ resourceId: null,
targetIds: [streamId],
provider: provider ?? "unknown",
errorMessage,
diff --git a/backend/src/ee/services/audit-log-stream/audit-log-stream-service.ts b/backend/src/ee/services/audit-log-stream/audit-log-stream-service.ts
index 3ed6c238b8..015d5bf648 100644
--- a/backend/src/ee/services/audit-log-stream/audit-log-stream-service.ts
+++ b/backend/src/ee/services/audit-log-stream/audit-log-stream-service.ts
@@ -9,7 +9,6 @@ import {
} from "@app/ee/services/audit-log-stream/audit-log-stream-fns";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { OrgServiceActor } from "@app/lib/types";
-import { TAlertServiceFactory } from "@app/services/alert/alert-service";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TLicenseServiceFactory } from "../license/license-service";
@@ -17,7 +16,6 @@ import { OrgPermissionActions, OrgPermissionSubjects } from "../permission/org-p
import { TPermissionServiceFactory } from "../permission/permission-service-types";
import { TAuditLogStreamDALFactory } from "./audit-log-stream-dal";
import { LogProvider, REDACTED_CREDENTIAL_VALUE, StreamMode } from "./audit-log-stream-enums";
-import { AUDIT_LOG_STREAM_RESOURCE_TYPE } from "./audit-log-stream-events";
import { LOG_STREAM_FACTORY_MAP } from "./audit-log-stream-factory";
import { TAuditLogStream, TCreateAuditLogStreamDTO, TUpdateAuditLogStreamDTO } from "./audit-log-stream-types";
import { TCustomProviderCredentials } from "./custom/custom-provider-types";
@@ -29,7 +27,6 @@ export type TAuditLogStreamServiceFactoryDep = {
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
- alertService: Pick<TAlertServiceFactory, "deleteAlertsForDeletedResource">;
};
export type TAuditLogStreamServiceFactory = ReturnType<typeof auditLogStreamServiceFactory>;
@@ -38,8 +35,7 @@ export const auditLogStreamServiceFactory = ({
auditLogStreamDAL,
permissionService,
licenseService,
- kmsService,
- alertService
+ kmsService
}: TAuditLogStreamServiceFactoryDep) => {
const create = async ({ provider, credentials, filters }: TCreateAuditLogStreamDTO, actor: OrgServiceActor) => {
const plan = await licenseService.getPlan(actor.orgId);
@@ -237,13 +233,7 @@ export const auditLogStreamServiceFactory = ({
});
}
- const deletedLogStream = await auditLogStreamDAL.transaction(async (tx) => {
- await alertService.deleteAlertsForDeletedResource(
- { resourceType: AUDIT_LOG_STREAM_RESOURCE_TYPE, resourceId: logStreamId },
- tx
- );
- return auditLogStreamDAL.deleteById(logStreamId, tx);
- });
+ const deletedLogStream = await auditLogStreamDAL.deleteById(logStreamId);
return decryptLogStream(deletedLogStream, kmsService);
};
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index bc06f482ee..ac8f049e29 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1243,8 +1243,7 @@ export const registerRoutes = async (
licenseService,
permissionService,
auditLogStreamDAL,
- kmsService,
- alertService
+ kmsService
});
const auditLogStreamOutboxService = auditLogStreamOutboxServiceFactory({
diff --git a/backend/src/services/alert/providers/audit-log-stream-alert-dal.ts b/backend/src/services/alert/providers/audit-log-stream-alert-dal.ts
index 3dd443f5a4..9258b0f2e5 100644
--- a/backend/src/services/alert/providers/audit-log-stream-alert-dal.ts
+++ b/backend/src/services/alert/providers/audit-log-stream-alert-dal.ts
@@ -1,5 +1,3 @@
-import { Knex } from "knex";
-
import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
@@ -12,22 +10,6 @@ export type TAuditLogStreamRef = {
};
export const auditLogStreamAlertDALFactory = (db: TDbClient) => {
- const findStreamInOrg = async (
- streamId: string,
- orgId: string,
- tx?: Knex
- ): Promise<TAuditLogStreamRef | undefined> => {
- try {
- const row = (await (tx || db.replicaNode())(TableName.AuditLogStream)
- .where({ id: streamId, orgId })
- .select("id", "provider")
- .first()) as TAuditLogStreamRef | undefined;
- return row;
- } catch (error) {
- throw new DatabaseError({ error, name: "AuditLogStreamAlert: findStreamInOrg" });
- }
- };
-
// Primary on purpose: the event is emitted in the transaction that flips the stream to failing, and
// an empty read here is terminal, so a lagging replica would swallow the notification.
const findStreamsByIds = async (streamIds: string[], orgId: string): Promise<TAuditLogStreamRef[]> => {
@@ -43,5 +25,5 @@ export const auditLogStreamAlertDALFactory = (db: TDbClient) => {
}
};
- return { findStreamInOrg, findStreamsByIds };
+ return { findStreamsByIds };
};
diff --git a/backend/src/services/alert/providers/audit-log-stream-alert-provider.ts b/backend/src/services/alert/providers/audit-log-stream-alert-provider.ts
index 1305ef12e3..171e736931 100644
--- a/backend/src/services/alert/providers/audit-log-stream-alert-provider.ts
+++ b/backend/src/services/alert/providers/audit-log-stream-alert-provider.ts
@@ -11,7 +11,7 @@ import { listProviderOptions } from "@app/ee/services/audit-log-stream/audit-log
import { OrgPermissionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { getConfig } from "@app/lib/config/env";
-import { NotFoundError } from "@app/lib/errors";
+import { BadRequestError } from "@app/lib/errors";
import { TAlertPayload } from "../alert-channel-types";
import {
@@ -118,16 +118,17 @@ export const auditLogStreamAlertProviderFactory = ({
};
};
+ // The delivery-failed event carries no resourceId, so only org-wide alerts can hear it. An alert bound
+ // to one stream would never fire.
const assertResourceInScope = async (input: {
orgId: string;
projectId?: string | null;
resourceId?: string | null;
}): Promise<void> => {
- if (!input.resourceId) return;
-
- const stream = await auditLogStreamAlertDAL.findStreamInOrg(input.resourceId, input.orgId);
- if (!stream) {
- throw new NotFoundError({ message: `Audit Log Stream '${input.resourceId}' was not found in this organization` });
+ if (input.resourceId) {
+ throw new BadRequestError({
+ message: "Audit log stream alerts apply to every stream in the organization and cannot target one stream"
+ });
}
};