|
25 | 25 |
|
26 | 26 | import software.amazon.awssdk.awscore.AwsClient; |
27 | 27 | import software.amazon.awssdk.s3accessgrants.plugin.S3AccessGrantsIdentityProvider; |
28 | | -import software.amazon.awssdk.services.s3.S3BaseClientBuilder; |
29 | 28 |
|
30 | 29 | import java.io.IOException; |
31 | 30 | import java.net.URI; |
|
38 | 37 | * Test S3 Access Grants configurations. |
39 | 38 | */ |
40 | 39 | public class TestS3AccessGrantConfiguration extends AbstractHadoopTestBase { |
41 | | - /** |
42 | | - * This credential provider will be attached to any client |
43 | | - * that has been configured with the S3 Access Grants plugin. |
44 | | - * {@link software.amazon.awssdk.s3accessgrants.plugin.S3AccessGrantsPlugin}. |
45 | | - */ |
46 | | - public static final String S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS = |
47 | | - S3AccessGrantsIdentityProvider.class.getName(); |
48 | | - |
49 | | - @Test |
50 | | - public void testS3AccessGrantsEnabled() throws IOException, URISyntaxException { |
51 | | - // Feature is explicitly enabled |
52 | | - AwsClient s3AsyncClient = getAwsClient(createConfig(true), true); |
53 | | - Assert.assertEquals( |
54 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
55 | | - getCredentialProviderName(s3AsyncClient)); |
56 | | - |
57 | | - AwsClient s3Client = getAwsClient(createConfig(true), false); |
58 | | - Assert.assertEquals( |
59 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
60 | | - getCredentialProviderName(s3Client)); |
61 | | - } |
62 | | - |
63 | | - @Test |
64 | | - public void testS3AccessGrantsDisabled() throws IOException, URISyntaxException { |
65 | | - // Disabled by default |
66 | | - AwsClient s3AsyncDefaultClient = getAwsClient(new Configuration(), true); |
67 | | - Assert.assertNotEquals( |
68 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
69 | | - getCredentialProviderName(s3AsyncDefaultClient)); |
70 | | - |
71 | | - AwsClient s3DefaultClient = getAwsClient(new Configuration(), true); |
72 | | - Assert.assertNotEquals( |
73 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
74 | | - getCredentialProviderName(s3DefaultClient)); |
75 | | - |
76 | | - // Disabled if explicitly set |
77 | | - AwsClient s3AsyncExplicitlyDisabledClient = getAwsClient(createConfig(false), true); |
78 | | - Assert.assertNotEquals( |
79 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
80 | | - getCredentialProviderName(s3AsyncExplicitlyDisabledClient)); |
81 | | - |
82 | | - AwsClient s3ExplicitlyDisabledClient = getAwsClient(createConfig(false), true); |
83 | | - Assert.assertNotEquals( |
84 | | - S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
85 | | - getCredentialProviderName(s3ExplicitlyDisabledClient)); |
86 | | - } |
87 | | - |
88 | | - private Configuration createConfig(boolean s3agEnabled) { |
89 | | - Configuration conf = new Configuration(); |
90 | | - conf.setBoolean(AWS_S3_ACCESS_GRANTS_ENABLED, s3agEnabled); |
91 | | - return conf; |
92 | | - } |
93 | | - |
94 | | - private String getCredentialProviderName(AwsClient awsClient) { |
95 | | - return awsClient.serviceClientConfiguration().credentialsProvider().getClass().getName(); |
96 | | - } |
97 | | - |
98 | | - private <BuilderT extends S3BaseClientBuilder<BuilderT, ClientT>, ClientT> AwsClient |
99 | | - getAwsClient(Configuration conf, boolean asyncClient) throws IOException, URISyntaxException { |
100 | | - DefaultS3ClientFactory factory = new DefaultS3ClientFactory(); |
101 | | - factory.setConf(conf); |
102 | | - S3ClientFactory.S3ClientCreationParameters parameters = |
103 | | - new S3ClientFactory.S3ClientCreationParameters(); |
104 | | - URI uri = new URI("any-uri"); |
105 | | - return asyncClient ? |
106 | | - factory.createS3AsyncClient(uri, parameters): factory.createS3Client(uri, parameters); |
107 | | - } |
| 40 | + /** |
| 41 | + * This credential provider will be attached to any client |
| 42 | + * that has been configured with the S3 Access Grants plugin. |
| 43 | + * {@link software.amazon.awssdk.s3accessgrants.plugin.S3AccessGrantsPlugin}. |
| 44 | + */ |
| 45 | + public static final String S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS = |
| 46 | + S3AccessGrantsIdentityProvider.class.getName(); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testS3AccessGrantsEnabled() throws IOException, URISyntaxException { |
| 50 | + // Feature is explicitly enabled |
| 51 | + AwsClient s3AsyncClient = getAwsClient(createConfig(true), true); |
| 52 | + Assert.assertEquals( |
| 53 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 54 | + getCredentialProviderName(s3AsyncClient)); |
| 55 | + |
| 56 | + AwsClient s3Client = getAwsClient(createConfig(true), false); |
| 57 | + Assert.assertEquals( |
| 58 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 59 | + getCredentialProviderName(s3Client)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testS3AccessGrantsDisabled() throws IOException, URISyntaxException { |
| 64 | + // Disabled by default |
| 65 | + AwsClient s3AsyncDefaultClient = getAwsClient(new Configuration(), true); |
| 66 | + Assert.assertNotEquals( |
| 67 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 68 | + getCredentialProviderName(s3AsyncDefaultClient)); |
| 69 | + |
| 70 | + AwsClient s3DefaultClient = getAwsClient(new Configuration(), true); |
| 71 | + Assert.assertNotEquals( |
| 72 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 73 | + getCredentialProviderName(s3DefaultClient)); |
| 74 | + |
| 75 | + // Disabled if explicitly set |
| 76 | + AwsClient s3AsyncExplicitlyDisabledClient = getAwsClient(createConfig(false), true); |
| 77 | + Assert.assertNotEquals( |
| 78 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 79 | + getCredentialProviderName(s3AsyncExplicitlyDisabledClient)); |
| 80 | + |
| 81 | + AwsClient s3ExplicitlyDisabledClient = getAwsClient(createConfig(false), true); |
| 82 | + Assert.assertNotEquals( |
| 83 | + S3_ACCESS_GRANTS_EXPECTED_CREDENTIAL_PROVIDER_CLASS, |
| 84 | + getCredentialProviderName(s3ExplicitlyDisabledClient)); |
| 85 | + } |
| 86 | + |
| 87 | + private Configuration createConfig(boolean s3agEnabled) { |
| 88 | + Configuration conf = new Configuration(); |
| 89 | + conf.setBoolean(AWS_S3_ACCESS_GRANTS_ENABLED, s3agEnabled); |
| 90 | + return conf; |
| 91 | + } |
| 92 | + |
| 93 | + private String getCredentialProviderName(AwsClient awsClient) { |
| 94 | + return awsClient.serviceClientConfiguration().credentialsProvider().getClass().getName(); |
| 95 | + } |
| 96 | + |
| 97 | + private AwsClient getAwsClient(Configuration conf, boolean asyncClient) |
| 98 | + throws IOException, URISyntaxException { |
| 99 | + DefaultS3ClientFactory factory = new DefaultS3ClientFactory(); |
| 100 | + factory.setConf(conf); |
| 101 | + S3ClientFactory.S3ClientCreationParameters parameters = |
| 102 | + new S3ClientFactory.S3ClientCreationParameters(); |
| 103 | + URI uri = new URI("any-uri"); |
| 104 | + return asyncClient ? |
| 105 | + factory.createS3AsyncClient(uri, parameters): factory.createS3Client(uri, parameters); |
| 106 | + } |
108 | 107 | } |
0 commit comments