xiaoxuandev commented on a change in pull request #4280: URL: https://github.com/apache/iceberg/pull/4280#discussion_r830695776
########## File path: aws/src/main/java/org/apache/iceberg/aws/LakeFormationAwsClientFactory.java ########## @@ -0,0 +1,153 @@ +/* + * 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 org.apache.iceberg.relocated.com.google.common.base.Preconditions; +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.glue.model.GetTableRequest; +import software.amazon.awssdk.services.glue.model.GetTableResponse; +import software.amazon.awssdk.services.kms.KmsClient; +import software.amazon.awssdk.services.kms.KmsClientBuilder; +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.s3.S3ClientBuilder; + +/** + * This implementation of AwsClientFactory is used if LakeFormation is enabled. + * It extends AssumeRoleAwsClientFactory to reuse the assuming-role approach for all clients except S3 and KMS. + * If a table is registered with LakeFormation, the S3/KMS client will use LakeFormation credentials, + * otherwise it uses AssumingRole credentials. + */ +public class LakeFormationAwsClientFactory extends AssumeRoleAwsClientFactory { + + public static final String LF_AUTHORIZED_CALLER = "LakeFormationAuthorizedCaller"; + + private String glueCatalogId; + private String dbName; + private String tableName; + private String glueAccountId; + private String region; + + public LakeFormationAwsClientFactory() { + } + + @Override + public void initialize(Map<String, String> catalogProperties) { + super.initialize(catalogProperties); + Preconditions.checkArgument(tags().stream().anyMatch(t -> t.key().equals(LF_AUTHORIZED_CALLER)), + "must set %s through %s", LF_AUTHORIZED_CALLER, AwsProperties.CLIENT_ASSUME_ROLE_TAGS_PREFIX); + this.glueCatalogId = catalogProperties.get(AwsProperties.GLUE_CATALOG_ID); + this.dbName = catalogProperties.get(AwsProperties.LAKE_FORMATION_DB_NAME); + this.tableName = catalogProperties.get(AwsProperties.LAKE_FORMATION_TABLE_NAME); + this.glueAccountId = catalogProperties.get(AwsProperties.GLUE_ACCOUNT_ID); + this.region = catalogProperties.get(AwsProperties.CLIENT_ASSUME_ROLE_REGION); + } + + @Override + public S3Client s3() { + if (isRegisteredWithLakeFormation()) { + S3ClientBuilder builder = S3Client.builder() + .endpointOverride(URI.create(s3Endpoint())) + .credentialsProvider(new LakeFormationCredentialsProvider(getLakeFormationClient(), buildTableArn())) + .region(Region.of(region())) + .httpClientBuilder(UrlConnectionHttpClient.builder()); + return builder.build(); + } else { + return super.s3(); + } + } + + @Override + public KmsClient kms() { + if (isRegisteredWithLakeFormation()) { + KmsClientBuilder builder = KmsClient.builder() + .credentialsProvider(new LakeFormationCredentialsProvider(getLakeFormationClient(), buildTableArn())) + .region(Region.of(region())) + .httpClientBuilder(UrlConnectionHttpClient.builder()); + return builder.build(); + } else { + return super.kms(); + } + } + + private boolean isRegisteredWithLakeFormation() { + Preconditions.checkArgument(dbName != null && !dbName.isEmpty(), "db name can not be empty"); + Preconditions.checkArgument(tableName != null && !tableName.isEmpty(), "table name can not be empty"); + + GetTableResponse response = glue().getTable(GetTableRequest.builder() + .catalogId(glueCatalogId) + .databaseName(dbName) + .name(tableName) + .build()); + return response.table().isRegisteredWithLakeFormation(); + } + + private String buildTableArn() { + Preconditions.checkArgument(region != null && !region.isEmpty(), "region can not be empty"); + Preconditions.checkArgument(glueAccountId != null && !glueAccountId.isEmpty(), "glueAccountId can not be empty"); + Preconditions.checkArgument(dbName != null && !dbName.isEmpty(), "db name can not be empty"); + Preconditions.checkArgument(tableName != null && !tableName.isEmpty(), "table name can not be empty"); + String partitionName = PartitionMetadata.of(Region.of(region)).id(); + return String.format("arn:%s:glue:%s:%s:table/%s/%s", + partitionName, + region, + glueAccountId, + dbName, + tableName); + } + + private LakeFormationClient getLakeFormationClient() { + return LakeFormationClient.builder() + .applyMutation(this::configure) + .build(); + } + + static class LakeFormationCredentialsProvider implements AwsCredentialsProvider { + private LakeFormationClient client; + private String tableArn; + + LakeFormationCredentialsProvider(LakeFormationClient lakeFormationClient, String tableArn) { + this.client = lakeFormationClient; + this.tableArn = tableArn; + } + + @Override + public AwsCredentials resolveCredentials() { + GetTemporaryGlueTableCredentialsRequest getTemporaryGlueTableCredentialsRequest = + GetTemporaryGlueTableCredentialsRequest.builder() + .tableArn(tableArn) + .permissions(Permission.ALL) Review comment: done. 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]
