diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfiguration.java index fa3df52ee..7f2cd89e0 100644 --- a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfiguration.java +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfiguration.java @@ -38,36 +38,42 @@ * * @author Siva Katamreddy * @author EddĂș MelĂ©ndez + * @author Wei Jiang */ @AutoConfiguration @ConditionalOnClass({ StaticRegionProvider.class, AwsRegionProvider.class, ProfileFile.class }) -@EnableConfigurationProperties(RegionProperties.class) +@EnableConfigurationProperties({ CredentialsProperties.class, RegionProperties.class }) public class RegionProviderAutoConfiguration { - private final RegionProperties properties; + private final CredentialsProperties credentialsProperties; - public RegionProviderAutoConfiguration(RegionProperties properties) { - this.properties = properties; + private final RegionProperties regionProperties; + + public RegionProviderAutoConfiguration(CredentialsProperties credentialsProperties, + RegionProperties regionProperties) { + this.credentialsProperties = credentialsProperties; + this.regionProperties = regionProperties; } @Bean @ConditionalOnMissingBean public AwsRegionProvider regionProvider() { - return createRegionProvider(this.properties); + return createRegionProvider(this.credentialsProperties, this.regionProperties); } - public static AwsRegionProvider createRegionProvider(RegionProperties properties) { + public static AwsRegionProvider createRegionProvider(CredentialsProperties credentialsProperties, + RegionProperties regionProperties) { final List providers = new ArrayList<>(); - if (properties.getStatic() != null && properties.isStatic()) { - providers.add(new StaticRegionProvider(properties.getStatic())); + if (regionProperties.getStatic() != null && regionProperties.isStatic()) { + providers.add(new StaticRegionProvider(regionProperties.getStatic())); } - if (properties.isInstanceProfile()) { + if (regionProperties.isInstanceProfile()) { providers.add(new InstanceProfileRegionProvider()); } - Profile profile = properties.getProfile(); + Profile profile = getConfigurationProfile(credentialsProperties, regionProperties); if (profile != null && profile.getName() != null) { providers.add(createProfileRegionProvider(profile)); } @@ -83,6 +89,17 @@ else if (providers.size() == 1) { } } + private static Profile getConfigurationProfile(CredentialsProperties credentialsProperties, + RegionProperties regionProperties) { + Profile profile = regionProperties.getProfile(); + + if (profile != null) { + return profile; + } + + return credentialsProperties.getProfile(); + } + private static AwsProfileRegionProvider createProfileRegionProvider(Profile profile) { Supplier profileFileFn = () -> { if (profile.getPath() != null) { diff --git a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfigurationTests.java b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfigurationTests.java index e766d9a56..a6e52fbbb 100644 --- a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfigurationTests.java +++ b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/core/RegionProviderAutoConfigurationTests.java @@ -84,6 +84,38 @@ void regionProvider_profileNameAndPathConfigured_profileRegionProviderConfigured }); } + @Test + void regionProvider_credentialProfileNameAndPathConfigured_profileRegionProviderConfiguredWithCustomProfile() + throws IOException { + this.contextRunner.withPropertyValues("spring.cloud.aws.credentials.profile.name:customProfile", + "spring.cloud.aws.credentials.profile.path:" + + new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile() + .getAbsolutePath()) + .run((context) -> { + AwsRegionProvider awsRegionProvider = context.getBean("regionProvider", + AwsProfileRegionProvider.class); + assertThat(awsRegionProvider).isNotNull(); + assertThat(awsRegionProvider.getRegion()).isEqualTo(Region.EU_WEST_1); + }); + } + + @Test + void regionProvider_credentialAndRegionProfileNameAndPathBothConfigured_profileRegionProviderConfiguredWithCustomProfile() + throws IOException { + this.contextRunner.withPropertyValues("spring.cloud.aws.credentials.profile.name:noneProfile", + "spring.cloud.aws.credentials.profile.path:", + "spring.cloud.aws.region.profile.name:customProfile", + "spring.cloud.aws.region.profile.path:" + + new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile() + .getAbsolutePath()) + .run((context) -> { + AwsRegionProvider awsRegionProvider = context.getBean("regionProvider", + AwsProfileRegionProvider.class); + assertThat(awsRegionProvider).isNotNull(); + assertThat(awsRegionProvider.getRegion()).isEqualTo(Region.EU_WEST_1); + }); + } + @Test void regionProvider_instanceProfileConfigured_configuresInstanceProfileCredentialsProvider() { this.contextRunner.withPropertyValues("spring.cloud.aws.region.instance-profile:true").run((context) -> {