use of software.amazon.awssdk.utils.SdkAutoCloseable in project aws-sdk-java-v2 by aws.
the class AssumeRoleIntegrationTest method profileCredentialsProviderCanAssumeRoles.
@Test
public void profileCredentialsProviderCanAssumeRoles() throws InterruptedException {
String ASSUME_ROLE_PROFILE = "[source]\n" + "aws_access_key_id = " + userCredentials.accessKeyId() + "\n" + "aws_secret_access_key = " + userCredentials.secretAccessKey() + "\n" + "\n" + "[test]\n" + "region = us-west-1\n" + "source_profile = source\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(profiles, 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();
}
use of software.amazon.awssdk.utils.SdkAutoCloseable in project aws-sdk-java-v2 by aws.
the class AssumeRoleIntegrationTest method profileCredentialProviderCanAssumeRolesWithEnvironmentCredentialSource.
@Test
public void profileCredentialProviderCanAssumeRolesWithEnvironmentCredentialSource() throws InterruptedException {
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" + "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(profiles, 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();
});
}
use of software.amazon.awssdk.utils.SdkAutoCloseable in project aws-sdk-java-v2 by aws.
the class AssumeRoleIntegrationTest method profileCredentialProviderWithEnvironmentCredentialSourceAndSystemProperties.
@Test
public void profileCredentialProviderWithEnvironmentCredentialSourceAndSystemProperties() throws InterruptedException {
System.setProperty("aws.accessKeyId", userCredentials.accessKeyId());
System.setProperty("aws.secretAccessKey", userCredentials.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(profiles, 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");
}
}
use of software.amazon.awssdk.utils.SdkAutoCloseable in project aws-sdk-java-v2 by aws.
the class AssumeRoleProfileTest method createAssumeRoleCredentialsProviderViaProfileSucceeds.
@Test
public void createAssumeRoleCredentialsProviderViaProfileSucceeds() {
String profileContent = "[profile source]\n" + "aws_access_key_id=defaultAccessKey\n" + "aws_secret_access_key=defaultSecretAccessKey\n" + "\n" + "[profile test]\n" + "source_profile=source\n" + "role_arn=arn:aws:iam::123456789012:role/testRole";
ProfileFile profiles = ProfileFile.builder().content(new StringInputStream(profileContent)).type(ProfileFile.Type.CONFIGURATION).build();
assertThat(profiles.profile("test")).hasValueSatisfying(profile -> {
assertThat(new ProfileCredentialsUtils(profiles, profile, profiles::profile).credentialsProvider()).hasValueSatisfying(credentialsProvider -> {
assertThat(credentialsProvider).isInstanceOf(SdkAutoCloseable.class);
((SdkAutoCloseable) credentialsProvider).close();
});
});
}
use of software.amazon.awssdk.utils.SdkAutoCloseable in project aws-sdk-java-v2 by aws.
the class WebIdentityTokenCredentialProviderTest method createAssumeRoleWithWebIdentityTokenCredentialsProviderViaProfileSucceeds.
@Test
public void createAssumeRoleWithWebIdentityTokenCredentialsProviderViaProfileSucceeds() {
String webIdentityTokenPath = Paths.get("/src/test/token.jwt").toAbsolutePath().toString();
String profileContent = "[profile test]\n" + "web_identity_token_file=" + webIdentityTokenPath + "\n" + "role_arn=arn:aws:iam::123456789012:role/testRole";
ProfileFile profiles = ProfileFile.builder().content(new StringInputStream(profileContent)).type(ProfileFile.Type.CONFIGURATION).build();
assertThat(profiles.profile("test")).hasValueSatisfying(profile -> {
assertThat(new ProfileCredentialsUtils(profiles, profile, profiles::profile).credentialsProvider()).hasValueSatisfying(credentialsProvider -> {
assertThat(credentialsProvider).isInstanceOf(SdkAutoCloseable.class);
assertThat(credentialsProvider).hasFieldOrProperty("stsClient");
((SdkAutoCloseable) credentialsProvider).close();
});
});
}
Aggregations