Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1285 Spring Cloud activates unexpected profiles when some conditions met #1286

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ private Set<String> addIncludedProfilesTo(Set<String> profiles, PropertySource<?

private List<String> addActiveProfilesTo(List<String> profiles, PropertySource<?> propertySource,
ConfigurableEnvironment environment) {
// According to Spring Boot, "spring.profiles.active" should have priority,
// only value from property source with the highest priority wins.
// Once settled, ignore others
if(!profiles.isEmpty())
return profiles;
return addProfilesTo(profiles, propertySource, AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, environment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,26 @@ public void setFoo(List<String> foo) {

}

@Test
void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveActiveProfileProperties_ShouldOnlyAcceptTheTopPriority() {
String[] properties = new String[]{"spring.config.use-legacy-processing=true"};
this.context = new SpringApplicationBuilder()
.web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run("--spring.profiles.active=prod,secure");
then(this.context.getEnvironment().acceptsProfiles("prod", "secure")).isTrue();
// active profile from property sources with lower priority should not be included
then(this.context.getEnvironment().acceptsProfiles("local")).isFalse();
then(this.context.getEnvironment().getActiveProfiles()).contains("prod", "secure");
then(this.context.getEnvironment().getActiveProfiles()).doesNotContain("local");
// check if active profile value could possibly exist in other property sources with lower priority
then(this.context.getEnvironment()
.getPropertySources()
.stream()
.map(p -> p.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME))
.anyMatch("local"::equals)
).isTrue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ debug:true
logging.level.org.hibernate=ERROR
logging.level.com.zaxxer.hikari=ERROR
myplaceholder=${vcap.services.myvcap.myvar}
spring.profiles.active=local