-
Notifications
You must be signed in to change notification settings - Fork 3k
AWS: Add LakeFormation credential support for GlueCatalog #4280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
...egration/java/org/apache/iceberg/aws/lakeformation/TestLakeFormationAwsClientFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * 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.lakeformation; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import org.apache.iceberg.aws.AwsIntegTestUtil; | ||
| import org.apache.iceberg.aws.AwsProperties; | ||
| import org.apache.iceberg.aws.glue.GlueCatalog; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.glue.model.AccessDeniedException; | ||
| import software.amazon.awssdk.services.glue.model.GlueException; | ||
| import software.amazon.awssdk.services.iam.IamClient; | ||
| import software.amazon.awssdk.services.iam.model.CreateRoleRequest; | ||
| import software.amazon.awssdk.services.iam.model.CreateRoleResponse; | ||
| import software.amazon.awssdk.services.iam.model.DeleteRolePolicyRequest; | ||
| import software.amazon.awssdk.services.iam.model.DeleteRoleRequest; | ||
| import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; | ||
|
|
||
| public class TestLakeFormationAwsClientFactory { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(TestLakeFormationAwsClientFactory.class); | ||
| private static final int IAM_PROPAGATION_DELAY = 10000; | ||
| private static final int ASSUME_ROLE_SESSION_DURATION = 3600; | ||
|
|
||
| private IamClient iam; | ||
| private String roleName; | ||
| private Map<String, String> assumeRoleProperties; | ||
| private String policyName; | ||
|
|
||
| @Before | ||
| public void before() { | ||
| roleName = UUID.randomUUID().toString(); | ||
| iam = IamClient.builder() | ||
| .region(Region.AWS_GLOBAL) | ||
| .httpClientBuilder(UrlConnectionHttpClient.builder()) | ||
| .build(); | ||
| CreateRoleResponse response = iam.createRole(CreateRoleRequest.builder() | ||
| .roleName(roleName) | ||
| .assumeRolePolicyDocument("{" + | ||
| "\"Version\":\"2012-10-17\"," + | ||
| "\"Statement\":[{" + | ||
| "\"Effect\":\"Allow\"," + | ||
| "\"Principal\":{" + | ||
| "\"AWS\":\"arn:aws:iam::" + AwsIntegTestUtil.testAccountId() + ":root\"}," + | ||
| "\"Action\": [\"sts:AssumeRole\"," + | ||
| "\"sts:TagSession\"]}]}") | ||
| .maxSessionDuration(ASSUME_ROLE_SESSION_DURATION) | ||
| .build()); | ||
| assumeRoleProperties = Maps.newHashMap(); | ||
| assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_REGION, "us-east-1"); | ||
| assumeRoleProperties.put(AwsProperties.GLUE_LAKEFORMATION_ENABLED, "true"); | ||
| assumeRoleProperties.put(AwsProperties.HTTP_CLIENT_TYPE, AwsProperties.HTTP_CLIENT_TYPE_APACHE); | ||
| assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_ARN, response.role().arn()); | ||
| assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_TAGS_PREFIX + | ||
| LakeFormationAwsClientFactory.LF_AUTHORIZED_CALLER, "emr"); | ||
| policyName = UUID.randomUUID().toString(); | ||
| } | ||
|
|
||
| @After | ||
| public void after() { | ||
| iam.deleteRolePolicy(DeleteRolePolicyRequest.builder().roleName(roleName).policyName(policyName).build()); | ||
| iam.deleteRole(DeleteRoleRequest.builder().roleName(roleName).build()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLakeFormationEnabledGlueCatalog() throws Exception { | ||
| String glueArnPrefix = "arn:aws:glue:*:" + AwsIntegTestUtil.testAccountId(); | ||
| iam.putRolePolicy(PutRolePolicyRequest.builder() | ||
| .roleName(roleName) | ||
| .policyName(policyName) | ||
| .policyDocument("{" + | ||
| "\"Version\":\"2012-10-17\"," + | ||
| "\"Statement\":[{" + | ||
| "\"Sid\":\"policy1\"," + | ||
| "\"Effect\":\"Allow\"," + | ||
| "\"Action\":[\"glue:CreateDatabase\",\"glue:DeleteDatabase\"," + | ||
| "\"glue:Get*\",\"lakeformation:GetDataAccess\"]," + | ||
| "\"Resource\":[\"" + glueArnPrefix + ":catalog\"," + | ||
| "\"" + glueArnPrefix + ":database/allowed_*\"," + | ||
| "\"" + glueArnPrefix + ":table/allowed_*/*\"," + | ||
| "\"" + glueArnPrefix + ":userDefinedFunction/allowed_*/*\"]}]}") | ||
| .build()); | ||
| waitForIamConsistency(); | ||
|
|
||
| GlueCatalog glueCatalog = new GlueCatalog(); | ||
| assumeRoleProperties.put("warehouse", "s3://path"); | ||
| glueCatalog.initialize("test", assumeRoleProperties); | ||
| Namespace deniedNamespace = Namespace.of("denied_" + UUID.randomUUID().toString().replace("-", "")); | ||
| try { | ||
| glueCatalog.createNamespace(deniedNamespace); | ||
| Assert.fail("Access to Glue should be denied"); | ||
| } catch (GlueException e) { | ||
| Assert.assertEquals(AccessDeniedException.class, e.getClass()); | ||
| } catch (AssertionError e) { | ||
| glueCatalog.dropNamespace(deniedNamespace); | ||
| throw e; | ||
| } | ||
|
|
||
| Namespace allowedNamespace = Namespace.of("allowed_" + UUID.randomUUID().toString().replace("-", "")); | ||
| try { | ||
| glueCatalog.createNamespace(allowedNamespace); | ||
| } catch (GlueException e) { | ||
| LOG.error("fail to create Glue database", e); | ||
| Assert.fail("create namespace should succeed"); | ||
| } finally { | ||
| glueCatalog.dropNamespace(allowedNamespace); | ||
| try { | ||
| glueCatalog.close(); | ||
| } catch (Exception e) { | ||
| // swallow exception during closing | ||
| LOG.error("Error closing GlueCatalog", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void waitForIamConsistency() throws Exception { | ||
| Thread.sleep(IAM_PROPAGATION_DELAY); // sleep to make sure IAM up to date | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,9 @@ | |
| import org.apache.iceberg.TableMetadata; | ||
| import org.apache.iceberg.TableOperations; | ||
| import org.apache.iceberg.aws.AwsClientFactories; | ||
| import org.apache.iceberg.aws.AwsClientFactory; | ||
| import org.apache.iceberg.aws.AwsProperties; | ||
| import org.apache.iceberg.aws.lakeformation.LakeFormationAwsClientFactory; | ||
| import org.apache.iceberg.aws.s3.S3FileIO; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.SupportsNamespaces; | ||
|
|
@@ -49,9 +51,11 @@ | |
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.util.LockManagers; | ||
| import org.apache.iceberg.util.PropertyUtil; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.services.glue.GlueClient; | ||
|
|
@@ -87,6 +91,7 @@ public class GlueCatalog extends BaseMetastoreCatalog | |
| private FileIO fileIO; | ||
| private LockManager lockManager; | ||
xiaoxuandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private CloseableGroup closeableGroup; | ||
| private Map<String, String> catalogProperties; | ||
|
|
||
| // Attempt to set versionId if available on the path | ||
| private static final DynMethods.UnboundMethod SET_VERSION_ID = DynMethods.builder("versionId") | ||
|
|
@@ -104,13 +109,36 @@ public GlueCatalog() { | |
|
|
||
| @Override | ||
| public void initialize(String name, Map<String, String> properties) { | ||
| AwsClientFactory awsClientFactory; | ||
| FileIO catalogFileIO; | ||
| if (PropertyUtil.propertyAsBoolean( | ||
| properties, | ||
| AwsProperties.GLUE_LAKEFORMATION_ENABLED, | ||
| AwsProperties.GLUE_LAKEFORMATION_ENABLED_DEFAULT)) { | ||
| String factoryImpl = PropertyUtil.propertyAsString(properties, AwsProperties.CLIENT_FACTORY, null); | ||
| ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder().putAll(properties); | ||
| if (factoryImpl == null) { | ||
| builder.put(AwsProperties.CLIENT_FACTORY, LakeFormationAwsClientFactory.class.getName()); | ||
| } | ||
jackye1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| this.catalogProperties = builder.build(); | ||
xiaoxuandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| awsClientFactory = AwsClientFactories.from(catalogProperties); | ||
| Preconditions.checkArgument(awsClientFactory instanceof LakeFormationAwsClientFactory, | ||
| "Detected LakeFormation enabled for Glue catalog, should use a client factory that extends %s, but found %s", | ||
| LakeFormationAwsClientFactory.class.getName(), factoryImpl); | ||
| catalogFileIO = null; | ||
| } else { | ||
| awsClientFactory = AwsClientFactories.from(properties); | ||
| catalogFileIO = initializeFileIO(properties); | ||
| } | ||
|
|
||
| initialize( | ||
xiaoxuandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name, | ||
| properties.get(CatalogProperties.WAREHOUSE_LOCATION), | ||
| new AwsProperties(properties), | ||
| AwsClientFactories.from(properties).glue(), | ||
| awsClientFactory.glue(), | ||
| initializeLockManager(properties), | ||
| initializeFileIO(properties)); | ||
| catalogFileIO); | ||
| } | ||
|
|
||
| private LockManager initializeLockManager(Map<String, String> properties) { | ||
|
|
@@ -162,6 +190,19 @@ private String cleanWarehousePath(String path) { | |
|
|
||
| @Override | ||
| protected TableOperations newTableOps(TableIdentifier tableIdentifier) { | ||
| if (catalogProperties != null) { | ||
jackye1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Map<String, String> tableSpecificCatalogProperties = ImmutableMap.<String, String>builder() | ||
| .putAll(catalogProperties) | ||
| .put(AwsProperties.LAKE_FORMATION_DB_NAME, | ||
| IcebergToGlueConverter.getDatabaseName(tableIdentifier)) | ||
| .put(AwsProperties.LAKE_FORMATION_TABLE_NAME, | ||
| IcebergToGlueConverter.getTableName(tableIdentifier)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xiaoxuandev and @jackye1995, I agree with the comment above that this seems confusing. If catalog properties are non-null, then lake formation logic is applied. I think at least the addition of these properties should be conditional on lake formation enabled. |
||
| .build(); | ||
| // FileIO initialization depends on tableSpecificCatalogProperties, so a new FileIO is initialized each time | ||
| return new GlueTableOperations(glue, lockManager, catalogName, awsProperties, | ||
| initializeFileIO(tableSpecificCatalogProperties), tableIdentifier); | ||
jackye1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return new GlueTableOperations(glue, lockManager, catalogName, awsProperties, fileIO, tableIdentifier); | ||
xiaoxuandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.