use of software.amazon.awssdk.services.sts.STSClient in project uploader by smoketurner.
the class AwsConfiguration method getCredentials.
@JsonIgnore
public AwsCredentialsProvider getCredentials() {
final AwsCredentialsProvider credentials;
if (!Strings.isNullOrEmpty(accessKey) && !Strings.isNullOrEmpty(secretKey)) {
credentials = StaticCredentialsProvider.create(AwsCredentials.create(accessKey, secretKey));
} else {
credentials = DefaultCredentialsProvider.create();
}
if (Strings.isNullOrEmpty(stsRoleArn)) {
return credentials;
}
final STSClient stsClient = STSClient.builder().credentialsProvider(credentials).region(region).build();
final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleArn(stsRoleArn).build();
return StsAssumeRoleCredentialsProvider.builder().stsClient(stsClient).refreshRequest(assumeRoleRequest).build();
}
use of software.amazon.awssdk.services.sts.STSClient in project aws-doc-sdk-examples by awsdocs.
the class IAMScenario method assumeGivenRole.
// Invoke an Amazon S3 operation using the Assumed Role.
public static void assumeGivenRole(String roleArn, String roleSessionName, String bucketName) {
StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();
try {
AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
Credentials myCreds = roleResponse.credentials();
String key = myCreds.accessKeyId();
String secKey = myCreds.secretAccessKey();
String secToken = myCreds.sessionToken();
// List all objects in an Amazon S3 bucket using the temp creds.
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(key, secKey, secToken))).region(region).build();
System.out.println("Created a S3Client using temp credentials.");
System.out.println("Listing objects in " + bucketName);
ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
ListObjectsResponse res = s3.listObjects(listObjects);
List<S3Object> objects = res.contents();
for (S3Object myValue : objects) {
System.out.println("The name of the key is " + myValue.key());
System.out.println("The owner is " + myValue.owner());
}
} catch (StsException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.sts.STSClient in project aws-doc-sdk-examples by awsdocs.
the class AssumeRole method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <roleArn> <roleSessionName> \n\n" + "Where:\n" + " roleArn - the Amazon Resource Name (ARN) of the role to assume (for example, rn:aws:iam::000008047983:role/s3role). \n" + " roleSessionName - an identifier for the assumed role session (for example, mysession). \n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String roleArn = args[0];
String roleSessionName = args[1];
Region region = Region.US_EAST_1;
StsClient stsClient = StsClient.builder().region(region).build();
assumeGivenRole(stsClient, roleArn, roleSessionName);
stsClient.close();
}
use of software.amazon.awssdk.services.sts.STSClient in project aws-doc-sdk-examples by awsdocs.
the class GetSessionToken method main.
public static void main(String[] args) {
Region region = Region.US_EAST_1;
StsClient stsClient = StsClient.builder().region(region).build();
getToken(stsClient);
stsClient.close();
}
use of software.amazon.awssdk.services.sts.STSClient in project pravega by pravega.
the class S3SimpleStorageFactory method getRoleCredentialsProvider.
private static AwsCredentialsProvider getRoleCredentialsProvider(String roleArn, String roleSessionName) {
AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
StsClient stsClient = StsClient.builder().build();
return StsAssumeRoleCredentialsProvider.builder().stsClient(stsClient).refreshRequest(assumeRoleRequest).asyncCredentialUpdateEnabled(true).build();
}
Aggregations