Repository navigation
Reload SAML identity provider metadata keys in the background - #55338
Open
SaintNick1214 wants to merge 1 commit into
Open
SaintNick1214 wants to merge 1 commit into
SaintNick1214 wants to merge 1 commit into
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
validUntilwould weaken that attribute.cacheDurationandvalidUntilare merged into one expiration time when the metadata is parsed, so serving stale keys forcacheDurationonly 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
maxCacheTimenow 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
minTimeBetweenRequestsand 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 beforeminTimeBetweenRequests.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
PublicKeyStorageProviderSPI: 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 blockinggetKeysbehaviour and does not reload on an unknown key.Alternatives considered: serving expired keys while they are refreshed (rejected for the
validUntilreason 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.
KcSamlMetadataSignedBrokerTestencoded the blocking reload and now retries the login until the background reload is stored.SamlBrokerMetadataKeysReloadTestcovers 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