use of com.amazonaws.auth.AWSStaticCredentialsProvider in project sic by belluccifranco.
the class AmazonServiceImpl method getSessionCredentials.
private Credentials getSessionCredentials() {
BasicAWSCredentials creds = new BasicAWSCredentials(accessKeyId, accessKeySecret);
AWSSecurityTokenService sts = AWSSecurityTokenServiceClientBuilder.standard().withRegion(this.region).withCredentials(new AWSStaticCredentialsProvider(creds)).build();
GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest().withDurationSeconds(43200);
sessionCredentials = sts.getSessionToken(getSessionTokenRequest).getCredentials();
return sessionCredentials;
}
use of com.amazonaws.auth.AWSStaticCredentialsProvider in project photon-model by vmware.
the class AWSUtils method getArnSessionCredentialsAsync.
/**
* Authenticates and returns a DeferredResult set of session credentials for a valid ARN that
* authorizes this system's account ID (validated through
* {@link #AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY} and
* {@link #AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY}) and the externalId parameter.
*
* If the system properties are unset, then this call will automatically fail.
*
* @param arn The Amazon Resource Name to validate.
* @param externalId The external ID this ARN has authorized.
* @param region The region to validate within.
* @param executorService The executor service to issue the request.
*/
public static DeferredResult<Credentials> getArnSessionCredentialsAsync(String arn, String externalId, String region, ExecutorService executorService) {
AWSCredentialsProvider serviceAwsCredentials;
try {
serviceAwsCredentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_MASTER_ACCOUNT_ACCESS_KEY, AWS_MASTER_ACCOUNT_SECRET_KEY));
} catch (Throwable t) {
return DeferredResult.failed(t);
}
AWSSecurityTokenServiceAsync awsSecurityTokenServiceAsync = AWSSecurityTokenServiceAsyncClientBuilder.standard().withRegion(region).withCredentials(serviceAwsCredentials).withExecutorFactory(() -> executorService).build();
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withRoleArn(arn).withRoleSessionName(UUID.randomUUID().toString()).withDurationSeconds(getArnSessionDurationSeconds()).withExternalId(externalId);
DeferredResult<AssumeRoleResult> r = new DeferredResult<>();
OperationContext operationContext = OperationContext.getOperationContext();
awsSecurityTokenServiceAsync.assumeRoleAsync(assumeRoleRequest, new AsyncHandler<AssumeRoleRequest, AssumeRoleResult>() {
@Override
public void onSuccess(AssumeRoleRequest request, AssumeRoleResult result) {
OperationContext.restoreOperationContext(operationContext);
r.complete(result);
}
@Override
public void onError(Exception ex) {
OperationContext.restoreOperationContext(operationContext);
r.fail(ex);
}
});
return r.thenApply(AssumeRoleResult::getCredentials);
}
use of com.amazonaws.auth.AWSStaticCredentialsProvider in project photon-model by vmware.
the class AWSEndpointAdapterService method getAccountId.
/**
* Method gets the aws accountId from the specified credentials.
* @param privateKeyId
* @param privateKey
* @return account ID
*/
private String getAccountId(String privateKeyId, String privateKey) {
AWSCredentials awsCredentials = new BasicAWSCredentials(privateKeyId, privateKey);
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
AmazonIdentityManagementClientBuilder amazonIdentityManagementClientBuilder = AmazonIdentityManagementClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion(Regions.DEFAULT_REGION);
AmazonIdentityManagementClient iamClient = (AmazonIdentityManagementClient) amazonIdentityManagementClientBuilder.build();
String userId = null;
try {
if ((iamClient.getUser() != null) && (iamClient.getUser().getUser() != null) && (iamClient.getUser().getUser().getArn() != null)) {
return getAccountId(iamClient.getUser().getUser().getArn());
}
} catch (AmazonServiceException ex) {
if (ex.getErrorCode().compareTo("AccessDenied") == 0) {
String msg = ex.getMessage();
userId = msg.split(":", 7)[5];
} else {
logSevere("Exception getting the accountId %s", ex);
}
}
return userId;
}
use of com.amazonaws.auth.AWSStaticCredentialsProvider in project photon-model by vmware.
the class TestUtils method getEC2SynchronousClient.
public static AmazonEC2 getEC2SynchronousClient(AuthCredentialsServiceState credentials, String region) {
ClientConfiguration configuration = AWSUtils.createClientConfiguration();
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(credentials.privateKeyId, EncryptionUtils.decrypt(credentials.privateKey)));
AmazonEC2ClientBuilder ec2ClientBuilder = AmazonEC2ClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion(region).withClientConfiguration(configuration);
return ec2ClientBuilder.build();
}
use of com.amazonaws.auth.AWSStaticCredentialsProvider in project tutorials by eugenp.
the class S3Application method main.
public static void main(String[] args) throws IOException {
// set-up the client
AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_2).build();
AWSS3Service awsService = new AWSS3Service(s3client);
bucketName = "baeldung-bucket";
// creating a bucket
if (awsService.doesBucketExist(bucketName)) {
System.out.println("Bucket name is not available." + " Try again with a different Bucket name.");
return;
}
awsService.createBucket(bucketName);
// list all the buckets
for (Bucket s : awsService.listBuckets()) {
System.out.println(s.getName());
}
// deleting bucket
awsService.deleteBucket("baeldung-bucket-test2");
// uploading object
awsService.putObject(bucketName, "Document/hello.txt", new File("/Users/user/Document/hello.txt"));
// listing objects
ObjectListing objectListing = awsService.listObjects(bucketName);
for (S3ObjectSummary os : objectListing.getObjectSummaries()) {
System.out.println(os.getKey());
}
// downloading an object
S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
S3ObjectInputStream inputStream = s3object.getObjectContent();
FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt"));
// copying an object
awsService.copyObject("baeldung-bucket", "picture/pic.png", "baeldung-bucket2", "Document/picture.png");
// deleting an object
awsService.deleteObject(bucketName, "Document/hello.txt");
// deleting multiple objects
String[] objkeyArr = { "Document/hello2.txt", "Document/picture.png" };
DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("baeldung-bucket").withKeys(objkeyArr);
awsService.deleteObjects(delObjReq);
}
Aggregations