adutra commented on code in PR #18756:
URL: https://github.com/apache/iceberg/pull/18756#discussion_r4492412711


##########
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()) {

Review Comment:
   That's a reasonable ask, but we can't fail unconditionally when `migrated` 
is empty, because in fact it's almost always empty: most engines don't expose 
`SessionContext`. In those cases, returning the parent session is the right 
choice.
   
   Instead, what we should do is fail fast when the `SessionContext` wasn't 
empty, and contained legacy properties that this manager cannot migrate. That's 
indeed a sane fail-fast attitude 👍 



-- 
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]

Reply via email to