Search in sources :

Example 11 with StringInputStream

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

use of software.amazon.awssdk.utils.StringInputStream 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 13 with StringInputStream

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

use of software.amazon.awssdk.utils.StringInputStream 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 15 with StringInputStream

use of software.amazon.awssdk.utils.StringInputStream 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)

Aggregations

StringInputStream (software.amazon.awssdk.utils.StringInputStream)47 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)9 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)7 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)7 Region (software.amazon.awssdk.regions.Region)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 Optional (java.util.Optional)6 ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)6 URI (java.net.URI)5 Duration (java.time.Duration)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)5 SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)4 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)4 ArrayList (java.util.ArrayList)3