Skip to content

Add a versioned event-export envelope and destination SPI #52616

Description

@clemlesne

Description

#45303 asks for reliable external event delivery. Today each EventListenerProvider must define its own payload, filtering, transaction behavior, retries, and broker guarantees.

Add a supported export pipeline:

flowchart LR
    E[Event and AdminEvent] --> C[Versioned export envelope]
    C --> O[(Durable delivery state)]
    O --> S[Destination SPI]
    S --> K[Kafka adapter]
    S --> N[NATS JetStream adapter]
Loading

Keycloak core owns normalization, filtering, transactional capture, asynchronous retry, and independent state per destination. Optional adapters map the envelope to a broker and report broker acceptance.

Delivery to the configured event queue is at least once. Duplicate queue entries are allowed and retain the same event ID. Keycloak does not provide producer-side deduplication or wait for consumer acknowledgement. Consumers decide how to acknowledge, process, and optionally deduplicate events.

Value Proposition

  • Provide a reliable event feed without a custom provider JAR.
  • Give every destination the same schema, privacy, transaction, and delivery contract.
  • Keep broker I/O and retry off authentication and admin request paths.
  • Keep Kafka and NATS dependencies in optional modules.

Goals

  • Envelope: Capture an immutable, versioned event-time snapshot with one stable event ID across destinations, retries, duplicates, and replay. Preserve custom admin-resource type strings and define schema and rolling-upgrade compatibility. CloudEvents 1.0 is a candidate, not a requirement.
  • Transactions: Capture in the transaction that dispatches the event. EventBuilder#success() and AdminEventBuilder#success() normally use the current transaction. EventBuilder#error() normally uses a separate transaction. EventBuilder#storeImmediately(boolean) can override the user-event default. Publish only committed captures.
  • Filtering: Filter before capture. Exclude sensitive or unbounded fields by default. Treat includeRepresentation=false as a hard ceiling. Never export secrets, credentials, tokens, request bodies, or event-controlled routing values.
  • Delivery: Retry every committed capture until the configured queue accepts it at least once or an operator explicitly discards it. Do not silently expire it. Isolate destination failures and expose queue age, backpressure, retry, and discard outcomes without payload data.
  • Destination SPI: Pass an immutable envelope and bounded delivery metadata to an adapter. Broker acceptance ends Keycloak's responsibility; it does not mean that a consumer processed the event.
    • Kafka maps the envelope to a configured topic and key. Its adapter reports successful broker acceptance.
    • NATS uses JetStream, not Core NATS, and maps the envelope to a configured subject. A successful PubAck means that the stream accepted the message. Stream retention and replication define persistence; consumer delivery and acknowledgement remain outside Keycloak.
  • Configuration: Let server operators provision named destinations and credentials. Realms may reference only permitted destinations. Event data and realm administrators must not select arbitrary endpoints or access credentials.
  • Lifecycle: Define pause, resume, delete, replay, credential rotation, cluster failover, metrics, and old-envelope handling during rolling upgrades. Keycloak does not provision broker infrastructure.

Non-Goals

  • Consumer processing, acknowledgement, idempotency, or deduplication.
  • Producer-side duplicate suppression, exactly-once delivery, or global ordering.
  • Inbound broker events or broker provisioning.
  • Replacing SSF, the existing event store, or Admin/Account Console event views.

Acceptance Criteria

  • Rollback behavior follows the transaction that dispatched the event, including EventBuilder#storeImmediately(boolean) overrides.
  • A node crash after committed capture does not lose the capture. Keycloak retries until the queue accepts it at least once; an ambiguous acceptance may create duplicates with the same event ID.
  • One unavailable destination does not block another. Capacity exhaustion and operator discard follow an explicit, observable policy.
  • Tests prove pre-capture filtering, the includeRepresentation=false ceiling, credential redaction, and absence of payloads from ordinary logs and metrics.
  • Kafka and JetStream integration tests stop at broker acceptance. They do not inspect or promise consumer acknowledgement, processing, idempotency, or deduplication.

Open Questions

  • CloudEvents or a smaller Keycloak envelope?
  • Server-owned destinations with realm references, constrained realm components, or both?
  • Which capture failures fail the originating operation when the durable queue is full?
  • What ordering scope, if any, can adapters support without serializing unrelated events?

Existing Building Block

At sampled main commit 3a720903102b161236a77ec6ec410fedc3e86545, Keycloak has generic OutboxStore, OutboxDrainerTask, and OutboxDeliveryHandler classes introduced for experimental SSF in #48256. The SSF factory constructs them directly; they are not a supported destination SPI. This issue asks whether core should keep them internal behind the new contract or expose a smaller stable outbox API.

Related

Parent #45303; CloudEvents #25133; webhooks #41175; Kafka/audit storage #44261; cache ordering #51532.

Activity

  1. changed the title [-]Provide a versioned external-event envelope and destination SPI[/-] [+]Provide a versioned external event envelope and reliable destination SPI[/+] on Sep 10, 2026
  2. changed the title [-]Provide a versioned external event envelope and reliable destination SPI[/-] [+]Add a versioned event-export envelope and destination SPI[/+] on Sep 10, 2026
  3. added
    kind/enhancement
    on Sep 23, 2026
  4. added
    area/events
    status/labeled-by-bot
    on Oct 22, 2026
  5. Prasad484 commented on Nov 4, 2026

    @Prasad484
    Contributor

    We poll GET /admin/realms/{realm}/admin-events to keep an external directory in step with users an identity provider pushes through Keycloak's built-in SCIM (26.8.1, PostgreSQL). Two things we measured make that polling route impossible to make lossless. Both look like what this proposal's transactional capture and "do not silently expire" goals would fix.

    1. An event is stamped when the request starts, but becomes readable only at commit. In our test, a SCIM update held open for 8 s by a row lock, and another held for 65 s, appeared with timestamps a reader had already passed. SCIM answered 200 both times. A poller has to re-read about 5.5 minutes behind (the 300 s transaction timeout plus a margin) to be sure nothing new can still appear.
    2. Events can disappear with no signal to a reader. DELETE /admin/realms/{realm}/admin-events removed 3,997 entries and left no trace. With admin events switched off, every change made in the meantime is lost. Entries past adminEventsExpiration are purged at the next cleanup. A reader can only detect this indirectly, for example by checking that an entry it already read is still there.

    Our workarounds are a delayed second read, a bookmark entry to detect clearing, and a full user listing to repair state. Together they recover each user's current state, but not the changes made while the log was missing. A committed, durable export as described here would remove the need for all three.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/eventskind/enhancementstatus/labeled-by-botstatus/triageteam/core-iam

    Fields

    Value

    None yet

    Effort

    None yet

    Votes

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions