Skip to content

Reload SAML identity provider metadata keys in the background - #55338

Open
SaintNick1214 wants to merge 1 commit into
keycloak:mainfrom
SaintNick1214:issue-54971
Open

SaintNick1214 wants to merge 1 commit into
keycloak:mainfrom
SaintNick1214:issue-54971

Conversation

@SaintNick1214

Copy link
Copy Markdown

Closes #54971

With a metadata descriptor URL, a SAML identity provider now never loads the metadata on the request thread while valid keys are cached: a key that is not in the cache is reported as not found, the message is validated with the cached keys, and the keys are reloaded in the background. First loads and reloads of expired keys are still done by the request that needs them.

Expired keys stay synchronous on purpose. Expiry is driven by the clock and by what the identity provider publishes, not by the key an inbound message asks for, and serving keys after validUntil would weaken that attribute. cacheDuration and validUntil are merged into one expiration time when the metadata is parsed, so serving stale keys for cacheDuration only would need a larger change that is left out.

The cost is in key rotation: a login signed with a key that is not cached yet is rejected, and logins succeed once the background reload has finished. To keep rotation smooth for providers that publish the new certificate in advance, the regular reload after maxCacheTime now also applies when a specific key is requested and found, and it runs in the background; before, it only happened when all keys were iterated. The Reload keys action is unchanged.

Background reloads keep minTimeBetweenRequests and go through the same single-flight map as synchronous reloads, so an administrator reload or an expiry reload joins a running background reload instead of fetching twice. In addition, at most one background reload per model key is queued or running, and a failed one is not repeated before minTimeBetweenRequests.

A reload started by an unknown key is logged once at WARN with the key name (limited to 128 characters, control characters removed) or the SHA-256 thumbprint of the key; lookups that do not start a reload log at DEBUG. A failed background reload logs one WARN and keeps the cached keys.

The change is additive in the private PublicKeyStorageProvider SPI: two default methods, implemented by the Infinispan provider. Existing methods are untouched, so OIDC JWKS and SAML clients keep reloading in the thread that needs the key. A provider that does not implement the new methods keeps the blocking getKeys behaviour and does not reload on an unknown key.

Alternatives considered: serving expired keys while they are refreshed (rejected for the validUntil reason above), and a scheduled refresh for all identity providers that use a metadata descriptor URL, which would remove request-triggered refreshes entirely but needs enumeration of identity providers across realms, cluster coordination and a new option.

The documentation of the key cache settings proposed in #55329 for #54974 describes the behavior before this change. If that pull request is merged first, the section it adds needs to be updated here.

KcSamlMetadataSignedBrokerTest encoded the blocking reload and now retries the login until the background reload is stored. SamlBrokerMetadataKeysReloadTest covers a metadata endpoint that does not answer, one that fails, the periodic reload, and the bound on reloads and warnings for repeated unknown keys; the class raises the HTTP client socket timeout through its server configuration so that the held metadata request cannot time out on a slow node, at the cost of a server restart for the class. Run locally: these two, SamlMetadataKeyLocatorTest, InfinispanKeyStorageProviderTest, and the OIDC JWKS and SAML client tests that show their behaviour is unchanged; the new testsuite ran in embedded mode only and the full suites were not run locally.

An AI coding agent (Claude Code) was used to produce this change from a prompt, including the tests, the documentation and the local test runs described above.

🤖 Generated with Claude Code

A message from a SAML identity provider that requested a key that was not
in the cache made the request wait for the metadata descriptor URL. When
valid keys are cached they are now returned straight away and the reload
runs in the background, at most once in the minimum time between requests
also when it fails. The cached keys are kept until a reload succeeds, and
a reload caused by an unknown key is logged as a warning with that key.
SAML clients and OIDC keep reloading in the thread that needs the key.

Closes keycloak#54971

Signed-off-by: Nicholas Geil <63267399+SaintNick1214@users.noreply.github.com>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
@SaintNick1214
SaintNick1214 marked this pull request as ready for review November 19, 2026 00:06
@SaintNick1214
SaintNick1214 requested review from a team as code owners November 19, 2026 00:06
Copilot AI balanced review requested due to automatic review settings November 19, 2026 00:06

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.

🟡 Changes recommended

Expired signing keys can remain usable when reloads are throttled, and stale reload jobs can restore retired keys after configuration changes or cache invalidation.

2 open issues
What changed in this PR

This PR adds background metadata-key refreshes for SAML identity providers, addressing #54971 by reducing request-thread dependence on metadata endpoint availability while valid keys are cached.

Changes:

  • Adds background-refresh SPI methods and Infinispan scheduling, throttling, and failure handling.
  • Updates SAML broker lookups and tests while retaining synchronous behavior for SAML clients and OIDC.
  • Documents background refreshes and their implications for certificate rotation.
File Description
testsuite/​integration-arquillian/​tests/​base/​src/​test/​java/​org/​keycloak/​testsuite/​broker/​KcSamlMetadataSignedBrokerTest.java Retries logins while background refresh completes.
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​broker/​saml/​SamlBrokerMetadataKeysReloadTest.java Tests slow and failing endpoints, periodic refreshes, and reload limits.
services/​src/​test/​java/​org/​keycloak/​protocol/​saml/​SamlMetadataKeyLocatorTest.java Tests background lookups, compatibility, and log sanitization.
services/​src/​main/​java/​org/​keycloak/​protocol/​saml/​SamlMetadataKeyLocator.java Uses cached keys and requests background refreshes on misses.
services/​src/​main/​java/​org/​keycloak/​broker/​saml/​SAMLEndpoint.java Enables background metadata loading for identity providers.
server-spi-private/​src/​main/​java/​org/​keycloak/​keys/​PublicKeyStorageProvider.java Adds default methods supporting background refreshes.
model/​infinispan/​src/​test/​java/​org/​keycloak/​keys/​infinispan/​InfinispanKeyStorageProviderTest.java Tests initial-load and refresh-throttling behavior.
model/​infinispan/​src/​main/​java/​org/​keycloak/​keys/​infinispan/​InfinispanPublicKeyStorageProviderFactory.java Shares background-reload tracking across provider instances.
model/​infinispan/​src/​main/​java/​org/​keycloak/​keys/​infinispan/​InfinispanPublicKeyStorageProvider.java Implements background scheduling and cached-key retrieval.
docs/​documentation/​upgrading/​topics/​changes/​changes-27_0_0.adoc Explains changed refresh and rotation behavior.
docs/​documentation/​server_admin/​topics/​identity-broker/​saml.adoc Adds administrator guidance for background refreshes.

Review effort: Balanced


if (isExpired(entry, currentTime)) {
// no valid keys to return, the caller has to wait for them
return getKeys(modelKey, loader);
Comment on lines +382 to +384
if (keys.containsKey(modelKey)) {
session.getProvider(PublicKeyStorageProvider.class, InfinispanPublicKeyStorageProviderFactory.PROVIDER_ID)
.reloadKeys(modelKey, loader.apply(session));
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.

Avoid synchronous SAML metadata fetches triggered by inbound key misses in SAML identity providers

3 participants