Skip to content
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

Enable support for STS regional endpoints when configured to assume a role #37

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider;
import com.amazonaws.auth.SystemPropertiesCredentialsProvider;
import com.amazonaws.auth.WebIdentityTokenCredentialsProvider;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -49,6 +51,7 @@ public class MSKCredentialProvider implements AWSCredentialsProvider, AutoClosea
private static final String AWS_PROFILE_NAME_KEY = "awsProfileName";
private static final String AWS_ROLE_ARN_KEY = "awsRoleArn";
private static final String AWS_ROLE_SESSION_KEY = "awsRoleSessionName";
private static final String AWS_STS_REGION = "awsStsRegion";

private final List<AutoCloseable> closeableProviders;
private final AWSCredentialsProvider compositeDelegate;
Expand Down Expand Up @@ -136,13 +139,20 @@ private Optional<STSAssumeRoleSessionCredentialsProvider> getStsRoleProvider() {
}
String sessionName = Optional.ofNullable((String) optionsMap.get(AWS_ROLE_SESSION_KEY))
.orElse("aws-msk-iam-auth");
return createSTSRoleCredentialProvider((String) p, sessionName);
String stsRegion = Optional.ofNullable((String)optionsMap.get(AWS_STS_REGION))
.orElse("aws-global");
return createSTSRoleCredentialProvider((String) p, sessionName, stsRegion);
});
}

STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String roleArn,
String sessionName) {
return new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, sessionName).build();
String sessionName, String stsRegion) {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(stsRegion)
sayantacC marked this conversation as resolved.
Show resolved Hide resolved
.build();
return new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, sessionName)
.withStsClient(stsClient)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void testAwsRoleArn() {

MSKCredentialProvider.ProviderBuilder providerBuilder = new MSKCredentialProvider.ProviderBuilder(optionsMap) {
STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String roleArn,
String sessionName) {
String sessionName, String stsRegion) {
assertEquals(TEST_ROLE_ARN, roleArn);
assertEquals("aws-msk-iam-auth", sessionName);
return mockStsRoleProvider;
Expand Down Expand Up @@ -183,7 +183,7 @@ public void testAwsRoleArnAndSessionName() {

MSKCredentialProvider.ProviderBuilder providerBuilder = new MSKCredentialProvider.ProviderBuilder(optionsMap) {
STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String roleArn,
String sessionName) {
String sessionName, String stsRegion) {
assertEquals(TEST_ROLE_ARN, roleArn);
assertEquals(TEST_ROLE_SESSION_NAME, sessionName);
return mockStsRoleProvider;
Expand All @@ -202,6 +202,40 @@ STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String r
Mockito.verify(mockStsRoleProvider, times(1)).close();
}

@Test
public void testAwsRoleArnSessionNameAndStsRegion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test.

STSAssumeRoleSessionCredentialsProvider mockStsRoleProvider = Mockito
.mock(STSAssumeRoleSessionCredentialsProvider.class);
Mockito.when(mockStsRoleProvider.getCredentials())
.thenReturn(new BasicSessionCredentials(ACCESS_KEY_VALUE, SECRET_KEY_VALUE, SESSION_TOKEN));

Map<String, String> optionsMap = new HashMap<>();
optionsMap.put(AWS_ROLE_ARN, TEST_ROLE_ARN);
optionsMap.put("awsRoleSessionName", TEST_ROLE_SESSION_NAME);
optionsMap.put("awsStsRegion", "eu-west-1");

MSKCredentialProvider.ProviderBuilder providerBuilder = new MSKCredentialProvider.ProviderBuilder(optionsMap) {
STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String roleArn,
String sessionName, String stsRegion) {
assertEquals(TEST_ROLE_ARN, roleArn);
assertEquals(TEST_ROLE_SESSION_NAME, sessionName);
assertEquals("eu-west-1", stsRegion);
return mockStsRoleProvider;
}
};
MSKCredentialProvider provider = new MSKCredentialProvider(providerBuilder);

AWSCredentials credentials = provider.getCredentials();
assertTrue(credentials instanceof BasicSessionCredentials);
BasicSessionCredentials sessionCredentials = (BasicSessionCredentials) credentials;
assertEquals(ACCESS_KEY_VALUE, sessionCredentials.getAWSAccessKeyId());
assertEquals(SECRET_KEY_VALUE, sessionCredentials.getAWSSecretKey());
assertEquals(SESSION_TOKEN, sessionCredentials.getSessionToken());

provider.close();
Mockito.verify(mockStsRoleProvider, times(1)).close();
}

@Test
public void testProfileNameAndRoleArn() {
ProfileFile profileFile = getProfileFile();
Expand All @@ -219,7 +253,7 @@ EnhancedProfileCredentialsProvider createEnhancedProfileCredentialsProvider(Stri
return new EnhancedProfileCredentialsProvider(profileFile, TEST_PROFILE_NAME);
}
STSAssumeRoleSessionCredentialsProvider createSTSRoleCredentialProvider(String roleArn,
String sessionName) {
String sessionName, String stsRegion) {
assertEquals(TEST_ROLE_ARN, roleArn);
assertEquals("aws-msk-iam-auth", sessionName);
return mockStsRoleProvider;
Expand Down