adutra commented on code in PR #3750:
URL: https://github.com/apache/polaris/pull/3750#discussion_r3021804120


##########
runtime/service/src/main/java/org/apache/polaris/service/catalog/DefaultAccessDelegationModeResolver.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.polaris.service.catalog;
+
+import static 
org.apache.polaris.service.catalog.AccessDelegationMode.REMOTE_SIGNING;
+import static 
org.apache.polaris.service.catalog.AccessDelegationMode.VENDED_CREDENTIALS;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.inject.Inject;
+import java.util.EnumSet;
+import java.util.Optional;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.polaris.core.config.FeatureConfiguration;
+import org.apache.polaris.core.config.RealmConfig;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
+import org.apache.polaris.core.storage.aws.AwsStorageConfigurationInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Default implementation of {@link AccessDelegationModeResolver} that 
resolves the optimal access
+ * delegation mode based on catalog capabilities and configuration.
+ *
+ * <p>The resolution logic:
+ *
+ * <ol>
+ *   <li>If no delegation mode is requested, returns empty.
+ *   <li>If exactly one delegation mode is requested:
+ *       <ul>
+ *         <li>If {@link AccessDelegationMode#VENDED_CREDENTIALS} is requested 
but the catalog is
+ *             external and {@link 
FeatureConfiguration#ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING}
+ *             is disabled, throws {@link 
org.apache.iceberg.exceptions.ForbiddenException}.
+ *         <li>Otherwise returns that mode as-is.
+ *       </ul>
+ *   <li>If both {@link AccessDelegationMode#VENDED_CREDENTIALS} and {@link
+ *       AccessDelegationMode#REMOTE_SIGNING} are requested, picks the best 
viable mode:
+ *       <ul>
+ *         <li>If the catalog is external and {@link
+ *             FeatureConfiguration#ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING} 
is disabled, returns
+ *             {@link AccessDelegationMode#REMOTE_SIGNING} (graceful 
degradation).
+ *         <li>If the catalog is external/federated and {@link
+ *             
FeatureConfiguration#ALLOW_FEDERATED_CATALOGS_CREDENTIAL_VENDING} is disabled,
+ *             returns {@link AccessDelegationMode#REMOTE_SIGNING}.
+ *         <li>If credential subscoping is skipped, returns {@link
+ *             AccessDelegationMode#REMOTE_SIGNING}.
+ *         <li>If STS is unavailable for the catalog's AWS storage, returns 
{@link
+ *             AccessDelegationMode#REMOTE_SIGNING}.
+ *         <li>Otherwise returns {@link 
AccessDelegationMode#VENDED_CREDENTIALS}.
+ *       </ul>
+ * </ol>
+ */
+@RequestScoped
+public class DefaultAccessDelegationModeResolver implements 
AccessDelegationModeResolver {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(DefaultAccessDelegationModeResolver.class);
+
+  private final RealmConfig realmConfig;
+
+  @Inject
+  public DefaultAccessDelegationModeResolver(CallContext callContext) {
+    this.realmConfig = callContext.getRealmConfig();
+  }
+
+  @Override
+  public @Nonnull Optional<AccessDelegationMode> resolve(
+      @Nonnull EnumSet<AccessDelegationMode> requestedModes,
+      @Nullable CatalogEntity catalogEntity) {
+
+    // Case 1: No valid delegation mode found, or none requested
+    if (requestedModes.isEmpty()) {
+      return Optional.empty();
+    }
+
+    // Case 2: Exactly one delegation mode requested
+    if (requestedModes.size() == 1) {
+      AccessDelegationMode mode = requestedModes.iterator().next();
+      // Credential vending is controlled by 
ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING for external
+      // catalogs. REMOTE_SIGNING does not involve Polaris vending credentials 
to the client, so it
+      // is not gated by this flag.
+      if (mode == VENDED_CREDENTIALS && 
isExternalCatalogCredentialVendingDisabled(catalogEntity)) {
+        throw new ForbiddenException(
+            "Credential vending is not enabled for this external catalog. 
Please consult "
+                + "applicable documentation for the catalog config property 
'%s' to enable this"
+                + " feature",
+            
FeatureConfiguration.ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING.catalogConfig());
+      }
+      LOGGER.debug("Single delegation mode requested: {}", mode);
+      return Optional.of(mode);
+    }
+
+    // Case 3: Both VENDED_CREDENTIALS and REMOTE_SIGNING requested
+
+    // If no catalog entity available, default to VENDED_CREDENTIALS for 
backward compatibility
+    if (catalogEntity == null) {
+      LOGGER.debug(
+          "No catalog entity available for mode resolution, defaulting to 
VENDED_CREDENTIALS");
+      return Optional.of(VENDED_CREDENTIALS);
+    }
+
+    // Check ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING first — this is the 
broader gate that
+    // controls whether external catalogs may vend credentials at all. When 
both modes are
+    // requested and this gate is closed, gracefully fall back to 
REMOTE_SIGNING instead of
+    // throwing, because REMOTE_SIGNING is a viable alternative that does not 
require credential
+    // vending.
+    if (isExternalCatalogCredentialVendingDisabled(catalogEntity)) {
+      LOGGER.debug(
+          "Credential vending not allowed for external catalog {}, selecting 
REMOTE_SIGNING",
+          catalogEntity.getName());
+      return Optional.of(REMOTE_SIGNING);
+    }
+
+    // Check if credential vending is enabled for this catalog.
+    // For internal catalogs, credential vending is always enabled.
+    // For external/federated catalogs, check if 
ALLOW_FEDERATED_CATALOGS_CREDENTIAL_VENDING is
+    // enabled. Note: reaching here means 
ALLOW_EXTERNAL_CATALOG_CREDENTIAL_VENDING is true.
+    boolean credentialVendingEnabled =
+        !catalogEntity.isExternal()
+            || realmConfig.getConfig(
+                
FeatureConfiguration.ALLOW_FEDERATED_CATALOGS_CREDENTIAL_VENDING, 
catalogEntity);

Review Comment:
   And one last small request: let's list `ForbiddenException`  as a possible 
`@throws` in the javadocs of this method.



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

Reply via email to