Skip to content

fix: use client UUID for SAML metadata public key cache key - #52108

Closed
waterWang wants to merge 1 commit into
keycloak:mainfrom
waterWang:fix-52106-saml-metadata-cache
Closed

waterWang wants to merge 1 commit into
keycloak:mainfrom
waterWang:fix-52106-saml-metadata-cache

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Fixes #52106

Problem

SamlProtocolUtils.createKeyLocatorForClient() builds the public-key storage cache key with client.getClientId() (the human-readable SAML client ID string), while InfinispanPublicKeyStorageProviderFactory invalidates cache entries built from client.getId() (the internal client UUID) on ClientUpdatedEvent/ClientRemovedEvent.

Because these two keys never match, a client update (e.g. rotating the SAML metadata URL or key material) does not evict the stale SAML metadata public-key entry. The retired signing certificate remains cached and trusted indefinitely (no validUntil/cacheDuration means no expiration), until cache eviction or manual reload.

Fix

Use client.getId() in the lookup so it uses the same UUID-based cache key that invalidation targets.

Verification

  • The lookup key now equals the invalidation key for the same client:
    • OLD lookup key: realm::client::<clientId-string>::keyuse::SIG (≠ invalidation)
    • NEW lookup key: realm::client::<client-uuid>::keyuse::SIG (== invalidation for SIG; ENC variant also covered)

No DCO / Apache-2.0 licensed repo contribution.

The SAML metadata-backed client key lookup builds the public-key storage
cache key from the realm ID and the client UUID via
getClientModelCacheKey(realmId, client.getId()). However, the lookup in
SamlProtocolUtils.createKeyLocatorForClient passed client.getClientId()
(the human-readable client ID string) instead of client.getId() (the
internal UUID). Client update/removal events invalidate cache entries
built from the internal client UUID, so the two keys never matched and a
stale SAML metadata signing key survived client updates indefinitely
(e.g. after a metadata URL or key-material rotation), being trusted until
cache eviction or manual reload.

Fix: use client.getId() so the lookup uses the same UUID-based cache key
that InfinispanPublicKeyStorageProviderFactory invalidates on
ClientUpdatedEvent/ClientRemovedEvent.

Signed-off-by: waterWang <waterWang@users.noreply.github.com>
@waterWang
waterWang requested a review from a team as a code owner August 26, 2026 20:21
Copilot AI balanced review requested due to automatic review settings August 26, 2026 20:21
@waterWang waterWang changed the title fix: use client UUID for SAML metadata public key cache key [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] fix: use client UUID for SAML metadata public key cache key Aug 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns SAML metadata public-key cache lookup with client update/removal invalidation.

Changes:

  • Uses the internal client UUID instead of the human-readable client ID for cache keys.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

if (StringUtil.isNotBlank(samlClient.getMetadataDescriptorUrl()) && samlClient.isUseMetadataDescriptorUrl()) {
// configured to use the metadata
String modelKey = PublicKeyStorageUtils.getClientModelCacheKey(samlClient.getClient().getRealm().getId(), samlClient.getClient().getClientId());
String modelKey = PublicKeyStorageUtils.getClientModelCacheKey(samlClient.getClient().getRealm().getId(), samlClient.getClient().getId());
@lhanusov

Copy link
Copy Markdown
Contributor

Hi @waterWang, thank you for creating the PR. Please can you also add a test, this is a must requirement to accept the bug fix. Thank you for understanding.

@lhanusov

Copy link
Copy Markdown
Contributor

closing the issue, since the reporter is not responding, it will be fixed in a separate PR.

@lhanusov lhanusov closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SAML metadata key cache is not invalidated on client updates

4 participants