Search in sources :

Example 11 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile 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 12 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class DualstackEndpointTest method resolvesCorrectEndpoint.

@Test
public void resolvesCorrectEndpoint() {
    String systemPropertyBeforeTest = System.getProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property());
    EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
    try {
        ProtocolRestJsonClientBuilder builder = ProtocolRestJsonClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create());
        if (testCase.clientSetting != null) {
            builder.dualstackEnabled(testCase.clientSetting);
        }
        if (testCase.systemPropSetting != null) {
            System.setProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property(), testCase.systemPropSetting);
        }
        if (testCase.envVarSetting != null) {
            helper.set(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.environmentVariable(), testCase.envVarSetting);
        }
        ProfileFile.Builder profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION);
        if (testCase.profileSetting != null) {
            profileFile.content(new StringInputStream("[default]\n" + ProfileProperty.USE_DUALSTACK_ENDPOINT + " = " + testCase.profileSetting));
        } else {
            profileFile.content(new StringInputStream(""));
        }
        EndpointCapturingInterceptor interceptor = new EndpointCapturingInterceptor();
        builder.overrideConfiguration(c -> c.defaultProfileFile(profileFile.build()).defaultProfileName("default").addExecutionInterceptor(interceptor));
        if (testCase instanceof SuccessCase) {
            ProtocolRestJsonClient client = builder.build();
            try {
                client.allTypes();
            } catch (EndpointCapturingInterceptor.CaptureCompletedException e) {
            // Expected
            }
            boolean expectedDualstackEnabled = ((SuccessCase) testCase).expectedValue;
            String expectedEndpoint = expectedDualstackEnabled ? "https://customresponsemetadata.us-west-2.api.aws/2016-03-11/allTypes" : "https://customresponsemetadata.us-west-2.amazonaws.com/2016-03-11/allTypes";
            assertThat(interceptor.endpoints()).singleElement().isEqualTo(expectedEndpoint);
        } else {
            FailureCase failure = Validate.isInstanceOf(FailureCase.class, testCase, "Unexpected test case type.");
            assertThatThrownBy(builder::build).hasMessageContaining(failure.exceptionMessage);
        }
    } finally {
        if (systemPropertyBeforeTest != null) {
            System.setProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property(), systemPropertyBeforeTest);
        } else {
            System.clearProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property());
        }
        helper.reset();
    }
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) EnvironmentVariableHelper(software.amazon.awssdk.testutils.EnvironmentVariableHelper) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) ProtocolRestJsonClientBuilder(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 13 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile 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 14 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class ProfileCredentialsUtilsTest method profileFileWithStaticCredentialsLoadsCorrectly.

@Test
public void profileFileWithStaticCredentialsLoadsCorrectly() {
    ProfileFile profileFile = allTypesProfile();
    assertThat(profileFile.profile("default")).hasValueSatisfying(profile -> {
        assertThat(profile.name()).isEqualTo("default");
        assertThat(profile.property(ProfileProperty.AWS_ACCESS_KEY_ID)).hasValue("defaultAccessKey");
        assertThat(profile.toString()).contains("default");
        assertThat(profile.property(ProfileProperty.REGION)).isNotPresent();
        assertThat(new ProfileCredentialsUtils(profileFile, profile, profileFile::profile).credentialsProvider()).hasValueSatisfying(credentialsProvider -> {
            assertThat(credentialsProvider.resolveCredentials()).satisfies(credentials -> {
                assertThat(credentials.accessKeyId()).isEqualTo("defaultAccessKey");
                assertThat(credentials.secretAccessKey()).isEqualTo("defaultSecretAccessKey");
            });
        });
    });
}
Also used : ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test) ProcessCredentialsProviderTest(software.amazon.awssdk.auth.credentials.ProcessCredentialsProviderTest)

Example 15 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class ProfileCredentialsUtilsTest method profileWithInvalidCredentialSourceThrowsException.

@Test
public void profileWithInvalidCredentialSourceThrowsException() {
    ProfileFile configFile = configFile("[profile test]\n" + "credential_source=foobar\n" + "role_arn=arn:aws:iam::123456789012:role/testRole3");
    assertThatThrownBy(() -> new ProfileCredentialsUtils(configFile, configFile.profile("test").get(), configFile::profile).credentialsProvider()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("foobar is not a valid credential_source");
}
Also used : ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test) ProcessCredentialsProviderTest(software.amazon.awssdk.auth.credentials.ProcessCredentialsProviderTest)

Aggregations

ProfileFile (software.amazon.awssdk.profiles.ProfileFile)47 Test (org.junit.jupiter.api.Test)22 StringInputStream (software.amazon.awssdk.utils.StringInputStream)20 Test (org.junit.Test)14 ProcessCredentialsProviderTest (software.amazon.awssdk.auth.credentials.ProcessCredentialsProviderTest)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)9 Region (software.amazon.awssdk.regions.Region)9 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)8 URI (java.net.URI)7 Optional (java.util.Optional)6 Arrays (java.util.Arrays)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)5 ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)5 Profile (software.amazon.awssdk.profiles.Profile)5 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)4