From bb94631c1830df7a1fc33cfc810d6eb24334df7e Mon Sep 17 00:00:00 2001 From: Tomasz Bielecki Date: Thu, 30 Nov 2023 23:37:04 -0800 Subject: [PATCH] Fix the order of importing properties in Secrets Manager integration (#882) Co-authored-by: Tomek Bielecki --- .../SecretsManagerConfigDataLocationResolver.java | 11 ++++------- ...retsManagerConfigDataLoaderIntegrationTests.java | 13 +++++++++++++ .../src/test/resources/config.properties | 1 + .../src/test/resources/config_second.properties | 1 + 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 spring-cloud-aws-autoconfigure/src/test/resources/config.properties create mode 100644 spring-cloud-aws-autoconfigure/src/test/resources/config_second.properties diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLocationResolver.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLocationResolver.java index f65290d19..6f837e91b 100644 --- a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLocationResolver.java +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLocationResolver.java @@ -24,10 +24,7 @@ import java.util.List; import org.apache.commons.logging.Log; import org.springframework.boot.BootstrapContext; -import org.springframework.boot.context.config.ConfigDataLocation; -import org.springframework.boot.context.config.ConfigDataLocationNotFoundException; -import org.springframework.boot.context.config.ConfigDataLocationResolverContext; -import org.springframework.boot.context.config.Profiles; +import org.springframework.boot.context.config.*; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.logging.DeferredLogFactory; @@ -62,9 +59,9 @@ protected String getPrefix() { } @Override - public List resolveProfileSpecific( - ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, Profiles profiles) - throws ConfigDataLocationNotFoundException { + public List resolve(ConfigDataLocationResolverContext resolverContext, + ConfigDataLocation location) + throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException { registerBean(resolverContext, AwsProperties.class, loadAwsProperties(resolverContext.getBinder())); registerBean(resolverContext, SecretsManagerProperties.class, loadProperties(resolverContext.getBinder())); registerBean(resolverContext, CredentialsProperties.class, diff --git a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLoaderIntegrationTests.java b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLoaderIntegrationTests.java index eb1c4795c..cc84e07c4 100644 --- a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLoaderIntegrationTests.java +++ b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLoaderIntegrationTests.java @@ -44,6 +44,8 @@ import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.PropertySource; +import org.springframework.test.context.TestPropertySource; import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -169,6 +171,17 @@ void resolvesPropertyFromSecretsManager_PlainTextSecret_WithoutSlashes() { } } + @Test + void respectsImportOrder() { + SpringApplication application = new SpringApplication(App.class); + application.setWebApplicationType(WebApplicationType.NONE); + + try (ConfigurableApplicationContext context = runApplication(application, + "classpath:config.properties")) { + assertThat(context.getEnvironment().getProperty("another-parameter")).isEqualTo("from properties file"); + } + } + @Test void clientIsConfiguredWithConfigurerProvidedToBootstrapRegistry() { SpringApplication application = new SpringApplication(App.class); diff --git a/spring-cloud-aws-autoconfigure/src/test/resources/config.properties b/spring-cloud-aws-autoconfigure/src/test/resources/config.properties new file mode 100644 index 000000000..af9b8ad5a --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/test/resources/config.properties @@ -0,0 +1 @@ +spring.config.import=aws-secretsmanager:/config/spring,classpath:config_second.properties diff --git a/spring-cloud-aws-autoconfigure/src/test/resources/config_second.properties b/spring-cloud-aws-autoconfigure/src/test/resources/config_second.properties new file mode 100644 index 000000000..01a949c2b --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/test/resources/config_second.properties @@ -0,0 +1 @@ +another-parameter=from properties file