-
Notifications
You must be signed in to change notification settings - Fork 23
Add token refresh and token exchange OAuth2 flows to polaris-synchronizer tool. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eric-maynard
merged 11 commits into
apache:main
from
mansehajsingh:client-credentials-token-refresh
Apr 25, 2025
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7d9bd7
Add token refresh and token exchange auth flow
mansehajsingh dc916eb
use credential instead of client id and client secret
mansehajsingh 2cc9b5e
Added token exchange flow
mansehajsingh e1fadbc
Remove spurious change
mansehajsingh 26a58c4
Addressed comments
mansehajsingh 19fd8ee
Removed token exchnage
mansehajsingh 2c1a76e
update comment
mansehajsingh 69a8b56
Services properly close resources and enforce closeability on abstrac…
mansehajsingh 3ee3d06
Removed credential and token from parent auth
mansehajsingh ca0e61b
addressed comments
mansehajsingh 565839e
Closed iceberg catalog service properly
mansehajsingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
90 changes: 90 additions & 0 deletions
90
...rc/main/java/org/apache/polaris/tools/sync/polaris/auth/AuthenticationSessionWrapper.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package org.apache.polaris.tools.sync.polaris.auth; | ||
|
|
||
| import org.apache.iceberg.rest.HTTPClient; | ||
| import org.apache.iceberg.rest.RESTClient; | ||
| import org.apache.iceberg.rest.auth.AuthConfig; | ||
| import org.apache.iceberg.rest.auth.OAuth2Properties; | ||
| import org.apache.iceberg.rest.auth.OAuth2Util; | ||
| import org.apache.iceberg.util.ThreadPools; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
|
|
||
| /** | ||
| * Wraps {@link OAuth2Util.AuthSession} to provide supported authentication flows. | ||
| */ | ||
| public class AuthenticationSessionWrapper implements Closeable { | ||
|
|
||
| private final RESTClient restClient; | ||
|
|
||
| private final OAuth2Util.AuthSession authSession; | ||
|
|
||
| private final ScheduledExecutorService executor; | ||
|
|
||
| public AuthenticationSessionWrapper(Map<String, String> properties) { | ||
| this.restClient = HTTPClient.builder(Map.of()) | ||
| .uri(properties.get(OAuth2Properties.OAUTH2_SERVER_URI)) | ||
| .build(); | ||
| this.authSession = this.newAuthSession(this.restClient, properties); | ||
| this.executor = ThreadPools.newScheduledPool(UUID.randomUUID() + "-token-refresh", 1); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes a new authentication session. Supports client_credentials and bearer token flow. | ||
| * @param properties properties to initialize the session with | ||
| * @return an authentication session, with token refresh if applicable | ||
| */ | ||
| private OAuth2Util.AuthSession newAuthSession(RESTClient restClient, Map<String, String> properties) { | ||
| OAuth2Util.AuthSession parent = new OAuth2Util.AuthSession( | ||
| Map.of(), | ||
| AuthConfig.builder() | ||
| .scope(properties.get(OAuth2Properties.SCOPE)) | ||
| .oauth2ServerUri(properties.get(OAuth2Properties.OAUTH2_SERVER_URI)) | ||
| .optionalOAuthParams(OAuth2Util.buildOptionalParam(properties)) | ||
| .build() | ||
| ); | ||
|
|
||
| // This is for client_credentials flow | ||
| if (properties.containsKey(OAuth2Properties.CREDENTIAL)) { | ||
| return OAuth2Util.AuthSession.fromCredential( | ||
| restClient, | ||
| // threads created here will be daemon threads, so termination of main program | ||
| // will terminate the token refresh thread automatically | ||
| this.executor, | ||
| properties.get(OAuth2Properties.CREDENTIAL), | ||
| parent | ||
| ); | ||
| } | ||
|
|
||
| // This is for regular bearer token flow | ||
| if (properties.containsKey(OAuth2Properties.TOKEN)) { | ||
| return OAuth2Util.AuthSession.fromAccessToken( | ||
| restClient, | ||
| // threads created here will be daemon threads, so termination of main program | ||
| // will terminate the token refresh thread automatically | ||
| this.executor, | ||
| properties.get(OAuth2Properties.TOKEN), | ||
| null, /* defaultExpiresAtMillis */ | ||
| parent | ||
| ); | ||
| } | ||
|
|
||
| throw new IllegalArgumentException("Unable to construct authenticated session with the provided properties."); | ||
| } | ||
|
|
||
| /** | ||
| * Get refreshed authentication headers for session. | ||
| */ | ||
| public Map<String, String> getSessionHeaders() { | ||
| return this.authSession.headers(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| try (restClient; executor) {} | ||
| } | ||
|
|
||
| } |
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
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
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
39 changes: 0 additions & 39 deletions
39
...s-synchronizer/api/src/main/java/org/apache/polaris/tools/sync/polaris/http/HttpUtil.java
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
...synchronizer/api/src/main/java/org/apache/polaris/tools/sync/polaris/http/OAuth2Util.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks suspicious; the catalogs won't be properly closed if an error is thrown above.
Suggestion: