Search in sources :

Example 6 with ProfileFile

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

the class EndpointDiscoveryTest method canBeEnabledViaProfileOnOverrideConfiguration.

@Test(timeout = 10_000)
public void canBeEnabledViaProfileOnOverrideConfiguration() throws InterruptedException {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "aws_endpoint_discovery_enabled = true";
    ProfileFile profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).content(new StringInputStream(profileFileContent)).build();
    DynamoDbClient dynamoDb = DynamoDbClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).overrideConfiguration(c -> c.defaultProfileFile(profileFile).defaultProfileName("default").addExecutionInterceptor(interceptor).retryPolicy(r -> r.numRetries(0))).build();
    assertThatThrownBy(dynamoDb::listTables).isInstanceOf(SdkException.class);
    ArgumentCaptor<Context.BeforeTransmission> context;
    do {
        Thread.sleep(1);
        context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
        Mockito.verify(interceptor, atLeastOnce()).beforeTransmission(context.capture(), any());
    } while (context.getAllValues().size() < 2);
    assertThat(context.getAllValues().stream().anyMatch(v -> v.httpRequest().firstMatchingHeader("X-Amz-Target").map(h -> h.equals("DynamoDB_20120810.DescribeEndpoints")).orElse(false))).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) SdkException(software.amazon.awssdk.core.exception.SdkException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) Context(software.amazon.awssdk.core.interceptor.Context) Mockito(org.mockito.Mockito) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AnonymousCredentialsProvider(software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider) Region(software.amazon.awssdk.regions.Region) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 7 with ProfileFile

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

the class SsoProfileTest method createSsoCredentialsProvider_SsoRegionMissing_throwException.

@Test
public void createSsoCredentialsProvider_SsoRegionMissing_throwException() {
    String profileContent = "[profile foo]\n" + "sso_account_id=012345678901\n" + "sso_role_name=SampleRole\n" + "sso_start_url=https://d-abc123.awsapps.com/start-beta\n";
    ProfileFile profiles = ProfileFile.builder().content(new StringInputStream(profileContent)).type(ProfileFile.Type.CONFIGURATION).build();
    assertThat(profiles.profile("foo")).hasValueSatisfying(profile -> {
        assertThatThrownBy(() -> new ProfileCredentialsUtils(profiles, profile, profiles::profile).credentialsProvider()).hasMessageContaining("Profile property 'sso_region' was not configured");
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test)

Example 8 with ProfileFile

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

the class SsoProfileTest method createSsoCredentialsProvider_SsoStartUrlMissing_throwException.

@Test
public void createSsoCredentialsProvider_SsoStartUrlMissing_throwException() {
    String profileContent = "[profile foo]\n" + "sso_account_id=012345678901\n" + "sso_region=us-east-1\n" + "sso_role_name=SampleRole\n";
    ProfileFile profiles = ProfileFile.builder().content(new StringInputStream(profileContent)).type(ProfileFile.Type.CONFIGURATION).build();
    assertThat(profiles.profile("foo")).hasValueSatisfying(profile -> {
        assertThatThrownBy(() -> new ProfileCredentialsUtils(profiles, profile, profiles::profile).credentialsProvider()).hasMessageContaining("Profile property 'sso_start_url' was not configured");
    });
}
Also used : StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProfileCredentialsUtils(software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test)

Example 9 with ProfileFile

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

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

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