Example usage for com.amazonaws.regions DefaultAwsRegionProviderChain DefaultAwsRegionProviderChain

List of usage examples for com.amazonaws.regions DefaultAwsRegionProviderChain DefaultAwsRegionProviderChain

Introduction

In this page you can find the example usage for com.amazonaws.regions DefaultAwsRegionProviderChain DefaultAwsRegionProviderChain.

Prototype

public DefaultAwsRegionProviderChain() 

Source Link

Usage

From source file:com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl.java

License:Open Source License

public AWSCredentials getCredentials() {
    AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText());

    if (StringUtils.isBlank(iamRoleArn)) {
        return initialCredentials;
    } else {//from   www. ja va 2 s . c om
        // Check for available region from the SDK, otherwise specify default
        String clientRegion = null;
        DefaultAwsRegionProviderChain sdkRegionLookup = new DefaultAwsRegionProviderChain();
        try {
            clientRegion = sdkRegionLookup.getRegion();
        } catch (com.amazonaws.SdkClientException e) {
            LOGGER.log(Level.WARNING, "Could not find default region using SDK lookup.", e);
        }
        if (clientRegion == null) {
            clientRegion = Regions.DEFAULT_REGION.getName();
        }

        AWSSecurityTokenService client;
        // Handle the case of delegation to instance profile
        if (StringUtils.isBlank(accessKey) && StringUtils.isBlank(secretKey.getPlainText())) {
            client = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
        } else {
            client = AWSSecurityTokenServiceClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(initialCredentials))
                    .withRegion(clientRegion).build();
        }

        AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn)
                .withDurationSeconds(this.getStsTokenDuration());

        AssumeRoleResult assumeResult = client.assumeRole(assumeRequest);

        return new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(),
                assumeResult.getCredentials().getSecretAccessKey(),
                assumeResult.getCredentials().getSessionToken());
    }
}

From source file:com.netflix.genie.common.internal.configs.AwsAutoConfiguration.java

License:Apache License

/**
 * Get an AWS region provider instance. The rules for this basically follow what Spring Cloud AWS does but uses
 * the interface from the AWS SDK instead and provides a sensible default.
 * <p>// w  w  w . j  av  a 2s  .  co  m
 * See: <a href="https://tinyurl.com/y9edl6yr">Spring Cloud AWS Region Documentation</a>
 *
 * @param awsRegionProperties The cloud.aws.region.* properties
 * @return A region provider based on whether static was set by user, else auto, else default of us-east-1
 */
@Bean
@ConditionalOnMissingBean(AwsRegionProvider.class)
public AwsRegionProvider awsRegionProvider(final AwsRegionProperties awsRegionProperties) {
    final String staticRegion = awsRegionProperties.getStatic();
    if (StringUtils.isNotBlank(staticRegion)) {
        // Make sure we have a valid region. Will throw runtime exception if not.
        final Regions region = Regions.fromName(staticRegion);
        return new AwsRegionProvider() {
            /**
             * Always return the static configured region.
             *
             * {@inheritDoc}
             */
            @Override
            public String getRegion() throws SdkClientException {
                return region.getName();
            }
        };
    } else if (awsRegionProperties.isAuto()) {
        return new DefaultAwsRegionProviderChain();
    } else {
        // Sensible default
        return new AwsRegionProvider() {
            /**
             * Always default to us-east-1.
             *
             * {@inheritDoc}
             */
            @Override
            public String getRegion() throws SdkClientException {
                return Regions.US_EAST_1.getName();
            }
        };
    }
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.sdkclient.SpinnakerAwsRegionProvider.java

License:Apache License

public SpinnakerAwsRegionProvider() {
    super(new Ec2RegionEnvVarRegionProvider(), new DefaultAwsRegionProviderChain(),
            new RegionsCurrentRegionProvider(), new DefaultRegionProvider());
}

From source file:zipkin.autoconfigure.storage.elasticsearch.aws.ZipkinElasticsearchAwsStorageAutoConfiguration.java

License:Apache License

@Bean
String region(ZipkinElasticsearchHttpStorageProperties es, ZipkinElasticsearchAwsStorageProperties aws) {
    List<String> hosts = es.getHosts();
    String domain = aws.getDomain();
    if (hosts != null && domain != null) {
        log.warning(format("Expected exactly one of hosts or domain: instead saw hosts '%s' and domain '%s'."
                + " Ignoring hosts and proceeding to look for domain. Either unset ES_HOSTS or "
                + "ES_AWS_DOMAIN to suppress this message.", hosts, domain));
    }/*from ww  w.j ava2 s.  c o m*/

    if (aws.getRegion() != null) {
        return aws.getRegion();
    } else if (domain != null) {
        return new DefaultAwsRegionProviderChain().getRegion();
    } else {
        String awsRegion = regionFromAwsUrls(hosts);
        checkArgument(awsRegion != null, "Couldn't awsRegion in '%s'", hosts);
        return awsRegion;
    }
}

From source file:zipkin.autoconfigure.storage.elasticsearch.aws.ZipkinElasticsearchAwsStorageProperties.java

License:Apache License

ElasticsearchStorage.Builder toBuilder() {
    if (!hostsIsDefaultOrEmpty() && aws.domain != null) {
        logger.warn("Expected exactly one of hosts or domain: instead saw hosts '{}' and domain '{}'."
                + " Ignoring hosts and proceeding to look for domain. Either unset ES_HOSTS or "
                + "ES_AWS_DOMAIN to suppress this message.", hosts, aws.domain);
    }/*from  w ww .  j  a  v  a2  s.  c om*/

    String region;
    if (aws.domain != null) {
        region = aws.region;
        if (region == null)
            region = new DefaultAwsRegionProviderChain().getRegion();
    } else {
        region = ZipkinElasticsearchHttpStorageAutoConfiguration.regionFromAwsUrls(hosts).get();
    }
    ElasticsearchAwsRequestSigner signer = new ElasticsearchAwsRequestSigner(region,
            new DefaultAWSCredentialsProviderChain());
    HttpClient.Builder httpBuilder = new HttpClient.Builder().addPostInterceptor(signer);

    if (aws.domain != null) {
        httpBuilder.hosts(new ElasticsearchDomainEndpoint(aws.domain, region));
    } else {
        httpBuilder.hosts(this.hosts);
    }

    return ElasticsearchStorage.builder(httpBuilder).index(index).indexShards(indexShards)
            .indexReplicas(indexReplicas);
}