xiaoxuandev commented on a change in pull request #4280: URL: https://github.com/apache/iceberg/pull/4280#discussion_r829356740
########## File path: aws/src/main/java/org/apache/iceberg/aws/LakeFormationAwsClientFactory.java ########## @@ -0,0 +1,201 @@ +/* + * 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.aws; + +import java.net.URI; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.PropertyUtil; +import software.amazon.awssdk.auth.credentials.AwsCredentials; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; +import software.amazon.awssdk.regions.PartitionMetadata; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.glue.GlueClient; +import software.amazon.awssdk.services.glue.model.GetTableRequest; +import software.amazon.awssdk.services.glue.model.GetTableResponse; +import software.amazon.awssdk.services.kms.KmsClient; +import software.amazon.awssdk.services.lakeformation.LakeFormationClient; +import software.amazon.awssdk.services.lakeformation.model.GetTemporaryGlueTableCredentialsRequest; +import software.amazon.awssdk.services.lakeformation.model.GetTemporaryGlueTableCredentialsResponse; +import software.amazon.awssdk.services.lakeformation.model.Permission; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; +import software.amazon.awssdk.services.sts.model.Tag; + +public class LakeFormationAwsClientFactory + implements AwsClientFactory { + private Map<String, String> properties; + private Region region; + private String s3Endpoint; + + public LakeFormationAwsClientFactory() { + } + + @Override + public void initialize(Map<String, String> catalogProperties) { + this.properties = Maps.newHashMap(catalogProperties); + this.region = region(properties); + this.s3Endpoint = properties.get(AwsProperties.S3FILEIO_ENDPOINT); + } + + @Override + public S3Client s3() { + return S3Client.builder() + .endpointOverride(URI.create(s3Endpoint)) + .credentialsProvider(buildS3CredentialProvider()) + .region(region) + .httpClientBuilder(UrlConnectionHttpClient.builder()) + .build(); + } + + @Override + public GlueClient glue() { + return GlueClient.builder() + .httpClientBuilder(UrlConnectionHttpClient.builder()) + .build(); + } + + @Override + public KmsClient kms() { + return KmsClient.builder() + .httpClientBuilder(UrlConnectionHttpClient.builder()) + .build(); + } + + @Override + public DynamoDbClient dynamo() { + return DynamoDbClient.builder() + .httpClientBuilder(UrlConnectionHttpClient.builder()) + .build(); + } + + private AwsCredentialsProvider buildS3CredentialProvider() { + + GlueClient glue = glue(); + String glueCatalogId = properties.get(AwsProperties.GLUE_CATALOG_ID); + String databaseName = properties.get(AwsProperties.LAKE_FORMATION_DB_NAME); + String tableName = properties.get(AwsProperties.LAKE_FORMATION_TABLE_NAME); + + GetTableResponse response = glue.getTable(GetTableRequest.builder() + .catalogId(glueCatalogId) + .databaseName(databaseName) + .name(tableName) + .build()); + boolean isRegisteredWithLakeFormation = response.table().isRegisteredWithLakeFormation() != null ? + response.table().isRegisteredWithLakeFormation() : Boolean.FALSE; + + if (isRegisteredWithLakeFormation) { + return new LakeFormationCredentialsProvider(properties); + } else { + return LakeFormationCredentialsProvider.getAssumeRoleCredentialsProvider(properties); + } + } + + private static Region region(Map<String, String> properties) { + String region = PropertyUtil.propertyAsString(properties, AwsProperties.CLIENT_ASSUME_ROLE_REGION, null); + if (region == null || region.isEmpty()) { + throw new RuntimeException("Region is empty"); + } + return Region.of(region); + } + + static class LakeFormationCredentialsProvider implements AwsCredentialsProvider { + private LakeFormationClient client; + private String tableArn; + + LakeFormationCredentialsProvider(Map<String, String> properties) { + AwsCredentialsProvider awsCredentialsProvider = getAssumeRoleCredentialsProvider(properties); + String region = PropertyUtil.propertyAsString(properties, AwsProperties.CLIENT_ASSUME_ROLE_REGION, null); + client = buildLakeFormationClient(awsCredentialsProvider, region); + tableArn = getTableArn(properties); + } + + @Override + public AwsCredentials resolveCredentials() { + GetTemporaryGlueTableCredentialsRequest getTemporaryGlueTableCredentialsRequest = + GetTemporaryGlueTableCredentialsRequest.builder() + .tableArn(tableArn) + .permissions(Permission.ALL) + .build(); + GetTemporaryGlueTableCredentialsResponse response = + client.getTemporaryGlueTableCredentials(getTemporaryGlueTableCredentialsRequest); + return AwsSessionCredentials.create(response.accessKeyId(), response.secretAccessKey(), response.sessionToken()); + } + + private static AwsCredentialsProvider getAssumeRoleCredentialsProvider(Map<String, String> properties) { + String roleArn = properties.get(AwsProperties.CLIENT_ASSUME_ROLE_ARN); + Preconditions.checkNotNull(roleArn, "Cannot initialize LakeFormationAwsClientFactory with empty roleArn"); + String sessionName = String.format("iceberg-aws-%s", UUID.randomUUID()); + String externalId = properties.get(AwsProperties.CLIENT_ASSUME_ROLE_EXTERNAL_ID); + String tagString = properties.get(AwsProperties.CLIENT_ASSUME_ROLE_TAGS_PREFIX); + Preconditions.checkNotNull(tagString, "Cannot initialize LakeFormationAwsClientFactory with empty tags"); + Set<Tag> tag = toTags(properties); + int timeout = PropertyUtil.propertyAsInt(properties, + AwsProperties.CLIENT_ASSUME_ROLE_TIMEOUT_SEC, AwsProperties.CLIENT_ASSUME_ROLE_TIMEOUT_SEC_DEFAULT); + AssumeRoleRequest request = AssumeRoleRequest.builder() + .roleArn(roleArn) + .roleSessionName(sessionName) + .durationSeconds(timeout) + .externalId(externalId) + .tags(tag) + .build(); + return StsAssumeRoleCredentialsProvider.builder() + .stsClient(StsClient.builder().httpClientBuilder(UrlConnectionHttpClient.builder()).build()) + .refreshRequest(request) + .build(); + } + + private static Set<Tag> toTags(Map<String, String> properties) { + return PropertyUtil.propertiesWithPrefix(properties, AwsProperties.CLIENT_ASSUME_ROLE_TAGS_PREFIX) + .entrySet().stream() + .map(e -> Tag.builder().key(e.getKey()).value(e.getValue()).build()) + .collect(Collectors.toSet()); + } + + private LakeFormationClient buildLakeFormationClient(AwsCredentialsProvider awsCredentialsProvider, String region) { + return LakeFormationClient.builder() + .credentialsProvider(awsCredentialsProvider) + .region(Region.of(region)).build(); + } + + private String getTableArn(Map<String, String> properties) { + String dbName = properties.get(AwsProperties.LAKE_FORMATION_DB_NAME); + String tableName = properties.get(AwsProperties.LAKE_FORMATION_TABLE_NAME); + String accountId = properties.get(AwsProperties.LAKE_FORMATION_ACCOUNT_ID); + String region = properties.get(AwsProperties.CLIENT_ASSUME_ROLE_REGION); + String partitionName = PartitionMetadata.of(Region.of(region)).name(); + return String.format("arn:%s:glue:%s:%s:table/%s/%s", + partitionName, Review comment: nice catch, fixed. thanks! -- 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]
