Example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain DefaultAWSCredentialsProviderChain

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

Introduction

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

Prototype

public DefaultAWSCredentialsProviderChain() 

Source Link

Usage

From source file:org.attribyte.metrics.cloudwatch.CloudwatchReporter.java

License:Apache License

@Override
public void init(final String name, final Properties _props, final MetricRegistry registry,
        final MetricFilter filter) throws Exception {
    if (isInit.compareAndSet(false, true)) {
        init(name, _props);// w w  w.j a v  a 2 s .c  om

        boolean useInstanceCredentials = init.getProperty(USE_INSTANCE_CREDENTIALS_PROPERTY, "false")
                .equalsIgnoreCase("true");
        boolean useDefaultProviderChain = init.getProperty(USE_DEFAULT_PROVIDER_CHAIN_PROPERTY, "false")
                .equalsIgnoreCase("true");

        if (useDefaultProviderChain) {
            client = new AmazonCloudWatchAsyncClient(new DefaultAWSCredentialsProviderChain());
        } else if (useInstanceCredentials) {
            client = new AmazonCloudWatchAsyncClient(new InstanceProfileCredentialsProvider());
        } else {
            String awsKeyId = init.getProperty(AWS_ACCESS_KEY_ID_PROPERTY, "");
            if (awsKeyId.isEmpty()) {
                init.throwRequiredException(AWS_ACCESS_KEY_ID_PROPERTY);
            }

            String awsKeySecret = init.getProperty(AWS_ACCESS_KEY_SECRET_PROPERTY, "");
            if (awsKeySecret.isEmpty()) {
                init.throwRequiredException(AWS_ACCESS_KEY_SECRET_PROPERTY);
            }

            client = new AmazonCloudWatchAsyncClient(new BasicAWSCredentials(awsKeyId, awsKeySecret));
        }

        String cloudwatchNamespace = init.getProperty(METRIC_NAMESPACE_PROPERTY, null);

        frequencyMillis = InitUtil.millisFromTime(init.getProperty(FREQUENCY_PROPERTY, "1m"));

        boolean disableTranslate = init.getProperty(DISABLE_TRANSLATE_PROPERTY, "false")
                .equalsIgnoreCase("true");

        if (disableTranslate) {
            reporter = new CloudWatchReporter(registry, cloudwatchNamespace, client);
        } else {
            MetricRegistry filteredRegistry = RegistryTranslation.translate(init.getProperties(), registry);
            reporter = new CloudWatchReporter(filteredRegistry, cloudwatchNamespace, client);
        }
    }
}

From source file:org.duracloud.audit.reader.impl.AuditLogReaderImpl.java

License:Apache License

protected StorageProvider getStorageProvider() {
    AWSCredentials creds = new DefaultAWSCredentialsProviderChain().getCredentials();
    AmazonS3 s3client = AmazonS3ClientBuilder.standard().build();
    return new S3StorageProvider(s3client, creds.getAWSAccessKeyId(), null);
}

From source file:org.eclipse.hawkbit.artifact.repository.S3RepositoryAutoConfiguration.java

License:Open Source License

/**
 * The {@link DefaultAWSCredentialsProviderChain} looks for credentials in
 * this order:/*from   w  w  w . jav  a  2  s .c  o  m*/
 * 
 * <pre>
 * 1. Environment Variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY)
 * 2. Java System Properties (aws.accessKeyId and aws.secretKey)
 * 3. The default credential profiles file (~/.aws/credentials)
 * 4. Amazon ECS container credentials
 * 5. Instance profile credentials
 * </pre>
 * 
 * @return the {@link DefaultAWSCredentialsProviderChain} if no other
 *         {@link AWSCredentialsProvider} bean is registered.
 */
@Bean
@ConditionalOnMissingBean
public AWSCredentialsProvider awsCredentialsProvider() {
    return new DefaultAWSCredentialsProviderChain();
}

From source file:org.elasticsearch.cloud.aws.AwsEc2ServiceImpl.java

License:Apache License

protected static AWSCredentialsProvider buildCredentials(Logger logger, Settings settings) {
    AWSCredentialsProvider credentials;//from ww w .j a  v  a  2 s .co  m

    String key = CLOUD_EC2.KEY_SETTING.get(settings);
    String secret = CLOUD_EC2.SECRET_SETTING.get(settings);
    if (key.isEmpty() && secret.isEmpty()) {
        logger.debug("Using either environment variables, system properties or instance profile credentials");
        credentials = new DefaultAWSCredentialsProviderChain();
    } else {
        logger.debug("Using basic key/secret credentials");
        credentials = new StaticCredentialsProvider(new BasicAWSCredentials(key, secret));
    }

    return credentials;
}

From source file:org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl.java

License:Apache License

protected static AWSCredentialsProvider buildCredentials(Logger logger, Settings settings) {
    AWSCredentialsProvider credentials;/* w  ww .jav a  2  s . com*/

    try (SecureString key = ACCESS_KEY_SETTING.get(settings);
            SecureString secret = SECRET_KEY_SETTING.get(settings)) {
        if (key.length() == 0 && secret.length() == 0) {
            logger.debug(
                    "Using either environment variables, system properties or instance profile credentials");
            credentials = new DefaultAWSCredentialsProviderChain();
        } else {
            logger.debug("Using basic key/secret credentials");
            credentials = new StaticCredentialsProvider(
                    new BasicAWSCredentials(key.toString(), secret.toString()));
        }
    }

    return credentials;
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * <p> Gets the {@link AWSCredentialsProvider} based on the credentials in the given parameters. </p> <p> Returns {@link DefaultAWSCredentialsProviderChain}
 * if either access or secret key is {@code null}. Otherwise returns a {@link StaticCredentialsProvider} with the credentials. </p>
 *
 * @param params - Access parameters// w w w .  ja  v  a  2s.  co m
 *
 * @return AWS credentials provider implementation
 */
private AWSCredentialsProvider getAWSCredentialsProvider(S3FileTransferRequestParamsDto params) {
    AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
    String accessKey = params.getS3AccessKey();
    String secretKey = params.getS3SecretKey();
    if (accessKey != null && secretKey != null) {
        awsCredentialsProvider = new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
    }
    return awsCredentialsProvider;
}

From source file:org.finra.herd.dao.credstash.JCredStashWrapper.java

License:Apache License

/**
 * Constructor for the JCredStashWrapper
 *
 * @param region the aws region location of the KMS Client
 * @param tableName name of the credentials table
 * @param clientConfiguration the AWS client configuration
 *//*from  ww w . j  a  v  a  2 s. c o  m*/
public JCredStashWrapper(String region, String tableName, ClientConfiguration clientConfiguration) {
    AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
    AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(provider, clientConfiguration)
            .withRegion(Regions.fromName(region));
    AWSKMSClient kms = new AWSKMSClient(provider, clientConfiguration).withRegion(Regions.fromName(region));
    credstash = new JCredStash(tableName, ddb, kms, new CredStashBouncyCastleCrypto());
}

From source file:org.finra.herd.dao.impl.GlacierDaoImpl.java

License:Apache License

/**
 * Performs a Glacier archive transfer./*from w w  w.  java2  s . c  o  m*/
 *
 * @param params the parameters
 * @param transferer a transferer that knows how to perform the transfer
 *
 * @return the Glacier archive transfer results
 * @throws InterruptedException if a problem is encountered
 * @throws FileNotFoundException if the specified file to upload doesn't exist
 */
private GlacierArchiveTransferResultsDto performTransfer(final GlacierArchiveTransferRequestParamsDto params,
        Transferer transferer) throws InterruptedException, FileNotFoundException {
    // Create an archive transfer manager that will internally use an appropriate number of threads.
    ArchiveTransferManager archiveTransferManager = new ArchiveTransferManager(getAmazonGlacierClient(params),
            new DefaultAWSCredentialsProviderChain());

    // Get the archive file size.
    File localFile = new File(params.getLocalFilePath());
    long fileSizeBytes = localFile.length();

    // Start a stop watch to keep track of how long the transfer takes.
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    // Perform the transfer.
    UploadResult uploadResult = transferer.performTransfer(archiveTransferManager);

    // Stop the stop watch and create a results object.
    stopWatch.stop();

    // Create the results object and populate it with the standard data.
    GlacierArchiveTransferResultsDto results = new GlacierArchiveTransferResultsDto();
    results.setArchiveId(uploadResult.getArchiveId());
    results.setDurationMillis(stopWatch.getTime());
    results.setTotalBytesTransferred(fileSizeBytes);

    // Return the results.
    return results;
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * <p> Gets the {@link AWSCredentialsProvider} based on the credentials in the given parameters. </p> <p> Returns {@link DefaultAWSCredentialsProviderChain}
 * if either access or secret key is {@code null}. Otherwise returns a {@link StaticCredentialsProvider} with the credentials. </p>
 *
 * @param params - Access parameters/*from  w w  w .  j av  a  2s  .  c  o m*/
 *
 * @return AWS credentials provider implementation
 */
private AWSCredentialsProvider getAWSCredentialsProvider(S3FileTransferRequestParamsDto params) {
    List<AWSCredentialsProvider> providers = new ArrayList<>();
    String accessKey = params.getAwsAccessKeyId();
    String secretKey = params.getAwsSecretKey();
    if (accessKey != null && secretKey != null) {
        providers.add(new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
    }
    for (HerdAWSCredentialsProvider herdAWSCredentialsProvider : params
            .getAdditionalAwsCredentialsProviders()) {
        providers.add(new HerdAwsCredentialsProviderWrapper(herdAWSCredentialsProvider));
    }
    providers.add(new DefaultAWSCredentialsProviderChain());
    return new AWSCredentialsProviderChain(providers.toArray(new AWSCredentialsProvider[providers.size()]));
}

From source file:org.finra.herd.dao.JestClientFactory.java

License:Apache License

/**
 * Builds and returns a JEST client./*from   w w  w. j  a v a2  s  .  c o  m*/
 *
 * @return the configured JEST client
 */
public JestClient getJestClient() {
    // Retrieve the configuration values used for setting up an Elasticsearch JEST client.
    final String esRegionName = configurationHelper
            .getProperty(ConfigurationValue.ELASTICSEARCH_AWS_REGION_NAME);
    final String hostname = configurationHelper
            .getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_HOSTNAME);
    final int port = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_PORT,
            Integer.class);
    final String scheme = configurationHelper
            .getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_SCHEME);
    final String serverUri = String.format("%s://%s:%d", scheme, hostname, port);
    final int connectionTimeout = configurationHelper
            .getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_CONNECTION_TIMEOUT, Integer.class);
    final int readTimeout = configurationHelper
            .getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_READ_TIMEOUT, Integer.class);

    LOGGER.info("Elasticsearch REST Client Settings:  scheme={}, hostname={}, port={}, serverUri={}", scheme,
            hostname, port, serverUri);

    DefaultAWSCredentialsProviderChain awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
    final AWSSigner awsSigner = new AWSSigner(awsCredentialsProvider, esRegionName, "es",
            () -> LocalDateTime.now(ZoneOffset.UTC));

    final AWSSigningRequestInterceptor requestInterceptor = new AWSSigningRequestInterceptor(awsSigner);

    JestClientFactoryStaticInner jestClientFactory = new JestClientFactoryStaticInner(requestInterceptor);

    if (StringUtils.equalsIgnoreCase(scheme, "https")) {
        SSLConnectionSocketFactory sslSocketFactory;
        try {
            sslSocketFactory = new SSLConnectionSocketFactory(SSLContext.getDefault(),
                    NoopHostnameVerifier.INSTANCE);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }

        jestClientFactory.setHttpClientConfig(
                new HttpClientConfig.Builder(serverUri).connTimeout(connectionTimeout).readTimeout(readTimeout)
                        .sslSocketFactory(sslSocketFactory).multiThreaded(true).build());
    } else {
        jestClientFactory.setHttpClientConfig(new HttpClientConfig.Builder(serverUri)
                .connTimeout(connectionTimeout).readTimeout(readTimeout).multiThreaded(true).build());
    }

    return jestClientFactory.getObject();
}