Skip to content

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

refactor(events): make the CloudEvents envelope the one event model - TEvent is the ADR's CloudEvents envelope (specversion, id, source, type, time, data); toCloudEvent and TCloudEvent are gone. Emitters pass { id, type, data, time? } and consumers validate `data` with `dataSchema`. - The event id is set when the event is built (defineEvent's build), not by the outbox. Consumers report results by it, and (consumer, eventId) is unique, so the secret reminder derives its id with uuid v5 instead of passing an idempotencyKey. idempotencyKey and its index stay for one release, because pods on the previous release still name them on insert. claimBatch gives rows written without an eventId one. - emitMany is folded into emit, which takes a batch, and definitions expose emit(eventEmitter, data, tx) for the single-event case. - A malformed event now throws and fails the caller's transaction instead of being dropped, so a write never commits without its event. It is still logged and counted, on infisical.event_outbox.rejected.count.

Calvin5d2ca0991170
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 148abad6c4..97327c689b 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -778,21 +778,24 @@ at-least-once delivery of a domain event registers next to it.
**Naming:** the outbox is an implementation detail to both sides of it. Emitters depend on
`TEventEmitter` and pass a `TEventInput`; consumers implement `IEventConsumer`, receive `TEvent[]`
-(the row minus its lock, attempt, and status columns) and return `TEventConsumerResult` with an
+(the CloudEvents envelope from the Event Standards and Versioning ADR) and return `TEventConsumerResult` with an
`EventResultStatus`. Names with "outbox" in them (the DAL, queue, cron jobs, metrics,
`EventOutboxStatus`, `TOutboxFlushKey`) describe the mechanism and stay inside the module and its
wiring. A dependency field for the emitter is `eventEmitter`, not `eventOutboxService`.
-**Emitting:** `eventEmitter.emit(event, tx)`. Pass the transaction of the write the event describes, so the event is exactly as durable as that write. Omit `tx` only when there is no such write left to be atomic with (for example after it already committed); the insert then runs on its own.
+**Emitting:** `eventEmitter.emit(events, tx)`. Pass the transaction of the write the event describes, so the event is exactly as durable as that write. Omit `tx` only when there is no such write left to be atomic with (for example after it already committed); the insert then runs on its own.
```ts
await someDAL.transaction(async (tx) => {
const request = await approvalRequestDAL.create({ ... }, tx);
await eventEmitter.emit(
- {
- eventType: "approval.workflow.request_opened",
- payload: { orgId, projectId, resourceType: "approval.workflow", resourceId: policyId, targetIds: [request.id] }
- },
+ [
+ {
+ id: crypto.randomUUID(),
+ type: "approval.workflow.request_opened",
+ data: { orgId, projectId, resourceType: "approval.workflow", resourceId: policyId, targetIds: [request.id] }
+ }
+ ],
tx
);
});
@@ -802,17 +805,19 @@ await someDAL.transaction(async (tx) => {
anything for this resource is decided in the worker; a row nobody wanted is marked `Delivered` and
pruned within a day. If that ever matters for a hot event type, cache inside the consumer rather than
adding a read to the emit path.
-- A malformed event is dropped, not thrown: a bad key, a payload over 16KB, or a payload a consumer's
- `payloadSchema` rejects (that consumer's row only) is logged and counted on
- `infisical.event_outbox.dropped.count`, and the caller's write carries on. It is a bug at the emit
- site, and failing a secret write over it is worse than the lost event. All three checks run before any
- DB access, so the transaction is untouched. The insert itself is not caught: a swallowed insert failure
- hands the caller a poisoned transaction (`25P02` on its next statement).
-- `emitMany(events, tx)` writes a batch in one go (eg one event per secret in a bulk write), in chunks
- of 1000 rows to stay under Postgres's bind parameter limit. Every event gets one `eventId` (UUID),
- shared by all its consumers' rows; `TEvent.id` is the consumer's row, `TEvent.eventId` the occurrence.
- `eventId` is nullable, because a pod on the previous release writes rows without it during a rolling
- deploy.
+- A malformed event throws, and fails the caller's write with it: an id that is not a UUID, a bad
+ type, `data` over 16KB, or `data` a consumer's `dataSchema` rejects is logged, counted on
+ `infisical.event_outbox.rejected.count`, and thrown before any DB access. Committing the write
+ without its event would break the guarantee that consumers hear about every change. The insert itself
+ is not caught either: a swallowed insert failure hands the caller a poisoned transaction (`25P02` on
+ its next statement).
+- `emit` writes a batch in one go (eg one event per secret in a bulk write), in chunks of 1000 rows to
+ stay under Postgres's bind parameter limit.
+- Every event carries its UUID `id` from the moment it is built, and every consumer's row for it stores
+ it as `eventId`. A consumer reports its results by that id. `(consumer, eventId)` is unique, so an
+ emitter that needs a retry dropped derives the id instead of generating it (the secret reminder uses
+ `uuid` v5 over the reminder and the date). `eventId` is nullable only in the table, because a pod on
+ the previous release writes rows without it during a rolling deploy; `claimBatch` gives those rows one.
**How delivery works:**
@@ -836,7 +841,7 @@ await someDAL.transaction(async (tx) => {
the fence protects the bookkeeping but delivery stays at-least-once.
- **The envelope carries only what the outbox queries on.** `consumer`, `eventType`, and the retry and
lock columns. Which tenant and resource an event concerns lives in `payload` under a shape the
- consumer's `payloadSchema` declares, and that schema is where it is validated (the alert consumer
+ consumer's `dataSchema` declares, and that schema is where it is validated (the alert consumer
requires `orgId` as a UUID). The outbox never groups, filters or indexes on any of it, so don't add a
tenant or resource column back for a query nothing runs.
- **Discovery only looks at consumers registered in this process.** A row for any other name has nowhere
@@ -857,8 +862,8 @@ await someDAL.transaction(async (tx) => {
don't add a partition key back until a consumer needs one: no consumer today depends on the order two
events for one resource arrive in, and each event is idempotent on its own through alert history.
- **Delivery is at-least-once.** `commitResults` is retried in-process, since by then the consumer has
- already sent; what's left is narrowed by the consumer's own delivery records and by the emitter's
- `idempotencyKey`.
+ already sent; what's left is narrowed by the consumer's own delivery records and by an emitter that
+ derives its event id.
**Watch `infisical.event_outbox.oldest_pending_age`.** It catches a dead relay, a wedged consumer and a
stuck claim alike. `lag` and `exhausted.count` are recorded by the outbox, labelled by consumer, so a
@@ -866,7 +871,7 @@ new consumer gets them for free.
**Adding an event-triggered alert** needs no outbox code: declare the event with
`triggerType: AlertTriggerType.Event`, implement `findEventTargets`, and emit with
-`payload: { orgId, projectId, resourceType, resourceId, targetIds, ...facts }` where `resourceType` is the
+`data: { orgId, projectId, resourceType, resourceId, targetIds, ...facts }` where `resourceType` is the
provider's. A
`resourceType` that doesn't declare the `eventType` fails the row terminally with both named, so a bad
emit site shows up in the logs on its first event.
@@ -877,19 +882,10 @@ service emits inside each transition's transaction (PAM folder alerts use it).
**New events follow the Event Standards and Versioning ADR (CloudEvents 1.0).** Define them with
`defineEvent` (`event-outbox/event-definition.ts`), which names the event
`com.infisical.<domain>.<entity>.<action>.v<version>` and throws at module load on a name that breaks the
-convention. A consumer uses the definition's `data` schema as its `payloadSchema`. `toCloudEvent` builds
-the CloudEvents envelope from a delivered `TEvent` for anything that leaves the backend. The events
-emitted before the ADR (eg `identity.authentication.auth-method-changed`) keep their names.
-
-**Secret, folder and import changes emit `com.infisical.secrets.{secret,folder,secret-import}.modified.v1`**
-(definitions in `secret-v2-bridge/secret-events.ts`, `secret-folder/secret-folder-events.ts`,
-`secret-import/secret-import-events.ts`). Secrets emit from inside `fnSecretBulkInsert`,
-`fnSecretBulkUpdate` and `fnSecretBulkDelete`, whose required `eventEmitter` and `eventScope` parameters
-make the type checker list every caller, so a new caller cannot skip the emit. A write to `secrets_v2`
-that bypasses those functions (eg a raw DAL `delete`) must emit with `emitSecretsModified` itself, in the
-same transaction. Only shared secrets emit. The e2e suite registers a recording consumer for these events
-(`e2e-test/fakes/event-outbox-registry.ts`), so `e2e-test/routes/v4/secret-events.spec.ts` can read what a
-write emitted.
+convention. Emit one with `SomeEventV1.emit(eventEmitter, data, tx)`, or `build` several and pass them to
+`eventEmitter.emit` together. A consumer uses the definition's `data` schema as its `dataSchema`. The
+events emitted before the ADR (eg `identity.authentication.auth-method-changed`) keep their names, and
+their `source` comes from the first segment of the name.
**The event contract belongs to the domain that emits it, not to a consumer.** The identity auth method
event (key, resource type, change enum, payload schema, and the `emitIdentityAuthMethodChanged` helper)
diff --git a/backend/e2e-test/audit-log-stream-outbox.integration.spec.ts b/backend/e2e-test/audit-log-stream-outbox.integration.spec.ts
index 2842d6dcdc..888396df81 100644
--- a/backend/e2e-test/audit-log-stream-outbox.integration.spec.ts
+++ b/backend/e2e-test/audit-log-stream-outbox.integration.spec.ts
@@ -62,7 +62,7 @@ const outboxDAL = auditLogStreamOutboxDALFactory(testDb as never);
const eventOutboxRegistry = eventOutboxRegistryFactory();
eventOutboxRegistry.register({
name: CONSUMER,
- payloadSchema: AuditLogStreamDeliveryFailedPayloadSchema as never,
+ dataSchema: AuditLogStreamDeliveryFailedPayloadSchema as never,
subscribesTo: (eventType) => eventType === AUDIT_LOG_STREAM_DELIVERY_FAILED_EVENT,
handle: async () => []
});
diff --git a/backend/e2e-test/event-outbox.spec.ts b/backend/e2e-test/event-outbox.spec.ts
index aa2f06d5f1..96cef06161 100644
--- a/backend/e2e-test/event-outbox.spec.ts
+++ b/backend/e2e-test/event-outbox.spec.ts
@@ -9,7 +9,7 @@ import { EventOutboxStatus, MAX_OUTBOX_ATTEMPTS } from "@app/services/event-outb
declare const testDb: Knex;
-// Unit tests only check query shape. Only real Postgres can prove the partial-index conflict target
+// Unit tests only check query shape. Only real Postgres can prove the conflict target
// compiles, two claimers never grab the same row, and the retry clock is computed in SQL.
const dal = eventOutboxDALFactory(testDb as never);
@@ -40,20 +40,38 @@ describe("event outbox (postgres)", () => {
afterEach(purge);
afterAll(purge);
- test("insertEvents dedupes on (consumer, idempotencyKey) and lets rows without a key repeat", async () => {
- await insert([makeRow({ idempotencyKey: "k-1" }), makeRow(), makeRow()]);
- await insert([makeRow({ idempotencyKey: "k-1" })]);
- await insert([makeRow({ idempotencyKey: "k-1", consumer: `${CONSUMER}-other` })]);
+ test("insertEvents drops an event a consumer already has, and keeps it for another consumer", async () => {
+ const eventId = randomUUID();
+ await insert([makeRow({ eventId }), makeRow(), makeRow()]);
+ await insert([makeRow({ eventId })]);
+ await insert([makeRow({ eventId, consumer: `${CONSUMER}-other` })]);
const mine = await rowsFor();
expect(mine).toHaveLength(3);
- expect(mine.filter((row) => row.idempotencyKey === "k-1")).toHaveLength(1);
+ expect(mine.filter((row) => row.eventId === eventId)).toHaveLength(1);
expect(mine[0].status).toBe(EventOutboxStatus.Pending);
expect(mine[0].attempts).toBe(0);
expect(await rowsFor(`${CONSUMER}-other`)).toHaveLength(1);
});
+ // A pod on the previous release inserts without an eventId during a rolling deploy.
+ test("claimBatch gives a row written without an eventId one of its own", async () => {
+ const { eventId, ...withoutEventId } = makeRow();
+ await testDb(TableName.EventOutbox).insert([
+ { ...withoutEventId, payload: JSON.stringify(withoutEventId.payload) },
+ { ...withoutEventId, payload: JSON.stringify(withoutEventId.payload) },
+ { ...withoutEventId, eventId, payload: JSON.stringify(withoutEventId.payload) }
+ ]);
+
+ const { rows } = await dal.claimBatch(KEY, 10);
+
+ expect(rows).toHaveLength(3);
+ expect(rows[2].eventId).toBe(eventId);
+ expect(new Set(rows.map((row) => row.eventId)).size).toBe(3);
+ rows.forEach((row) => expect(row.eventId).toMatch(/^[0-9a-f-]{36}$/));
+ });
+
// A bulk write emits one row per secret, past the 1000-row chunk insertEvents splits on.
test("insertEvents writes a batch larger than one chunk in the caller's transaction", async () => {
await insert(Array.from({ length: 1001 }, () => makeRow()));
diff --git a/backend/e2e-test/fakes/event-outbox-registry.ts b/backend/e2e-test/fakes/event-outbox-registry.ts
index b8cbdb4082..79e1fdc2be 100644
--- a/backend/e2e-test/fakes/event-outbox-registry.ts
+++ b/backend/e2e-test/fakes/event-outbox-registry.ts
@@ -20,9 +20,9 @@ const secretEventsRecorder: IEventConsumer = {
name: SECRET_EVENTS_RECORDER,
// The definitions' own schemas, so an emit site that builds a bad payload is dropped here exactly as
// it would be for a real consumer, and the spec waiting for its row fails.
- payloadSchema: z.union([RECORDED_EVENTS[0].data, RECORDED_EVENTS[1].data, RECORDED_EVENTS[2].data]),
+ dataSchema: z.union([RECORDED_EVENTS[0].data, RECORDED_EVENTS[1].data, RECORDED_EVENTS[2].data]),
subscribesTo: (eventType) => RECORDED_EVENTS.some((definition) => definition.type === eventType),
- handle: async (events) => events.map((event) => ({ id: String(event.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
};
export const eventOutboxRegistryFactory: typeof RealEventOutboxRegistry.eventOutboxRegistryFactory = () => {
diff --git a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
index 148a8206d7..4120781d10 100644
--- a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
+++ b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
@@ -2,23 +2,35 @@ import { Knex } from "knex";
import { TableName } from "../schemas";
-// One id per event, shared by every consumer's row, so the same occurrence can be recognised across
-// consumers and handed out as the CloudEvents `id`. The row `id` is per consumer and can't serve.
-// Nullable with no default: a pod still running the previous release inserts without it during a rolling
-// deploy, and NOT NULL would fail that insert along with the write it belongs to. Those rows are pruned
-// within a day.
+const EVENT_ID_INDEX = `${TableName.EventOutbox}_event_id_idx`;
+
+// The CloudEvents `id`, shared by every consumer's row for one event. It replaces `idempotencyKey`: an
+// emitter that needs a retry deduplicated derives the id instead of passing a key. Both stay for one
+// release, because a pod still on the previous release inserts into `idempotencyKey` and names its
+// unique index in ON CONFLICT during a rolling deploy; dropping either would fail that insert along with
+// the write it belongs to. For the same reason `eventId` is nullable with no default: `claimBatch` gives
+// those rows one.
export async function up(knex: Knex): Promise<void> {
- if (await knex.schema.hasColumn(TableName.EventOutbox, "eventId")) return;
+ if (!(await knex.schema.hasColumn(TableName.EventOutbox, "eventId"))) {
+ await knex.schema.alterTable(TableName.EventOutbox, (t) => {
+ t.uuid("eventId");
+ });
+ }
- await knex.schema.alterTable(TableName.EventOutbox, (t) => {
- t.uuid("eventId");
- });
+ // Not partial, so ON CONFLICT can name the columns. NULLs never conflict, so rows without an id
+ // still insert.
+ await knex.schema.raw(`
+ CREATE UNIQUE INDEX IF NOT EXISTS "${EVENT_ID_INDEX}"
+ ON "${TableName.EventOutbox}" ("consumer", "eventId")
+ `);
}
export async function down(knex: Knex): Promise<void> {
- if (!(await knex.schema.hasColumn(TableName.EventOutbox, "eventId"))) return;
+ await knex.schema.raw(`DROP INDEX IF EXISTS "${EVENT_ID_INDEX}"`);
- await knex.schema.alterTable(TableName.EventOutbox, (t) => {
- t.dropColumn("eventId");
- });
+ if (await knex.schema.hasColumn(TableName.EventOutbox, "eventId")) {
+ await knex.schema.alterTable(TableName.EventOutbox, (t) => {
+ t.dropColumn("eventId");
+ });
+ }
}
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
index 90439eb2f2..0759107de3 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
@@ -120,8 +120,7 @@ const createService = () => {
};
const eventEmitter = {
- emit: vi.fn<(event: unknown, tx?: unknown) => Promise<void>>(async () => undefined),
- emitMany: vi.fn(async () => undefined)
+ emit: vi.fn<(events: unknown[], tx?: unknown) => Promise<void>>(async () => undefined)
};
const projectDAL = {
@@ -408,7 +407,7 @@ describe("audit-log-stream-outbox-service enqueueForLogs batch fanout", () => {
kmsService: {} as never,
keyStore: keyStore as never,
queueService: queueService as never,
- eventEmitter: { emit: vi.fn(async () => undefined), emitMany: vi.fn(async () => undefined) }
+ eventEmitter: { emit: vi.fn(async () => undefined) }
});
return { service, auditLogStreamOutboxDAL, auditLogStreamDAL, projectDAL, keyStore, queueService };
@@ -620,8 +619,8 @@ describe("audit-log-stream-outbox-service sweepStaleClaims", () => {
{ streamId: "stream-b", orgId: ORG_ID, provider: LogProvider.Splunk }
]);
- const payloads = eventEmitter.emit.mock.calls.map(
- ([event]) => (event as { payload: { targetIds: string[]; droppedCount: number } }).payload
+ const payloads = eventEmitter.emit.mock.calls.flatMap(([events]) =>
+ (events as { data: { targetIds: string[]; droppedCount: number } }[]).map((event) => event.data)
);
expect(payloads).toHaveLength(2);
expect(payloads).toEqual(
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 efc6f5fc42..e85ee32a1f 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
@@ -1,3 +1,5 @@
+import crypto from "node:crypto";
+
import { Knex } from "knex";
import { z } from "zod";
@@ -51,5 +53,8 @@ export const emitAuditLogStreamDeliveryFailed = (
failingSince: failingSince.toISOString()
};
- return eventEmitter.emit({ eventType: AUDIT_LOG_STREAM_DELIVERY_FAILED_EVENT, payload }, tx);
+ return eventEmitter.emit(
+ [{ id: crypto.randomUUID(), type: AUDIT_LOG_STREAM_DELIVERY_FAILED_EVENT, data: payload }],
+ tx
+ );
};
diff --git a/backend/src/ee/services/pam-access-request/pam-access-request-events.ts b/backend/src/ee/services/pam-access-request/pam-access-request-events.ts
index 4908f3ee48..99b9cd66ee 100644
--- a/backend/src/ee/services/pam-access-request/pam-access-request-events.ts
+++ b/backend/src/ee/services/pam-access-request/pam-access-request-events.ts
@@ -1,3 +1,5 @@
+import crypto from "node:crypto";
+
import { ApprovalNotificationEvent } from "@app/services/approval-policy/approval-policy-enums";
import { TEventInput } from "@app/services/event-outbox/event-outbox-types";
@@ -30,8 +32,9 @@ export const buildPamAccessRequestEvent = ({
projectId: string;
folderId: string;
}): TEventInput => ({
- eventType: EVENT_BY_APPROVAL_EVENT[event],
- payload: {
+ id: crypto.randomUUID(),
+ type: EVENT_BY_APPROVAL_EVENT[event],
+ data: {
orgId,
projectId,
resourceType: PAM_FOLDER_RESOURCE_TYPE,
diff --git a/backend/src/ee/services/secret-replication/secret-replication-service.ts b/backend/src/ee/services/secret-replication/secret-replication-service.ts
index 09f6d09915..be424e2a41 100644
--- a/backend/src/ee/services/secret-replication/secret-replication-service.ts
+++ b/backend/src/ee/services/secret-replication/secret-replication-service.ts
@@ -392,13 +392,14 @@ export const secretReplicationServiceFactory = ({
},
tx
);
- await eventEmitter.emit(
- FolderModifiedEventV1.build({
+ await FolderModifiedEventV1.emit(
+ eventEmitter,
+ {
folderId: createdFolder.id,
projectId,
environment: destinationFolder.environmentSlug,
secretPath: destinationReplicationFolderPath
- }),
+ },
tx
);
return createdFolder;
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 d75d8aca7c..084ddce169 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
@@ -1212,7 +1212,7 @@ export const secretRotationV2ServiceFactory = ({
);
// Moved in place, so each secret keeps its id and is modified at both locations.
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
[
...secretModifiedEvents(
{ projectId, environment: environment.slug, secretPath: folder.path },
diff --git a/backend/src/lib/telemetry/metrics.ts b/backend/src/lib/telemetry/metrics.ts
index 7fd705a840..5d91a6f67d 100644
--- a/backend/src/lib/telemetry/metrics.ts
+++ b/backend/src/lib/telemetry/metrics.ts
@@ -831,28 +831,28 @@ export const recordEventOutboxExhaustedMetric = (params: { consumer: string; cou
eventOutboxExhaustedCounter.add(params.count, { "event_outbox.consumer": params.consumer });
});
-export enum EventOutboxDropReason {
+export enum EventOutboxRejectReason {
InvalidEvent = "invalid_event",
- PayloadTooLarge = "payload_too_large",
- PayloadRejected = "payload_rejected"
+ DataTooLarge = "data_too_large",
+ DataRejected = "data_rejected"
}
-export const eventOutboxDroppedCounter = infisicalCoreMeter.createCounter("infisical.event_outbox.dropped.count", {
+export const eventOutboxRejectedCounter = infisicalCoreMeter.createCounter("infisical.event_outbox.rejected.count", {
description:
- "Events never written to the outbox because the emit site built a bad one, by event type and reason. The business write still committed, so every one is a change no consumer will hear about. Alarm on any occurrence: it is a bug at the emit site.",
+ "Events the outbox refused because the emit site built a bad one, by event type and reason. The emit throws, so the business write the event belongs to fails with it. Alarm on any occurrence: it is a bug at the emit site.",
unit: "{event}"
});
-export const recordEventOutboxDroppedMetric = (params: {
+export const recordEventOutboxRejectedMetric = (params: {
eventType: string;
- reason: EventOutboxDropReason;
+ reason: EventOutboxRejectReason;
consumer?: string;
}) =>
safely(() => {
if (!isTelemetryEnabled()) return;
- eventOutboxDroppedCounter.add(1, {
+ eventOutboxRejectedCounter.add(1, {
"event_outbox.event_type": params.eventType,
- "event_outbox.drop_reason": params.reason,
+ "event_outbox.reject_reason": params.reason,
...(params.consumer ? { "event_outbox.consumer": params.consumer } : {})
});
});
diff --git a/backend/src/lib/telemetry/telemetry-attributes.ts b/backend/src/lib/telemetry/telemetry-attributes.ts
index d97965ebec..4e7e44b379 100644
--- a/backend/src/lib/telemetry/telemetry-attributes.ts
+++ b/backend/src/lib/telemetry/telemetry-attributes.ts
@@ -26,7 +26,7 @@ export const INFISICAL_CORE_METER_ATTRIBUTES = [
"event_outbox.consumer",
"event_outbox.status",
"event_outbox.event_type",
- "event_outbox.drop_reason",
+ "event_outbox.reject_reason",
"scim.operation",
"sso.provider",
"sso.action",
diff --git a/backend/src/services/alert/alert-event-consumer.test.ts b/backend/src/services/alert/alert-event-consumer.test.ts
index 7ef5ef92b5..98ee6cce38 100644
--- a/backend/src/services/alert/alert-event-consumer.test.ts
+++ b/backend/src/services/alert/alert-event-consumer.test.ts
@@ -24,14 +24,15 @@ const makePayload = (overrides?: Record<string, unknown>) => ({
...overrides
});
-const makeEvent = (overrides?: Partial<TEvent>): TEvent =>
- ({
- id: 1,
- eventType: EVENT_TYPE,
- payload: makePayload(),
- occurredAt: new Date(),
- ...overrides
- }) as TEvent;
+const makeEvent = (overrides?: Partial<TEvent>): TEvent => ({
+ specversion: "1.0",
+ id: "event-1",
+ source: "urn:infisical:approval",
+ type: EVENT_TYPE,
+ time: new Date().toISOString(),
+ data: makePayload(),
+ ...overrides
+});
const buildConsumer = (opts?: {
alerts?: unknown[];
@@ -111,7 +112,7 @@ describe("alert event consumer", () => {
test("accepts a scope-wide event that carries no resource", async () => {
const { consumer, runs } = buildConsumer();
- const [result] = await consumer.handle([makeEvent({ payload: makePayload({ resourceId: null }) })]);
+ const [result] = await consumer.handle([makeEvent({ data: makePayload({ resourceId: null }) })]);
expect(result.status).toBe(EventResultStatus.Delivered);
expect(runs[0].targetIds).toEqual(["req-1"]);
@@ -155,14 +156,14 @@ describe("alert event consumer", () => {
);
});
- // The outbox row id is stable across attempts, so it's the only key a retry can use to find what
- // already went out.
+ // The event id is stable across attempts, so it's the only key a retry can use to find what already
+ // went out.
test("hands the engine the event id so a retry can skip channels that already delivered", async () => {
const { consumer, runs } = buildConsumer();
- await consumer.handle([makeEvent({ id: 42 })]);
+ await consumer.handle([makeEvent({ id: "550e8400-e29b-41d4-a716-446655440000" })]);
- expect(runs[0].eventId).toBe("42");
+ expect(runs[0].eventId).toBe("550e8400-e29b-41d4-a716-446655440000");
});
// The alert was deleted or disabled before the worker ran, so nobody is owed a notification. That's
@@ -189,7 +190,7 @@ describe("alert event consumer", () => {
test("fails terminally on an unreadable payload", async () => {
const { consumer } = buildConsumer();
- const [result] = await consumer.handle([makeEvent({ payload: makePayload({ targetIds: [] }) })]);
+ const [result] = await consumer.handle([makeEvent({ data: makePayload({ targetIds: [] }) })]);
expect(result.status).toBe(EventResultStatus.Failed);
expect(result.error).toContain("Unreadable alert event payload");
@@ -212,7 +213,7 @@ describe("alert event consumer", () => {
test("fails terminally when no provider declares the event for that resource type", async () => {
const { consumer, runs, getLookups } = buildConsumer();
- const [result] = await consumer.handle([makeEvent({ payload: makePayload({ resourceType: "pki.certificate" }) })]);
+ const [result] = await consumer.handle([makeEvent({ data: makePayload({ resourceType: "pki.certificate" }) })]);
expect(result.status).toBe(EventResultStatus.Failed);
expect(result.error).toContain("pki.certificate");
@@ -235,9 +236,9 @@ describe("alert event consumer", () => {
});
const results = await consumer.handle([
- makeEvent({ id: 1, eventType: EVENT_TYPE }),
- makeEvent({ id: 2, eventType: "approval.workflow.request_closed" }),
- makeEvent({ id: 3, eventType: "approval.workflow.request_reopened" })
+ makeEvent({ id: "event-1", type: EVENT_TYPE }),
+ makeEvent({ id: "event-2", type: "approval.workflow.request_closed" }),
+ makeEvent({ id: "event-3", type: "approval.workflow.request_reopened" })
]);
expect(results.map((result) => result.status)).toEqual([
@@ -253,9 +254,9 @@ describe("alert event consumer", () => {
const { consumer, getLookups } = buildConsumer();
await consumer.handle([
- makeEvent({ id: 1, payload: makePayload({ targetIds: ["req-1"] }) }),
- makeEvent({ id: 2, payload: makePayload({ targetIds: ["req-2"] }) }),
- makeEvent({ id: 3, payload: makePayload({ targetIds: ["req-3"] }) })
+ makeEvent({ id: "event-1", data: makePayload({ targetIds: ["req-1"] }) }),
+ makeEvent({ id: "event-2", data: makePayload({ targetIds: ["req-2"] }) }),
+ makeEvent({ id: "event-3", data: makePayload({ targetIds: ["req-3"] }) })
]);
expect(getLookups()).toBe(1);
@@ -267,9 +268,9 @@ describe("alert event consumer", () => {
const { consumer, getLookups } = buildConsumer();
await consumer.handle([
- makeEvent({ id: 1, payload: makePayload({ resourceId: "policy-1", targetIds: ["req-1"] }) }),
- makeEvent({ id: 2, payload: makePayload({ resourceId: "policy-2", targetIds: ["req-2"] }) }),
- makeEvent({ id: 3, payload: makePayload({ resourceId: "policy-1", targetIds: ["req-3"] }) })
+ makeEvent({ id: "event-1", data: makePayload({ resourceId: "policy-1", targetIds: ["req-1"] }) }),
+ makeEvent({ id: "event-2", data: makePayload({ resourceId: "policy-2", targetIds: ["req-2"] }) }),
+ makeEvent({ id: "event-3", data: makePayload({ resourceId: "policy-1", targetIds: ["req-3"] }) })
]);
expect(getLookups()).toBe(2);
@@ -281,11 +282,11 @@ describe("alert event consumer", () => {
const { consumer, runs } = buildConsumer();
const results = await consumer.handle([
- makeEvent({ id: 1, payload: makePayload({ targetIds: ["req-1"] }) }),
- makeEvent({ id: 2, payload: makePayload({ targetIds: ["req-2"] }) })
+ makeEvent({ id: "event-1", data: makePayload({ targetIds: ["req-1"] }) }),
+ makeEvent({ id: "event-2", data: makePayload({ targetIds: ["req-2"] }) })
]);
- expect(results.map((result) => result.id)).toEqual(["1", "2"]);
+ expect(results.map((result) => result.id)).toEqual(["event-1", "event-2"]);
expect(runs.map((run) => run.targetIds)).toEqual([["req-1"], ["req-2"]]);
});
});
diff --git a/backend/src/services/alert/alert-event-consumer.ts b/backend/src/services/alert/alert-event-consumer.ts
index 4765954f3c..d115eaaf88 100644
--- a/backend/src/services/alert/alert-event-consumer.ts
+++ b/backend/src/services/alert/alert-event-consumer.ts
@@ -50,9 +50,9 @@ export const alertEventConsumerFactory = ({
);
const $handleEvent = async (event: TEvent, findAlerts: TAlertLookup): Promise<TEventConsumerResult> => {
- const id = String(event.id);
+ const { id } = event;
- const payload = AlertEventPayloadSchema.safeParse(event.payload);
+ const payload = AlertEventPayloadSchema.safeParse(event.data);
if (!payload.success) {
return {
id,
@@ -62,17 +62,17 @@ export const alertEventConsumerFactory = ({
}
const { orgId, projectId, resourceType, resourceId, targetIds } = payload.data;
- if (!$isDeclaredEvent(event.eventType, resourceType)) {
+ if (!$isDeclaredEvent(event.type, resourceType)) {
return {
id,
status: EventResultStatus.Failed,
- error: `No alert provider for resource type '${resourceType}' declares '${event.eventType}' as an event-triggered alert`
+ error: `No alert provider for resource type '${resourceType}' declares '${event.type}' as an event-triggered alert`
};
}
- const sourcedEvents = alertProviderRegistry.findEventsBySource({ resourceType, eventKey: event.eventType });
+ const sourcedEvents = alertProviderRegistry.findEventsBySource({ resourceType, eventKey: event.type });
const lookups = [
- { resourceType, resourceId, eventType: event.eventType },
+ { resourceType, resourceId, eventType: event.type },
...sourcedEvents.map((sourced) => ({
resourceType: sourced.resourceType,
resourceId: null,
@@ -93,9 +93,9 @@ export const alertEventConsumerFactory = ({
const outcome = await alertEngine.runAlertForEvent(alert, {
eventId: id,
eventType: alert.eventType,
- occurredAt: event.occurredAt,
+ occurredAt: new Date(event.time),
targetIds,
- payload: (event.payload ?? {}) as Record<string, unknown>
+ payload: (event.data ?? {}) as Record<string, unknown>
});
recordAlertDispatchOutcomeMetric({ resourceType: alert.resourceType, outcome });
@@ -136,7 +136,7 @@ export const alertEventConsumerFactory = ({
results.push(await $handleEvent(event, findAlerts));
} catch (error) {
results.push({
- id: String(event.id),
+ id: event.id,
status: EventResultStatus.Retry,
error: error instanceof Error ? error.message : "Unknown error"
});
@@ -148,7 +148,7 @@ export const alertEventConsumerFactory = ({
return {
name: ALERT_EVENT_CONSUMER,
- payloadSchema: AlertEventPayloadSchema,
+ dataSchema: AlertEventPayloadSchema,
subscribesTo,
handle
};
diff --git a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
index 80bc32a415..f2c4e7d46f 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
@@ -200,8 +200,7 @@ describe("identity credential alert provider", () => {
test("the emitted payload is accepted by the delivery schema", async () => {
const emitted: unknown[] = [];
const emitter = {
- emit: async (event: { payload: unknown }) => void emitted.push(event.payload),
- emitMany: async () => {}
+ emit: async (events: { data: unknown }[]) => void emitted.push(...events.map((event) => event.data))
};
const membership = { identity: { id: "ident-1", projectId: null }, scopeOrgId: "org-1" };
diff --git a/backend/src/services/alert/providers/secret-reminder-alert-provider.test.ts b/backend/src/services/alert/providers/secret-reminder-alert-provider.test.ts
index 464d1a0d12..b37074f358 100644
--- a/backend/src/services/alert/providers/secret-reminder-alert-provider.test.ts
+++ b/backend/src/services/alert/providers/secret-reminder-alert-provider.test.ts
@@ -104,10 +104,9 @@ describe("secret reminder alert provider", () => {
let emitted: Record<string, unknown> | undefined;
await emitSecretReminderDue(
{
- emit: async (event) => {
- emitted = event.payload;
- },
- emitMany: async () => {}
+ emit: async ([event]) => {
+ emitted = event.data;
+ }
},
{
orgId: "org-1",
diff --git a/backend/src/services/approval-policy/approval-policy-service.ts b/backend/src/services/approval-policy/approval-policy-service.ts
index 10da716c92..5701adf81a 100644
--- a/backend/src/services/approval-policy/approval-policy-service.ts
+++ b/backend/src/services/approval-policy/approval-policy-service.ts
@@ -173,7 +173,7 @@ export const approvalPolicyServiceFactory = ({
const $emitEvent = async (args: { event: ApprovalNotificationEvent; request: TApprovalRequests }, tx: Knex) => {
const event = resources[args.request.type as ApprovalPolicyType]?.buildEvent?.(args);
- if (event) await eventEmitter.emit(event, tx);
+ if (event) await eventEmitter.emit([event], tx);
};
const $buildDecorationContext = (actor: TApprovalActor): TDecorationContext => {
diff --git a/backend/src/services/certificate/certificate-alert-events.test.ts b/backend/src/services/certificate/certificate-alert-events.test.ts
index 0bae382ab2..08397cda04 100644
--- a/backend/src/services/certificate/certificate-alert-events.test.ts
+++ b/backend/src/services/certificate/certificate-alert-events.test.ts
@@ -5,11 +5,11 @@ import {
} from "./certificate-alert-events";
const buildEmitter = () => {
- const emitted: { eventType: string; payload: Record<string, unknown> }[] = [];
+ const emitted: { id: string; type: string; data: Record<string, unknown> }[] = [];
const emitter = certificateAlertEventEmitterFactory({
eventEmitter: {
- emit: async (event: { eventType: string; payload: Record<string, unknown> }) => {
- emitted.push(event);
+ emit: async (events: { id: string; type: string; data: Record<string, unknown> }[]) => {
+ emitted.push(...events);
}
} as never,
projectDAL: { findById: async () => ({ orgId: "org-1" }) } as never
@@ -27,8 +27,9 @@ describe("certificate alert event emitter", () => {
expect(emitted).toEqual([
{
- eventType: CertificateApplicationAlertEvent.Issuance,
- payload: {
+ id: expect.any(String),
+ type: CertificateApplicationAlertEvent.Issuance,
+ data: {
orgId: "org-1",
projectId: "proj-1",
resourceType: CERT_MANAGER_APPLICATION_RESOURCE_TYPE,
@@ -52,7 +53,7 @@ describe("certificate alert event emitter", () => {
{} as never
);
- expect(emitted.map((event) => [event.eventType, event.payload.resourceType, event.payload.resourceId])).toEqual([
+ expect(emitted.map((event) => [event.type, event.data.resourceType, event.data.resourceId])).toEqual([
[CertificateApplicationAlertEvent.Revocation, CERT_MANAGER_APPLICATION_RESOURCE_TYPE, "app-1"]
]);
});
diff --git a/backend/src/services/certificate/certificate-alert-events.ts b/backend/src/services/certificate/certificate-alert-events.ts
index 6c06c79de1..ea1fd0819f 100644
--- a/backend/src/services/certificate/certificate-alert-events.ts
+++ b/backend/src/services/certificate/certificate-alert-events.ts
@@ -1,3 +1,5 @@
+import crypto from "node:crypto";
+
import { Knex } from "knex";
import { logger } from "@app/lib/logger";
@@ -49,8 +51,9 @@ export const certificateAlertEventEmitterFactory = ({ eventEmitter, projectDAL }
if (!resolvedOrgId) return;
const event = {
- eventType,
- payload: {
+ id: crypto.randomUUID(),
+ type: eventType,
+ data: {
orgId: resolvedOrgId,
projectId,
resourceType: CERT_MANAGER_APPLICATION_RESOURCE_TYPE,
@@ -59,7 +62,7 @@ export const certificateAlertEventEmitterFactory = ({ eventEmitter, projectDAL }
}
};
- await eventEmitter.emit(event, tx);
+ await eventEmitter.emit([event], tx);
};
const emit = (input: TCertificateAlertEventInput, tx: Knex) => $emit(input, tx);
diff --git a/backend/src/services/event-outbox/event-definition.test.ts b/backend/src/services/event-outbox/event-definition.test.ts
index 1e2995537c..2f9d12ae1d 100644
--- a/backend/src/services/event-outbox/event-definition.test.ts
+++ b/backend/src/services/event-outbox/event-definition.test.ts
@@ -1,7 +1,7 @@
import { z } from "zod";
-import { defineEvent, toCloudEvent } from "./event-definition";
-import { EventInputSchema } from "./event-outbox-types";
+import { defineEvent, eventSource } from "./event-definition";
+import { EventInputSchema, TEventInput } from "./event-outbox-types";
const ExampleEvent = defineEvent({
domain: "secrets",
@@ -12,20 +12,42 @@ const ExampleEvent = defineEvent({
});
describe("defineEvent", () => {
- test("names the event and its source by the ADR convention", () => {
+ test("names the event by the ADR convention", () => {
expect(ExampleEvent.type).toBe("com.infisical.secrets.secret-import.modified.v2");
- expect(ExampleEvent.source).toBe("urn:infisical:secrets");
});
- // The outbox validates every emitted key; a definition must never produce one it drops.
- test("builds an input the outbox accepts", () => {
+ // The outbox validates every emitted event; a definition must never produce one it rejects.
+ test("builds an input the outbox accepts, with a UUID of its own", () => {
const input = ExampleEvent.build({ secretImportId: "import-1" });
expect(input).toEqual({
- eventType: "com.infisical.secrets.secret-import.modified.v2",
- payload: { secretImportId: "import-1" }
+ id: expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/),
+ type: "com.infisical.secrets.secret-import.modified.v2",
+ data: { secretImportId: "import-1" }
});
expect(EventInputSchema.safeParse(input).success).toBe(true);
+ expect(ExampleEvent.build({ secretImportId: "import-1" }).id).not.toBe(input.id);
+ });
+
+ test("emits the built event through the emitter, inside the caller's tx", async () => {
+ const calls: { events: TEventInput[]; tx: unknown }[] = [];
+ const tx = { sentinel: true } as never;
+
+ await ExampleEvent.emit(
+ {
+ emit: async (events, emitTx) => {
+ calls.push({ events, tx: emitTx });
+ }
+ },
+ { secretImportId: "import-1" },
+ tx
+ );
+
+ expect(calls).toHaveLength(1);
+ expect(calls[0].tx).toBe(tx);
+ expect(calls[0].events).toEqual([
+ expect.objectContaining({ type: ExampleEvent.type, data: { secretImportId: "import-1" } })
+ ]);
});
test.each([
@@ -53,33 +75,12 @@ describe("defineEvent", () => {
});
});
-describe("toCloudEvent", () => {
- const event = {
- id: 42,
- eventId: "550e8400-e29b-41d4-a716-446655440000",
- eventType: ExampleEvent.type,
- payload: { secretImportId: "import-1" },
- occurredAt: new Date("2026-08-20T17:42:31.000Z")
- };
-
- test("maps the stored event onto the CloudEvents envelope", () => {
- expect(toCloudEvent(ExampleEvent, event)).toEqual({
- specversion: "1.0",
- id: "550e8400-e29b-41d4-a716-446655440000",
- source: "urn:infisical:secrets",
- type: "com.infisical.secrets.secret-import.modified.v2",
- time: "2026-08-20T17:42:31.000Z",
- data: { secretImportId: "import-1" }
- });
+describe("eventSource", () => {
+ test("is the domain of an event named by the ADR convention", () => {
+ expect(eventSource(ExampleEvent.type)).toBe("urn:infisical:secrets");
});
- test("refuses an event recorded without an eventId", () => {
- expect(() => toCloudEvent(ExampleEvent, { ...event, eventId: null })).toThrow("without an eventId");
- });
-
- test("refuses an event of another type", () => {
- expect(() =>
- toCloudEvent(ExampleEvent, { ...event, eventType: "com.infisical.secrets.secret.modified.v1" })
- ).toThrow("cannot be read as");
+ test("is the first segment of an event named before the ADR", () => {
+ expect(eventSource("identity.authentication.auth-method-changed")).toBe("urn:infisical:identity");
});
});
diff --git a/backend/src/services/event-outbox/event-definition.ts b/backend/src/services/event-outbox/event-definition.ts
index c90e509505..2285ac24c6 100644
--- a/backend/src/services/event-outbox/event-definition.ts
+++ b/backend/src/services/event-outbox/event-definition.ts
@@ -1,7 +1,10 @@
+import crypto from "node:crypto";
+
+import { Knex } from "knex";
import RE2 from "re2";
import { z } from "zod";
-import { TEvent, TEventInput } from "./event-outbox-types";
+import { TEventEmitter, TEventInput } from "./event-outbox-types";
// The naming and envelope rules from the Event Standards and Versioning ADR, which adopts CloudEvents
// 1.0: an event is named com.infisical.<domain>.<entity>.<action>.v<version>, every segment
@@ -34,45 +37,23 @@ export const defineEvent = <TData extends z.AnyZodObject>({
}
const type = `com.infisical.${domain}.${entity}.${action}.v${version}`;
+ const build = (eventData: z.input<TData>): TEventInput => ({ id: crypto.randomUUID(), type, data: eventData });
return {
type,
- source: `urn:infisical:${domain}`,
data,
- build: (payload: z.input<TData>): TEventInput => ({ eventType: type, payload })
+ build,
+ emit: (eventEmitter: TEventEmitter, eventData: z.input<TData>, tx?: Knex): Promise<void> =>
+ eventEmitter.emit([build(eventData)], tx)
};
};
export type TEventDefinition<TData extends z.AnyZodObject> = ReturnType<typeof defineEvent<TData>>;
-// The CloudEvents 1.0 envelope with the ADR's constraints. Consumers inside the backend read `TEvent`;
-// this is the shape an event takes when it leaves the backend (eg a customer webhook).
-export type TCloudEvent<TData> = {
- specversion: "1.0";
- id: string;
- source: string;
- type: string;
- time: string;
- data: TData;
-};
-
-export const toCloudEvent = <TData extends z.AnyZodObject>(
- definition: TEventDefinition<TData>,
- event: TEvent
-): TCloudEvent<z.infer<TData>> => {
- if (event.eventType !== definition.type) {
- throw new Error(`Event '${event.eventType}' cannot be read as '${definition.type}'`);
- }
- if (!event.eventId) {
- throw new Error(`Event '${event.id}' was recorded without an eventId, by a release that predates it`);
- }
-
- return {
- specversion: "1.0",
- id: event.eventId,
- source: definition.source,
- type: definition.type,
- time: new Date(event.occurredAt).toISOString(),
- data: definition.data.parse(event.payload)
- };
+// `urn:infisical:<domain>`. Events named before the ADR (eg `identity.authentication.auth-method-changed`)
+// have no domain segment, so their first segment stands in for it.
+export const eventSource = (type: string): string => {
+ const segments = type.split(".");
+ const domain = type.startsWith("com.infisical.") ? segments[2] : segments[0];
+ return `urn:infisical:${domain}`;
};
diff --git a/backend/src/services/event-outbox/event-outbox-dal.test.ts b/backend/src/services/event-outbox/event-outbox-dal.test.ts
index 7c0606855f..30b28cfc45 100644
--- a/backend/src/services/event-outbox/event-outbox-dal.test.ts
+++ b/backend/src/services/event-outbox/event-outbox-dal.test.ts
@@ -109,9 +109,8 @@ const buildDAL = (opts?: { returning?: unknown[]; updated?: number; selected?: u
};
describe("event outbox dal", () => {
- // Postgres can't infer a partial unique index from a bare column list, so the conflict target has to
- // repeat the predicate or it's a 42P10 at runtime.
- test("insertEvents ignores a conflict on the partial idempotency index", async () => {
+ // An emitter that derives its event id (eg a reminder occurrence) relies on this to drop a retry.
+ test("insertEvents ignores an event the consumer already has", async () => {
const { dal, calls, tx } = buildDAL();
await dal.insertEvents(
@@ -127,7 +126,7 @@ describe("event outbox dal", () => {
tx
);
- expect(calls.onConflict[0]).toBe('("consumer", "idempotencyKey") WHERE "idempotencyKey" IS NOT NULL');
+ expect(calls.onConflict[0]).toEqual(["consumer", "eventId"]);
expect(calls.ignored).toBe(1);
});
diff --git a/backend/src/services/event-outbox/event-outbox-dal.ts b/backend/src/services/event-outbox/event-outbox-dal.ts
index 7a0d28f021..f3c49c09d5 100644
--- a/backend/src/services/event-outbox/event-outbox-dal.ts
+++ b/backend/src/services/event-outbox/event-outbox-dal.ts
@@ -11,9 +11,12 @@ import { computeBackoffMs, EventOutboxStatus, TOutboxFlushKey } from "./event-ou
export type TEventOutboxDALFactory = ReturnType<typeof eventOutboxDALFactory>;
+// A claim always carries an eventId, even on a row written without one (see claimBatch).
+export type TClaimedOutboxRow = TEventOutbox & { eventId: string };
+
export type TOutboxClaim = {
lockToken: string;
- rows: TEventOutbox[];
+ rows: TClaimedOutboxRow[];
};
export type TOutboxInsertRow = {
@@ -21,12 +24,11 @@ export type TOutboxInsertRow = {
eventId: string;
eventType: string;
payload: unknown;
- idempotencyKey?: string | null;
occurredAt: Date;
};
-// Postgres caps a statement at 65535 bind parameters, and a row takes seven, so a bulk secret write
-// emitting one event per secret would otherwise fail the whole write past ~9k secrets.
+// Postgres caps a statement at 65535 bind parameters, and a row takes five, so a bulk secret write
+// emitting one event per secret would otherwise fail the whole write past ~13k secrets.
const INSERT_BATCH_SIZE = 1000;
export const eventOutboxDALFactory = (db: TDbClient) => {
@@ -39,11 +41,10 @@ export const eventOutboxDALFactory = (db: TDbClient) => {
.insert(
chunk.map((row) => ({
...row,
- payload: JSON.stringify(row.payload),
- idempotencyKey: row.idempotencyKey ?? null
+ payload: JSON.stringify(row.payload)
}))
)
- .onConflict(db.raw('("consumer", "idempotencyKey") WHERE "idempotencyKey" IS NOT NULL'))
+ .onConflict(["consumer", "eventId"])
.ignore();
}
} catch (error) {
@@ -69,7 +70,9 @@ export const eventOutboxDALFactory = (db: TDbClient) => {
};
// One statement on purpose so the row locks only live as long as the UPDATE. The fresh lockToken is
- // how extendClaims and commitResults tell this claim apart from a later one on the same rows.
+ // how extendClaims and commitResults tell this claim apart from a later one on the same rows. A pod on
+ // the previous release writes rows without an eventId during a rolling deploy, so the claim gives
+ // them one; consumers rely on every event having an id.
const claimBatch = async (key: TOutboxFlushKey, limit: number): Promise<TOutboxClaim> => {
const lockToken = crypto.randomUUID();
try {
@@ -86,11 +89,16 @@ export const eventOutboxDALFactory = (db: TDbClient) => {
.forUpdate()
.skipLocked();
})
- .update({ status: EventOutboxStatus.Processing, lockedAt: db.fn.now(), lockToken })
+ .update({
+ status: EventOutboxStatus.Processing,
+ lockedAt: db.fn.now(),
+ lockToken,
+ eventId: db.raw('COALESCE("eventId", gen_random_uuid())')
+ })
.returning("*");
// RETURNING order is arbitrary, and handle() is promised id order.
- const rows = (claimed as unknown as TEventOutbox[]).sort((a, b) => (BigInt(a.id) < BigInt(b.id) ? -1 : 1));
+ const rows = (claimed as unknown as TClaimedOutboxRow[]).sort((a, b) => (BigInt(a.id) < BigInt(b.id) ? -1 : 1));
return { lockToken, rows };
} catch (error) {
throw new DatabaseError({ error, name: "EventOutbox: claimBatch" });
diff --git a/backend/src/services/event-outbox/event-outbox-service.test.ts b/backend/src/services/event-outbox/event-outbox-service.test.ts
index 5270e1582c..6bfed7c8f1 100644
--- a/backend/src/services/event-outbox/event-outbox-service.test.ts
+++ b/backend/src/services/event-outbox/event-outbox-service.test.ts
@@ -1,7 +1,8 @@
-import { z } from "zod";
+import crypto from "node:crypto";
-import { TEventOutbox } from "@app/db/schemas";
+import { z } from "zod";
+import { TClaimedOutboxRow } from "./event-outbox-dal";
import { eventOutboxRegistryFactory } from "./event-outbox-registry";
import { eventOutboxServiceFactory } from "./event-outbox-service";
import {
@@ -19,22 +20,22 @@ vi.mock("@app/lib/logger", () => ({
initLogger: () => {}
}));
-const { exhaustedMetrics, droppedMetrics } = vi.hoisted(() => ({
+const { exhaustedMetrics, rejectedMetrics } = vi.hoisted(() => ({
exhaustedMetrics: [] as { consumer: string; count: number }[],
- droppedMetrics: [] as { eventType: string; reason: string; consumer?: string }[]
+ rejectedMetrics: [] as { eventType: string; reason: string; consumer?: string }[]
}));
vi.mock("@app/lib/telemetry/metrics", () => ({
- EventOutboxDropReason: {
+ EventOutboxRejectReason: {
InvalidEvent: "invalid_event",
- PayloadTooLarge: "payload_too_large",
- PayloadRejected: "payload_rejected"
+ DataTooLarge: "data_too_large",
+ DataRejected: "data_rejected"
},
recordEventOutboxLagMetric: () => {},
recordEventOutboxExhaustedMetric: (params: { consumer: string; count: number }) => {
exhaustedMetrics.push(params);
},
- recordEventOutboxDroppedMetric: (params: { eventType: string; reason: string; consumer?: string }) => {
- droppedMetrics.push(params);
+ recordEventOutboxRejectedMetric: (params: { eventType: string; reason: string; consumer?: string }) => {
+ rejectedMetrics.push(params);
}
}));
@@ -49,16 +50,17 @@ const makePayload = (overrides?: Record<string, unknown>) => ({
});
const makeEvent = (overrides?: Partial<TEventInput>): TEventInput => ({
- eventType: "approval.workflow.request_opened",
- payload: makePayload(),
+ id: crypto.randomUUID(),
+ type: "approval.workflow.request_opened",
+ data: makePayload(),
...overrides
});
const makeConsumer = (overrides?: Partial<IEventConsumer>): IEventConsumer =>
({
name: "alert",
- payloadSchema: z.object({ targetIds: z.array(z.string()).min(1) }),
- subscribesTo: (eventType: string) => eventType === "approval.workflow.request_opened",
+ dataSchema: z.object({ targetIds: z.array(z.string()).min(1) }),
+ subscribesTo: (type: string) => type === "approval.workflow.request_opened",
handle: async () => [],
...overrides
}) as IEventConsumer;
@@ -91,7 +93,7 @@ describe("event outbox emit", () => {
makeConsumer({ name: "other", subscribesTo: () => false })
]);
- await service.emit(makeEvent(), TX);
+ await service.emit([makeEvent()], TX);
expect(insertedRows()).toHaveLength(1);
expect((insertedRows()[0] as { consumer: string }).consumer).toBe("alert");
@@ -100,7 +102,7 @@ describe("event outbox emit", () => {
test("does not touch the database when no consumer handles the event type", async () => {
const { service, inserted } = buildService([makeConsumer({ subscribesTo: () => false })]);
- await service.emit(makeEvent(), TX);
+ await service.emit([makeEvent()], TX);
expect(inserted).toHaveLength(0);
});
@@ -110,96 +112,95 @@ describe("event outbox emit", () => {
test("writes a row without consulting the database about the resource", async () => {
const { service, inserted } = buildService([makeConsumer()]);
- await service.emit(makeEvent(), TX);
+ await service.emit([makeEvent()], TX);
expect(inserted).toHaveLength(1);
expect(inserted[0].tx).toBe(TX);
});
- // A bad event is a bug at the emit site, but it must not fail the business write it describes.
- describe("drops a malformed event without failing the write", () => {
+ // Committing the business write without its event would break the guarantee that consumers hear
+ // about every change, so a bad event fails the caller's transaction.
+ describe("rejects a malformed event, failing the write it belongs to", () => {
beforeEach(() => {
- droppedMetrics.length = 0;
+ rejectedMetrics.length = 0;
});
- test("when the payload does not match the consumer's schema", async () => {
- const { service, inserted } = buildService([makeConsumer()]);
+ test("when the data does not match a consumer's schema", async () => {
+ const { service, inserted } = buildService([
+ makeConsumer({ name: "lenient", dataSchema: z.object({}) as never }),
+ makeConsumer({ name: "alert" })
+ ]);
- await service.emit(makeEvent({ payload: makePayload({ targetIds: [] }) }), TX);
+ await expect(service.emit([makeEvent({ data: makePayload({ targetIds: [] }) })], TX)).rejects.toThrow(
+ "could not record an event for this change"
+ );
expect(inserted).toHaveLength(0);
- expect(droppedMetrics).toEqual([
- { eventType: "approval.workflow.request_opened", reason: "payload_rejected", consumer: "alert" }
+ expect(rejectedMetrics).toEqual([
+ { eventType: "approval.workflow.request_opened", reason: "data_rejected", consumer: "alert" }
]);
});
- test("only for the consumer that rejects it", async () => {
- const { service, insertedRows } = buildService([
- makeConsumer({ name: "alert" }),
- makeConsumer({ name: "lenient", payloadSchema: z.object({}) as never })
+ test("when the data is oversized", async () => {
+ const { service, inserted } = buildService([
+ makeConsumer({ dataSchema: z.object({ blob: z.string() }) as never })
]);
- await service.emit(makeEvent({ payload: makePayload({ targetIds: [] }) }), TX);
+ await expect(
+ service.emit([makeEvent({ data: { blob: "x".repeat(MAX_OUTBOX_PAYLOAD_BYTES + 1) } })], TX)
+ ).rejects.toThrow();
- expect(insertedRows().map((row) => (row as { consumer: string }).consumer)).toEqual(["lenient"]);
+ expect(inserted).toHaveLength(0);
+ expect(rejectedMetrics).toEqual([{ eventType: "approval.workflow.request_opened", reason: "data_too_large" }]);
});
- test("when the payload is oversized", async () => {
- const { service, inserted } = buildService([
- makeConsumer({ payloadSchema: z.object({ blob: z.string() }) as never })
- ]);
+ test("when the id is not a UUID", async () => {
+ const { service, inserted } = buildService([makeConsumer()]);
- await service.emit(makeEvent({ payload: { blob: "x".repeat(MAX_OUTBOX_PAYLOAD_BYTES + 1) } }), TX);
+ await expect(service.emit([makeEvent({ id: "reminder-1" })], TX)).rejects.toThrow();
expect(inserted).toHaveLength(0);
- expect(droppedMetrics).toEqual([{ eventType: "approval.workflow.request_opened", reason: "payload_too_large" }]);
+ expect(rejectedMetrics).toEqual([{ eventType: "approval.workflow.request_opened", reason: "invalid_event" }]);
});
test("when the envelope is malformed", async () => {
const { service, inserted } = buildService([makeConsumer()]);
- await service.emit(makeEvent({ eventType: "Not A Key" }), TX);
+ await expect(service.emit([makeEvent({ type: "Not A Key" })], TX)).rejects.toThrow();
expect(inserted).toHaveLength(0);
- expect(droppedMetrics).toEqual([{ eventType: "Not A Key", reason: "invalid_event" }]);
+ expect(rejectedMetrics).toEqual([{ eventType: "Not A Key", reason: "invalid_event" }]);
});
- test("and still writes the well-formed events emitted with it", async () => {
- const { service, insertedRows } = buildService([makeConsumer()]);
+ test("without writing the well-formed events emitted with it", async () => {
+ const { service, inserted } = buildService([makeConsumer()]);
- await service.emitMany([makeEvent({ eventType: "Not A Key" }), makeEvent()], TX);
+ await expect(service.emit([makeEvent(), makeEvent({ type: "Not A Key" })], TX)).rejects.toThrow();
- expect(insertedRows()).toHaveLength(1);
+ expect(inserted).toHaveLength(0);
});
});
- test("gives every consumer's row for one event the same eventId", async () => {
+ test("gives every consumer's row for one event the event's id", async () => {
const { service, insertedRows } = buildService([makeConsumer({ name: "alert" }), makeConsumer({ name: "other" })]);
+ const event = makeEvent();
- await service.emit(makeEvent(), TX);
+ await service.emit([event], TX);
- const [first, second] = insertedRows() as { eventId: string }[];
- expect(first.eventId).toMatch(/^[0-9a-f-]{36}$/);
- expect(second.eventId).toBe(first.eventId);
+ expect((insertedRows() as { eventId: string }[]).map((row) => row.eventId)).toEqual([event.id, event.id]);
});
- test("writes many events in one insert, each with its own eventId, inside the caller's tx", async () => {
+ test("writes many events in one insert inside the caller's tx", async () => {
const { service, inserted } = buildService([makeConsumer()]);
+ const events = [makeEvent(), makeEvent(), makeEvent()];
- await service.emitMany([makeEvent(), makeEvent(), makeEvent()], TX);
+ await service.emit(events, TX);
expect(inserted).toHaveLength(1);
expect(inserted[0].tx).toBe(TX);
- const eventIds = (inserted[0].rows as { eventId: string }[]).map((row) => row.eventId);
- expect(new Set(eventIds).size).toBe(3);
- });
-
- test("does not touch the database when every event is dropped", async () => {
- const { service, inserted } = buildService([makeConsumer()]);
-
- await service.emitMany([makeEvent({ eventType: "Not A Key" })], TX);
-
- expect(inserted).toHaveLength(0);
+ expect((inserted[0].rows as { eventId: string }[]).map((row) => row.eventId)).toEqual(
+ events.map((event) => event.id)
+ );
});
// Swallowing it would hand the caller a poisoned transaction instead of the real error.
@@ -215,19 +216,19 @@ describe("event outbox emit", () => {
eventOutboxRegistry: registry
});
- await expect(service.emit(makeEvent(), TX)).rejects.toThrow("connection terminated");
+ await expect(service.emit([makeEvent()], TX)).rejects.toThrow("connection terminated");
});
});
describe("event outbox drain", () => {
- const makeRow = (overrides?: Partial<TEventOutbox>): TEventOutbox =>
+ const makeRow = (overrides?: Partial<TClaimedOutboxRow>): TClaimedOutboxRow =>
({
id: 1,
+ eventId: `event-${overrides?.id ?? 1}`,
consumer: "alert",
eventType: "approval.workflow.request_opened",
payload: makePayload(),
- idempotencyKey: null,
- occurredAt: new Date(),
+ occurredAt: new Date("2026-08-20T17:42:31.000Z"),
status: EventOutboxStatus.Processing,
attempts: 0,
nextRetryAt: new Date(),
@@ -236,10 +237,10 @@ describe("event outbox drain", () => {
createdAt: new Date(),
updatedAt: new Date(),
...overrides
- }) as TEventOutbox;
+ }) as TClaimedOutboxRow;
const buildDrain = (opts: {
- batches: TEventOutbox[][];
+ batches: TClaimedOutboxRow[][];
handle: IEventConsumer["handle"];
consumerName?: string;
settled?: (input: { delivered: { ids: string[] }[] }) => number;
@@ -293,7 +294,7 @@ describe("event outbox drain", () => {
test("commits a delivered result and stops when a claim comes back empty", async () => {
const { service, commits } = buildDrain({
batches: [[makeRow()]],
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
});
const result = await service.drain(KEY);
@@ -302,6 +303,31 @@ describe("event outbox drain", () => {
expect(commits[0].delivered).toEqual([{ ids: ["1"] }]);
});
+ test("hands the consumer each row as a CloudEvent and settles its result on that row", async () => {
+ let received: unknown[] = [];
+ const { service, commits } = buildDrain({
+ batches: [[makeRow({ id: 7, eventId: "550e8400-e29b-41d4-a716-446655440000" })]],
+ handle: async (events) => {
+ received = events;
+ return events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }));
+ }
+ });
+
+ await service.drain(KEY);
+
+ expect(received).toEqual([
+ {
+ specversion: "1.0",
+ id: "550e8400-e29b-41d4-a716-446655440000",
+ source: "urn:infisical:approval",
+ type: "approval.workflow.request_opened",
+ time: "2026-08-20T17:42:31.000Z",
+ data: makePayload()
+ }
+ ]);
+ expect(commits[0].delivered).toEqual([{ ids: ["7"] }]);
+ });
+
test("retries the whole batch when the consumer throws", async () => {
const { service, commits } = buildDrain({
batches: [[makeRow()]],
@@ -329,7 +355,7 @@ describe("event outbox drain", () => {
test("fails a row terminally once it has used up its attempts", async () => {
const { service, commits } = buildDrain({
batches: [[makeRow({ attempts: MAX_OUTBOX_ATTEMPTS - 1 })]],
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Retry })),
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Retry })),
consumerName: "alert"
});
@@ -344,11 +370,11 @@ describe("event outbox drain", () => {
test("groups rows that share an outcome into one commit entry", async () => {
const { service, commits } = buildDrain({
batches: [[makeRow({ id: 1 }), makeRow({ id: 2 }), makeRow({ id: 3 }), makeRow({ id: 4 })]],
- handle: async (rows) =>
- rows.map((row) =>
- Number(row.id) <= 2
- ? { id: String(row.id), status: EventResultStatus.Delivered }
- : { id: String(row.id), status: EventResultStatus.Retry, error: "slack 502" }
+ handle: async (events) =>
+ events.map((event) =>
+ ["event-1", "event-2"].includes(event.id)
+ ? { id: event.id, status: EventResultStatus.Delivered }
+ : { id: event.id, status: EventResultStatus.Retry, error: "slack 502" }
)
});
@@ -365,7 +391,7 @@ describe("event outbox drain", () => {
test("commits under the token the batch was claimed with", async () => {
const { service, commits, claimTokens } = buildDrain({
batches: [[makeRow()]],
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
});
await service.drain(KEY);
@@ -379,7 +405,7 @@ describe("event outbox drain", () => {
loggerWarn.mockClear();
const { service } = buildDrain({
batches: [[makeRow({ id: 1 }), makeRow({ id: 2 })]],
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered })),
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered })),
settled: () => 1
});
@@ -393,7 +419,7 @@ describe("event outbox drain", () => {
loggerWarn.mockClear();
const { service } = buildDrain({
batches: [[makeRow()]],
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
});
await service.drain(KEY);
@@ -409,9 +435,9 @@ describe("event outbox drain", () => {
let release: () => void = () => {};
const { service, extended, claimTokens } = buildDrain({
batches: [[makeRow({ id: 1 }), makeRow({ id: 2 })]],
- handle: (rows) =>
+ handle: (events) =>
new Promise((resolve) => {
- release = () => resolve(rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered })));
+ release = () => resolve(events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered })));
})
});
@@ -477,7 +503,7 @@ describe("event outbox drain", () => {
registry.register(
makeConsumer({
name: "alert",
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
})
);
let commitCalls = 0;
@@ -515,7 +541,7 @@ describe("event outbox drain", () => {
registry.register(
makeConsumer({
name: "alert",
- handle: async (rows) => rows.map((row) => ({ id: String(row.id), status: EventResultStatus.Delivered }))
+ handle: async (events) => events.map((event) => ({ id: event.id, status: EventResultStatus.Delivered }))
})
);
let commitCalls = 0;
diff --git a/backend/src/services/event-outbox/event-outbox-service.ts b/backend/src/services/event-outbox/event-outbox-service.ts
index 13c04b9b26..c3090ac7f4 100644
--- a/backend/src/services/event-outbox/event-outbox-service.ts
+++ b/backend/src/services/event-outbox/event-outbox-service.ts
@@ -1,17 +1,16 @@
-import crypto from "node:crypto";
-
import { Knex } from "knex";
-import { TEventOutbox } from "@app/db/schemas";
+import { InternalServerError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import {
- EventOutboxDropReason,
- recordEventOutboxDroppedMetric,
+ EventOutboxRejectReason,
recordEventOutboxExhaustedMetric,
- recordEventOutboxLagMetric
+ recordEventOutboxLagMetric,
+ recordEventOutboxRejectedMetric
} from "@app/lib/telemetry/metrics";
-import { TEventOutboxDALFactory, TOutboxInsertRow } from "./event-outbox-dal";
+import { eventSource } from "./event-definition";
+import { TClaimedOutboxRow, TEventOutboxDALFactory, TOutboxInsertRow } from "./event-outbox-dal";
import { TEventOutboxRegistry } from "./event-outbox-registry";
import {
computeBackoffMs,
@@ -26,6 +25,7 @@ import {
MAX_OUTBOX_PAYLOAD_BYTES,
OUTBOX_CLAIM_BATCH_SIZE,
STALE_CLAIM_THRESHOLD_MS,
+ TEvent,
TEventConsumerResult,
TEventInput,
TOutboxFlushKey
@@ -73,6 +73,15 @@ const withTimeout = async <T>(promise: Promise<T>, ms: number, makeError: () =>
}
};
+const toEvent = (row: TClaimedOutboxRow): TEvent => ({
+ specversion: "1.0",
+ id: row.eventId,
+ source: eventSource(row.eventType),
+ type: row.eventType,
+ time: new Date(row.occurredAt).toISOString(),
+ data: row.payload
+});
+
const formatIssues = (issues: { path: (string | number)[]; message: string }[]) =>
issues.map((issue) => `${issue.path.join(".")} ${issue.message}`).join(", ");
@@ -103,71 +112,69 @@ export type TEventOutboxServiceFactoryDep = {
export type TEventOutboxServiceFactory = ReturnType<typeof eventOutboxServiceFactory>;
export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry }: TEventOutboxServiceFactoryDep) => {
- // A malformed event is dropped, logged and counted, never thrown: it is a bug at the emit site, and
- // failing the caller's write over it would turn a missed notification into a failed secret update.
- // Every check runs before any DB access, so a drop leaves the caller's transaction untouched.
- const $drop = (eventType: string, reason: EventOutboxDropReason, detail: string, consumer?: string) => {
+ // A malformed event is a bug at the emit site. Throwing fails the caller's transaction, and the
+ // business write with it, because committing the write without its event would break the guarantee
+ // that consumers hear about every change. Every check runs before any DB access.
+ const $reject = (type: string, reason: EventOutboxRejectReason, detail: string, consumer?: string): never => {
logger.error(
- `event-outbox: dropped event [eventType=${eventType}] [reason=${reason}]${consumer ? ` [consumer=${consumer}]` : ""}: ${detail}`
+ `event-outbox: rejected event [type=${type}] [reason=${reason}]${consumer ? ` [consumer=${consumer}]` : ""}: ${detail}`
);
- recordEventOutboxDroppedMetric({ eventType, reason, consumer });
- return [];
+ recordEventOutboxRejectedMetric({ eventType: type, reason, consumer });
+ throw new InternalServerError({
+ message:
+ "Infisical could not record an event for this change, so the change was not saved. Contact support and include the request ID."
+ });
};
- const $buildRows = (event: TEventInput, eventId: string): TOutboxInsertRow[] => {
+ const $buildRows = (event: TEventInput): TOutboxInsertRow[] => {
const parsed = EventInputSchema.safeParse(event);
if (!parsed.success) {
- return $drop(event.eventType, EventOutboxDropReason.InvalidEvent, formatIssues(parsed.error.issues));
+ return $reject(event.type, EventOutboxRejectReason.InvalidEvent, formatIssues(parsed.error.issues));
}
const validated = parsed.data;
- const payloadBytes = Buffer.byteLength(JSON.stringify(validated.payload), "utf8");
- if (payloadBytes > MAX_OUTBOX_PAYLOAD_BYTES) {
- return $drop(
- validated.eventType,
- EventOutboxDropReason.PayloadTooLarge,
- `payload is ${payloadBytes} bytes, over the ${MAX_OUTBOX_PAYLOAD_BYTES} byte limit; the outbox carries identifiers, not documents`
+ const dataBytes = Buffer.byteLength(JSON.stringify(validated.data), "utf8");
+ if (dataBytes > MAX_OUTBOX_PAYLOAD_BYTES) {
+ return $reject(
+ validated.type,
+ EventOutboxRejectReason.DataTooLarge,
+ `data is ${dataBytes} bytes, over the ${MAX_OUTBOX_PAYLOAD_BYTES} byte limit; the outbox carries identifiers, not documents`
);
}
- const occurredAt = validated.occurredAt ?? new Date();
+ const occurredAt = validated.time ?? new Date();
- return eventOutboxRegistry.subscribersOf(validated.eventType).flatMap((consumer) => {
- const payload = consumer.payloadSchema.safeParse(validated.payload);
- if (!payload.success) {
- return $drop(
- validated.eventType,
- EventOutboxDropReason.PayloadRejected,
- formatIssues(payload.error.issues),
+ return eventOutboxRegistry.subscribersOf(validated.type).map((consumer) => {
+ const data = consumer.dataSchema.safeParse(validated.data);
+ if (!data.success) {
+ return $reject(
+ validated.type,
+ EventOutboxRejectReason.DataRejected,
+ formatIssues(data.error.issues),
consumer.name
);
}
- return [
- {
- consumer: consumer.name,
- eventId,
- eventType: validated.eventType,
- payload: validated.payload,
- idempotencyKey: validated.idempotencyKey ?? null,
- occurredAt
- }
- ];
+ return {
+ consumer: consumer.name,
+ eventId: validated.id,
+ eventType: validated.type,
+ payload: validated.data,
+ occurredAt
+ };
});
};
// Pass the caller's `tx` so the rows commit with the business write; omit it only when there is no
// business write to be atomic with. A failed insert is not caught: swallowing it would leave the
// caller with a poisoned transaction.
- const emitMany = async (events: TEventInput[], tx?: Knex): Promise<void> => {
- const rows = events.flatMap((event) => $buildRows(event, crypto.randomUUID()));
+ const emit = async (events: TEventInput[], tx?: Knex): Promise<void> => {
+ const rows = events.flatMap($buildRows);
if (rows.length === 0) return;
await eventOutboxDAL.insertEvents(rows, tx);
};
- const emit = (event: TEventInput, tx?: Knex): Promise<void> => emitMany([event], tx);
-
const $commitWithRetry = async (
{ consumer }: TOutboxFlushKey,
input: Parameters<TEventOutboxDALFactory["commitResults"]>[0]
@@ -189,11 +196,11 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
};
const $applyResults = async (
- rows: TEventOutbox[],
+ rows: TClaimedOutboxRow[],
lockToken: string,
results: TEventConsumerResult[]
): Promise<void> => {
- const byId = new Map(results.map((result) => [result.id, result]));
+ const byEventId = new Map(results.map((result) => [result.id, result]));
const now = Date.now();
const delivered: { id: string }[] = [];
@@ -204,8 +211,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
for (const row of rows) {
const id = String(row.id);
// No result means unaccounted for, not delivered.
- const result = byId.get(id) ?? {
- id,
+ const result = byEventId.get(row.eventId) ?? {
status: EventResultStatus.Retry,
error: "Consumer returned no result for this event"
};
@@ -260,7 +266,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
const $handleClaimed = async (
consumer: IEventConsumer,
key: TOutboxFlushKey,
- claimed: TEventOutbox[],
+ claimed: TClaimedOutboxRow[],
lockToken: string
): Promise<TEventConsumerResult[]> => {
const ids = claimed.map((row) => String(row.id));
@@ -272,7 +278,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
try {
return await withTimeout(
- consumer.handle(claimed),
+ consumer.handle(claimed.map(toEvent)),
CONSUMER_HANDLE_TIMEOUT_MS,
() => new ConsumerHandleTimeoutError(key.consumer, CONSUMER_HANDLE_TIMEOUT_MS)
);
@@ -285,7 +291,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
} else {
logger.error(error, `event-outbox: consumer '${key.consumer}' threw while handling a batch`);
}
- return claimed.map((row) => ({ id: String(row.id), status: EventResultStatus.Retry, error: message }));
+ return claimed.map((row) => ({ id: row.eventId, status: EventResultStatus.Retry, error: message }));
} finally {
clearInterval(heartbeat);
}
@@ -371,5 +377,5 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry
}
};
- return { emit, emitMany, drain, sweepStaleClaims, pruneTerminalRows };
+ return { emit, drain, sweepStaleClaims, pruneTerminalRows };
};
diff --git a/backend/src/services/event-outbox/event-outbox-types.ts b/backend/src/services/event-outbox/event-outbox-types.ts
index ad25b6335e..e42c044230 100644
--- a/backend/src/services/event-outbox/event-outbox-types.ts
+++ b/backend/src/services/event-outbox/event-outbox-types.ts
@@ -2,8 +2,6 @@ import { Knex } from "knex";
import RE2 from "re2";
import { z } from "zod";
-import { TEventOutbox } from "@app/db/schemas";
-
export enum EventOutboxStatus {
Pending = "pending",
Processing = "processing",
@@ -48,24 +46,31 @@ const keySchema = z
"Must be lowercase and dot-namespaced, e.g. 'pki.certificate.issued'"
);
+// `id` is the CloudEvents id. It is fixed when the event is built rather than at insert, so an emitter
+// that needs a retry to be recognised as the same event can derive it (eg a reminder occurrence).
export const EventInputSchema = z.object({
- eventType: keySchema,
- payload: z.record(z.unknown()),
- idempotencyKey: z.string().trim().min(1).max(MAX_OUTBOX_KEY_LENGTH).optional(),
- occurredAt: z.date().optional()
+ id: z.string().uuid(),
+ type: keySchema,
+ data: z.record(z.unknown()),
+ time: z.date().optional()
});
export type TEventInput = z.input<typeof EventInputSchema>;
export type TEventEmitter = {
- emit: (event: TEventInput, tx?: Knex) => Promise<void>;
- emitMany: (events: TEventInput[], tx?: Knex) => Promise<void>;
+ emit: (events: TEventInput[], tx?: Knex) => Promise<void>;
};
-// What a consumer sees. Lock, attempt and status columns are the outbox's business. `id` is this
-// consumer's row, which is what a result reports on; `eventId` is the occurrence, shared by every
-// consumer's row for it.
-export type TEvent = Pick<TEventOutbox, "id" | "eventId" | "eventType" | "payload" | "occurredAt">;
+// What a consumer sees: the CloudEvents 1.0 envelope from the Event Standards and Versioning ADR. Lock,
+// attempt and status columns are the outbox's business.
+export type TEvent<TData = unknown> = {
+ specversion: "1.0";
+ id: string;
+ source: string;
+ type: string;
+ time: string;
+ data: TData;
+};
export enum EventResultStatus {
Delivered = "delivered",
@@ -73,6 +78,7 @@ export enum EventResultStatus {
Failed = "failed"
}
+// `id` is the event's id, which is unique within one consumer's events.
export type TEventConsumerResult = {
id: string;
status: EventResultStatus;
@@ -83,19 +89,19 @@ export type TOutboxFlushKey = {
consumer: string;
};
-export interface IEventConsumer<TPayload = unknown> {
+export interface IEventConsumer<TData = unknown> {
// Stored on every row and part of the flush job id, so renaming it orphans in-flight rows.
name: string;
// Checked at emit. Don't make it `.strict()`: one event can feed several consumers, so each schema
// has to tolerate fields it doesn't care about.
- payloadSchema: z.ZodType<TPayload>;
+ dataSchema: z.ZodType<TData>;
// Runs on the request path for every emit, so keep it pure and in-memory. Whether anyone actually
// wants the event is decided later, in handle().
- subscribesTo(eventType: string): boolean;
+ subscribesTo(type: string): boolean;
- // Events arrive oldest first by id and one flush runs per consumer at a time, so a batch is ordered
- // but nothing is promised across flushes. Must return one result per event.
+ // Events arrive in the order they were written and one flush runs per consumer at a time, so a batch
+ // is ordered but nothing is promised across flushes. Must return one result per event.
handle(events: TEvent[]): Promise<TEventConsumerResult[]>;
}
diff --git a/backend/src/services/folder-commit/folder-commit-service.test.ts b/backend/src/services/folder-commit/folder-commit-service.test.ts
index 75e351e9c7..7f2db6c47b 100644
--- a/backend/src/services/folder-commit/folder-commit-service.test.ts
+++ b/backend/src/services/folder-commit/folder-commit-service.test.ts
@@ -97,8 +97,7 @@ describe("folderCommitServiceFactory", () => {
};
const mockEventEmitter = {
- emit: vi.fn().mockResolvedValue(undefined),
- emitMany: vi.fn().mockResolvedValue(undefined)
+ emit: vi.fn().mockResolvedValue(undefined)
};
const mockFolderDAL = {
diff --git a/backend/src/services/folder-commit/folder-commit-service.ts b/backend/src/services/folder-commit/folder-commit-service.ts
index 77351515e7..badb2fcf60 100644
--- a/backend/src/services/folder-commit/folder-commit-service.ts
+++ b/backend/src/services/folder-commit/folder-commit-service.ts
@@ -1468,7 +1468,7 @@ export const folderCommitServiceFactory = ({
}
if (parentLocation) {
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
unique(modifiedFolders, (folder) => `${folder.folderId}:${folder.secretPath}`).map((folder) =>
FolderModifiedEventV1.build({ ...folder, projectId, environment: parentLocation.environmentSlug })
),
diff --git a/backend/src/services/identity/identity-auth-method-events.ts b/backend/src/services/identity/identity-auth-method-events.ts
index 80197cf784..3ec351ca9d 100644
--- a/backend/src/services/identity/identity-auth-method-events.ts
+++ b/backend/src/services/identity/identity-auth-method-events.ts
@@ -1,3 +1,5 @@
+import crypto from "node:crypto";
+
import { Knex } from "knex";
import { z } from "zod";
@@ -42,21 +44,24 @@ export const emitIdentityAuthMethodChanged = (
tx: Knex
): Promise<void> =>
eventEmitter.emit(
- {
- eventType: IDENTITY_AUTH_METHOD_CHANGED_EVENT,
- payload: {
- orgId: membership.scopeOrgId,
- projectId: membership.identity.projectId,
- resourceType: IDENTITY_AUTHENTICATION_RESOURCE_TYPE,
- resourceId: membership.identity.id,
- targetIds: [membership.identity.id],
- authMethod,
- change,
- actorType: actor,
- actorId,
- changedAt: new Date().toISOString(),
- ...(credential ? { credentialId: credential.id, credentialName: credential.name ?? undefined } : {})
+ [
+ {
+ id: crypto.randomUUID(),
+ type: IDENTITY_AUTH_METHOD_CHANGED_EVENT,
+ data: {
+ orgId: membership.scopeOrgId,
+ projectId: membership.identity.projectId,
+ resourceType: IDENTITY_AUTHENTICATION_RESOURCE_TYPE,
+ resourceId: membership.identity.id,
+ targetIds: [membership.identity.id],
+ authMethod,
+ change,
+ actorType: actor,
+ actorId,
+ changedAt: new Date().toISOString(),
+ ...(credential ? { credentialId: credential.id, credentialName: credential.name ?? undefined } : {})
+ }
}
- },
+ ],
tx
);
diff --git a/backend/src/services/reminder/reminder-events.ts b/backend/src/services/reminder/reminder-events.ts
index f76d28a6e8..6344b97ce3 100644
--- a/backend/src/services/reminder/reminder-events.ts
+++ b/backend/src/services/reminder/reminder-events.ts
@@ -1,5 +1,6 @@
import { Knex } from "knex";
import RE2 from "re2";
+import { v5 as uuidv5 } from "uuid";
import { z } from "zod";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
@@ -7,6 +8,9 @@ import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
export const SECRET_REMINDER_RESOURCE_TYPE = "secret.reminder";
export const SECRET_REMINDER_DUE_EVENT = "secret.reminder.due";
+// Namespaces the event ids derived below, so they can't collide with ids derived the same way elsewhere.
+const SECRET_REMINDER_DUE_EVENT_ID_NAMESPACE = "18460d3b-7d67-44a8-a6fb-1f84f78f29c4";
+
export const SecretReminderDuePayloadSchema = z.object({
note: z.string().max(1024).nullish(),
repeatDays: z.number().int().min(1).nullish(),
@@ -30,21 +34,24 @@ export const emitSecretReminderDue = (
tx: Knex
): Promise<void> =>
eventEmitter.emit(
- {
- eventType: SECRET_REMINDER_DUE_EVENT,
- // A cron retry after a partial run must not send the same occurrence twice. Keyed on the reminder
- // rather than the secret, so a reminder deleted and set again for a date that already fired still sends.
- idempotencyKey: `${SECRET_REMINDER_DUE_EVENT}:${reminderId}:${occurrenceDate}`,
- payload: {
- orgId,
- projectId,
- resourceType: SECRET_REMINDER_RESOURCE_TYPE,
- resourceId: secretId,
- targetIds: [secretId],
- note: note ?? null,
- repeatDays: repeatDays ?? null,
- occurrenceDate
+ [
+ {
+ // Derived rather than random, so a cron retry after a partial run is the same event and the outbox
+ // drops it. Derived from the reminder rather than the secret, so a reminder deleted and set again
+ // for a date that already fired still sends.
+ id: uuidv5(`${reminderId}:${occurrenceDate}`, SECRET_REMINDER_DUE_EVENT_ID_NAMESPACE),
+ type: SECRET_REMINDER_DUE_EVENT,
+ data: {
+ orgId,
+ projectId,
+ resourceType: SECRET_REMINDER_RESOURCE_TYPE,
+ resourceId: secretId,
+ targetIds: [secretId],
+ note: note ?? null,
+ repeatDays: repeatDays ?? null,
+ occurrenceDate
+ }
}
- },
+ ],
tx
);
diff --git a/backend/src/services/reminder/reminder-service.test.ts b/backend/src/services/reminder/reminder-service.test.ts
index 284da17383..482f00e9f6 100644
--- a/backend/src/services/reminder/reminder-service.test.ts
+++ b/backend/src/services/reminder/reminder-service.test.ts
@@ -35,8 +35,7 @@ const buildService = (opts: {
orphanBatches?: string[][];
}) => {
const log: string[] = [];
- const emitted: Array<{ eventType: string; idempotencyKey?: string; payload: Record<string, unknown>; tx: unknown }> =
- [];
+ const emitted: Array<{ id: string; type: string; data: Record<string, unknown>; tx: unknown }> = [];
const updates: Array<{ id: string; nextReminderDate: Date; tx: unknown }> = [];
const deletes: Array<{ id: string; tx: unknown }> = [];
const reaped: string[][] = [];
@@ -69,12 +68,11 @@ const buildService = (opts: {
}
},
eventEmitter: {
- emit: async (
- event: { eventType: string; idempotencyKey?: string; payload: Record<string, unknown> },
- tx: unknown
- ) => {
- if (opts.emitFails?.(event.payload.resourceId as string)) throw new Error("outbox insert failed");
- emitted.push({ ...event, tx });
+ emit: async (events: { id: string; type: string; data: Record<string, unknown> }[], tx: unknown) => {
+ events.forEach((event) => {
+ if (opts.emitFails?.(event.data.resourceId as string)) throw new Error("outbox insert failed");
+ emitted.push({ ...event, tx });
+ });
}
},
alertService: {
@@ -105,9 +103,8 @@ describe("reminder dispatch", () => {
expect(emitted).toHaveLength(1);
expect(emitted[0]).toMatchObject({
- eventType: SECRET_REMINDER_DUE_EVENT,
- idempotencyKey: `${SECRET_REMINDER_DUE_EVENT}:rem-1:2026-10-10`,
- payload: {
+ type: SECRET_REMINDER_DUE_EVENT,
+ data: {
orgId: "org-1",
projectId: "proj-1",
resourceType: SECRET_REMINDER_RESOURCE_TYPE,
@@ -123,6 +120,25 @@ describe("reminder dispatch", () => {
]);
});
+ // A cron retry after a partial run must be the same event, so the outbox drops it.
+ test("gives one occurrence of a reminder the same event id every time it is emitted", async () => {
+ const first = buildService({ due: [reminder()] });
+ const retry = buildService({ due: [reminder()] });
+ const otherDate = buildService({ due: [reminder({ nextReminderDate: new Date("2026-10-09T00:00:00.000Z") })] });
+ const otherReminder = buildService({ due: [reminder({ id: "rem-2" })] });
+
+ await first.service.dispatchDueReminders({ now });
+ await retry.service.dispatchDueReminders({ now });
+ await otherDate.service.dispatchDueReminders({ now });
+ await otherReminder.service.dispatchDueReminders({ now });
+
+ expect(first.emitted[0].id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/);
+ expect(retry.emitted[0].id).toBe(first.emitted[0].id);
+ expect(otherDate.emitted[0].data.occurrenceDate).toBe("2026-10-09");
+ expect(otherDate.emitted[0].id).not.toBe(first.emitted[0].id);
+ expect(otherReminder.emitted[0].id).not.toBe(first.emitted[0].id);
+ });
+
test("deletes a one-time reminder once its event is emitted", async () => {
const { service, emitted, deletes, updates } = buildService({ due: [reminder({ repeatDays: null })] });
await service.dispatchDueReminders({ now });
@@ -139,7 +155,7 @@ describe("reminder dispatch", () => {
await service.dispatchDueReminders({ now });
expect(emitted).toHaveLength(1);
- expect(emitted[0].payload.occurrenceDate).toBe("2026-10-05");
+ expect(emitted[0].data.occurrenceDate).toBe("2026-10-05");
expect(updates[0].nextReminderDate).toEqual(new Date("2026-10-11T00:00:00.000Z"));
});
@@ -150,7 +166,7 @@ describe("reminder dispatch", () => {
});
await service.dispatchDueReminders({ now });
- expect(emitted.map((event) => event.payload.resourceId)).toEqual(["secret-2"]);
+ expect(emitted.map((event) => event.data.resourceId)).toEqual(["secret-2"]);
expect(updates.map((update) => update.id)).toEqual(["rem-2"]);
});
diff --git a/backend/src/services/secret-folder/secret-folder-service.ts b/backend/src/services/secret-folder/secret-folder-service.ts
index e2044470da..b82e881afb 100644
--- a/backend/src/services/secret-folder/secret-folder-service.ts
+++ b/backend/src/services/secret-folder/secret-folder-service.ts
@@ -330,7 +330,7 @@ export const secretFolderServiceFactory = ({
}
createdFolders.push({ folderId: doc.id, secretPath: folderWithFullPath.path });
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
createdFolders.map((createdFolder) =>
FolderModifiedEventV1.build({ ...createdFolder, projectId, environment })
),
@@ -484,7 +484,7 @@ export const secretFolderServiceFactory = ({
}
if (doc.name !== folder.name) {
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
[folder.name, doc.name].map((folderName) =>
FolderModifiedEventV1.build({
folderId: doc.id,
@@ -631,7 +631,7 @@ export const secretFolderServiceFactory = ({
}
if (newFolderWithPath.path !== oldFolderWithPath.path) {
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
[oldFolderWithPath.path, newFolderWithPath.path].map((folderPath) =>
FolderModifiedEventV1.build({ folderId: doc.id, projectId, environment: env.slug, secretPath: folderPath })
),
@@ -893,13 +893,14 @@ export const secretFolderServiceFactory = ({
tx
);
- await eventEmitter.emit(
- FolderModifiedEventV1.build({
+ await FolderModifiedEventV1.emit(
+ eventEmitter,
+ {
folderId: doc.id,
projectId,
environment,
secretPath: path.posix.join(parentFolder.path, doc.name)
- }),
+ },
tx
);
return doc;
@@ -1407,7 +1408,7 @@ export const secretFolderServiceFactory = ({
tx
);
createdFolderPaths.push({ folderId: doc.id, secretPath: pathWithFolder });
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
createdFolderPaths.map((createdFolder) =>
FolderModifiedEventV1.build({ ...createdFolder, projectId, environment })
),
@@ -1599,13 +1600,14 @@ export const secretFolderServiceFactory = ({
);
}
- await eventEmitter.emit(
- FolderModifiedEventV1.build({
+ await FolderModifiedEventV1.emit(
+ eventEmitter,
+ {
folderId: doc.id,
projectId,
environment,
secretPath: path.posix.join(parentFolder.path, doc.name)
- }),
+ },
tx
);
@@ -2037,7 +2039,7 @@ export const secretFolderServiceFactory = ({
description: entry.description
}));
const createdFolders = await folderDAL.insertMany(newFolderRows, tx);
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
plan.map((entry) =>
FolderModifiedEventV1.build({
folderId: entry.newFolderId,
@@ -2130,13 +2132,14 @@ export const secretFolderServiceFactory = ({
// removing only the source root removes the entire subtree. the source is always fully emptied here: a
// subtree governed by a source approval policy was rejected up front, so no secret is left pending.
const [deletedRoot] = await folderDAL.delete({ id: sourceFolder.id }, tx);
- await eventEmitter.emit(
- FolderModifiedEventV1.build({
+ await FolderModifiedEventV1.emit(
+ eventEmitter,
+ {
folderId: deletedRoot.id,
projectId,
environment: sourceEnvironment,
secretPath: sourceFolderPath
- }),
+ },
tx
);
const latestVersions = await folderVersionDAL.findLatestFolderVersions([deletedRoot.id], tx);
diff --git a/backend/src/services/secret-import/secret-import-service.ts b/backend/src/services/secret-import/secret-import-service.ts
index ec80885e79..d8f68d4515 100644
--- a/backend/src/services/secret-import/secret-import-service.ts
+++ b/backend/src/services/secret-import/secret-import-service.ts
@@ -264,8 +264,9 @@ export const secretImportServiceFactory = ({
);
}
- await eventEmitter.emit(
- SecretImportModifiedEventV1.build({ secretImportId: doc.id, projectId, environment, secretPath: folder.path }),
+ await SecretImportModifiedEventV1.emit(
+ eventEmitter,
+ { secretImportId: doc.id, projectId, environment, secretPath: folder.path },
tx
);
return doc;
@@ -457,8 +458,9 @@ export const secretImportServiceFactory = ({
tx
);
- await eventEmitter.emit(
- SecretImportModifiedEventV1.build({ secretImportId: doc.id, projectId, environment, secretPath: folder.path }),
+ await SecretImportModifiedEventV1.emit(
+ eventEmitter,
+ { secretImportId: doc.id, projectId, environment, secretPath: folder.path },
tx
);
return doc;
@@ -515,13 +517,14 @@ export const secretImportServiceFactory = ({
tx
);
await folderDAL.deleteById(replicatedFolder.id, tx);
- await eventEmitter.emit(
- FolderModifiedEventV1.build({
+ await FolderModifiedEventV1.emit(
+ eventEmitter,
+ {
folderId: replicatedFolder.id,
projectId,
environment,
secretPath: replicatedFolder.path
- }),
+ },
tx
);
}
@@ -530,8 +533,9 @@ export const secretImportServiceFactory = ({
await secretImportDAL.updateAllPosition(folder.id, doc.position, -1, 1, tx);
}
- await eventEmitter.emit(
- SecretImportModifiedEventV1.build({ secretImportId: doc.id, projectId, environment, secretPath: folder.path }),
+ await SecretImportModifiedEventV1.emit(
+ eventEmitter,
+ { secretImportId: doc.id, projectId, environment, secretPath: folder.path },
tx
);
diff --git a/backend/src/services/secret-v2-bridge/secret-events.ts b/backend/src/services/secret-v2-bridge/secret-events.ts
index 1ff5e5b1dc..2fab55be74 100644
--- a/backend/src/services/secret-v2-bridge/secret-events.ts
+++ b/backend/src/services/secret-v2-bridge/secret-events.ts
@@ -40,4 +40,4 @@ export const emitSecretsModified = (
scope: TSecretEventScope,
secrets: { id: string; type: string }[],
tx?: Knex
-): Promise<void> => eventEmitter.emitMany(secretModifiedEvents(scope, secrets), tx);
+): Promise<void> => eventEmitter.emit(secretModifiedEvents(scope, secrets), tx);
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.test.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.test.ts
index d83cab73aa..ceca51639b 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.test.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.test.ts
@@ -322,7 +322,7 @@ describe("expandSecretReferencesGroupedByPath", () => {
});
describe("fnSecretBulkDelete", () => {
- const emitMany = vi.fn().mockResolvedValue(undefined);
+ const emit = vi.fn().mockResolvedValue(undefined);
test("removes the reminder of every deleted shared secret, whatever the legacy repeat-days column holds", async () => {
const removeSecretReminder = vi.fn().mockResolvedValue(undefined);
@@ -344,7 +344,7 @@ describe("fnSecretBulkDelete", () => {
secretQueueService: { removeSecretReminder },
folderCommitService: { createCommit: vi.fn() },
secretVersionDAL: { findLatestVersionMany: vi.fn().mockResolvedValue({}) },
- eventEmitter: { emitMany },
+ eventEmitter: { emit },
eventScope: { projectId: "proj-1", environment: "dev", secretPath: "/" }
} as never);
@@ -372,15 +372,16 @@ describe("fnSecretBulkDelete", () => {
secretQueueService: { removeSecretReminder: vi.fn() },
folderCommitService: { createCommit: vi.fn() },
secretVersionDAL: { findLatestVersionMany: vi.fn().mockResolvedValue({}) },
- eventEmitter: { emitMany },
+ eventEmitter: { emit },
eventScope: { projectId: "proj-1", environment: "dev", secretPath: "/app" }
} as never);
- expect(emitMany).toHaveBeenLastCalledWith(
+ expect(emit).toHaveBeenLastCalledWith(
[
{
- eventType: "com.infisical.secrets.secret.modified.v1",
- payload: { secretId: "secret-1", projectId: "proj-1", environment: "dev", secretPath: "/app" }
+ id: expect.any(String),
+ type: "com.infisical.secrets.secret.modified.v1",
+ data: { secretId: "secret-1", projectId: "proj-1", environment: "dev", secretPath: "/app" }
}
],
tx
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts
index 1d08ed3bed..0157d39460 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts
@@ -996,7 +996,7 @@ export const fnUpdateSecretLinkedReferences = async ({
if (updatedFolderIds.length > 0) {
const folderPaths = await folderDAL.findSecretPathByFolderIds(projectId, updatedFolderIds, tx);
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
folderPaths.flatMap((folder) =>
folder
? secretModifiedEvents(
@@ -1415,7 +1415,7 @@ export const fnUpdateMovedSecretReferences = async ({
if (updatedFolderIds.length > 0) {
const folderPaths = await folderDAL.findSecretPathByFolderIds(projectId, updatedFolderIds, tx);
- await eventEmitter.emitMany(
+ await eventEmitter.emit(
folderPaths.flatMap((folder) =>
folder
? secretModifiedEvents(