|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.example.s3; |
| 4 | +// |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Tag; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import software.amazon.awssdk.policybuilder.iam.IamConditionKey; |
| 9 | +import software.amazon.awssdk.policybuilder.iam.IamConditionOperator; |
| 10 | +import software.amazon.awssdk.policybuilder.iam.IamEffect; |
| 11 | +import software.amazon.awssdk.policybuilder.iam.IamPolicy; |
| 12 | +import software.amazon.awssdk.policybuilder.iam.IamPrincipalType; |
| 13 | +import software.amazon.awssdk.regions.Region; |
| 14 | +import software.amazon.awssdk.services.iam.IamClient; |
| 15 | +import software.amazon.awssdk.services.iam.model.PutRolePolicyResponse; |
| 16 | +import software.amazon.awssdk.services.s3.S3Client; |
| 17 | +import software.amazon.awssdk.services.sts.StsClient; |
| 18 | +import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +class DoesBucketExistTest { |
| 27 | + private static final String ROLE_NAME = "minimal-s3-perms-role"; |
| 28 | + private static final String POLICY_NAME = "minimum-s3-ability-policy"; |
| 29 | + private static final IamClient iamClient = IamClient.builder().build(); |
| 30 | + |
| 31 | + @Test |
| 32 | + @Tag("IntegrationTest") |
| 33 | + void doesBucketExist_exists_but_not_in_client_region_should_return_true() { |
| 34 | + |
| 35 | + S3Client usWestS3Client = S3Client.builder().region(Region.US_WEST_2).build(); |
| 36 | + final String bucketName = "my-us-west2-bucket-" + UUID.randomUUID(); |
| 37 | + createBucket(usWestS3Client, bucketName); |
| 38 | + |
| 39 | + DoesBucketExist doesBucketExist = new DoesBucketExist(); |
| 40 | + S3Client euCentralS3Client = S3Client.builder().region(Region.EU_CENTRAL_1).build(); |
| 41 | + boolean exists = doesBucketExist.doesBucketExist(bucketName, euCentralS3Client); |
| 42 | + assertTrue(exists); |
| 43 | + |
| 44 | + deleteBucket(usWestS3Client, bucketName); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @Tag("IntegrationTest") |
| 49 | + void doesBucketExist_does_not_exist_should_return_false() { |
| 50 | + |
| 51 | + DoesBucketExist doesBucketExist = new DoesBucketExist(); |
| 52 | + final String bucketName = "xx-xx-xxxx-xxxx" + UUID.randomUUID(); |
| 53 | + |
| 54 | + boolean exists = doesBucketExist.doesBucketExist(bucketName, S3Client.create()); |
| 55 | + assertFalse(exists); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @Tag("IntegrationTest") |
| 60 | + void doesBucketExist_returns_true_when_bucket_exists_but_caller_does_not_have_permission() { |
| 61 | + StsClient stsClient = StsClient.create(); |
| 62 | + createAssumableRole(stsClient); |
| 63 | + |
| 64 | + S3Client s3Client = S3Client.create(); |
| 65 | + final String bucketName = "my-bucket-" + UUID.randomUUID(); |
| 66 | + createBucket(s3Client, bucketName); |
| 67 | + |
| 68 | + try { |
| 69 | + |
| 70 | + String roleArn = iamClient.getRole(b -> b.roleName(ROLE_NAME)).role().arn(); |
| 71 | + |
| 72 | + S3Client s3ClientWithoutPermission = S3Client.builder() |
| 73 | + .credentialsProvider(StsAssumeRoleCredentialsProvider.builder() |
| 74 | + .stsClient(stsClient) |
| 75 | + .refreshRequest(arr -> arr |
| 76 | + .roleArn(roleArn) |
| 77 | + .roleSessionName("test-session")) |
| 78 | + .build()) |
| 79 | + .build(); |
| 80 | + DoesBucketExist doesBucketExist = new DoesBucketExist(); |
| 81 | + boolean existsButNoAccess = doesBucketExist.doesBucketExist(bucketName, s3ClientWithoutPermission); |
| 82 | + assertTrue(existsButNoAccess); |
| 83 | + |
| 84 | + boolean exists = doesBucketExist.doesBucketExist("non-existent-bucket" + UUID.randomUUID(), s3ClientWithoutPermission); |
| 85 | + assertFalse(exists); |
| 86 | + } finally { |
| 87 | + deleteBucket(s3Client, bucketName); |
| 88 | + deleteRole(); |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void createBucket(S3Client s3Client, String bucketName) { |
| 94 | + s3Client.createBucket(b -> b.bucket(bucketName)); |
| 95 | + s3Client.waiter().waitUntilBucketExists(b -> b.bucket(bucketName)); |
| 96 | + } |
| 97 | + |
| 98 | + private static void deleteBucket(S3Client s3Client, String bucketName) { |
| 99 | + s3Client.deleteBucket(b -> b.bucket(bucketName)); |
| 100 | + s3Client.waiter().waitUntilBucketNotExists(b -> b.bucket(bucketName)); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private static void createAssumableRole(StsClient stsClient) { |
| 105 | + final String accountID = stsClient.getCallerIdentity().account(); |
| 106 | + |
| 107 | + IamPolicy trustIamPolicyForAnyoneInSameAccount = IamPolicy.builder() |
| 108 | + .addStatement(statement -> statement |
| 109 | + .effect(IamEffect.ALLOW) |
| 110 | + .addPrincipal(principal -> principal |
| 111 | + .type(IamPrincipalType.AWS) |
| 112 | + .id("arn:aws:iam::" + accountID + ":root") |
| 113 | + ) |
| 114 | + .addAction("sts:AssumeRole") |
| 115 | + .addConditions(IamConditionOperator.STRING_EQUALS, |
| 116 | + IamConditionKey.create("aws:PrincipalType"), |
| 117 | + List.of("User", "AssumedRole") |
| 118 | + ).addConditions(IamConditionOperator.STRING_LIKE, |
| 119 | + "aws:userId", |
| 120 | + List.of("AIDAX*", "AROA*:*", "AIDA*:*")) |
| 121 | + ) |
| 122 | + .build(); |
| 123 | + |
| 124 | + iamClient.createRole(crb -> crb |
| 125 | + .roleName(ROLE_NAME) |
| 126 | + .assumeRolePolicyDocument(trustIamPolicyForAnyoneInSameAccount.toJson()) |
| 127 | + ); |
| 128 | + |
| 129 | + IamPolicy getBucketLocationOnlyPolicy = IamPolicy.builder() |
| 130 | + .addStatement(statement -> statement |
| 131 | + .effect(IamEffect.ALLOW) |
| 132 | + .addAction("s3:GetBucketLocation") |
| 133 | + .addResource("arn:aws:s3:::*") |
| 134 | + ) |
| 135 | + .build(); |
| 136 | + |
| 137 | + iamClient.putRolePolicy(prprb -> prprb |
| 138 | + .roleName(ROLE_NAME) |
| 139 | + .policyName(POLICY_NAME) |
| 140 | + .policyDocument(getBucketLocationOnlyPolicy.toJson())); |
| 141 | + |
| 142 | + // Add a delay for the role to propagate. |
| 143 | + try { |
| 144 | + Thread.sleep(8000); |
| 145 | + } catch (InterruptedException e) { |
| 146 | + throw new RuntimeException(e); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + private static void deleteRole(){ |
| 152 | + |
| 153 | + iamClient.deleteRolePolicy(drbrb -> drbrb |
| 154 | + .roleName(ROLE_NAME) |
| 155 | + .policyName(POLICY_NAME)); |
| 156 | + |
| 157 | + iamClient.deleteRole(drb -> drb |
| 158 | + .roleName(ROLE_NAME) |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments