Example usage for com.amazonaws.auth AWSCredentialsProviderChain AWSCredentialsProviderChain

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

Introduction

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

Prototype

public AWSCredentialsProviderChain(AWSCredentialsProvider... credentialsProviders) 

Source Link

Document

Constructs a new AWSCredentialsProviderChain with the specified credential providers.

Usage

From source file:alluxio.underfs.s3a.S3AUnderFileSystem.java

License:Apache License

/**
 * Constructs a new instance of {@link S3AUnderFileSystem}.
 *
 * @param uri the {@link AlluxioURI} for this UFS
 *//*w  ww. j  a  va2 s.  com*/
public S3AUnderFileSystem(AlluxioURI uri) {
    super(uri);
    mBucketName = uri.getHost();
    mBucketPrefix = PathUtils.normalizePath(Constants.HEADER_S3A + mBucketName, PATH_SEPARATOR);

    // Set the aws credential system properties based on Alluxio properties, if they are set
    if (Configuration.containsKey(PropertyKey.S3A_ACCESS_KEY)) {
        System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY,
                Configuration.get(PropertyKey.S3A_ACCESS_KEY));
    }
    if (Configuration.containsKey(PropertyKey.S3A_SECRET_KEY)) {
        System.setProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY,
                Configuration.get(PropertyKey.S3A_SECRET_KEY));
    }

    // Checks, in order, env variables, system properties, profile file, and instance profile
    AWSCredentialsProvider credentials = new AWSCredentialsProviderChain(
            new DefaultAWSCredentialsProviderChain());

    // Set the client configuration based on Alluxio configuration values
    ClientConfiguration clientConf = new ClientConfiguration();

    // Socket timeout
    clientConf.setSocketTimeout(Configuration.getInt(PropertyKey.UNDERFS_S3A_SOCKET_TIMEOUT_MS));

    // HTTP protocol
    if (Configuration.getBoolean(PropertyKey.UNDERFS_S3A_SECURE_HTTP_ENABLED)) {
        clientConf.setProtocol(Protocol.HTTPS);
    } else {
        clientConf.setProtocol(Protocol.HTTP);
    }

    // Proxy host
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_PROXY_HOST)) {
        clientConf.setProxyHost(Configuration.get(PropertyKey.UNDERFS_S3_PROXY_HOST));
    }

    // Proxy port
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_PROXY_PORT)) {
        clientConf.setProxyPort(Configuration.getInt(PropertyKey.UNDERFS_S3_PROXY_PORT));
    }

    mClient = new AmazonS3Client(credentials, clientConf);
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_ENDPOINT)) {
        mClient.setEndpoint(Configuration.get(PropertyKey.UNDERFS_S3_ENDPOINT));
    }
    mManager = new TransferManager(mClient);

    TransferManagerConfiguration transferConf = new TransferManagerConfiguration();
    transferConf.setMultipartCopyThreshold(MULTIPART_COPY_THRESHOLD);
    mManager.setConfiguration(transferConf);

    mAccountOwnerId = mClient.getS3AccountOwner().getId();
    // Gets the owner from user-defined static mapping from S3 canonical user id  to Alluxio
    // user name.
    String owner = CommonUtils.getValueFromStaticMapping(
            Configuration.get(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING), mAccountOwnerId);
    // If there is no user-defined mapping, use the display name.
    if (owner == null) {
        owner = mClient.getS3AccountOwner().getDisplayName();
    }
    mAccountOwner = owner == null ? mAccountOwnerId : owner;

    AccessControlList acl = mClient.getBucketAcl(mBucketName);
    mBucketMode = S3AUtils.translateBucketAcl(acl, mAccountOwnerId);
}

From source file:br.com.ingenieux.jenkins.plugins.awsebdeployment.Deployer.java

License:Apache License

private void initAWS() {
    log("Creating S3 and AWSEB Client (AWS Access Key Id: %s, region: %s)", context.getAwsAccessKeyId(),
            context.getAwsRegion());//  w w  w  .  j  ava  2s .  c o  m

    AWSCredentialsProvider credentials = new AWSCredentialsProviderChain(new StaticCredentialsProvider(
            new BasicAWSCredentials(context.getAwsAccessKeyId(), context.getAwsSecretSharedKey())));
    Region region = Region.getRegion(Regions.fromName(context.getAwsRegion()));
    ClientConfiguration clientConfig = new ClientConfiguration();

    clientConfig.setUserAgent("ingenieux CloudButler/" + getVersion());

    s3 = region.createClient(AmazonS3Client.class, credentials, clientConfig);
    awseb = region.createClient(AWSElasticBeanstalkClient.class, credentials, clientConfig);
}

From source file:com.thinkbiganalytics.kylo.catalog.aws.S3FileSystemProvider.java

License:Apache License

/**
 * Creates an S3 client with the standard credential providers.
 *//*from   w  w  w  .  j  av  a 2  s.  c  o  m*/
@VisibleForTesting
protected AmazonS3 createS3Client(@Nonnull final URI uri, @Nonnull final Configuration conf) {
    // Create list of credential providers
    final List<AWSCredentialsProvider> credentials = new ArrayList<>();
    getCustomCredentialsProvider(uri, conf).ifPresent(credentials::add);
    getBasicCredentialsProvider(uri, conf).ifPresent(credentials::add);
    credentials.add(InstanceProfileCredentialsProvider.getInstance());

    // Create client
    final AWSCredentialsProviderChain chain = new AWSCredentialsProviderChain(credentials);
    chain.setReuseLastProvider(true);
    return AmazonS3ClientBuilder.standard().withCredentials(chain).build();
}

From source file:jp.classmethod.aws.gradle.AwsPluginExtension.java

License:Apache License

public AWSCredentialsProvider newCredentialsProvider(String profileName) {
    if (credentialsProvider != null) {
        return credentialsProvider;
    }//from   w  w w .j av a 2s  . co  m
    String profileNameToUse = profileName != null ? profileName : this.profileName;
    if (Strings.isNullOrEmpty(profileNameToUse) == false) {
        List<AWSCredentialsProvider> providers = new ArrayList<AWSCredentialsProvider>();
        providers.add(new EnvironmentVariableCredentialsProvider());
        providers.add(new SystemPropertiesCredentialsProvider());
        providers.add(new ProfileCredentialsProvider(profileNameToUse));
        providers.add(new EC2ContainerCredentialsProviderWrapper());
        return new AWSCredentialsProviderChain(providers);
    }
    return DefaultAWSCredentialsProviderChain.getInstance();
}

From source file:org.apache.hadoop.dynamodb.DynamoDBClient.java

License:Open Source License

protected AWSCredentialsProvider getAWSCredentialsProvider(Configuration conf) {
    List<AWSCredentialsProvider> providersList = new ArrayList<>();

    // try to load custom credential provider, fail if a provider is specified but cannot be
    // initialized
    String providerClass = conf.get(DynamoDBConstants.CUSTOM_CREDENTIALS_PROVIDER_CONF);
    if (!Strings.isNullOrEmpty(providerClass)) {
        try {/*from   w  w w.  j  av a2  s.  c o  m*/
            providersList.add(
                    (AWSCredentialsProvider) ReflectionUtils.newInstance(Class.forName(providerClass), conf));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Custom AWSCredentialsProvider not found: " + providerClass, e);
        }
    }

    // try to fetch credentials from core-site
    String accessKey = conf.get(DYNAMODB_CREDENTIAL_PAIR_NAME.getAccessKeyName());
    String secretKey;
    if (Strings.isNullOrEmpty(accessKey)) {
        accessKey = conf.get(DEFAULT_CREDENTIAL_PAIR_NAME.getAccessKeyName());
        secretKey = conf.get(DEFAULT_CREDENTIAL_PAIR_NAME.getSecretKeyName());
    } else {
        secretKey = conf.get(DYNAMODB_CREDENTIAL_PAIR_NAME.getSecretKeyName());
    }

    if (Strings.isNullOrEmpty(accessKey) || Strings.isNullOrEmpty(secretKey)) {
        providersList.add(new InstanceProfileCredentialsProvider());
    } else {
        final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        providersList.add(new AWSCredentialsProvider() {
            @Override
            public AWSCredentials getCredentials() {
                return credentials;
            }

            @Override
            public void refresh() {
            }
        });
    }

    AWSCredentialsProvider[] providerArray = providersList
            .toArray(new AWSCredentialsProvider[providersList.size()]);

    AWSCredentialsProviderChain providerChain = new AWSCredentialsProviderChain(providerArray);
    providerChain.setReuseLastProvider(true);
    return providerChain;
}

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  a  v  a 2 s.  co 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.jooby.internal.aws.CredentialsFactory.java

License:Apache License

public static AWSCredentialsProvider create(final Config conf, final String serviceName) {
    LinkedList<AWSCredentialsProvider> chain = chain();

    applicationCredentials(conf, serviceName, chain::addFirst);

    return new AWSCredentialsProviderChain(chain);
}

From source file:org.springframework.cloud.aws.core.credentials.CredentialsProviderFactoryBean.java

License:Apache License

@Override
protected AWSCredentialsProvider createInstance() throws Exception {
    AWSCredentialsProviderChain awsCredentialsProviderChain;
    if (this.delegates.isEmpty()) {
        awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain();
    } else {//from  w  ww.j  a  v a 2 s  . c  om
        awsCredentialsProviderChain = new AWSCredentialsProviderChain(
                this.delegates.toArray(new AWSCredentialsProvider[this.delegates.size()]));
    }

    awsCredentialsProviderChain.setReuseLastProvider(false);
    return awsCredentialsProviderChain;
}