Skip to content

Support reloading OAuth2 key file - #1441

Merged
BewareMyPower merged 8 commits into
apache:masterfrom
RobertIndie:oauth-keyfile-reload
Nov 27, 2025
Merged

BewareMyPower merged 8 commits into
apache:masterfrom
RobertIndie:oauth-keyfile-reload

Conversation

@RobertIndie

@RobertIndie RobertIndie commented Nov 26, 2025 •

Copy link
Copy Markdown
Member

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:

    • Load the private key file if specified.
    • Create an OAuth2 flow and generate the grant.
    • Exchange the token and set it in grant.Token.
    • Store the grant in the grant store.
  • During token refresh:

    • Check if the token has expired in the token cache.
    • Check if the grant token has expired in the grant store.
    • If both are expired, refresh the token using the refresher.

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:

    • Load the private key file if specified.
    • Create an OAuth2 flow and set it to the tokenCache
  • During token refresh:

    • Check if the token has expired in the token cache.
    • If expired, generate a new grant using flow.Authorize, which reloads the key file. The new token is stored in grant.Token.
    • Update the token in the token cache.

This involves the following changes:

  • Removal of the grant store, including MemoryStore and KeyringStore.
  • Introduction of GrantProvider to 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:

    func NewDefaultTokenCache(store store.Store, audience string, refresher oauth2.AuthorizationGrantRefresher) (CachingTokenSource, error)

    To:

    func NewDefaultTokenCache(audience string, flow *oauth2.ClientCredentialsFlow) (CachingTokenSource, error)

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): (yes / no)
  • The public API: (yes / no)
  • The schema: (yes / no / don't know)
  • The default values of configurations: (yes / no)
  • The wire protocol: (yes / no)

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / GoDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

@RobertIndie RobertIndie self-assigned this Nov 26, 2025

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

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/store package) and replaces it with direct flow-based token retrieval
  • Introduces a GrantProvider interface 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.

Comment thread oauth2/cache/cache.go Resolved
Comment thread oauth2/cache/cache.go Resolved
Comment thread oauth2/cache/cache.go Resolved
Comment thread pulsaradmin/pkg/admin/auth/oauth2.go Resolved
Comment thread pulsar/auth/oauth2_test.go Resolved
Comment thread pulsar/auth/oauth2.go Resolved
Comment thread oauth2/cache/cache.go Resolved

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

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.

Comment thread oauth2/auth_suite_test.go Resolved
Comment thread oauth2/auth_suite_test.go Resolved

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

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.

Comment thread pulsaradmin/pkg/admin/auth/oauth2.go Resolved
Comment thread pulsar/auth/oauth2_test.go Resolved
Comment thread oauth2/client_credentials_flow.go Resolved
Comment thread pulsar/auth/oauth2_test.go Resolved
Comment thread oauth2/client_credentials_flow.go Resolved
Comment thread oauth2/client_credentials_flow.go Resolved
Comment thread oauth2/client_credentials_flow.go Resolved

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

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.

Comment thread oauth2/client_credentials_flow.go Resolved
@RobertIndie
RobertIndie marked this pull request as ready for review November 26, 2025 14:22
@BewareMyPower
BewareMyPower merged commit 5b0e054 into apache:master Nov 27, 2025
@RobertIndie
RobertIndie deleted the oauth-keyfile-reload branch November 27, 2025 09:35
@RobertIndie RobertIndie added this to the v0.18.0 milestone Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants