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();
}
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();
});
}
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");
}
}
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();
}
}
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();
});
});
}
Aggregations