flyrain commented on code in PR #389:
URL: https://github.com/apache/polaris/pull/389#discussion_r1980789090


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleCredentialsStorageIntegration.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.core.storage.s3compatible;
+
+import static 
org.apache.polaris.core.PolarisConfiguration.STORAGE_CREDENTIAL_DURATION_SECONDS;
+import static org.apache.polaris.core.PolarisConfiguration.loadConfig;
+
+import jakarta.ws.rs.NotAuthorizedException;
+import java.net.URI;
+import java.util.EnumMap;
+import java.util.Set;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.storage.InMemoryStorageIntegration;
+import org.apache.polaris.core.storage.PolarisCredentialProperty;
+import org.apache.polaris.core.storage.StorageUtil;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.profiles.ProfileFileSupplier;
+import software.amazon.awssdk.services.sts.StsClient;
+import software.amazon.awssdk.services.sts.StsClientBuilder;
+import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
+import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
+
+/** S3 compatible implementation of PolarisStorageIntegration */
+public class S3CompatibleCredentialsStorageIntegration
+    extends InMemoryStorageIntegration<S3CompatibleStorageConfigurationInfo> {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(S3CompatibleCredentialsStorageIntegration.class);
+
+  public S3CompatibleCredentialsStorageIntegration() {
+    super(S3CompatibleCredentialsStorageIntegration.class.getName());
+  }
+
+  @Override
+  public EnumMap<PolarisCredentialProperty, String> getSubscopedCreds(
+      @NotNull PolarisDiagnostics diagnostics,
+      @NotNull S3CompatibleStorageConfigurationInfo storageConfig,
+      boolean allowListOperation,
+      @NotNull Set<String> allowedReadLocations,
+      @NotNull Set<String> allowedWriteLocations) {
+
+    String caI = 
System.getenv(storageConfig.getS3CredentialsCatalogAccessKeyId());
+    String caS = 
System.getenv(storageConfig.getS3CredentialsCatalogSecretAccessKey());
+
+    EnumMap<PolarisCredentialProperty, String> propertiesMap =
+        new EnumMap<>(PolarisCredentialProperty.class);
+    propertiesMap.put(PolarisCredentialProperty.AWS_ENDPOINT, 
storageConfig.getS3Endpoint());
+    propertiesMap.put(
+        PolarisCredentialProperty.AWS_PATH_STYLE_ACCESS,
+        storageConfig.getS3PathStyleAccess().toString());
+    if (storageConfig.getS3Region() != null) {
+      propertiesMap.put(PolarisCredentialProperty.CLIENT_REGION, 
storageConfig.getS3Region());
+    }
+
+    LOGGER.debug("S3Compatible - createStsClient()");
+    StsClientBuilder stsBuilder = 
software.amazon.awssdk.services.sts.StsClient.builder();
+    stsBuilder.endpointOverride(URI.create(storageConfig.getS3Endpoint()));
+    if (storageConfig.getS3ProfileName() != null) {
+      stsBuilder.credentialsProvider(
+          ProfileCredentialsProvider.builder()
+              .profileFile(ProfileFileSupplier.defaultSupplier())
+              .profileName(storageConfig.getS3ProfileName())
+              .build());
+      LOGGER.debug("S3Compatible - stsClient using profile from catalog 
settings");
+    } else if (caI != null && caS != null) {
+      stsBuilder.credentialsProvider(
+          StaticCredentialsProvider.create(AwsBasicCredentials.create(caI, 
caS)));
+      LOGGER.debug("S3Compatible - stsClient using keys from catalog 
settings");
+    }
+    try (StsClient stsClient = stsBuilder.build()) {
+      LOGGER.debug("S3Compatible - stsClient successfully built");
+      AssumeRoleResponse response =
+          stsClient.assumeRole(
+              AssumeRoleRequest.builder()
+                  .roleSessionName("PolarisCredentialsSTS")
+                  .roleArn(storageConfig.getS3RoleArn())
+                  .policy(
+                      StorageUtil.policyString(
+                              storageConfig.getS3RoleArn(),
+                              allowListOperation,
+                              allowedReadLocations,
+                              allowedWriteLocations)
+                          .toJson())
+                  
.durationSeconds(loadConfig(STORAGE_CREDENTIAL_DURATION_SECONDS))
+                  .build());
+
+      propertiesMap.put(PolarisCredentialProperty.AWS_KEY_ID, 
response.credentials().accessKeyId());
+      propertiesMap.put(
+          PolarisCredentialProperty.AWS_SECRET_KEY, 
response.credentials().secretAccessKey());
+      propertiesMap.put(PolarisCredentialProperty.AWS_TOKEN, 
response.credentials().sessionToken());
+      LOGGER.debug(
+          "S3Compatible - assumeRole - Obtained token expiration : {}",
+          response.credentials().expiration().toString());
+    } catch (Exception e) {
+      throw new NotAuthorizedException(
+          "Unable to build S3 Security Token Service client - " + 
e.getMessage());

Review Comment:
   ```suggestion
         throw new NotAuthorizedException(
             "Unable to build S3 Security Token Service client", e);
   ```



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleStorageConfigurationInfo.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.core.storage.s3compatible;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.MoreObjects;
+import java.util.List;
+import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Polaris Storage Configuration information for an S3 Compatible solution, 
MinIO, Ceph, Dell ECS...
+ */
+public class S3CompatibleStorageConfigurationInfo extends 
PolarisStorageConfigurationInfo {
+
+  // 5 is the approximate max allowed locations for the size of AccessPolicy 
when LIST is required
+  // for allowed read and write locations for subscoping creds.
+  @JsonIgnore private static final int MAX_ALLOWED_LOCATIONS = 5;
+  private @NotNull String s3Endpoint;
+  private @Nullable String s3CredentialsCatalogAccessKeyId;
+  private @Nullable String s3CredentialsCatalogSecretAccessKey;
+  private @Nullable Boolean s3PathStyleAccess;
+  private @NotNull Boolean skipCredentialSubscopingIndirection;
+  private @Nullable String s3CredentialsClientAccessKeyId;
+  private @Nullable String s3CredentialsClientSecretAccessKey;
+  private @Nullable String s3Region;
+  private @Nullable String s3RoleArn;
+
+  // Constructor
+  @JsonCreator
+  public S3CompatibleStorageConfigurationInfo(
+      @JsonProperty(value = "storageType", required = true) @NotNull 
StorageType storageType,
+      @JsonProperty(value = "s3Endpoint", required = true) @NotNull String 
s3Endpoint,
+      @JsonProperty(value = "s3CredentialsCatalogAccessKeyId", required = 
true) @NotNull
+          String s3CredentialsCatalogAccessKeyId,
+      @JsonProperty(value = "s3CredentialsCatalogSecretAccessKey", required = 
true) @NotNull
+          String s3CredentialsCatalogSecretAccessKey,
+      @JsonProperty(value = "SkipCredentialSubscopingIndirection", required = 
false) @Nullable
+          Boolean skipCredentialSubscopingIndirection,
+      @JsonProperty(value = "s3CredentialsClientAccessKeyId", required = 
false) @Nullable
+          String s3CredentialsClientAccessKeyId,
+      @JsonProperty(value = "s3CredentialsClientSecretAccessKey", required = 
false) @Nullable
+          String s3CredentialsClientSecretAccessKey,
+      @JsonProperty(value = "s3PathStyleAccess", required = false) @Nullable
+          Boolean s3PathStyleAccess,
+      @JsonProperty(value = "s3Region", required = false) @Nullable String 
s3Region,
+      @JsonProperty(value = "s3RoleArn", required = false) @Nullable String 
s3RoleArn,
+      @JsonProperty(value = "allowedLocations", required = true) @Nullable
+          List<String> allowedLocations) {
+
+    // storing properties
+    super(storageType, allowedLocations);
+    validateMaxAllowedLocations(MAX_ALLOWED_LOCATIONS);
+    this.s3PathStyleAccess = s3PathStyleAccess;

Review Comment:
   I refer to the field(`s3PathStyleAccess`) in this class, which can be set to 
`null`. In that case, `storageConfig.getS3PathStyleAccess().toString()` will 
throw a NPE.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleCredentialsStorageIntegration.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.core.storage.s3compatible;
+
+import static 
org.apache.polaris.core.PolarisConfiguration.STORAGE_CREDENTIAL_DURATION_SECONDS;
+import static org.apache.polaris.core.PolarisConfiguration.loadConfig;
+
+import jakarta.ws.rs.NotAuthorizedException;
+import java.net.URI;
+import java.util.EnumMap;
+import java.util.Set;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.storage.InMemoryStorageIntegration;
+import org.apache.polaris.core.storage.PolarisCredentialProperty;
+import org.apache.polaris.core.storage.StorageUtil;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.profiles.ProfileFileSupplier;
+import software.amazon.awssdk.services.sts.StsClient;
+import software.amazon.awssdk.services.sts.StsClientBuilder;
+import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
+import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
+
+/** S3 compatible implementation of PolarisStorageIntegration */
+public class S3CompatibleCredentialsStorageIntegration
+    extends InMemoryStorageIntegration<S3CompatibleStorageConfigurationInfo> {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(S3CompatibleCredentialsStorageIntegration.class);
+
+  public S3CompatibleCredentialsStorageIntegration() {
+    super(S3CompatibleCredentialsStorageIntegration.class.getName());
+  }
+
+  @Override
+  public EnumMap<PolarisCredentialProperty, String> getSubscopedCreds(
+      @NotNull PolarisDiagnostics diagnostics,
+      @NotNull S3CompatibleStorageConfigurationInfo storageConfig,
+      boolean allowListOperation,
+      @NotNull Set<String> allowedReadLocations,
+      @NotNull Set<String> allowedWriteLocations) {
+
+    String caI = 
System.getenv(storageConfig.getS3CredentialsCatalogAccessKeyId());
+    String caS = 
System.getenv(storageConfig.getS3CredentialsCatalogSecretAccessKey());
+
+    EnumMap<PolarisCredentialProperty, String> propertiesMap =
+        new EnumMap<>(PolarisCredentialProperty.class);
+    propertiesMap.put(PolarisCredentialProperty.AWS_ENDPOINT, 
storageConfig.getS3Endpoint());
+    propertiesMap.put(
+        PolarisCredentialProperty.AWS_PATH_STYLE_ACCESS,
+        storageConfig.getS3PathStyleAccess().toString());
+    if (storageConfig.getS3Region() != null) {
+      propertiesMap.put(PolarisCredentialProperty.CLIENT_REGION, 
storageConfig.getS3Region());
+    }
+
+    LOGGER.debug("S3Compatible - createStsClient()");
+    StsClientBuilder stsBuilder = 
software.amazon.awssdk.services.sts.StsClient.builder();
+    stsBuilder.endpointOverride(URI.create(storageConfig.getS3Endpoint()));
+    if (storageConfig.getS3ProfileName() != null) {
+      stsBuilder.credentialsProvider(
+          ProfileCredentialsProvider.builder()
+              .profileFile(ProfileFileSupplier.defaultSupplier())
+              .profileName(storageConfig.getS3ProfileName())
+              .build());
+      LOGGER.debug("S3Compatible - stsClient using profile from catalog 
settings");
+    } else if (caI != null && caS != null) {
+      stsBuilder.credentialsProvider(
+          StaticCredentialsProvider.create(AwsBasicCredentials.create(caI, 
caS)));
+      LOGGER.debug("S3Compatible - stsClient using keys from catalog 
settings");
+    }
+    try (StsClient stsClient = stsBuilder.build()) {
+      LOGGER.debug("S3Compatible - stsClient successfully built");
+      AssumeRoleResponse response =
+          stsClient.assumeRole(
+              AssumeRoleRequest.builder()
+                  .roleSessionName("PolarisCredentialsSTS")
+                  .roleArn(storageConfig.getS3RoleArn())
+                  .policy(
+                      StorageUtil.policyString(
+                              storageConfig.getS3RoleArn(),
+                              allowListOperation,
+                              allowedReadLocations,
+                              allowedWriteLocations)
+                          .toJson())
+                  
.durationSeconds(loadConfig(STORAGE_CREDENTIAL_DURATION_SECONDS))
+                  .build());
+
+      propertiesMap.put(PolarisCredentialProperty.AWS_KEY_ID, 
response.credentials().accessKeyId());
+      propertiesMap.put(
+          PolarisCredentialProperty.AWS_SECRET_KEY, 
response.credentials().secretAccessKey());
+      propertiesMap.put(PolarisCredentialProperty.AWS_TOKEN, 
response.credentials().sessionToken());

Review Comment:
   Nit: using static import so that we can keep them in one line each.
   ```suggestion
         propertiesMap.put(AWS_KEY_ID, response.credentials().accessKeyId());
         propertiesMap.put(AWS_SECRET_KEY, 
response.credentials().secretAccessKey());
         propertiesMap.put(AWS_TOKEN, response.credentials().sessionToken());
   ```



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleStorageConfigurationInfo.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.core.storage.s3compatible;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.MoreObjects;
+import jakarta.annotation.Nonnull;
+import java.util.List;
+import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * S3-Compatible Storage Configuration. This class holds the parameters needed 
to connect to
+ * S3-compatible storage services such as MinIO, Ceph, Dell ECS, etc.
+ */
+public class S3CompatibleStorageConfigurationInfo extends 
PolarisStorageConfigurationInfo {
+
+  // 5 is the approximate max allowed locations for the size of AccessPolicy 
when LIST is required
+  // for allowed read and write locations for sub-scoping credentials.
+  @JsonIgnore private static final int MAX_ALLOWED_LOCATIONS = 5;
+  private final @NotNull String s3Endpoint;
+  private final @Nullable String s3ProfileName;
+  private final @Nullable String s3CredentialsCatalogAccessKeyId;
+  private final @Nullable String s3CredentialsCatalogSecretAccessKey;
+  private final @NotNull Boolean s3PathStyleAccess;
+  private final @Nullable String s3Region;
+  private final @Nullable String s3RoleArn;
+
+  @JsonCreator
+  public S3CompatibleStorageConfigurationInfo(
+      @JsonProperty(value = "storageType", required = true) @NotNull 
StorageType storageType,

Review Comment:
   This is not necessary, as line 64 hard codes the `StorageType`.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleCredentialsStorageIntegration.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.core.storage.s3compatible;
+
+import static 
org.apache.polaris.core.PolarisConfiguration.STORAGE_CREDENTIAL_DURATION_SECONDS;
+import static org.apache.polaris.core.PolarisConfiguration.loadConfig;
+
+import jakarta.ws.rs.NotAuthorizedException;
+import java.net.URI;
+import java.util.EnumMap;
+import java.util.Set;
+import org.apache.polaris.core.PolarisDiagnostics;
+import org.apache.polaris.core.storage.InMemoryStorageIntegration;
+import org.apache.polaris.core.storage.PolarisCredentialProperty;
+import org.apache.polaris.core.storage.StorageUtil;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.profiles.ProfileFileSupplier;
+import software.amazon.awssdk.services.sts.StsClient;
+import software.amazon.awssdk.services.sts.StsClientBuilder;
+import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
+import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
+
+/** S3 compatible implementation of PolarisStorageIntegration */
+public class S3CompatibleCredentialsStorageIntegration
+    extends InMemoryStorageIntegration<S3CompatibleStorageConfigurationInfo> {
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(S3CompatibleCredentialsStorageIntegration.class);
+
+  public S3CompatibleCredentialsStorageIntegration() {
+    super(S3CompatibleCredentialsStorageIntegration.class.getName());
+  }
+
+  @Override
+  public EnumMap<PolarisCredentialProperty, String> getSubscopedCreds(
+      @NotNull PolarisDiagnostics diagnostics,
+      @NotNull S3CompatibleStorageConfigurationInfo storageConfig,
+      boolean allowListOperation,
+      @NotNull Set<String> allowedReadLocations,
+      @NotNull Set<String> allowedWriteLocations) {
+
+    String caI = 
System.getenv(storageConfig.getS3CredentialsCatalogAccessKeyId());
+    String caS = 
System.getenv(storageConfig.getS3CredentialsCatalogSecretAccessKey());
+
+    EnumMap<PolarisCredentialProperty, String> propertiesMap =
+        new EnumMap<>(PolarisCredentialProperty.class);
+    propertiesMap.put(PolarisCredentialProperty.AWS_ENDPOINT, 
storageConfig.getS3Endpoint());
+    propertiesMap.put(
+        PolarisCredentialProperty.AWS_PATH_STYLE_ACCESS,
+        storageConfig.getS3PathStyleAccess().toString());
+    if (storageConfig.getS3Region() != null) {
+      propertiesMap.put(PolarisCredentialProperty.CLIENT_REGION, 
storageConfig.getS3Region());
+    }
+
+    LOGGER.debug("S3Compatible - createStsClient()");
+    StsClientBuilder stsBuilder = 
software.amazon.awssdk.services.sts.StsClient.builder();

Review Comment:
   Nit: `software.amazon.awssdk.services.sts.StsClient.builder();` -> 
`StsClient.builder();`



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