Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit 159200b

Browse files
Add option to use default AWS region provider chain.
Fixes gh-553 Fixes gh-120 Closes gh-559 Closes gh-560
1 parent e72a3d2 commit 159200b

File tree

13 files changed

+156
-22
lines changed

13 files changed

+156
-22
lines changed

docs/src/main/asciidoc/_configprops.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
|===
22
|Name | Default | Description
33

4-
|aws.paramstore.default-context | application |
4+
|aws.paramstore.default-context | application |
55
|aws.paramstore.enabled | true | Is AWS Parameter Store support enabled.
66
|aws.paramstore.fail-fast | true | Throw exceptions during config lookup if true, otherwise, log warnings.
77
|aws.paramstore.name | | Alternative to spring.application.name to use in looking up values in AWS Parameter Store.
88
|aws.paramstore.prefix | /config | 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".
9-
|aws.paramstore.profile-separator | _ |
10-
|aws.secretsmanager.default-context | application |
9+
|aws.paramstore.profile-separator | _ |
10+
|aws.secretsmanager.default-context | application |
1111
|aws.secretsmanager.enabled | true | Is AWS Secrets Manager support enabled.
1212
|aws.secretsmanager.fail-fast | true | Throw exceptions during config lookup if true, otherwise, log warnings.
1313
|aws.secretsmanager.name | | Alternative to spring.application.name to use in looking up values in AWS Secrets Manager.
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".
15-
|aws.secretsmanager.profile-separator | _ |
15+
|aws.secretsmanager.profile-separator | _ |
1616
|cloud.aws.credentials.access-key | | The access key to be used with a static provider.
1717
|cloud.aws.credentials.instance-profile | true | Configures an instance profile credentials provider with no further configuration.
1818
|cloud.aws.credentials.profile-name | | The AWS profile name.
@@ -23,7 +23,8 @@
2323
|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)
2424
|cloud.aws.loader.queue-capacity | | The maximum queue capacity for backed up S3 requests. @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setQueueCapacity(int)
2525
|cloud.aws.region.auto | true | Enables automatic region detection based on the EC2 meta data service.
26-
|cloud.aws.region.static | |
26+
|cloud.aws.region.use-default-aws-region-chain | false | Use the DefaultAwsRegion Chain instead of configuring a custom region provider chain.
27+
|cloud.aws.region.static | |
2728
|cloud.aws.stack.auto | true | Enables the automatic stack name detection for the application.
2829
|cloud.aws.stack.name | myStackName | The name of the manually configured stack name that will be used to retrieve the resources.
2930

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ The properties to configure the region are shown below
315315
|true
316316
|Enables automatic region detection based on the EC2 meta data service
317317

318+
|cloud.aws.region.useDefaultAwsRegionChain
319+
|true
320+
|Use the DefaultAWSRegion Chain instead of configuring a custom region chain
321+
318322
|cloud.aws.region.static
319323
|eu-west-1
320324
|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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
8282
return;
8383
}
8484

85-
Boolean useDefaultCredentialsChain = this.environment.getProperty(
86-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".use-default-aws-credentials-chain",
87-
Boolean.class, false);
85+
Boolean useDefaultCredentialsChain = this.environment
86+
.getProperty(
87+
AWS_CREDENTIALS_PROPERTY_PREFIX
88+
+ ".use-default-aws-credentials-chain",
89+
Boolean.class, false);
8890
String accessKey = this.environment
8991
.getProperty(AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key");
9092
String secretKey = this.environment
@@ -99,7 +101,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
99101
AWS_CREDENTIALS_PROPERTY_PREFIX + ".instance-profile",
100102
Boolean.class, true)
101103
&& !this.environment.containsProperty(
102-
AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key"),
104+
AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key"),
103105
this.environment.getProperty(
104106
AWS_CREDENTIALS_PROPERTY_PREFIX + ".profile-name",
105107
DEFAULT_PROFILE_NAME),

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
7070
return;
7171
}
7272

73-
registerRegionProvider(registry, this.environment.getProperty(
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(
7481
AWS_REGION_PROPERTIES_PREFIX + ".auto", Boolean.class, true)
75-
&& !StringUtils.hasText(this.environment
76-
.getProperty(AWS_REGION_PROPERTIES_PREFIX + ".static")),
77-
this.environment
78-
.getProperty(AWS_REGION_PROPERTIES_PREFIX + ".static"));
82+
&& !StringUtils.hasText(staticRegion);
83+
84+
registerRegionProvider(registry, autoDetect, useDefaultRegionChain,
85+
staticRegion);
7986
}
8087

8188
@Override

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Properties related to AWS region configuration.
2121
*
2222
* @author Tom Gianos
23+
* @author Maciej Walkowiak
2324
* @since 2.0.2
2425
* @see org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration
2526
*/
@@ -30,6 +31,12 @@ public class AwsRegionProperties {
3031
*/
3132
private boolean auto = true;
3233

34+
/**
35+
* Whether default AWS SDK region provider chain should be used when auto is set to
36+
* true.
37+
*/
38+
private boolean useDefaultAwsRegionChain;
39+
3340
/**
3441
* Configures a static region for the application. Possible regions are (currently)
3542
* us-east-1, us-west-1, us-west-2, eu-west-1, eu-central-1, ap-southeast-1,
@@ -60,4 +67,12 @@ public void setStatic(String staticRegion) {
6067
this.staticRegion = staticRegion;
6168
}
6269

70+
public boolean isUseDefaultAwsRegionChain() {
71+
return useDefaultAwsRegionChain;
72+
}
73+
74+
public void setUseDefaultAwsRegionChain(boolean useDefaultAwsRegionChain) {
75+
this.useDefaultAwsRegionChain = useDefaultAwsRegionChain;
76+
}
77+
6378
}

spring-cloud-aws-autoconfigure/src/test/java/org/springframework/cloud/aws/autoconfigure/context/ContextCredentialsAutoConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public void credentialsProvider_propertyToUseDefaultIsSet_configuresDefaultAwsCr
148148
public void credentialsProvider_dashSeparatedPropertyToUseDefaultIsSet_configuresDefaultAwsCredentialsProvider() {
149149
this.context = new AnnotationConfigApplicationContext();
150150
this.context.register(ContextCredentialsAutoConfiguration.class);
151-
TestPropertyValues.of("cloud.aws.credentials.use-default-aws-credentials-chain:true")
151+
TestPropertyValues
152+
.of("cloud.aws.credentials.use-default-aws-credentials-chain:true")
152153
.applyTo(this.context);
153154
this.context.refresh();
154155

spring-cloud-aws-autoconfigure/src/test/java/org/springframework/cloud/aws/autoconfigure/context/ContextRegionProviderAutoConfigurationTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
import org.junit.After;
2222
import org.junit.Test;
2323

24+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2425
import org.springframework.boot.test.util.TestPropertyValues;
26+
import org.springframework.cloud.aws.core.region.DefaultAwsRegionProviderChainDelegate;
2527
import org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider;
2628
import org.springframework.cloud.aws.core.region.StaticRegionProvider;
2729
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2830

2931
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3033

3134
/**
3235
* @author Agim Emruli
3336
* @author Petromir Dzhunev
37+
* @author Maciej Walkowiak
3438
*/
3539
public class ContextRegionProviderAutoConfigurationTest {
3640

@@ -93,4 +97,23 @@ public void regionProvider_staticRegionConfigured_staticRegionProviderWithConfig
9397
.isEqualTo(Region.getRegion(Regions.EU_WEST_1));
9498
}
9599

100+
@Test
101+
public void regionProvider_autoDetectionAndDefaultChainConfigured_DefaultAwsRegionProviderChainDelegateConfigured() {
102+
// Arrange
103+
this.context = new AnnotationConfigApplicationContext();
104+
this.context.register(ContextRegionProviderAutoConfiguration.class);
105+
TestPropertyValues.of("cloud.aws.region.auto:true").applyTo(this.context);
106+
TestPropertyValues.of("cloud.aws.region.useDefaultAwsRegionChain:true")
107+
.applyTo(this.context);
108+
109+
// Act
110+
this.context.refresh();
111+
112+
// Assert
113+
assertThat(this.context.getBean(DefaultAwsRegionProviderChainDelegate.class))
114+
.isNotNull();
115+
assertThatThrownBy(() -> this.context.getBean(Ec2MetadataRegionProvider.class))
116+
.isInstanceOf(NoSuchBeanDefinitionException.class);
117+
}
118+
96119
}

spring-cloud-aws-context/src/main/java/org/springframework/cloud/aws/context/config/annotation/ContextRegionConfigurationRegistrar.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
4343
+ importingClassMetadata.getClassName());
4444

4545
boolean autoDetect = annotationAttributes.getBoolean("autoDetect");
46+
boolean useDefaultAwsRegionChain = annotationAttributes
47+
.getBoolean("useDefaultAwsRegionChain");
4648
String configuredRegion = annotationAttributes.getString("region");
4749

48-
registerRegionProvider(registry, autoDetect, configuredRegion);
50+
registerRegionProvider(registry, autoDetect, useDefaultAwsRegionChain,
51+
configuredRegion);
4952
}
5053

5154
}

spring-cloud-aws-context/src/main/java/org/springframework/cloud/aws/context/config/annotation/EnableContextRegion.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import com.amazonaws.regions.AwsRegionProviderChain;
25+
2426
import org.springframework.context.annotation.Import;
2527

2628
/**
@@ -29,11 +31,15 @@
2931
* Service clients that are created inside the application context (by the Spring Cloud
3032
* AWS classes). A region can be either manually configured
3133
* {@link EnableContextRegion#region()} with a constant expression, dynamic expression
32-
* (using a SpEL expression) or a place holder. The region can also be dynamically
33-
* retrieved from the EC2 instance meta-data if the application context is running inside
34-
* a EC2 instance by enabling the {@link EnableContextRegion#autoDetect()} attribute.
34+
* (using a SpEL expression) or a place holder. If the application context is running
35+
* inside a EC2 instance The region can also be dynamically retrieved from the EC2
36+
* instance meta-data by enabling the {@link EnableContextRegion#autoDetect()} attribute
37+
* or from the default AWS SDK {@link AwsRegionProviderChain} by enabling
38+
* {@link EnableContextRegion#autoDetect()} and
39+
* {@link EnableContextRegion#useDefaultAwsRegionChain()}.
3540
*
3641
* @author Agim Emruli
42+
* @author Maciej Walkowiak
3743
*/
3844
@Retention(RetentionPolicy.RUNTIME)
3945
@Target(ElementType.TYPE)
@@ -58,4 +64,12 @@
5864
*/
5965
boolean autoDetect() default false;
6066

67+
/**
68+
* Whether default AWS SDK region provider chain should be used when auto is set to
69+
* true.
70+
* @return - if default AWS SDK region provider chain should be used for region
71+
* resolution.
72+
*/
73+
boolean useDefaultAwsRegionChain() default false;
74+
6175
}

spring-cloud-aws-context/src/main/java/org/springframework/cloud/aws/context/config/support/ContextConfigurationUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.beans.factory.support.ManagedList;
3232
import org.springframework.cloud.aws.core.config.AmazonWebserviceClientConfigurationUtils;
3333
import org.springframework.cloud.aws.core.credentials.CredentialsProviderFactoryBean;
34+
import org.springframework.cloud.aws.core.region.DefaultAwsRegionProviderChainDelegate;
3435
import org.springframework.cloud.aws.core.region.Ec2MetadataRegionProvider;
3536
import org.springframework.cloud.aws.core.region.StaticRegionProvider;
3637
import org.springframework.util.StringUtils;
@@ -55,7 +56,7 @@ private ContextConfigurationUtils() {
5556
}
5657

5758
public static void registerRegionProvider(BeanDefinitionRegistry registry,
58-
boolean autoDetect, String configuredRegion) {
59+
boolean autoDetect, boolean useDefaultRegionChain, String configuredRegion) {
5960
if (autoDetect && StringUtils.hasText(configuredRegion)) {
6061
throw new IllegalArgumentException(
6162
"No region must be configured if autoDetect is defined as true");
@@ -64,8 +65,9 @@ public static void registerRegionProvider(BeanDefinitionRegistry registry,
6465
AbstractBeanDefinition beanDefinition;
6566

6667
if (autoDetect) {
67-
beanDefinition = BeanDefinitionBuilder
68-
.genericBeanDefinition(Ec2MetadataRegionProvider.class)
68+
beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
69+
useDefaultRegionChain ? DefaultAwsRegionProviderChainDelegate.class
70+
: Ec2MetadataRegionProvider.class)
6971
.getBeanDefinition();
7072
}
7173
else if (StringUtils.hasText(configuredRegion)) {

spring-cloud-aws-context/src/test/java/org/springframework/cloud/aws/context/config/annotation/ContextRegionConfigurationRegistrarTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public void regionProvider_withAutoDetectedRegion_dynamicRegionProviderConfigure
8282
assertThat(staticRegionProvider).isNotNull();
8383
}
8484

85+
@Test
86+
public void regionProvider_withAutoDetectedRegionAndDefaultChain_defaulAwsChainRegionProviderConfigured()
87+
throws Exception {
88+
// Arrange
89+
this.context = new AnnotationConfigApplicationContext(
90+
ApplicationConfigurationWithDynamicRegionProvider.class);
91+
92+
// Act
93+
Ec2MetadataRegionProvider staticRegionProvider = this.context
94+
.getBean(Ec2MetadataRegionProvider.class);
95+
96+
// Assert
97+
assertThat(staticRegionProvider).isNotNull();
98+
}
99+
85100
@Test
86101
public void regionProvider_withExpressionConfiguredRegion_staticRegionProviderConfigured()
87102
throws Exception {
@@ -219,4 +234,10 @@ static class ApplicationConfigurationWithWrongRegion {
219234

220235
}
221236

237+
@Configuration(proxyBeanMethods = false)
238+
@EnableContextRegion(autoDetect = true, useDefaultAwsRegionChain = true)
239+
static class ApplicationConfigurationWithAutoDetectionAndDefaultChain {
240+
241+
}
242+
222243
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.aws.core.region;
18+
19+
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
20+
import com.amazonaws.regions.Region;
21+
import com.amazonaws.regions.RegionUtils;
22+
23+
/**
24+
* {@link RegionProvider} implementation that delegates to
25+
* {@link DefaultAwsRegionProviderChain} enabling loading region configuration from
26+
* environment variables, system properties, AWS profile, and instance metadata.
27+
*
28+
* @author Maciej Walkowiak
29+
* @since 1.0
30+
*/
31+
public class DefaultAwsRegionProviderChainDelegate implements RegionProvider {
32+
33+
private final DefaultAwsRegionProviderChain delegate = new DefaultAwsRegionProviderChain();
34+
35+
@Override
36+
public Region getRegion() {
37+
return RegionUtils.getRegion(delegate.getRegion());
38+
}
39+
40+
}

spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/config/annotation/SnsWebConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ public void addArgumentResolvers(
4646
}
4747
};
4848
}
49+
4950
}

0 commit comments

Comments
 (0)