From d1b256a1693657fd32cc0991214101a24e838cce Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 3 Jun 2021 16:33:03 -0700 Subject: [PATCH] Prevent indirect standard profile-specific imports Update `StandardConfigDataLocationResolver` so that profile-specific imports can only be used when there is no parent import. Prior to this commit, given the following application.properties file: spring.profiles.active=p1,p2 spring.config.import=other.properties We would attempt to import `other.properties`, `other-p1.properties` and `other-p2.properties`. This seems quite confusing and when we really only need to support profile-specific properties for the initial root set of locations. Fixes gh-26752 --- .../StandardConfigDataLocationResolver.java | 3 +++ ...ironmentPostProcessorIntegrationTests.java | 19 +++++++++++++++++-- ...t-and-direct-profile-import-dev.properties | 2 +- ...riant-and-direct-profile-import.properties | 2 +- ...import-with-profile-variant-dev.properties | 2 +- ...th-profile-variant-imported-dev.properties | 2 +- ...t-with-profile-variant-imported.properties | 2 +- ...ion-import-with-profile-variant.properties | 2 +- ...ic-import-with-import-import-p1.properties | 1 + ...ic-import-with-import-import-p2.properties | 1 + ...cific-import-with-import-import.properties | 1 + ...-specific-import-with-import-p1.properties | 2 ++ ...ile-specific-import-with-import.properties | 2 ++ 13 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p1.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p2.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-p1.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import.properties diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java index d00c91a24cfb..967a7d986a2e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java @@ -135,6 +135,9 @@ private Set getReferences(ConfigDataLocationResolve @Override public List resolveProfileSpecific(ConfigDataLocationResolverContext context, ConfigDataLocation location, Profiles profiles) { + if (context.getParent() != null) { + return null; + } return resolve(getProfileSpecificReferences(context, location, profiles)); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 4754b54f0e8a..50eed302ebe1 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -614,7 +614,8 @@ void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() { this.application.setAdditionalProfiles("dev"); ConfigurableApplicationContext context = this.application .run("--spring.config.location=classpath:application-import-with-profile-variant.properties"); - assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev"); + assertThat(context.getEnvironment().getProperty("my.value")) + .isEqualTo("application-import-with-profile-variant-dev"); } @Test @@ -622,7 +623,8 @@ void runWhenImportWithProfileVariantAndDirectProfileImportOrdersPropertySourcesC this.application.setAdditionalProfiles("dev"); ConfigurableApplicationContext context = this.application.run( "--spring.config.location=classpath:application-import-with-profile-variant-and-direct-profile-import.properties"); - assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev"); + assertThat(context.getEnvironment().getProperty("my.value")) + .isEqualTo("application-import-with-profile-variant-imported-dev"); } @Test @@ -746,6 +748,19 @@ void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() { assertThat(environment.getProperty("test2")).isEqualTo("test2"); } + @Test // gh-26752 + void runWhenHasProfileSpecificImportWithImportDoesNotImportSecondProfileSpecificFile() { + ConfigurableApplicationContext context = this.application + .run("--spring.config.name=application-profile-specific-import-with-import"); + ConfigurableEnvironment environment = context.getEnvironment(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import")).isTrue(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import-p1")).isTrue(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import-p2")).isFalse(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import-import")).isTrue(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isFalse(); + assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isFalse(); + } + private Condition matchingPropertySource(final String sourceName) { return new Condition("environment containing property source " + sourceName) { diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import-dev.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import-dev.properties index 7cc417b78923..0ee51673facf 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import-dev.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import-dev.properties @@ -1,2 +1,2 @@ spring.config.import=classpath:application-import-with-profile-variant-imported-dev.properties -my.value=notimported-dev +my.value=application-import-with-profile-variant-and-direct-profile-import-dev diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import.properties index 0dee106b622f..3bde4a37706a 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-and-direct-profile-import.properties @@ -1,2 +1,2 @@ spring.config.import=classpath:application-import-with-profile-variant-imported.properties -my.value=notimported +my.value=application-import-with-profile-variant-and-direct-profile-import diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-dev.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-dev.properties index ec5f485d7505..2fab0529571c 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-dev.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-dev.properties @@ -1 +1 @@ -my.value=notimported-dev +my.value=application-import-with-profile-variant-dev diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported-dev.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported-dev.properties index 721c5ce5e5d8..358d119e620a 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported-dev.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported-dev.properties @@ -1 +1 @@ -my.value=iwasimported-dev +my.value=application-import-with-profile-variant-imported-dev diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported.properties index 545e85f1b698..7627d37b4b5e 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant-imported.properties @@ -1 +1 @@ -my.value=iwasimported +my.value=application-import-with-profile-variant-imported diff --git a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant.properties b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant.properties index 0dee106b622f..d2354c3974ed 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application-import-with-profile-variant.properties @@ -1,2 +1,2 @@ spring.config.import=classpath:application-import-with-profile-variant-imported.properties -my.value=notimported +my.value=application-import-with-profile-variant diff --git a/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p1.properties b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p1.properties new file mode 100644 index 000000000000..c9ad018fd9f4 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p1.properties @@ -0,0 +1 @@ +application-profile-specific-import-with-import-import-p1=true diff --git a/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p2.properties b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p2.properties new file mode 100644 index 000000000000..e72f2f191d51 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import-p2.properties @@ -0,0 +1 @@ +application-profile-specific-import-with-import-import-p2=true diff --git a/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import.properties b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import.properties new file mode 100644 index 000000000000..e92241ad1edd --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-import.properties @@ -0,0 +1 @@ +application-profile-specific-import-with-import-import=true diff --git a/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-p1.properties b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-p1.properties new file mode 100644 index 000000000000..836dcc3f5eee --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import-p1.properties @@ -0,0 +1,2 @@ +application-profile-specific-import-with-import-p1=true +spring.config.import=application-profile-specific-import-with-import-import.properties diff --git a/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import.properties b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import.properties new file mode 100644 index 000000000000..4b94ad9212ab --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-profile-specific-import-with-import.properties @@ -0,0 +1,2 @@ +spring.profiles.active=p1,p2 +application-profile-specific-import-with-import=true