From 80dbbaf01270962e7d70666adec4bd694d0f5c57 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 19 Jan 2021 15:53:10 -0800 Subject: [PATCH] Add ConfigData IGNORE_PROFILES support Add a new `ConfigData.Option` that allows profile properties to be ignored. This update will allow Spring Cloud Config Server to provide `ConfigData` that only has profile properties processed on the server-side. Closes gh-24890 --- .../boot/context/config/ConfigData.java | 10 ++++- .../context/config/ConfigDataEnvironment.java | 25 ++++++----- .../ConfigDataEnvironmentContributor.java | 41 ++++++++++++------- .../ConfigDataEnvironmentContributors.java | 35 ++++++++++------ .../InvalidConfigDataPropertyException.java | 2 +- ...tContributorPlaceholdersResolverTests.java | 2 +- .../config/ConfigDataEnvironmentTests.java | 30 +++++++++++++- ...validConfigDataPropertyExceptionTests.java | 27 ++++++++++-- ...sFromIgnoreProfilesContributors.properties | 1 + 9 files changed, 128 insertions(+), 45 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributors.properties diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java index b902fd87da..5a1431d21a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigData.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,7 +90,13 @@ public final class ConfigData { /** * Ignore all imports properties from the sources. */ - IGNORE_IMPORTS; + IGNORE_IMPORTS, + + /** + * Ignore all profile activation and include properties. + * @since 2.4.3 + */ + IGNORE_PROFILES; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java index a01c9be065..3ea39a829f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java @@ -181,6 +181,11 @@ class ConfigDataEnvironment { this.logger.trace("Creating wrapped config data contributor for default property source"); contributors.add(ConfigDataEnvironmentContributor.ofExisting(defaultPropertySource)); } + return createContributors(contributors); + } + + protected ConfigDataEnvironmentContributors createContributors( + List contributors) { return new ConfigDataEnvironmentContributors(this.logFactory, this.bootstrapContext, contributors); } @@ -263,7 +268,8 @@ class ConfigDataEnvironment { private ConfigDataActivationContext withProfiles(ConfigDataEnvironmentContributors contributors, ConfigDataActivationContext activationContext) { this.logger.trace("Deducing profiles from current config data environment contributors"); - Binder binder = contributors.getBinder(activationContext, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE); + Binder binder = contributors.getBinder(activationContext, + ConfigDataEnvironmentContributor::isNotIgnoringProfiles, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE); try { Set additionalProfiles = new LinkedHashSet<>(this.additionalProfiles); additionalProfiles.addAll(getIncludedProfiles(contributors, activationContext)); @@ -285,16 +291,15 @@ class ConfigDataEnvironment { Set result = new LinkedHashSet<>(); for (ConfigDataEnvironmentContributor contributor : contributors) { ConfigurationPropertySource source = contributor.getConfigurationPropertySource(); - if (source == null) { - continue; + if (source != null && contributor.isNotIgnoringProfiles()) { + Binder binder = new Binder(Collections.singleton(source), placeholdersResolver); + binder.bind(Profiles.INCLUDE_PROFILES, STRING_LIST).ifBound((includes) -> { + if (!contributor.isActive(activationContext)) { + InactiveConfigDataAccessException.throwIfPropertyFound(contributor, Profiles.INCLUDE_PROFILES); + } + result.addAll(includes); + }); } - Binder binder = new Binder(Collections.singleton(source), placeholdersResolver); - binder.bind(Profiles.INCLUDE_PROFILES, STRING_LIST).ifBound((includes) -> { - if (!contributor.isActive(activationContext)) { - InactiveConfigDataAccessException.throwIfPropertyFound(contributor, Profiles.INCLUDE_PROFILES); - } - result.addAll(includes); - }); } return result; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java index d4f6bd4637..c53265dec4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java @@ -23,6 +23,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Set; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -51,6 +52,9 @@ import org.springframework.core.env.PropertySource; */ class ConfigDataEnvironmentContributor implements Iterable { + private static final Set EMPTY_LOCATION_OPTIONS = Collections + .unmodifiableSet(Collections.singleton(ConfigData.Option.IGNORE_IMPORTS)); + private final ConfigDataLocation location; private final ConfigDataResource resource; @@ -63,7 +67,7 @@ class ConfigDataEnvironmentContributor implements Iterable configDataOptions; private final Map> children; @@ -79,13 +83,14 @@ class ConfigDataEnvironmentContributor implements Iterable propertySource, ConfigurationPropertySource configurationPropertySource, ConfigDataProperties properties, - boolean ignoreImports, Map> children) { + Set configDataOptions, + Map> children) { this.kind = kind; this.location = location; this.resource = resource; @@ -93,7 +98,7 @@ class ConfigDataEnvironmentContributor implements Iterable> updatedChildren = new LinkedHashMap<>(this.children); updatedChildren.put(importPhase, children); return new ConfigDataEnvironmentContributor(this.kind, this.location, this.resource, this.profileSpecific, - this.propertySource, this.configurationPropertySource, this.properties, this.ignoreImports, + this.propertySource, this.configurationPropertySource, this.properties, this.configDataOptions, updatedChildren); } @@ -255,7 +269,7 @@ class ConfigDataEnvironmentContributor implements Iterable contributors) { Map> children = new LinkedHashMap<>(); children.put(ImportPhase.BEFORE_PROFILE_ACTIVATION, Collections.unmodifiableList(contributors)); - return new ConfigDataEnvironmentContributor(Kind.ROOT, null, null, false, null, null, null, false, children); + return new ConfigDataEnvironmentContributor(Kind.ROOT, null, null, false, null, null, null, null, children); } /** @@ -281,7 +295,7 @@ class ConfigDataEnvironmentContributor implements Iterable imports = Collections.singletonList(initialImport); ConfigDataProperties properties = new ConfigDataProperties(imports, null); return new ConfigDataEnvironmentContributor(Kind.INITIAL_IMPORT, null, null, false, null, null, properties, - false, null); + null, null); } /** @@ -293,7 +307,7 @@ class ConfigDataEnvironmentContributor implements Iterable propertySource) { return new ConfigDataEnvironmentContributor(Kind.EXISTING, null, null, false, propertySource, - ConfigurationPropertySource.from(propertySource), null, false, null); + ConfigurationPropertySource.from(propertySource), null, null, null); } /** @@ -311,9 +325,8 @@ class ConfigDataEnvironmentContributor implements Iterable propertySource = configData.getPropertySources().get(propertySourceIndex); ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource); - boolean ignoreImports = configData.getOptions().contains(ConfigData.Option.IGNORE_IMPORTS); return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific, - propertySource, configurationPropertySource, null, ignoreImports, null); + propertySource, configurationPropertySource, null, configData.getOptions(), null); } /** @@ -324,7 +337,7 @@ class ConfigDataEnvironmentContributor implements Iterable { + private static final Predicate NO_CONTRIBUTOR_FILTER = (contributor) -> true; + private final Log logger; private final ConfigDataEnvironmentContributor root; @@ -186,13 +188,25 @@ class ConfigDataEnvironmentContributors implements Iterable filter, + BinderOption... options) { + return getBinder(activationContext, filter, asBinderOptionsSet(options)); } private Set asBinderOptionsSet(BinderOption... options) { @@ -200,10 +214,11 @@ class ConfigDataEnvironmentContributors implements Iterable options) { + private Binder getBinder(ConfigDataActivationContext activationContext, + Predicate filter, Set options) { boolean failOnInactiveSource = options.contains(BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE); Iterable sources = () -> getBinderSources(activationContext, - !options.contains(BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE)); + filter.and((contributor) -> failOnInactiveSource || contributor.isActive(activationContext))); PlaceholdersResolver placeholdersResolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(this.root, activationContext, failOnInactiveSource); BindHandler bindHandler = !failOnInactiveSource ? null : new InactiveSourceChecker(activationContext); @@ -211,13 +226,9 @@ class ConfigDataEnvironmentContributors implements Iterable getBinderSources(ConfigDataActivationContext activationContext, - boolean filterInactive) { - Stream sources = this.root.stream() - .filter(this::hasConfigurationPropertySource); - if (filterInactive) { - sources = sources.filter((contributor) -> contributor.isActive(activationContext)); - } - return sources.map(ConfigDataEnvironmentContributor::getConfigurationPropertySource).iterator(); + Predicate filter) { + return this.root.stream().filter(this::hasConfigurationPropertySource).filter(filter) + .map(ConfigDataEnvironmentContributor::getConfigurationPropertySource).iterator(); } private boolean hasConfigurationPropertySource(ConfigDataEnvironmentContributor contributor) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java index cc85e435f3..21be4abaac 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java @@ -114,7 +114,7 @@ public class InvalidConfigDataPropertyException extends ConfigDataException { logger.warn(getMessage(property, false, replacement, contributor.getResource())); } }); - if (contributor.isProfileSpecific()) { + if (contributor.isProfileSpecific() && contributor.isNotIgnoringProfiles()) { PROFILE_SPECIFIC_ERRORS.forEach((name) -> { ConfigurationProperty property = propertySource.getConfigurationProperty(name); if (property != null) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorPlaceholdersResolverTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorPlaceholdersResolverTests.java index 47e96695fb..6186352c3b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorPlaceholdersResolverTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorPlaceholdersResolverTests.java @@ -122,7 +122,7 @@ class ConfigDataEnvironmentContributorPlaceholdersResolverTests { private final boolean active; protected TestConfigDataEnvironmentContributor(PropertySource propertySource, boolean active) { - super(Kind.ROOT, null, null, false, propertySource, null, null, false, null); + super(Kind.ROOT, null, null, false, propertySource, null, null, null, null); this.active = active; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java index 3d0d07e5cb..c690f079f5 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,9 @@ package org.springframework.boot.context.config; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -34,6 +36,7 @@ import org.springframework.boot.context.config.TestConfigDataEnvironmentUpdateLi import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; @@ -42,6 +45,7 @@ import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; /** * Tests for {@link ConfigDataEnvironment}. @@ -188,6 +192,30 @@ class ConfigDataEnvironmentTests { assertThat(this.environment.getActiveProfiles()).containsExactly("one", "four", "five", "two", "three"); } + @Test + void processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributors(TestInfo info) { + this.environment.setProperty("spring.config.location", getConfigLocation(info)); + ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext, + this.environment, this.resourceLoader, this.additionalProfiles, null) { + + @Override + protected ConfigDataEnvironmentContributors createContributors( + List contributors) { + Map source = new LinkedHashMap<>(); + source.put("spring.profiles.active", "ignore1"); + source.put("spring.profiles.include", "ignore2"); + ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source)), + ConfigData.Option.IGNORE_PROFILES); + contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"), + mock(ConfigDataResource.class), false, data, 0)); + return super.createContributors(contributors); + } + + }; + configDataEnvironment.processAndApply(); + assertThat(this.environment.getActiveProfiles()).containsExactly("test"); + } + @Test @Disabled("Disabled until spring.profiles support is dropped") void processAndApplyWhenHasInvalidPropertyThrowsException() { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java index bd94cb365f..fae33cb880 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.context.config; +import java.util.Arrays; +import java.util.HashSet; + import org.apache.commons.logging.Log; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -29,6 +32,7 @@ import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -125,16 +129,31 @@ class InvalidConfigDataPropertyExceptionTests { throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException("spring.profiles"); } + @Test + void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyOnIgnoringProfilesContributorDoesNotThrowException() { + ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor( + "spring.profiles.active", ConfigData.Option.IGNORE_PROFILES); + assertThatNoException() + .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)); + } + private void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException(String name) { - MockPropertySource propertySource = new MockPropertySource(); - propertySource.setProperty(name, "a"); - ConfigDataEnvironmentContributor contributor = new ConfigDataEnvironmentContributor(Kind.BOUND_IMPORT, null, - null, true, propertySource, ConfigurationPropertySource.from(propertySource), null, false, null); + ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(name); assertThatExceptionOfType(InvalidConfigDataPropertyException.class) .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)) .withMessageStartingWith("Property '" + name + "' is invalid in a profile specific resource"); } + private ConfigDataEnvironmentContributor createInvalidProfileSpecificPropertyContributor(String name, + ConfigData.Option... configDataOptions) { + MockPropertySource propertySource = new MockPropertySource(); + propertySource.setProperty(name, "a"); + ConfigDataEnvironmentContributor contributor = new ConfigDataEnvironmentContributor(Kind.BOUND_IMPORT, null, + null, true, propertySource, ConfigurationPropertySource.from(propertySource), null, + new HashSet<>(Arrays.asList(configDataOptions)), null); + return contributor; + } + @Test void throwOrWarnWhenHasNoInvalidPropertyDoesNothing() { ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor diff --git a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributors.properties b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributors.properties new file mode 100644 index 0000000000..f841722a2f --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/context/config/ConfigDataEnvironmentTests-processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributors.properties @@ -0,0 +1 @@ +spring.profiles.active=test