Search in sources :

Example 1 with Profile

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

the class DualstackEnabledProvider method isDualstackEnabled.

/**
 * Returns true when dualstack should be used, false when dualstack should not be used, and empty when there is no global
 * dualstack configuration.
 */
public Optional<Boolean> isDualstackEnabled() {
    Optional<Boolean> setting = SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.getBooleanValue();
    if (setting.isPresent()) {
        return setting;
    }
    ProfileFile profileFile = this.profileFile.get();
    Optional<Profile> profile = profileFile.profile(profileName());
    return profile.flatMap(p -> p.booleanProperty(ProfileProperty.USE_DUALSTACK_ENDPOINT));
}
Also used : Profile(software.amazon.awssdk.profiles.Profile) ProfileFile(software.amazon.awssdk.profiles.ProfileFile)

Example 2 with Profile

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

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

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

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

the class SsoProfileCredentialsProviderFactoryTest method createSsoCredentialsProviderWithFactorySucceed.

@Test
public void createSsoCredentialsProviderWithFactorySucceed() throws IOException {
    String startUrl = "https//d-abc123.awsapps.com/start";
    String generatedTokenFileName = "6a888bdb653a4ba345dd68f21b896ec2e218c6f4.json";
    Map<String, String> properties = new HashMap<>();
    properties.put("sso_account_id", "accountId");
    properties.put("sso_region", "region");
    properties.put("sso_role_name", "roleName");
    properties.put("sso_start_url", "https//d-abc123.awsapps.com/start");
    Profile profile = Profile.builder().name("foo").properties(properties).build();
    String tokenFile = "{\n" + "\"accessToken\": \"base64string\",\n" + "\"expiresAt\": \"2090-01-01T00:00:00Z\",\n" + "\"region\": \"us-west-2\", \n" + "\"startUrl\": \"" + startUrl + "\"\n" + "}";
    SsoAccessTokenProvider tokenProvider = new SsoAccessTokenProvider(prepareTestCachedTokenFile(tokenFile, generatedTokenFileName));
    SsoProfileCredentialsProviderFactory factory = new SsoProfileCredentialsProviderFactory();
    assertThat(factory.create(profile, tokenProvider)).isInstanceOf(AwsCredentialsProvider.class);
}
Also used : SsoAccessTokenProvider(software.amazon.awssdk.services.sso.internal.SsoAccessTokenProvider) HashMap(java.util.HashMap) Profile(software.amazon.awssdk.profiles.Profile) Test(org.junit.jupiter.api.Test)

Aggregations

Profile (software.amazon.awssdk.profiles.Profile)6 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)5 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)4 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)4 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)4 Test (org.junit.Test)3 ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)3 StsException (software.amazon.awssdk.services.sts.model.StsException)3 StringInputStream (software.amazon.awssdk.utils.StringInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Test (org.junit.jupiter.api.Test)1