Search in sources :

Example 1 with SdkAutoCloseable

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();
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) StsException(software.amazon.awssdk.services.sts.model.StsException) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) Profile(software.amazon.awssdk.profiles.Profile) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 2 with SdkAutoCloseable

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();
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) StsException(software.amazon.awssdk.services.sts.model.StsException) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) Profile(software.amazon.awssdk.profiles.Profile) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 3 with SdkAutoCloseable

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");
    }
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) StsException(software.amazon.awssdk.services.sts.model.StsException) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) Profile(software.amazon.awssdk.profiles.Profile) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 4 with SdkAutoCloseable

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();
        });
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test) AssumeRoleIntegrationTest(software.amazon.awssdk.services.sts.AssumeRoleIntegrationTest)

Example 5 with SdkAutoCloseable

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();
        });
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test)

Aggregations

ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)5 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)5 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)5 StringInputStream (software.amazon.awssdk.utils.StringInputStream)5 Test (org.junit.Test)4 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)3 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)3 Profile (software.amazon.awssdk.profiles.Profile)3 StsException (software.amazon.awssdk.services.sts.model.StsException)3 Test (org.junit.jupiter.api.Test)1 AssumeRoleIntegrationTest (software.amazon.awssdk.services.sts.AssumeRoleIntegrationTest)1