Skip to content

Commit 492c794

Browse files
Use DefaultAWSCredentialsProvider and DefaultAWSRegionProviderChain by default.
Fixes spring-attic#583 Closes spring-attic#568
1 parent 8311119 commit 492c794

File tree

11 files changed

+211
-627
lines changed

11 files changed

+211
-627
lines changed

docs/src/main/asciidoc/_configprops.adoc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
|aws.secretsmanager.prefix | /secret | Prefix indicating first level for every property. Value must start with a forward slash followed by a valid path segment or be empty. Defaults to "/config".
1515
|aws.secretsmanager.profile-separator | _ |
1616
|cloud.aws.credentials.access-key | | The access key to be used with a static provider.
17-
|cloud.aws.credentials.instance-profile | true | Configures an instance profile credentials provider with no further configuration.
17+
|cloud.aws.credentials.instance-profile | false | Configures an instance profile credentials provider with no further configuration.
1818
|cloud.aws.credentials.profile-name | | The AWS profile name.
1919
|cloud.aws.credentials.profile-path | | The AWS profile path.
2020
|cloud.aws.credentials.secret-key | | The secret key to be used with a static provider.
21-
|cloud.aws.credentials.use-default-aws-credentials-chain | false | Use the DefaultAWSCredentials Chain instead of configuring a custom credentials chain.
2221
|cloud.aws.loader.core-pool-size | 1 | The core pool size of the Task Executor used for parallel S3 interaction. @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setCorePoolSize(int)
2322
|cloud.aws.loader.max-pool-size | | The maximum pool size of the Task Executor used for parallel S3 interaction. @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setMaxPoolSize(int)
2423
|cloud.aws.loader.queue-capacity | | The maximum queue capacity for backed up S3 requests. @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setQueueCapacity(int)
25-
|cloud.aws.region.auto | true | Enables automatic region detection based on the EC2 meta data service.
2624
|cloud.aws.region.static | |
27-
|cloud.aws.region.use-default-aws-region-chain | false | Whether default AWS SDK region provider chain should be used when auto is set to true.
2825
|cloud.aws.stack.auto | true | Enables the automatic stack name detection for the application.
2926
|cloud.aws.stack.name | | The name of the manually configured stack name that will be used to retrieve the resources.
3027

docs/src/main/asciidoc/spring-cloud-aws.adoc

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,16 @@ only configure classes that are available in the Spring Boot application's class
305305
===== Configuring credentials
306306
Spring Boot provides a standard way to define properties with property file or YAML configuration files. Spring Cloud
307307
AWS provides support to configure the credential information with the Spring Boot application configuration files.
308-
Spring Cloud AWS provides the following properties to configure the credentials setup for the whole application.
309308

310-
Unless `cloud.aws.credentials.use-default-aws-credentials-chain` is set to `true`, Spring Cloud AWS configures following
311-
credentials chain:
309+
By default Spring Cloud AWS configures https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html[DefaultAWSCredentialsProviderChain] to resolve AWS credentials.
310+
311+
If other credentials providers are configured, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html[DefaultAWSCredentialsProviderChain] is not used and Spring Cloud AWS configures following credentials chain:
312312

313313
1. `AWSStaticCredentialsProvider` if `cloud.aws.credentials.access-key` is provided
314-
2. `EC2ContainerCredentialsProviderWrapper` unless `cloud.aws.credentials.instance-profile` is set to `false`
315-
3. `ProfileCredentialsProvider`
314+
2. `EC2ContainerCredentialsProviderWrapper` if `cloud.aws.credentials.instance-profile` is set to `true`
315+
3. `ProfileCredentialsProvider` if `cloud.aws.credentials.profile-name` is provided
316+
317+
Spring Cloud AWS provides the following properties to configure the credentials setup for the whole application.
316318

317319
[cols="3*", options="header"]
318320
|===
@@ -339,15 +341,11 @@ credentials chain:
339341
|cloud.aws.credentials.profile-path
340342
|`~/.aws/credentials`
341343
|The file path where the profile configuration file is located. Defaults to `~/.aws/credentials` if value is not provided
342-
343-
|cloud.aws.credentials.use-default-aws-credentials-chain
344-
|true
345-
|Use the DefaultAWSCredentials Chain instead of configuring a custom credentials chain
346344
|===
347345

348346
===== Configuring region
349347
Like for the credentials, the Spring Cloud AWS module also supports the configuration of the region inside the Spring
350-
Boot configuration files. The region can be automatically detected or explicitly configured (e.g. in case of local tests
348+
Boot configuration files. The region can be automatically detected using https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/DefaultAwsRegionProviderChain.html[DefaultAwsRegionProviderChain] or explicitly configured (e.g. in case of local tests
351349
against the AWS cloud).
352350

353351
The properties to configure the region are shown below
@@ -358,14 +356,6 @@ The properties to configure the region are shown below
358356
|example
359357
|description
360358

361-
|cloud.aws.region.auto
362-
|true
363-
|Enables automatic region detection based on the EC2 meta data service
364-
365-
|cloud.aws.region.use-default-aws-region-chain
366-
|true
367-
|Use the DefaultAWSRegion Chain instead of configuring a custom region chain
368-
369359
|cloud.aws.region.static
370360
|eu-west-1
371361
|Configures a static region for the application. Possible regions are (currently) us-east-1, us-west-1, us-west-2,

spring-cloud-aws-autoconfigure/src/main/java/org/springframework/cloud/aws/autoconfigure/context/ContextCredentialsAutoConfiguration.java

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,100 +16,76 @@
1616

1717
package org.springframework.cloud.aws.autoconfigure.context;
1818

19-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import com.amazonaws.auth.AWSCredentialsProvider;
23+
import com.amazonaws.auth.AWSCredentialsProviderChain;
24+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
25+
import com.amazonaws.auth.BasicAWSCredentials;
26+
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
27+
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper;
28+
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
29+
30+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2031
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21-
import org.springframework.boot.context.properties.ConfigurationProperties;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
33+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2234
import org.springframework.cloud.aws.autoconfigure.context.properties.AwsCredentialsProperties;
23-
import org.springframework.cloud.aws.context.config.annotation.ContextDefaultConfigurationRegistrar;
24-
import org.springframework.cloud.aws.core.credentials.CredentialsProviderFactoryBean;
25-
import org.springframework.context.EnvironmentAware;
2635
import org.springframework.context.annotation.Bean;
2736
import org.springframework.context.annotation.Configuration;
28-
import org.springframework.context.annotation.Import;
29-
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
30-
import org.springframework.core.env.Environment;
31-
import org.springframework.core.type.AnnotationMetadata;
3237
import org.springframework.util.StringUtils;
3338

34-
import static com.amazonaws.auth.profile.internal.AwsProfileNameLoader.DEFAULT_PROFILE_NAME;
35-
import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerCredentialsProvider;
36-
import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerDefaultAWSCredentialsProvider;
39+
import static org.springframework.cloud.aws.core.config.AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME;
3740

3841
/**
42+
* {@link EnableAutoConfiguration} for {@link AWSCredentialsProvider}.
43+
*
3944
* @author Agim Emruli
45+
* @author Maciej Walkowiak
4046
*/
4147
@Configuration(proxyBeanMethods = false)
42-
@Import({ ContextDefaultConfigurationRegistrar.class,
43-
ContextCredentialsAutoConfiguration.Registrar.class })
44-
@ConditionalOnClass(name = "com.amazonaws.auth.AWSCredentialsProvider")
48+
@EnableConfigurationProperties(AwsCredentialsProperties.class)
49+
@ConditionalOnClass(com.amazonaws.auth.AWSCredentialsProvider.class)
4550
public class ContextCredentialsAutoConfiguration {
4651

47-
/**
48-
* The prefix used for AWS credentials related properties.
49-
*/
50-
public static final String AWS_CREDENTIALS_PROPERTY_PREFIX = "cloud.aws.credentials";
52+
@Bean(name = CREDENTIALS_PROVIDER_BEAN_NAME)
53+
@ConditionalOnMissingBean(name = CREDENTIALS_PROVIDER_BEAN_NAME)
54+
public AWSCredentialsProvider awsCredentialsProvider(
55+
AwsCredentialsProperties properties) {
5156

52-
/**
53-
* Bind AWS credentials related properties to a property instance.
54-
* @return An {@link AwsCredentialsProperties} instance
55-
*/
56-
@Bean
57-
@ConfigurationProperties(prefix = AWS_CREDENTIALS_PROPERTY_PREFIX)
58-
public AwsCredentialsProperties awsCredentialsProperties() {
59-
return new AwsCredentialsProperties();
60-
}
57+
List<AWSCredentialsProvider> providers = resolveCredentialsProviders(properties);
6158

62-
/**
63-
* Registrar for the credentials provider.
64-
*/
65-
public static class Registrar
66-
implements ImportBeanDefinitionRegistrar, EnvironmentAware {
59+
if (providers.isEmpty()) {
60+
return new DefaultAWSCredentialsProviderChain();
61+
}
62+
else {
63+
return new AWSCredentialsProviderChain(providers);
64+
}
65+
}
6766

68-
private Environment environment;
67+
private List<AWSCredentialsProvider> resolveCredentialsProviders(
68+
AwsCredentialsProperties properties) {
69+
List<AWSCredentialsProvider> providers = new ArrayList<>();
6970

70-
@Override
71-
public void setEnvironment(Environment environment) {
72-
this.environment = environment;
71+
if (StringUtils.hasText(properties.getAccessKey())
72+
&& StringUtils.hasText(properties.getSecretKey())) {
73+
providers.add(new AWSStaticCredentialsProvider(new BasicAWSCredentials(
74+
properties.getAccessKey(), properties.getSecretKey())));
7375
}
7476

75-
@Override
76-
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
77-
BeanDefinitionRegistry registry) {
78-
// Do not register a credentials provider if a bean with the same name is
79-
// already registered.
80-
if (registry.containsBeanDefinition(
81-
CredentialsProviderFactoryBean.CREDENTIALS_PROVIDER_BEAN_NAME)) {
82-
return;
83-
}
77+
if (properties.isInstanceProfile()) {
78+
providers.add(new EC2ContainerCredentialsProviderWrapper());
79+
}
8480

85-
Boolean useDefaultCredentialsChain = this.environment
86-
.getProperty(
87-
AWS_CREDENTIALS_PROPERTY_PREFIX
88-
+ ".use-default-aws-credentials-chain",
89-
Boolean.class, false);
90-
String accessKey = this.environment
91-
.getProperty(AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key");
92-
String secretKey = this.environment
93-
.getProperty(AWS_CREDENTIALS_PROPERTY_PREFIX + ".secret-key");
94-
if (useDefaultCredentialsChain && (StringUtils.isEmpty(accessKey)
95-
|| StringUtils.isEmpty(secretKey))) {
96-
registerDefaultAWSCredentialsProvider(registry);
97-
}
98-
else {
99-
registerCredentialsProvider(registry, accessKey, secretKey,
100-
this.environment.getProperty(
101-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".instance-profile",
102-
Boolean.class, true)
103-
&& !this.environment.containsProperty(
104-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key"),
105-
this.environment.getProperty(
106-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".profile-name",
107-
DEFAULT_PROFILE_NAME),
108-
this.environment.getProperty(
109-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".profile-path"));
110-
}
81+
if (properties.getProfileName() != null) {
82+
providers.add(properties.getProfilePath() != null
83+
? new ProfileCredentialsProvider(properties.getProfilePath(),
84+
properties.getProfileName())
85+
: new ProfileCredentialsProvider(properties.getProfileName()));
11186
}
11287

88+
return providers;
11389
}
11490

11591
}

spring-cloud-aws-autoconfigure/src/main/java/org/springframework/cloud/aws/autoconfigure/context/ContextRegionProviderAutoConfiguration.java

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@
1616

1717
package org.springframework.cloud.aws.autoconfigure.context;
1818

19-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
20-
import org.springframework.boot.context.properties.ConfigurationProperties;
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2121
import org.springframework.cloud.aws.autoconfigure.context.properties.AwsRegionProperties;
22-
import org.springframework.context.EnvironmentAware;
22+
import org.springframework.cloud.aws.core.region.DefaultAwsRegionProviderChainDelegate;
23+
import org.springframework.cloud.aws.core.region.RegionProvider;
24+
import org.springframework.cloud.aws.core.region.StaticRegionProvider;
2325
import org.springframework.context.annotation.Bean;
2426
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.context.annotation.Import;
26-
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
27-
import org.springframework.core.env.Environment;
28-
import org.springframework.core.type.AnnotationMetadata;
2927
import org.springframework.util.StringUtils;
3028

3129
import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.REGION_PROVIDER_BEAN_NAME;
32-
import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerRegionProvider;
3330

3431
/**
3532
* Region auto configuration, based on <a
@@ -38,58 +35,21 @@
3835
*
3936
* @author Agim Emruli
4037
* @author Petromir Dzhunev
38+
* @author Maciej Walkowiak
4139
*/
4240
@Configuration(proxyBeanMethods = false)
43-
@Import(ContextRegionProviderAutoConfiguration.Registrar.class)
41+
@EnableConfigurationProperties(AwsRegionProperties.class)
4442
public class ContextRegionProviderAutoConfiguration {
4543

46-
/**
47-
* The prefix used for AWS region related properties.
48-
*/
49-
public static final String AWS_REGION_PROPERTIES_PREFIX = "cloud.aws.region";
50-
51-
/**
52-
* Bind AWS region related properties to a property instance.
53-
* @return An {@link AwsRegionProperties} instance
54-
*/
55-
@Bean
56-
@ConfigurationProperties(prefix = AWS_REGION_PROPERTIES_PREFIX)
57-
public AwsRegionProperties awsRegionProperties() {
58-
return new AwsRegionProperties();
59-
}
60-
61-
static class Registrar implements EnvironmentAware, ImportBeanDefinitionRegistrar {
62-
63-
private Environment environment;
64-
65-
@Override
66-
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
67-
BeanDefinitionRegistry registry) {
68-
// Do not register region provider if already existing
69-
if (registry.containsBeanDefinition(REGION_PROVIDER_BEAN_NAME)) {
70-
return;
71-
}
72-
73-
boolean useDefaultRegionChain = this.environment.getProperty(
74-
AWS_REGION_PROPERTIES_PREFIX + ".use-default-aws-region-chain",
75-
Boolean.class, false);
76-
77-
String staticRegion = this.environment
78-
.getProperty(AWS_REGION_PROPERTIES_PREFIX + ".static");
79-
80-
boolean autoDetect = this.environment.getProperty(
81-
AWS_REGION_PROPERTIES_PREFIX + ".auto", Boolean.class, true)
82-
&& !StringUtils.hasText(staticRegion);
83-
84-
registerRegionProvider(registry, autoDetect, useDefaultRegionChain,
85-
staticRegion);
44+
@ConditionalOnMissingBean(name = REGION_PROVIDER_BEAN_NAME)
45+
@Bean(name = REGION_PROVIDER_BEAN_NAME)
46+
RegionProvider regionProvider(AwsRegionProperties properties) {
47+
if (StringUtils.hasText(properties.getStatic())) {
48+
return new StaticRegionProvider(properties.getStatic());
8649
}
87-
88-
@Override
89-
public void setEnvironment(Environment environment) {
90-
this.environment = environment;
50+
else {
51+
return new DefaultAwsRegionProviderChainDelegate();
9152
}
92-
9353
}
9454

9555
}

spring-cloud-aws-autoconfigure/src/main/java/org/springframework/cloud/aws/autoconfigure/context/properties/AwsCredentialsProperties.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.cloud.aws.autoconfigure.context.properties;
1818

19-
import com.amazonaws.auth.profile.internal.AwsProfileNameLoader;
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
2020

2121
/**
2222
* Properties related to AWS credentials.
@@ -25,6 +25,7 @@
2525
* @since 2.0.2
2626
* @see org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration
2727
*/
28+
@ConfigurationProperties(prefix = "cloud.aws.credentials")
2829
public class AwsCredentialsProperties {
2930

3031
/**
@@ -40,18 +41,12 @@ public class AwsCredentialsProperties {
4041
/**
4142
* Configures an instance profile credentials provider with no further configuration.
4243
*/
43-
private boolean instanceProfile = true;
44-
45-
/**
46-
* Use the DefaultAWSCredentials Chain instead of configuring a custom credentials
47-
* chain.
48-
*/
49-
private boolean useDefaultAwsCredentialsChain;
44+
private boolean instanceProfile = false;
5045

5146
/**
5247
* The AWS profile name.
5348
*/
54-
private String profileName = AwsProfileNameLoader.DEFAULT_PROFILE_NAME;
49+
private String profileName;
5550

5651
/**
5752
* The AWS profile path.
@@ -82,14 +77,6 @@ public void setInstanceProfile(boolean instanceProfile) {
8277
this.instanceProfile = instanceProfile;
8378
}
8479

85-
public boolean isUseDefaultAwsCredentialsChain() {
86-
return this.useDefaultAwsCredentialsChain;
87-
}
88-
89-
public void setUseDefaultAwsCredentialsChain(boolean useDefaultAwsCredentialsChain) {
90-
this.useDefaultAwsCredentialsChain = useDefaultAwsCredentialsChain;
91-
}
92-
9380
public String getProfileName() {
9481
return this.profileName;
9582
}

0 commit comments

Comments
 (0)