You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by em...@apache.org on 2024/11/12 06:21:14 UTC

(polaris) branch main updated: Fix 403 error message hints (#437)

This is an automated email from the ASF dual-hosted git repository.

emaynard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git


The following commit(s) were added to refs/heads/main by this push:
     new 67cea241 Fix 403 error message hints (#437)
67cea241 is described below

commit 67cea241e43515d21fa1feb856349cea30b8c798
Author: Andrew Guterman <an...@gmail.com>
AuthorDate: Mon Nov 11 22:21:10 2024 -0800

    Fix 403 error message hints (#437)
    
    * Fix 403 error message hints
    
    * Add VisibleForTesting annotation
    
    * update to set
    
    ---------
    
    Co-authored-by: Eric Maynard <er...@snowflake.com>
---
 .../service/catalog/BasePolarisCatalog.java        | 12 +-------
 .../service/exception/IcebergExceptionMapper.java  | 33 ++++++++++++++--------
 .../service/catalog/BasePolarisCatalogTest.java    | 11 ++++++--
 .../service/catalog/io/FileIOIntegrationTest.java  | 16 +++++++----
 4 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/polaris-service/src/main/java/org/apache/polaris/service/catalog/BasePolarisCatalog.java b/polaris-service/src/main/java/org/apache/polaris/service/catalog/BasePolarisCatalog.java
index 42009d33..20a44263 100644
--- a/polaris-service/src/main/java/org/apache/polaris/service/catalog/BasePolarisCatalog.java
+++ b/polaris-service/src/main/java/org/apache/polaris/service/catalog/BasePolarisCatalog.java
@@ -30,7 +30,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
@@ -2104,16 +2103,7 @@ public class BasePolarisCatalog extends BaseMetastoreViewCatalog
   private static boolean isAccessDenied(String errorMsg) {
     // Corresponding error messages for storage providers Aws/Azure/Gcp
     boolean isAccessDenied =
-        errorMsg != null
-            && (errorMsg
-                    .toLowerCase(Locale.ENGLISH)
-                    .contains(IcebergExceptionMapper.AWS_ACCESS_DENIED_HINT)
-                || errorMsg
-                    .toLowerCase(Locale.ENGLISH)
-                    .contains(IcebergExceptionMapper.AZURE_ACCESS_DENIED_HINT)
-                || errorMsg
-                    .toLowerCase(Locale.ENGLISH)
-                    .contains(IcebergExceptionMapper.GCP_ACCESS_DENIED_HINT));
+        errorMsg != null && IcebergExceptionMapper.containsAnyAccessDeniedHint(errorMsg);
     if (isAccessDenied) {
       LOGGER.debug("Access Denied or Forbidden error: {}", errorMsg);
       return true;
diff --git a/polaris-service/src/main/java/org/apache/polaris/service/exception/IcebergExceptionMapper.java b/polaris-service/src/main/java/org/apache/polaris/service/exception/IcebergExceptionMapper.java
index dc217765..6be8d9f4 100644
--- a/polaris-service/src/main/java/org/apache/polaris/service/exception/IcebergExceptionMapper.java
+++ b/polaris-service/src/main/java/org/apache/polaris/service/exception/IcebergExceptionMapper.java
@@ -20,12 +20,15 @@ package org.apache.polaris.service.exception;
 
 import com.azure.core.exception.AzureException;
 import com.google.cloud.storage.StorageException;
+import com.google.common.collect.ImmutableSet;
 import jakarta.ws.rs.WebApplicationException;
 import jakarta.ws.rs.core.MediaType;
 import jakarta.ws.rs.core.Response;
 import jakarta.ws.rs.ext.ExceptionMapper;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Locale;
+import java.util.Set;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.iceberg.exceptions.AlreadyExistsException;
 import org.apache.iceberg.exceptions.CherrypickAncestorCommitException;
@@ -48,6 +51,7 @@ import org.apache.iceberg.exceptions.ServiceUnavailableException;
 import org.apache.iceberg.exceptions.UnprocessableEntityException;
 import org.apache.iceberg.exceptions.ValidationException;
 import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.jetbrains.annotations.VisibleForTesting;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import software.amazon.awssdk.services.s3.model.S3Exception;
@@ -59,10 +63,8 @@ public class IcebergExceptionMapper implements ExceptionMapper<RuntimeException>
   // to lack of permissions
   // We may want to consider a change to Iceberg Core to wrap cloud provider IO exceptions to
   // Iceberg ForbiddenException
-  public static final String AWS_ACCESS_DENIED_HINT = "access denied";
-  public static final String AZURE_ACCESS_DENIED_HINT =
-      "this request is not authorized to perform this operation";
-  public static final String GCP_ACCESS_DENIED_HINT = "forbidden";
+  private static final Set<String> ACCESS_DENIED_HINTS =
+      Set.of("access denied", "not authorized", "forbidden");
 
   public IcebergExceptionMapper() {}
 
@@ -94,13 +96,11 @@ public class IcebergExceptionMapper implements ExceptionMapper<RuntimeException>
           case RESTException e -> Response.Status.SERVICE_UNAVAILABLE.getStatusCode();
           case IllegalArgumentException e -> Response.Status.BAD_REQUEST.getStatusCode();
           case UnsupportedOperationException e -> Response.Status.NOT_ACCEPTABLE.getStatusCode();
-          case S3Exception e when doesAnyThrowableContainInsensitive(e, AWS_ACCESS_DENIED_HINT) ->
+          case S3Exception e when doesAnyThrowableContainAccessDeniedHint(e) ->
               Response.Status.FORBIDDEN.getStatusCode();
-          case AzureException e when doesAnyThrowableContainInsensitive(
-                  e, AZURE_ACCESS_DENIED_HINT) ->
+          case AzureException e when doesAnyThrowableContainAccessDeniedHint(e) ->
               Response.Status.FORBIDDEN.getStatusCode();
-          case StorageException e when doesAnyThrowableContainInsensitive(
-                  e, GCP_ACCESS_DENIED_HINT) ->
+          case StorageException e when doesAnyThrowableContainAccessDeniedHint(e) ->
               Response.Status.FORBIDDEN.getStatusCode();
           case WebApplicationException e -> e.getResponse().getStatus();
           default -> Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
@@ -128,9 +128,18 @@ public class IcebergExceptionMapper implements ExceptionMapper<RuntimeException>
    * @return whether any throwable in the exception chain case-insensitive-contains the given
    *     message
    */
-  static boolean doesAnyThrowableContainInsensitive(Exception e, String message) {
-    String messageLower = message.toLowerCase(Locale.ENGLISH);
+  static boolean doesAnyThrowableContainAccessDeniedHint(Exception e) {
     return Arrays.stream(ExceptionUtils.getThrowables(e))
-        .anyMatch(t -> t.getMessage().toLowerCase(Locale.ENGLISH).contains(messageLower));
+        .anyMatch(t -> containsAnyAccessDeniedHint(t.getMessage()));
+  }
+
+  public static boolean containsAnyAccessDeniedHint(String message) {
+    String messageLower = message.toLowerCase(Locale.ENGLISH);
+    return ACCESS_DENIED_HINTS.stream().anyMatch(messageLower::contains);
+  }
+
+  @VisibleForTesting
+  public static Collection<String> getAccessDeniedHints() {
+    return ImmutableSet.copyOf(ACCESS_DENIED_HINTS);
   }
 }
diff --git a/polaris-service/src/test/java/org/apache/polaris/service/catalog/BasePolarisCatalogTest.java b/polaris-service/src/test/java/org/apache/polaris/service/catalog/BasePolarisCatalogTest.java
index 4c773d2f..bb9bc558 100644
--- a/polaris-service/src/test/java/org/apache/polaris/service/catalog/BasePolarisCatalogTest.java
+++ b/polaris-service/src/test/java/org/apache/polaris/service/catalog/BasePolarisCatalogTest.java
@@ -25,11 +25,13 @@ import static org.mockito.ArgumentMatchers.isA;
 import static org.mockito.Mockito.when;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterators;
 import java.io.IOException;
 import java.time.Clock;
 import java.util.Arrays;
 import java.util.EnumMap;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -88,6 +90,7 @@ import org.apache.polaris.service.admin.PolarisAdminService;
 import org.apache.polaris.service.catalog.io.DefaultFileIOFactory;
 import org.apache.polaris.service.catalog.io.FileIOFactory;
 import org.apache.polaris.service.catalog.io.TestFileIOFactory;
+import org.apache.polaris.service.exception.IcebergExceptionMapper;
 import org.apache.polaris.service.persistence.InMemoryPolarisMetaStoreManagerFactory;
 import org.apache.polaris.service.task.TableCleanupTaskHandler;
 import org.apache.polaris.service.task.TaskExecutor;
@@ -1449,9 +1452,11 @@ public class BasePolarisCatalogTest extends CatalogTests<BasePolarisCatalog> {
 
   @Test
   public void testRetriableException() {
-    RuntimeException s3Exception = new RuntimeException(AWS_ACCESS_DENIED_HINT);
-    RuntimeException azureBlobStorageException = new RuntimeException(AZURE_ACCESS_DENIED_HINT);
-    RuntimeException gcsException = new RuntimeException(GCP_ACCESS_DENIED_HINT);
+    Iterator<String> accessDeniedHint =
+        Iterators.cycle(IcebergExceptionMapper.getAccessDeniedHints());
+    RuntimeException s3Exception = new RuntimeException(accessDeniedHint.next());
+    RuntimeException azureBlobStorageException = new RuntimeException(accessDeniedHint.next());
+    RuntimeException gcsException = new RuntimeException(accessDeniedHint.next());
     RuntimeException otherException = new RuntimeException(new IOException("Connection reset"));
     Assertions.assertThat(BasePolarisCatalog.SHOULD_RETRY_REFRESH_PREDICATE.test(s3Exception))
         .isFalse();
diff --git a/polaris-service/src/test/java/org/apache/polaris/service/catalog/io/FileIOIntegrationTest.java b/polaris-service/src/test/java/org/apache/polaris/service/catalog/io/FileIOIntegrationTest.java
index 487e269b..77317c00 100644
--- a/polaris-service/src/test/java/org/apache/polaris/service/catalog/io/FileIOIntegrationTest.java
+++ b/polaris-service/src/test/java/org/apache/polaris/service/catalog/io/FileIOIntegrationTest.java
@@ -24,11 +24,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.azure.core.exception.AzureException;
 import com.google.cloud.storage.StorageException;
+import com.google.common.collect.Iterators;
 import io.dropwizard.testing.ConfigOverride;
 import io.dropwizard.testing.ResourceHelpers;
 import io.dropwizard.testing.junit5.DropwizardAppExtension;
 import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -160,6 +162,8 @@ public class FileIOIntegrationTest {
   }
 
   private static Stream<Arguments> getIOExceptionTypeTestConfigs() {
+    Iterator<String> accessDeniedHint =
+        Iterators.cycle(IcebergExceptionMapper.getAccessDeniedHints());
     List<IOExceptionTypeTestConfig<?>> configs =
         Stream.of(
                 IOExceptionTypeTestConfig.allVariants(
@@ -167,32 +171,32 @@ public class FileIOIntegrationTest {
                     () ->
                         S3Exception.builder()
                             .statusCode(403)
-                            .message(IcebergExceptionMapper.AWS_ACCESS_DENIED_HINT)
+                            .message(accessDeniedHint.next())
                             .build(),
                     FileIOIntegrationTest::workloadCreateTable),
                 IOExceptionTypeTestConfig.allVariants(
                     ForbiddenException.class,
-                    () -> new AzureException(IcebergExceptionMapper.AZURE_ACCESS_DENIED_HINT),
+                    () -> new AzureException(accessDeniedHint.next()),
                     FileIOIntegrationTest::workloadCreateTable),
                 IOExceptionTypeTestConfig.allVariants(
                     ForbiddenException.class,
-                    () -> new StorageException(403, IcebergExceptionMapper.GCP_ACCESS_DENIED_HINT),
+                    () -> new StorageException(403, accessDeniedHint.next()),
                     FileIOIntegrationTest::workloadCreateTable),
                 IOExceptionTypeTestConfig.allVariants(
                     ForbiddenException.class,
                     () ->
                         S3Exception.builder()
                             .statusCode(403)
-                            .message(IcebergExceptionMapper.AWS_ACCESS_DENIED_HINT)
+                            .message(accessDeniedHint.next())
                             .build(),
                     FileIOIntegrationTest::workloadUpdateTableProperties),
                 IOExceptionTypeTestConfig.allVariants(
                     ForbiddenException.class,
-                    () -> new AzureException(IcebergExceptionMapper.AZURE_ACCESS_DENIED_HINT),
+                    () -> new AzureException(accessDeniedHint.next()),
                     FileIOIntegrationTest::workloadUpdateTableProperties),
                 IOExceptionTypeTestConfig.allVariants(
                     ForbiddenException.class,
-                    () -> new StorageException(403, IcebergExceptionMapper.GCP_ACCESS_DENIED_HINT),
+                    () -> new StorageException(403, accessDeniedHint.next()),
                     FileIOIntegrationTest::workloadUpdateTableProperties))
             .flatMap(Collection::stream)
             .collect(Collectors.toList());