rdblue commented on code in PR #4830:
URL: https://github.com/apache/iceberg/pull/4830#discussion_r880752384
##########
core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java:
##########
@@ -87,18 +106,54 @@ public RESTSessionCatalog() {
@Override
public void initialize(String name, Map<String, String> props) {
Preconditions.checkArgument(props != null, "Invalid configuration: null");
- ConfigResponse config = fetchConfig(props);
- Map<String, String> properties = config.merge(props);
- this.client = clientBuilder.apply(properties);
- this.baseHeaders = RESTUtil.extractPrefixMap(properties, "header.");
- this.paths = ResourcePaths.forCatalogProperties(properties);
- String ioImpl = properties.get(CatalogProperties.FILE_IO_IMPL);
- this.io = CatalogUtil.loadFileIO(ioImpl != null ? ioImpl :
ResolvingFileIO.class.getName(), properties, conf);
- super.initialize(name, properties);
+
+ long startTimeMillis = System.currentTimeMillis(); // keep track of the
init start time for token refresh
+ String initToken = props.get(OAuth2Properties.TOKEN);
+
+ // fetch auth and config to complete initialization
+ ConfigResponse config;
+ OAuthTokenResponse authResponse;
+ try (RESTClient initClient = clientBuilder.apply(props)) {
+ Map<String, String> initHeaders = RESTUtil.merge(configHeaders(props),
OAuth2Util.authHeaders(initToken));
+ String credential = props.get(OAuth2Properties.CREDENTIAL);
+ if (credential != null && !credential.isEmpty()) {
+ String scope = props.getOrDefault(OAuth2Properties.SCOPE,
OAuth2Properties.CATALOG_SCOPE);
+ authResponse = OAuth2Util.fetchToken(initClient, initHeaders,
credential, scope);
+ config = fetchConfig(initClient, RESTUtil.merge(initHeaders,
OAuth2Util.authHeaders(authResponse.token())));
+ } else {
+ authResponse = null;
+ config = fetchConfig(initClient, initHeaders);
+ }
+ } catch (IOException e) {
+ throw new UncheckedIOException("Failed to close HTTP client", e);
+ }
+
+ // build the final configuration and set up the catalog's auth
+ Map<String, String> mergedProps = config.merge(props);
+ Map<String, String> baseHeaders = configHeaders(mergedProps);
+ this.catalogAuth = new AuthSession(baseHeaders, null, null);
+ if (authResponse != null) {
+ this.catalogAuth = newSession(authResponse, startTimeMillis,
catalogAuth);
+ } else if (initToken != null) {
+ this.catalogAuth = newSession(initToken, expiresInMs(mergedProps),
catalogAuth);
Review Comment:
This is the case where you specify an bearer token directly using "token".
In that case, we use a catalog property to determine when to refresh that
bearer token. This is the path for a scheduler to get an access token and pass
it into a job. It can also pass the expiration interval, or just rely on the
default.
Right now, we don't support an initial token exchange. We could go through
the same logic as a new session or table load, where we look for different
token-exchange types and exchange for an access token. The only trouble there
is that I though it would be weird to take the token and exchange it right
away. It seems like you should be using client credentials to bootstrap.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]