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


##########
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:
   I worry that if we silently fallback to the parent credential when the 
migrated properties is empty, a user using an outdated property thinking it is 
scoped to a different permission will silently start using catalogSession.
   
   I'd rather fail closed here, because we should expect `migrated` to have at 
least the client_credentials, if it is configured properly.



##########
core/src/main/java/org/apache/iceberg/rest/auth/oauth2/EndpointProvider.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.nimbusds.oauth2.sdk.AbstractConfigurationRequest;
+import com.nimbusds.oauth2.sdk.ParseException;
+import com.nimbusds.oauth2.sdk.as.AuthorizationServerConfigurationRequest;
+import com.nimbusds.oauth2.sdk.as.AuthorizationServerEndpointMetadata;
+import com.nimbusds.oauth2.sdk.as.ReadOnlyAuthorizationServerEndpointMetadata;
+import com.nimbusds.oauth2.sdk.http.HTTPResponse;
+import com.nimbusds.oauth2.sdk.id.Issuer;
+import com.nimbusds.openid.connect.sdk.op.OIDCProviderConfigurationRequest;
+import com.nimbusds.openid.connect.sdk.op.OIDCProviderEndpointMetadata;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Locale;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/**
+ * Resolves the token endpoint, either from the configuration or through 
metadata discovery on the
+ * issuer URL. OpenID Connect discovery is attempted first, then RFC 8414 
authorization server
+ * metadata. The resolved endpoint is cached.
+ */
+final class EndpointProvider {
+
+  private final OAuth2Config config;
+  private final OAuth2Runtime runtime;
+
+  private volatile URI tokenEndpoint;
+
+  EndpointProvider(OAuth2Config config, OAuth2Runtime runtime) {
+    this.config = config;
+    this.runtime = runtime;
+    this.tokenEndpoint = config.tokenEndpoint();
+  }
+
+  URI tokenEndpoint() {
+    if (tokenEndpoint == null) {
+      synchronized (this) {
+        if (tokenEndpoint == null) {
+          tokenEndpoint = fetchServerMetadata().getTokenEndpointURI();
+          Preconditions.checkState(
+              tokenEndpoint != null,
+              "Server metadata for issuer %s does not contain a token 
endpoint",
+              config.issuerUrl());
+        }
+      }
+    }
+
+    return tokenEndpoint;
+  }
+
+  private ReadOnlyAuthorizationServerEndpointMetadata fetchServerMetadata() {
+    URI issuerUrl = config.issuerUrl();
+    Preconditions.checkState(issuerUrl != null, "No issuer URL configured");
+    Issuer issuer = new Issuer(issuerUrl);
+
+    Exception oidcFailure;
+    try {
+      return fetchMetadata(new OIDCProviderConfigurationRequest(issuer), 
"OpenID Connect");
+    } catch (IOException | ParseException | RuntimeException e) {
+      oidcFailure = e;
+    }
+
+    try {
+      return fetchMetadata(new 
AuthorizationServerConfigurationRequest(issuer), "OAuth2");
+    } catch (IOException | ParseException | RuntimeException e) {
+      RuntimeException error =
+          new RuntimeException("Failed to fetch server metadata for issuer " + 
issuerUrl, e);
+      error.addSuppressed(oidcFailure);
+      throw error;
+    }
+  }
+
+  private ReadOnlyAuthorizationServerEndpointMetadata fetchMetadata(
+      AbstractConfigurationRequest request, String type) throws IOException, 
ParseException {
+    HTTPResponse httpResponse = 
request.toHTTPRequest().send(runtime.httpClient());
+    if (!httpResponse.indicatesSuccess()) {
+      throw new IOException(
+          String.format(
+              Locale.ROOT,
+              "Failed to fetch %s server metadata: server returned status %d 
with body: %s",
+              type,
+              httpResponse.getStatusCode(),
+              httpResponse.getBody()));
+    }
+

Review Comment:
   should we check the issuer here before returning?
   
   https://datatracker.ietf.org/doc/html/rfc8414#section-3.3



##########
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:
   If I understand this logic correctly, we don't define the connection 
settings like the `issueUrl` or the `tokenEndpoint` in the properties of the 
session context. This means that the client credentials will be sent to the 
deprecated fallback catalog relative token endpoint, rather than to the 
configured `issueUrl`, `tokenEndpoint`, even if it was configured in the 
catalog properties.



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