Example usage for com.amazonaws.auth InstanceProfileCredentialsProvider InstanceProfileCredentialsProvider

List of usage examples for com.amazonaws.auth InstanceProfileCredentialsProvider InstanceProfileCredentialsProvider

Introduction

In this page you can find the example usage for com.amazonaws.auth InstanceProfileCredentialsProvider InstanceProfileCredentialsProvider.

Prototype

public InstanceProfileCredentialsProvider(boolean refreshCredentialsAsync) 

Source Link

Document

Spins up a new thread to refresh the credentials asynchronously if refreshCredentialsAsync is set to true, otherwise the credentials will be refreshed from the instance metadata service synchronously,

It is strongly recommended to reuse instances of this credentials provider, especially when async refreshing is used since a background thread is created.

Usage

From source file:com.yahoo.athenz.zms.store.impl.AWSObjectStoreFactory.java

License:Apache License

String getAuthToken(String hostname, int port, String rdsUser, String rdsIamRole) {

    InstanceProfileCredentialsProvider awsCredProvider = new InstanceProfileCredentialsProvider(true);

    if (LOG.isDebugEnabled()) {
        LOG.debug("getAuthToken: Access key id: {}", awsCredProvider.getCredentials().getAWSAccessKeyId());
    }// w  w  w .  j  a v  a  2  s . co m

    RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder().credentials(awsCredProvider)
            .region(EC2MetadataUtils.getEC2InstanceRegion()).build();

    if (LOG.isDebugEnabled()) {
        LOG.debug("Instance {} Port {} User {} Region: {} Role: {}", hostname, port, rdsUser,
                EC2MetadataUtils.getEC2InstanceRegion(), rdsIamRole);
    }

    return generator.getAuthToken(
            GetIamAuthTokenRequest.builder().hostname(hostname).port(port).userName(rdsUser).build());
}

From source file:io.konig.camel.component.aws.s3.client.impl.S3ClientIAMOptimizedImpl.java

License:Apache License

/**
 * Getting the s3 aws client that is used.
 * @return Amazon S3 Client.//from w w w  .  ja  v  a 2s .c  o  m
 */
public AmazonS3 getS3Client() {
    AmazonS3 client = null;
    AmazonS3ClientBuilder clientBuilder = null;
    AmazonS3EncryptionClientBuilder encClientBuilder = null;
    ClientConfiguration clientConfiguration = null;
    if (configuration.hasProxyConfiguration()) {
        clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProxyHost(configuration.getProxyHost());
        clientConfiguration.setProxyPort(configuration.getProxyPort());
        clientConfiguration.setMaxConnections(maxConnections);
    } else {
        clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(maxConnections);
    }

    if (configuration.getAccessKey() != null || configuration.getSecretKey() != null) {
        LOG.trace("Do not pass in unnecessary static credentials when selecting the IAM credential option.");
    }

    if (!configuration.isUseEncryption()) {
        clientBuilder = AmazonS3ClientBuilder.standard()
                .withCredentials(new InstanceProfileCredentialsProvider(false));
    } else if (configuration.isUseEncryption()) {
        StaticEncryptionMaterialsProvider encryptionMaterialsProvider = new StaticEncryptionMaterialsProvider(
                configuration.getEncryptionMaterials());
        encClientBuilder = AmazonS3EncryptionClientBuilder.standard()
                .withClientConfiguration(clientConfiguration)
                .withEncryptionMaterials(encryptionMaterialsProvider)
                .withCredentials(new InstanceProfileCredentialsProvider(false));
    } else {
        clientBuilder = AmazonS3ClientBuilder.standard().withClientConfiguration(clientConfiguration)
                .withCredentials(new InstanceProfileCredentialsProvider(false));
    }

    if (!configuration.isUseEncryption()) {
        if (ObjectHelper.isNotEmpty(configuration.getRegion())) {
            clientBuilder = clientBuilder.withRegion(Regions.valueOf(configuration.getRegion()));
        }
        clientBuilder = clientBuilder.withPathStyleAccessEnabled(configuration.isPathStyleAccess());
        client = clientBuilder.build();
    } else {
        if (ObjectHelper.isNotEmpty(configuration.getRegion())) {
            encClientBuilder = encClientBuilder.withRegion(Regions.valueOf(configuration.getRegion()));
        }
        encClientBuilder = encClientBuilder.withPathStyleAccessEnabled(configuration.isPathStyleAccess());
        client = encClientBuilder.build();
    }

    return client;
}