adutra commented on code in PR #18756: URL: https://github.com/apache/iceberg/pull/18756#discussion_r4492781126
########## core/src/main/java/org/apache/iceberg/rest/auth/oauth2/OAuth2Manager.java: ########## @@ -0,0 +1,185 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.rest.auth.oauth2; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.time.Duration; +import java.util.Map; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.catalog.SessionCatalog.SessionContext; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.rest.RESTClient; +import org.apache.iceberg.rest.RESTUtil; +import org.apache.iceberg.rest.auth.AuthManager; +import org.apache.iceberg.rest.auth.AuthSession; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.ThreadPools; + +/** + * An {@link AuthManager} that authenticates with an OAuth2 authorization server using the client + * credentials grant, and renews access tokens before they expire. + * + * <p>Callers must stop using this manager before closing it. + */ +public class OAuth2Manager implements AuthManager { + + private final LegacyConfigAdapter legacyConfigAdapter = new LegacyConfigAdapter(); + + // The client used by the init, catalog and contextual sessions. The init client is closed once + // the catalog is initialized, so sessions must always resolve the current client lazily. + private volatile RESTClient restClient; + private volatile String catalogUri; + + private OAuth2Session initSession; + private OAuth2Session catalogSession; + + private volatile Cache<String, OAuth2Session> cacheById; + private volatile Cache<OAuth2Config, OAuth2Session> cacheByConfig; + + public OAuth2Manager(String ignoredManagerName) {} + + @Override + public AuthSession initSession(RESTClient initClient, Map<String, String> initProperties) { + this.restClient = initClient; + this.catalogUri = initProperties.get(CatalogProperties.URI); + OAuth2Config initConfig = createConfig(initProperties, catalogUri); + this.initSession = new OAuth2Session(initConfig, () -> restClient); + return initSession; + } + + @Override + public AuthSession catalogSession(RESTClient sharedClient, Map<String, String> catalogProps) { + this.restClient = sharedClient; + this.catalogUri = catalogProps.get(CatalogProperties.URI); + this.cacheById = newCache(sessionTimeout(catalogProps)); + OAuth2Config catalogConfig = createConfig(catalogProps, catalogUri); + this.catalogSession = new OAuth2Session(catalogConfig, () -> restClient); + return catalogSession; + } + + @Override + public AuthSession contextualSession(SessionContext context, AuthSession parent) { + Map<String, String> contextProperties = + RESTUtil.merge( + context.properties() != null ? context.properties() : Map.of(), + context.credentials() != null ? context.credentials() : Map.of()); + Map<String, String> migrated = legacyConfigAdapter.migrate(contextProperties); + if (migrated.isEmpty()) { + return parent; + } + + // the contextual configuration is self-contained: nothing is inherited from the parent config + OAuth2Config contextConfig = + OAuth2Config.fromProperties(legacyConfigAdapter.resolveTokenEndpoint(migrated, catalogUri)); Review Comment: Indeed we don't carry over any defaults from the catalog properties, so if the `SessionContext` does not contain a token endpoint, but contains other OAuth2 properties, the default catalog internal token endpoint would be used. Why I think it's generally not a good idea to merge catalog properties with context properties: consider the following setup: * Catalog properties: ```properties rest.auth.oauth2.issuer-url=issuer1 rest.auth.oauth2.client-id=client1 rest.auth.oauth2.client-secret=secret1 rest.auth.oauth2.resource=resource1 ``` * Context properties (note no client secret, maybe hinting at a public client): ```properties rest.auth.oauth2.issuer-url=issuer2 rest.auth.oauth2.client-id=client2 rest.auth.oauth2.audience=audience2 ``` If we blindly merge the two, we'd get: ```properties rest.auth.oauth2.token-endpoint=issuer2 rest.auth.oauth2.client-id=client2 rest.auth.oauth2.client-secret=secret1 rest.auth.oauth2.resource=resource1 rest.auth.oauth2.audience=audience2 ``` Which is almost certainly a wrong setup. -- 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]
