Skip to content

Commit 38c8533

Browse files
authored
Refactor CrossAccountCredentialsProviderV2 to use StsAssumeRoleCreden… (#3217)
1 parent 49ac3a2 commit 38c8533

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

athena-dynamodb/src/main/java/com/amazonaws/athena/connectors/dynamodb/credentials/CrossAccountCredentialsProviderV2.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,16 +19,16 @@
1919
*/
2020
package com.amazonaws.athena.connectors.dynamodb.credentials;
2121

22+
import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
25-
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2626
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
27-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
27+
import software.amazon.awssdk.services.glue.model.ErrorDetails;
28+
import software.amazon.awssdk.services.glue.model.FederationSourceErrorCode;
2829
import software.amazon.awssdk.services.sts.StsClient;
30+
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
2931
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
30-
import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
31-
import software.amazon.awssdk.services.sts.model.Credentials;
3232

3333
import java.util.Map;
3434

@@ -48,10 +48,17 @@ public static AwsCredentialsProvider getCrossAccountCredentialsIfPresent(Map<Str
4848
.roleArn(configOptions.get(CROSS_ACCOUNT_ROLE_ARN_CONFIG))
4949
.roleSessionName(roleSessionName)
5050
.build();
51-
AssumeRoleResponse assumeRoleResponse = stsClient.assumeRole(assumeRoleRequest);
52-
Credentials credentials = assumeRoleResponse.credentials();
53-
AwsSessionCredentials sessionCredentials = AwsSessionCredentials.create(credentials.accessKeyId(), credentials.secretAccessKey(), credentials.sessionToken());
54-
return StaticCredentialsProvider.create(sessionCredentials);
51+
StsAssumeRoleCredentialsProvider provider = StsAssumeRoleCredentialsProvider.builder()
52+
.stsClient(stsClient)
53+
.refreshRequest(assumeRoleRequest)
54+
.build();
55+
try {
56+
provider.resolveCredentials();
57+
}
58+
catch (Exception e) {
59+
throw new AthenaConnectorException("Failed to assume role: " + assumeRoleRequest.roleArn() + ". " + e.getMessage(), ErrorDetails.builder().errorCode(FederationSourceErrorCode.INVALID_CREDENTIALS_EXCEPTION.toString()).build());
60+
}
61+
return provider;
5562
}
5663
return DefaultCredentialsProvider.create();
5764
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*-
2+
* #%L
3+
* athena-dynamodb
4+
* %%
5+
* Copyright (C) 2019 - 2024 Amazon Web Services
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.amazonaws.athena.connectors.dynamodb.credentials;
21+
22+
import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException;
23+
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
27+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
28+
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
import static org.junit.Assert.assertTrue;
33+
34+
public class CrossAccountCredentialsProviderV2Test
35+
{
36+
@BeforeClass
37+
public static void setUp()
38+
{
39+
if (System.getProperty("aws.region") == null && System.getenv("AWS_REGION") == null) {
40+
System.setProperty("aws.region", "us-east-1");
41+
}
42+
}
43+
44+
@Test
45+
public void testReturnsDefaultProviderWhenNoRoleArn()
46+
{
47+
Map<String, String> configOptions = new HashMap<>();
48+
49+
AwsCredentialsProvider provider = CrossAccountCredentialsProviderV2
50+
.getCrossAccountCredentialsIfPresent(configOptions, "test-session");
51+
52+
assertTrue(provider instanceof DefaultCredentialsProvider);
53+
}
54+
55+
@Test(expected = AthenaConnectorException.class)
56+
public void testThrowsAthenaConnectorExceptionForInvalidRoleArn()
57+
{
58+
Map<String, String> configOptions = new HashMap<>();
59+
configOptions.put("cross_account_role_arn", "arn:aws:iam::000000000000:role/NonExistentRole");
60+
61+
CrossAccountCredentialsProviderV2.getCrossAccountCredentialsIfPresent(configOptions, "test-session");
62+
}
63+
}

0 commit comments

Comments
 (0)