Skip to content

Instantly share code, notes, and snippets.

@joelforjava
Created November 26, 2019 14:09
Show Gist options
  • Save joelforjava/21c932111e21b73b40cb3d6dc63555d0 to your computer and use it in GitHub Desktop.
Save joelforjava/21c932111e21b73b40cb3d6dc63555d0 to your computer and use it in GitHub Desktop.
Example of making requests using AssumeRole when using v 2.x of the AWS SDK
package com.joelforjava.aws.assumerole.example;
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.sts.StsAsyncClient;
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
import software.amazon.awssdk.services.sts.model.Credentials;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class MakingAssumeRoleRequestsV2 {
private static final Region REGION = Region.US_EAST_1;
private static AwsCredentialsProvider loadCredentials(boolean isLocal) throws ExecutionException, InterruptedException {
final AwsCredentialsProvider credentialsProvider;
if (isLocal) {
ProfileCredentialsProvider devProfile = ProfileCredentialsProvider.builder()
.profileName("dev")
.build();
StsAsyncClient stsAsyncClient = StsAsyncClient.builder()
.credentialsProvider(devProfile)
.region(REGION)
.build();
AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder()
.durationSeconds(3600)
.roleArn("arn:aws:iam::1234567890987:role/Super-Important-Role")
.roleSessionName("S3_2_Session")
.build();
Future<AssumeRoleResponse> responseFuture = stsAsyncClient.assumeRole(assumeRoleRequest);
AssumeRoleResponse response = responseFuture.get();
Credentials credentials = response.credentials();
AwsSessionCredentials sessionCredentials = AwsSessionCredentials.create(credentials.accessKeyId(), credentials.secretAccessKey(), credentials.sessionToken());
credentialsProvider = AwsCredentialsProviderChain.builder()
.credentialsProviders(StaticCredentialsProvider.create(sessionCredentials))
.build();
} else {
credentialsProvider = DefaultCredentialsProvider.builder().build();
}
return credentialsProvider;
}
public static void main(String[] args) {
try {
AwsCredentialsProvider credentialsProvider = loadCredentials(true);
S3Client s3Client = S3Client.builder().credentialsProvider(credentialsProvider).region(REGION).build();
ListObjectsRequest request = ListObjectsRequest.builder().bucket("bucket-name").build();
ListObjectsResponse response = s3Client.listObjects(request);
System.out.printf("No. of Objects: %d", response.contents().size());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment