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


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/s3compatible/S3CompatibleCredentialsStorageIntegration.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.core.storage.s3compatible;
+
+import jakarta.annotation.Nonnull;
+import java.net.URI;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+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.StaticCredentialsProvider;
+import software.amazon.awssdk.policybuilder.iam.IamConditionOperator;
+import software.amazon.awssdk.policybuilder.iam.IamEffect;
+import software.amazon.awssdk.policybuilder.iam.IamPolicy;
+import software.amazon.awssdk.policybuilder.iam.IamResource;
+import software.amazon.awssdk.policybuilder.iam.IamStatement;
+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);
+  private StsClient stsClient;
+  private String caI, caS; // catalogAccessKeyId & catalogSecretAccessKey
+  private String clI, clS; // clientAccessKeyId & clientSecretAccessKey
+
+  public S3CompatibleCredentialsStorageIntegration() {
+    super(S3CompatibleCredentialsStorageIntegration.class.getName());
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public EnumMap<PolarisCredentialProperty, String> getSubscopedCreds(
+      @NotNull PolarisDiagnostics diagnostics,
+      @NotNull S3CompatibleStorageConfigurationInfo storageConfig,
+      boolean allowListOperation,
+      @NotNull Set<String> allowedReadLocations,
+      @NotNull Set<String> allowedWriteLocations) {
+
+    caI = System.getenv(storageConfig.getS3CredentialsCatalogAccessKeyId());
+    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());
+    }
+
+    if (storageConfig.getSkipCredentialSubscopingIndirection() == true) {
+      LOGGER.debug("S3Compatible - skipCredentialSubscopingIndirection !");
+      clI = System.getenv(storageConfig.getS3CredentialsClientAccessKeyId());
+      clS = 
System.getenv(storageConfig.getS3CredentialsClientSecretAccessKey());
+      if (clI != null && clS != null) {
+        propertiesMap.put(PolarisCredentialProperty.AWS_KEY_ID, clI);
+        propertiesMap.put(PolarisCredentialProperty.AWS_SECRET_KEY, clS);
+      } else {
+        propertiesMap.put(PolarisCredentialProperty.AWS_KEY_ID, caI);
+        propertiesMap.put(PolarisCredentialProperty.AWS_SECRET_KEY, caS);
+      }
+    } else {
+      LOGGER.debug("S3Compatible - assumeRole !");
+      createStsClient(storageConfig);
+      AssumeRoleResponse response =
+          stsClient.assumeRole(
+              AssumeRoleRequest.builder()
+                  .roleSessionName("PolarisCredentialsSTS")
+                  .roleArn(
+                      (storageConfig.getS3RoleArn() == null) ? "" : 
storageConfig.getS3RoleArn())
+                  .policy(
+                      policyString(allowListOperation, allowedReadLocations, 
allowedWriteLocations)
+                          .toJson())
+                  .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 - Token Expiration at : {}",
+          response.credentials().expiration().toString());
+    }
+
+    return propertiesMap;
+  }
+
+  public void createStsClient(S3CompatibleStorageConfigurationInfo 
storageConfig) {
+    LOGGER.debug("S3Compatible - createStsClient()");
+    try {
+      StsClientBuilder stsBuilder = 
software.amazon.awssdk.services.sts.StsClient.builder();
+      stsBuilder.endpointOverride(URI.create(storageConfig.getS3Endpoint()));
+      if (caI != null && caS != null) {
+        // else default provider build credentials from profile or standard 
AWS env var
+        stsBuilder.credentialsProvider(
+            StaticCredentialsProvider.create(AwsBasicCredentials.create(caI, 
caS)));
+      }
+      this.stsClient = stsBuilder.build();
+      LOGGER.debug("S3Compatible - stsClient successfully built");
+
+    } catch (Exception e) {
+      System.err.println("S3Compatible - stsClient - build failure : " + 
e.getMessage());
+    }
+  }
+
+  /*
+   * function from AwsCredentialsStorageIntegration but without roleArn 
parameter
+   */
+  private IamPolicy policyString(

Review Comment:
   Any suggestion ?
   Something like calling the awsCredentialsStorageIntegration one  ? (I have 
failed to deal with roleArn pattern onPrem not allowed by this method i.e :  
"arn:ecs:s3:...." )



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