Repository navigation
Support reloading OAuth2 key file - #1441
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the OAuth2 authentication implementation to support dynamic reloading of OAuth2 key files. The key change is moving from a persistent store-based approach to a flow-based approach where credentials are read fresh from the key file on each token refresh.
Key Changes:
- Removes the store-based persistence layer (
oauth2/storepackage) and replaces it with direct flow-based token retrieval - Introduces a
GrantProviderinterface that reads credentials from the key file on each authorization request, enabling dynamic reloading - Simplifies the token cache to call
flow.Authorize()directly instead of interacting with a store
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| oauth2/store/store.go | Deleted - Removes store interface for grant persistence |
| oauth2/store/memory.go | Deleted - Removes in-memory store implementation |
| oauth2/store/keyring.go | Deleted - Removes keyring-based store implementation |
| oauth2/client_credentials_flow.go | Refactored to use GrantProvider interface that reads credentials from key files dynamically |
| oauth2/cache/cache.go | Simplified to call flow.Authorize() directly instead of using store, enabling key file reloading |
| oauth2/auth_suite_test.go | Added MockGrantProvider for testing the new grant provider interface |
| oauth2/client_credentials_flow_test.go | Updated tests to use new MockGrantProvider instead of mocked credentials |
| pulsar/auth/oauth2.go | Updated to use flow-based approach, removed store dependency |
| pulsar/auth/oauth2_test.go | Added test for OAuth2 key file reloading functionality and scope merging |
| pulsaradmin/pkg/admin/auth/oauth2.go | Updated to use flow-based approach, removed store and keyring dependencies |
| pulsaradmin/pkg/admin/auth/oauth2_test.go | Simplified test to use new flow-based API |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Motivation
When using OAuth2 authentication, if the key file is updated, the token refresh process does not use the updated credentials, which can lead to token refresh failures.
The root cause is that the client stores the grant information in a grant store, which cannot handle updates to the key file.
In fact, token refreshes are infrequent, so introducing a cache is unnecessary.
Modifications
Before this PR, the OAuth2 behavior was as follows:
During OAuth2 initialization:
grant.Token.During token refresh:
Currently, the grant is always stored in the grant store, and there is no mechanism to reload the key file. Token checks are performed in both the token cache and the grant store.
After this PR, the behavior is changed as follows:
During OAuth2 initialization:
During token refresh:
flow.Authorize, which reloads the key file. The new token is stored ingrant.Token.This involves the following changes:
MemoryStoreandKeyringStore.GrantProviderto load the grant from the key file. This abstraction also facilitates testing.This PR introduces the following breaking changes to the OAuth2 package. They are generally safe, as most use cases do not rely on the removed features. And the oauth2 package is used internally for the pulsarclient and pulsaradmin. They shouldn't be used by the client users:
Removal of the grant store.
Removal of
NewAuthenticationOAuth2.Change to
NewDefaultTokenCache:From:
To:
Does this pull request potentially affect one of the following parts:
If
yeswas chosen, please highlight the changesDocumentation