Skip to content

Update AssumeRoleIntegrationTest to work even if the current environment includes a AWS_SESSION_TOKEN #2054

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

Merged
merged 1 commit into from
Sep 22, 2020
Merged
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 @@ -186,6 +186,7 @@ public void profileCredentialProviderCanAssumeRolesWithEnvironmentCredentialSour
EnvironmentVariableHelper.run(helper -> {
helper.set("AWS_ACCESS_KEY_ID", userCredentials.accessKeyId());
helper.set("AWS_SECRET_ACCESS_KEY", userCredentials.secretAccessKey());
helper.remove("AWS_SESSION_TOKEN");

String ASSUME_ROLE_PROFILE =
"[test]\n"
Expand Down Expand Up @@ -218,36 +219,39 @@ public void profileCredentialProviderWithEnvironmentCredentialSourceAndSystemPro
System.setProperty("aws.accessKeyId", userCredentials.accessKeyId());
System.setProperty("aws.secretAccessKey", userCredentials.secretAccessKey());

EnvironmentVariableHelper.run(helper -> {
helper.remove("AWS_ACCESS_KEY_ID");
helper.remove("AWS_SECRET_ACCESS_KEY");

String ASSUME_ROLE_PROFILE =
"[test]\n"
+ "region = us-west-1\n"
+ "credential_source = Environment\n"
+ "role_arn = " + ROLE_ARN;

ProfileFile profiles = ProfileFile.builder()
.content(new StringInputStream(ASSUME_ROLE_PROFILE))
.type(ProfileFile.Type.CREDENTIALS)
.build();
Optional<Profile> profile = profiles.profile("test");
AwsCredentialsProvider awsCredentialsProvider =
new ProfileCredentialsUtils(profile.get(), profiles::profile).credentialsProvider().get();


// Try to assume the role until the eventual consistency catches up.
AwsCredentials awsCredentials = Waiter.run(awsCredentialsProvider::resolveCredentials)
.ignoringException(StsException.class)
.orFail();

assertThat(awsCredentials.accessKeyId()).isNotBlank();
assertThat(awsCredentials.secretAccessKey()).isNotBlank();
((SdkAutoCloseable) awsCredentialsProvider).close();
});

System.clearProperty("aws.accessKeyId");
System.clearProperty("aws.secretAccessKey");
try {
EnvironmentVariableHelper.run(helper -> {
helper.remove("AWS_ACCESS_KEY_ID");
helper.remove("AWS_SECRET_ACCESS_KEY");
helper.remove("AWS_SESSION_TOKEN");

String ASSUME_ROLE_PROFILE =
"[test]\n"
+ "region = us-west-1\n"
+ "credential_source = Environment\n"
+ "role_arn = " + ROLE_ARN;

ProfileFile profiles = ProfileFile.builder()
.content(new StringInputStream(ASSUME_ROLE_PROFILE))
.type(ProfileFile.Type.CREDENTIALS)
.build();
Optional<Profile> profile = profiles.profile("test");
AwsCredentialsProvider awsCredentialsProvider =
new ProfileCredentialsUtils(profile.get(), profiles::profile).credentialsProvider().get();


// Try to assume the role until the eventual consistency catches up.
AwsCredentials awsCredentials = Waiter.run(awsCredentialsProvider::resolveCredentials)
.ignoringException(StsException.class)
.orFail();

assertThat(awsCredentials.accessKeyId()).isNotBlank();
assertThat(awsCredentials.secretAccessKey()).isNotBlank();
((SdkAutoCloseable) awsCredentialsProvider).close();
});
} finally {
System.clearProperty("aws.accessKeyId");
System.clearProperty("aws.secretAccessKey");
}
}
}