|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.aws.lakeformation; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.UUID; |
| 24 | +import org.apache.iceberg.aws.AwsIntegTestUtil; |
| 25 | +import org.apache.iceberg.aws.AwsProperties; |
| 26 | +import org.apache.iceberg.aws.glue.GlueCatalog; |
| 27 | +import org.apache.iceberg.catalog.Namespace; |
| 28 | +import org.apache.iceberg.relocated.com.google.common.collect.Maps; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; |
| 36 | +import software.amazon.awssdk.regions.Region; |
| 37 | +import software.amazon.awssdk.services.glue.model.AccessDeniedException; |
| 38 | +import software.amazon.awssdk.services.glue.model.GlueException; |
| 39 | +import software.amazon.awssdk.services.iam.IamClient; |
| 40 | +import software.amazon.awssdk.services.iam.model.CreateRoleRequest; |
| 41 | +import software.amazon.awssdk.services.iam.model.CreateRoleResponse; |
| 42 | +import software.amazon.awssdk.services.iam.model.DeleteRolePolicyRequest; |
| 43 | +import software.amazon.awssdk.services.iam.model.DeleteRoleRequest; |
| 44 | +import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; |
| 45 | + |
| 46 | +public class TestLakeFormationAwsClientFactory { |
| 47 | + |
| 48 | + private static final Logger LOG = LoggerFactory.getLogger(TestLakeFormationAwsClientFactory.class); |
| 49 | + private static final int IAM_PROPAGATION_DELAY = 10000; |
| 50 | + private static final int ASSUME_ROLE_SESSION_DURATION = 3600; |
| 51 | + |
| 52 | + private IamClient iam; |
| 53 | + private String roleName; |
| 54 | + private Map<String, String> assumeRoleProperties; |
| 55 | + private String policyName; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void before() { |
| 59 | + roleName = UUID.randomUUID().toString(); |
| 60 | + iam = IamClient.builder() |
| 61 | + .region(Region.AWS_GLOBAL) |
| 62 | + .httpClientBuilder(UrlConnectionHttpClient.builder()) |
| 63 | + .build(); |
| 64 | + CreateRoleResponse response = iam.createRole(CreateRoleRequest.builder() |
| 65 | + .roleName(roleName) |
| 66 | + .assumeRolePolicyDocument("{" + |
| 67 | + "\"Version\":\"2012-10-17\"," + |
| 68 | + "\"Statement\":[{" + |
| 69 | + "\"Effect\":\"Allow\"," + |
| 70 | + "\"Principal\":{" + |
| 71 | + "\"AWS\":\"arn:aws:iam::" + AwsIntegTestUtil.testAccountId() + ":root\"}," + |
| 72 | + "\"Action\": [\"sts:AssumeRole\"," + |
| 73 | + "\"sts:TagSession\"]}]}") |
| 74 | + .maxSessionDuration(ASSUME_ROLE_SESSION_DURATION) |
| 75 | + .build()); |
| 76 | + assumeRoleProperties = Maps.newHashMap(); |
| 77 | + assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_REGION, "us-east-1"); |
| 78 | + assumeRoleProperties.put(AwsProperties.GLUE_LAKEFORMATION_ENABLED, "true"); |
| 79 | + assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_ARN, response.role().arn()); |
| 80 | + assumeRoleProperties.put(AwsProperties.CLIENT_ASSUME_ROLE_TAGS_PREFIX + |
| 81 | + LakeFormationAwsClientFactory.LF_AUTHORIZED_CALLER, "emr"); |
| 82 | + policyName = UUID.randomUUID().toString(); |
| 83 | + } |
| 84 | + |
| 85 | + @After |
| 86 | + public void after() { |
| 87 | + iam.deleteRolePolicy(DeleteRolePolicyRequest.builder().roleName(roleName).policyName(policyName).build()); |
| 88 | + iam.deleteRole(DeleteRoleRequest.builder().roleName(roleName).build()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testLakeFormationEnabledGlueCatalog() throws Exception { |
| 93 | + String glueArnPrefix = "arn:aws:glue:*:" + AwsIntegTestUtil.testAccountId(); |
| 94 | + iam.putRolePolicy(PutRolePolicyRequest.builder() |
| 95 | + .roleName(roleName) |
| 96 | + .policyName(policyName) |
| 97 | + .policyDocument("{" + |
| 98 | + "\"Version\":\"2012-10-17\"," + |
| 99 | + "\"Statement\":[{" + |
| 100 | + "\"Sid\":\"policy1\"," + |
| 101 | + "\"Effect\":\"Allow\"," + |
| 102 | + "\"Action\":[\"glue:CreateDatabase\",\"glue:DeleteDatabase\"," + |
| 103 | + "\"glue:Get*\",\"lakeformation:GetDataAccess\"]," + |
| 104 | + "\"Resource\":[\"" + glueArnPrefix + ":catalog\"," + |
| 105 | + "\"" + glueArnPrefix + ":database/allowed_*\"," + |
| 106 | + "\"" + glueArnPrefix + ":table/allowed_*/*\"," + |
| 107 | + "\"" + glueArnPrefix + ":userDefinedFunction/allowed_*/*\"]}]}") |
| 108 | + .build()); |
| 109 | + waitForIamConsistency(); |
| 110 | + |
| 111 | + GlueCatalog glueCatalog = new GlueCatalog(); |
| 112 | + assumeRoleProperties.put("warehouse", "s3://path"); |
| 113 | + glueCatalog.initialize("test", assumeRoleProperties); |
| 114 | + Namespace deniedNamespace = Namespace.of("denied_" + UUID.randomUUID().toString().replace("-", "")); |
| 115 | + try { |
| 116 | + glueCatalog.createNamespace(deniedNamespace); |
| 117 | + Assert.fail("Access to Glue should be denied"); |
| 118 | + } catch (GlueException e) { |
| 119 | + Assert.assertEquals(AccessDeniedException.class, e.getClass()); |
| 120 | + } catch (AssertionError e) { |
| 121 | + glueCatalog.dropNamespace(deniedNamespace); |
| 122 | + throw e; |
| 123 | + } |
| 124 | + |
| 125 | + Namespace allowedNamespace = Namespace.of("allowed_" + UUID.randomUUID().toString().replace("-", "")); |
| 126 | + try { |
| 127 | + glueCatalog.createNamespace(allowedNamespace); |
| 128 | + } catch (GlueException e) { |
| 129 | + LOG.error("fail to create Glue database", e); |
| 130 | + Assert.fail("create namespace should succeed"); |
| 131 | + } finally { |
| 132 | + glueCatalog.dropNamespace(allowedNamespace); |
| 133 | + try { |
| 134 | + glueCatalog.close(); |
| 135 | + } catch (Exception e) { |
| 136 | + // swallow exception during closing |
| 137 | + LOG.error("Error closing GlueCatalog", e); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void waitForIamConsistency() throws Exception { |
| 143 | + Thread.sleep(IAM_PROPAGATION_DELAY); // sleep to make sure IAM up to date |
| 144 | + } |
| 145 | +} |
0 commit comments