8000 Emit RISC account-purged SSF events on user deletion by ssilvert · Pull Request #51870 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4060403
Emit RISC account-purged SSF events on user deletion
ssilvert Aug 19, 2026
f676a5c
Resolve organizations live for purged users instead of copying their …
ssilvert Aug 19, 2026
834922b
Gate purge snapshots on an active request instead of capping retention
ssilvert Aug 21, 2026
bf6202c
Bound purge snapshot retention for bulk deletions inside a request
ssilvert Aug 22, 2026
ae7816b
Publish UserPreRemovedEvent before federated pre-removal
ssilvert Aug 22, 2026
f79cd87
Preserve group and role memberships in the purge snapshot
ssilvert Aug 26, 2026
54f811c
Resolve purge subjects from the snapshot rather than live-first
ssilvert Aug 26, 2026
9d4d886
Scope purge snapshot session keys by realm
ssilvert Aug 26, 2026
196c85c
Make purge snapshot cleanup best-effort
ssilvert Aug 26, 2026
f7d01cf
Set the duplicate-email policy at realm creation in the purge tests
ssilvert Aug 26, 2026
4de20a9
Hold purge snapshots in one session-scoped set
ssilvert Aug 31, 2026
ddfe990
Match a purge snapshot only for subjects this transmitter issued
ssilvert Sep 1, 2026
b6460a4
Read organization membership as the system on the SSF emission path
ssilvert Sep 1, 2026
a3a873d
Suppress account-purged when the account outlives its local record
ssilvert Sep 1, 2026
8afa3b6
Cover the self-service purge path through the event listener
ssilvert Sep 1, 2026
d931734
Treat an unknown federated deletion as not a purge
ssilvert Sep 1, 2026
5dec229
Tidy the purge path: one membership query, blank emails, drop-path lo…
ssilvert Sep 1, 2026
17db223
Resolve a purged user's organizations once per request
ssilvert Sep 1, 2026
cc56a5a
Resolve the purge realm the same way on both sides of the emission path
ssilvert Sep 1, 2026
69fc364
Ask the organization question instead of handing out the list
ssilvert Sep 1, 2026
90074c3
Fix import order and drop an unused test import
ssilvert Sep 2, 2026
6371d57
Keep the purge snapshot indexes in step on replace
ssilvert Sep 2, 2026
be494ee
Refuse to emit account-purged without a pre-removal snapshot
ssilvert Sep 2, 2026
51b3a12
Rename purgeRealm to resolveRealm
ssilvert Sep 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/guides/securing-apps/ssf-support.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,17 @@ The {project_name} SSF Transmitter targets conformance with:
* https://openid.net/specs/openid-caep-interoperability-profile-1_0.html[CAEP Interoperability Profile 1.0 (draft)]
-- `session-revoked` and `credential-change` event types.
* https://openid.net/specs/openid-risc-1_0-final.html[RISC 1.0 (Final)]
-- `account-disabled` and `account-enabled` event types. Emitted when an admin toggles a
user's enabled state, and when brute-force protection permanently locks an account out
(`reason=disabled-by-bruteforce`).
-- `account-disabled`, `account-enabled` and `account-purged` event types.
`account-disabled` / `account-enabled` are emitted when an admin toggles a user's enabled
state, and when brute-force protection permanently locks an account out
(`reason=disabled-by-bruteforce`). `account-purged` is emitted when a user account is
deleted, either by an administrator or by the user themselves from the account console.
Deletions that do not remove the account -- a partial import under the `OVERWRITE` policy,
or the service-account user removed alongside its client -- do not emit it.
+
NOTE: `account-purged` is currently emitted only for deletions performed through the admin
API or the account console. Deletions performed by a workflow's delete-user step, or by
removing an organization that owns managed members, do not yet emit it.
* Legacy SSE CAEP profile for Apple Business Manager interop (gated behind the `sse-caep-enabled` SPI flag).

</@tmpl.guide>
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,16 @@ public UserModel addUser(RealmModel realm, String username) {

@Override
public boolean removeUser(RealmModel realm, UserModel user) {
// Published before any removal work, including the federated pre-removal
// below. That call deletes the user's federated attributes, so a listener
// running after it would observe a user that has already lost part of its
// state -- which the "pre removed" contract does not lead anyone to expect.
publishUserPreRemovedEvent(realm, user);
Comment thread
ssilvert marked this conversation as resolved.
Resolved

if (getFederatedStorage() != null && user.getServiceAccountClientLink() == null) {
getFederatedStorage().preRemove(realm, user);
}

publishUserPreRemovedEvent(realm, user);

StorageId storageId = new StorageId(user.getId());

if (storageId.getProviderId() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.keycloak.ssf.event.caep.CaepSessionRevoked;
import org.keycloak.ssf.event.risc.RiscAccountDisabled;
import org.keycloak.ssf.event.risc.RiscAccountEnabled;
import org.keycloak.ssf.event.risc.RiscAccountPurged;
import org.keycloak.ssf.event.stream.SsfStreamUpdatedEvent;
import org.keycloak.ssf.event.stream.SsfStreamVerificationEvent;

Expand Down Expand Up @@ -55,6 +56,7 @@ public class DefaultSsfEventProviderFactory implements SsfEventProviderFactory,
// RISC events
events.put(RiscAccountDisabled.TYPE, RiscAccountDisabled::new);
events.put(RiscAccountEnabled.TYPE, RiscAccountEnabled::new);
events.put(RiscAccountPurged.TYPE, RiscAccountPurged::new);

STANDARD_EVENT_FACTORIES = Map.copyOf(events);
}
Expand All @@ -70,9 +72,9 @@ public class DefaultSsfEventProviderFactory implements SsfEventProviderFactory,
* use-cases enumerated by the OpenID CAEP Interoperability Profile 1.0.
* The profile is opt-in per use-case ("Implementations MAY choose to
* support one or more …"), so supporting any one of them is enough to
* count as interoperable. {@code account-disabled} and
* {@code account-enabled} are RISC 1.0 events, not part of that CAEP
* profile, added alongside it.
* count as interoperable. {@code account-disabled},
* {@code account-enabled} and {@code account-purged} are RISC 1.0
* events, not part of that CAEP profile, added alongside it.
*
* @see <a href="https://openid.github.io/sharedsignals/openid-caep-interoperability-profile-1_0.html">OpenID CAEP Interoperability Profile 1.0</a>
* @see <a href="https://openid.net/specs/openid-risc-1_0-final.html">OpenID RISC 1.0 (Final)</a>
Expand All @@ -81,7 +83,8 @@ public class DefaultSsfEventProviderFactory implements SsfEventProviderFactory,
CaepCredentialChange.TYPE,
CaepSessionRevoked.TYPE,
RiscAccountDisabled.TYPE,
RiscAccountEnabled.TYPE);
RiscAccountEnabled.TYPE,
RiscAccountPurged.TYPE);

/**
* Subset of {@link #EMITTABLE_EVENT_TYPES} that {@code SecurityEventTokenMapper}
Expand All @@ -92,7 +95,8 @@ public class DefaultSsfEventProviderFactory implements SsfEventProviderFactory,
CaepCredentialChange.TYPE,
CaepSessionRevoked.TYPE,
RiscAccountDisabled.TYPE,
RiscAccountEnabled.TYPE);
RiscAccountEnabled.TYPE,
RiscAccountPurged.TYPE);

private volatile SsfEventRegistry registry;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.keycloak.ssf.event.risc;

import java.util.Map;

import org.keycloak.ssf.event.InitiatingEntity;
import org.keycloak.ssf.event.InitiatingEntityAware;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The Account Purged event indicates that an account has been permanently deleted.
*
* <p>Unlike {@link RiscAccountDisabled}, RISC defines no {@code reason} claim for this
* event — a purge is terminal and carries no rationale enumeration.
*
* <p>Receivers use this to drive their own data-retention cleanup, so it is emitted only
* for a genuine purge. Keycloak has several code paths that delete a user row without the
* account ceasing to exist — most notably a partial import under the OVERWRITE policy,
* which deletes and immediately recreates the user — and those deliberately do not emit.
*
* <p><b>Known gap.</b> Emission is driven by the admin / user event that follows a
* deletion, so deletion paths that fire neither produce no event. The significant one is
* the workflow delete step ({@code DeleteUserStepProvider}): the workflow engine raises no
* Keycloak events at all, so scheduled data-retention deletions are currently silent —
* exactly the case receivers would most expect to hear about. Deleting an organization's
* managed members is silent for the same reason. Both are tracked separately from the
* initial account-purged support.
*
* See: https://openid.net/specs/openid-risc-1_0-final.html
*/
public class RiscAccountPurged extends RiscEvent implements InitiatingEntityAware {

public static final String TYPE = "https://schemas.openid.net/secevent/risc/event-type/account-purged";

/**
* The time of the event (UNIX timestamp). RISC 1.0 lists no attributes for
* account-purged, so this is a CAEP-defined claim carried as an extension —
* declared here rather than on {@link RiscEvent} because RISC does not define
* it profile-wide. Nullable so an absent timestamp is omitted from the wire
* JSON rather than serialized as {@code "event_timestamp": 0}.
*/
@JsonProperty("event_timestamp")
protected Long eventTimestamp;

/**
* The entity that initiated the deletion — {@link InitiatingEntity#ADMIN} for a
* deletion through the admin API, {@link InitiatingEntity#USER} for self-service
* deletion from the account console. Unlike account-disabled, there is no policy
* -driven trigger to account for: Keycloak never purges an account on its own.
*/
@JsonProperty("initiating_entity")
protected InitiatingEntity initiatingEntity;

public RiscAccountPurged() {
super(TYPE);
}

public Long getEventTimestamp() {
return eventTimestamp;
}

public void setEventTimestamp(long eventTimestamp) {
this.eventTimestamp = eventTimestamp;
}

@Override
public InitiatingEntity getInitiatingEntity() {
return initiatingEntity;
}

@Override
public void setInitiatingEntity(InitiatingEntity initiatingEntity) {
this.initiatingEntity = initiatingEntity;
}

@Override
protected void appendFields(Map<String, Object> fields) {
super.appendFields(fields);
fields.put("eventTimestamp", eventTimestamp);
fields.put("initiatingEntity", initiatingEntity);
Comment thread
ssilvert marked this conversation as resolved.
Resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,31 @@ void accountEnabled_serializesTimestampAndInitiatingEntity() throws Exception {
assertEquals("admin", json.path("initiating_entity").asText());
}

@Test
void accountPurged_serializesTimestampAndInitiatingEntity() throws Exception {
RiscAccountPurged event = new RiscAccountPurged();
event.setEventTimestamp(1712345678L);
event.setInitiatingEntity(InitiatingEntity.USER);

JsonNode json = JsonSerialization.mapper.valueToTree(event);

assertEquals(1712345678L, json.path("event_timestamp").asLong());
assertEquals("user", json.path("initiating_entity").asText());
// RISC defines no reason for a purge; the claim must not leak in from
// the sibling account-disabled shape this class was modelled on.
assertTrue(json.path("reason").isMissingNode(),
"account-purged must not carry a reason claim: " + json);
}

@Test
void unsetClaims_areOmittedFromWireJson() throws Exception {
JsonNode disabled = JsonSerialization.mapper.valueToTree(new RiscAccountDisabled());
JsonNode purged = JsonSerialization.mapper.valueToTree(new RiscAccountPurged());

assertTrue(purged.path("event_timestamp").isMissingNode(),
"unset event_timestamp must be omitted, not serialized as 0: " + purged);
assertTrue(purged.path("initiating_entity").isMissingNode(),
"unset initiating_entity must be omitted: " + purged);

// @JsonInclude(NON_NULL) on SsfEvent must still apply to the claims now
// declared on the concrete class — a primitive long would have emitted 0.
Expand Down
Loading
Loading
0