feat: Add client secret expiry tracking and automatic renew for DCR#4194
Draft
Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Draft
feat: Add client secret expiry tracking and automatic renew for DCR#4194Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Sanskarzz wants to merge 2 commits intostacklok:mainfrom
Conversation
Signed-off-by: Sanskarzz <sanskar.gur@gmail.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4194 +/- ##
==========================================
- Coverage 69.04% 68.82% -0.23%
==========================================
Files 468 469 +1
Lines 47003 47185 +182
==========================================
+ Hits 32453 32473 +20
- Misses 11974 12072 +98
- Partials 2576 2640 +64 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details.
This review will be automatically dismissed once you add the justification section.
Signed-off-by: Sanskarzz <sanskar.gur@gmail.com>
294c25f to
a104b56
Compare
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.
[WIP] Pending Local E2E testing with Keycloak
Summary
Implements client secret expiry tracking and automatic renewal for Dynamic Client Registration (DCR)
Fixes: #3631
ToolHive already stored the
client_idandclient_secretfrom DCR responses, but discarded theregistration_access_token,registration_client_uri, andclient_secret_expires_atfields. Without these, there was no way to detect or renew an expired client secret, causing silent authentication failures for long-running workloads on providers like Keycloak that issue expiring secrets.This PR implements the full RFC 7591 / RFC 7592 secret lifecycle in three phases:
Note
All the phases are added in the same PR.
Changes
pkg/auth/remote/persisting_token_source.goExtended
ClientCredentialsPersisterfunction type signature:pkg/auth/remote/config.goCachedRegClientURIfield to storeregistration_client_urias plain textClearCachedClientCredentials()to also clear the new fieldpkg/auth/discovery/discovery.goOAuthFlowConfig(used as the threading vehicle fromhandleDynamicRegistrationthrough to the result)OAuthFlowResultwithSecretExpiry,RegistrationAccessToken,RegistrationClientURIhandleDynamicRegistration()to capture all three fromDynamicClientRegistrationResponse:ClientSecretExpiresAt > 0→time.Unix(...)(zero if the field is0, meaning never expires)RegistrationAccessTokenandRegistrationClientURIcopied as-isnewOAuthFlow()to populate the new fields inOAuthFlowResultpkg/auth/remote/handler.gowrapWithPersistence()to pass all 5 arguments toclientCredentialsPersister"time"importresolveClientCredentials()now proactively callsrenewClientSecret()when the secret is expiring within 24 h; renewal failures are soft-logged and execution continuestryRestoreFromCachedTokens()now checks expiry before attempting token refresh:pkg/runner/runner.goUpdated
SetClientCredentialsPersistercallback to match the new 5-argument signature and persist:CachedSecretExpiry— stored directly in configregistrationAccessToken— stored securely in the secret manager, reference saved toCachedRegTokenRefregistrationClientURI— stored as plain text inCachedRegClientURIpkg/auth/remote/secret_renewal.go(new file)Implements RFC 7592 §2.2 client secret renewal:
isSecretExpiredOrExpiringSoon()— returns true when the secret is withinsecretExpiryBuffer(24 h) of expiry or already past it; false for zero expiry (never expires)renewClientSecret(ctx)— sends an authenticated HTTPPUTtoregistration_client_uriper RFC 7592 §2.2:registrationAccessTokenfrom the secret managerregistration_client_uri(must be HTTPS or localhost)client_secret,client_secret_expires_at, and optionally a rotatedregistration_access_tokenclientCredentialsPersistervalidateRegistrationClientURI(uri)— validates the URI is HTTPS (or localhost for development)pkg/auth/remote/secret_renewal_test.go(new file)16 new tests covering:
TestIsSecretExpiredOrExpiringSoon(5 cases)TestValidateRegistrationClientURI(6 cases)TestRenewClientSecret_MissingConfig(3 cases)TestRenewClientSecret_SuccessTestRenewClientSecret_ServerErrorTestRenewClientSecret_NoPersisterTestRenewClientSecret_ZeroExpiryInResponseclient_secret_expires_at: 0→time.Time{}(never expires)Breaking Changes
Warning
ClientCredentialsPersisterfunction type signature changed.Any code outside this repository that implements or calls
ClientCredentialsPersistermust be updated to use the new 5-parameter signature. Within this repository, the only call site ispkg/runner/runner.go, which is updated in this PR.Backward Compatibility
client_secret_expires_at = 0)CachedSecretExpiryistime.Time{}.isSecretExpiredOrExpiringSoon()returnsfalse. No renewal is attempted.registration_access_token/ URI)renewClientSecret()returns an immediate error. Caller logs a warning and continues with the existing secret.RFC References
client_secret_expires_at,registration_access_token,registration_client_uriin registration responseTest Results
Type of change
Test plan
task test)task test-e2e)task lint-fix)