Run all EnvironmentPostProcessors on refresh. (#969)

This includes ConfigDataEnvironmentPostProcessors which replaces the static call.

Issue #968 was specifically about the cloud foundry vcap post processor. Previously the DecryptEnvironmentPostProcessor was hard coded in ConfigDataContextRefresher

Fixes gh-968
This commit is contained in:
Spencer Gibb
2021-06-23 15:23:31 -04:00
committed by GitHub
parent 2e0db8e21e
commit 74a0b68287
9 changed files with 290 additions and 124 deletions

View File

@@ -23,8 +23,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.test.TestConfigDataLocationResolver;
import org.springframework.cloud.context.test.TestEnvPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
@@ -124,20 +125,21 @@ public class EncryptionIntegrationTests {
@Test
public void decryptAfterRefresh() {
TestConfigDataLocationResolver.config.put("foo.password",
"{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95");
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestAutoConfiguration.class)
.web(WebApplicationType.NONE)
.properties("encrypt.key:pie",
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95",
.web(WebApplicationType.NONE).properties("encrypt.key:pie", TestEnvPostProcessor.EPP_ENABLED + "=true",
"spring.cloud.refresh.enabled:true")
.run();
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
ContextRefresher refresher = context.getBean(ContextRefresher.class);
ConfigurableEnvironment env = context.getBean(ConfigurableEnvironment.class);
then(env.getProperty("foo.password")).isEqualTo("test");
TestPropertyValues.of("foo.password={cipher}" + encryptor.encrypt("newValue")).applyTo(env);
TestConfigDataLocationResolver.config.put("foo.password", "{cipher}" + encryptor.encrypt("newValue"));
refresher.refresh();
then(env.getProperty("foo.password")).isEqualTo("newValue");
context.close();
TestConfigDataLocationResolver.config.clear();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -16,49 +16,26 @@
package org.springframework.cloud.context.refresh;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.context.test.TestConfigDataLocationResolver;
import org.springframework.cloud.context.test.TestEnvPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import static org.assertj.core.api.BDDAssertions.then;
public class ConfigDataContextRefresherIntegrationTests {
private static final String EPP_VALUE = "configdatarefresh.epp.count";
private static final String EPP_ENABLED = "configdatarefresh.epp.enabled";
private TestProperties properties;
private ConfigurableEnvironment environment;
@@ -69,8 +46,12 @@ public class ConfigDataContextRefresherIntegrationTests {
@BeforeEach
public void setup() {
TestConfigDataLocationResolver.instance = new MyTestBean();
System.setProperty("VCAP_SERVICES",
"{\"user-provided\":[{\"label\": \"user-provided\",\"name\": \"myvcap\",\"myvar\": \"myval\"}]}");
context = new SpringApplication(TestConfiguration.class).run("--spring.datasource.hikari.read-only=false",
"--spring.profiles.active=configdatarefresh", "--" + EPP_ENABLED + "=true", "--server.port=0");
"--spring.profiles.active=configdatarefresh", "--" + TestEnvPostProcessor.EPP_ENABLED + "=true",
"--server.port=0");
properties = context.getBean(TestProperties.class);
environment = context.getBean(ConfigurableEnvironment.class);
refresher = context.getBean(ContextRefresher.class);
@@ -78,9 +59,12 @@ public class ConfigDataContextRefresherIntegrationTests {
@AfterEach
public void after() {
System.clearProperty("VCAP_SERVICES");
if (context != null) {
context.close();
}
TestConfigDataLocationResolver.count.set(1);
TestConfigDataLocationResolver.instance = null;
}
@Test
@@ -94,12 +78,12 @@ public class ConfigDataContextRefresherIntegrationTests {
@Test
public void testAdditionalPropertySourcesToRetain() {
then(environment.getProperty(EPP_VALUE)).isEqualTo("1");
then(environment.getProperty(TestEnvPostProcessor.EPP_VALUE)).isEqualTo("1");
// ...and then refresh, to see if property source is retained during refresh
// that means an updated test datasource with EPP_VALUE set to 10
TestConfigDataLocationResolver.count.set(10);
this.refresher.refresh();
then(environment.getProperty(EPP_VALUE)).isEqualTo("10");
then(environment.getProperty(TestEnvPostProcessor.EPP_VALUE)).isEqualTo("10");
}
@Test
@@ -112,6 +96,13 @@ public class ConfigDataContextRefresherIntegrationTests {
then(this.properties.getMessage()).isEqualTo("Hello scope!");
}
@Test
public void testVcapPlaceholderAfterRefresh() {
// an error will be thrown if count is 99 and myplaceholder contains ${vcap
TestConfigDataLocationResolver.count.set(99);
this.refresher.refresh();
}
@Test
public void testUpdateHikari() {
then(this.properties.getMessage()).isEqualTo("Hello scope!");
@@ -138,84 +129,6 @@ public class ConfigDataContextRefresherIntegrationTests {
then(bootstrapContext.isRegistered(MyTestBean.class)).isTrue();
}
protected static class TestEnvPostProcessor implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
if (environment.getProperty(EPP_ENABLED, Boolean.class, false)) {
Map<String, Object> source = new HashMap<>();
source.put("spring.cloud.refresh.additional-property-sources-to-retain", getClass().getSimpleName());
source.put("spring.config.import", "testdatasource:");
MapPropertySource propertySource = new MapPropertySource(getClass().getSimpleName(), source);
environment.getPropertySources().addFirst(propertySource);
}
}
@Override
public int getOrder() {
return ConfigDataEnvironmentPostProcessor.ORDER - 1;
}
}
protected static class TestConfigDataResource extends ConfigDataResource {
private final int count;
public TestConfigDataResource(int count) {
this.count = count;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestConfigDataResource that = (TestConfigDataResource) o;
return Objects.equals(this.count, that.count);
}
@Override
public int hashCode() {
return Objects.hash(this.count);
}
}
protected static class TestConfigDataLocationResolver
implements ConfigDataLocationResolver<TestConfigDataResource> {
static AtomicInteger count = new AtomicInteger(1);
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
return location.hasPrefix("testdatasource:");
}
@Override
public List<TestConfigDataResource> resolve(ConfigDataLocationResolverContext context,
ConfigDataLocation location)
throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException {
context.getBootstrapContext().registerIfAbsent(MyTestBean.class, InstanceSupplier.of(new MyTestBean()));
return Collections.singletonList(new TestConfigDataResource(count.get()));
}
}
protected static class TestConfigDataLoader implements ConfigDataLoader<TestConfigDataResource> {
@Override
public ConfigData load(ConfigDataLoaderContext context, TestConfigDataResource resource)
throws ConfigDataResourceNotFoundException {
Map<String, Object> stringStringMap = Collections.singletonMap(EPP_VALUE, resource.count);
return new ConfigData(
Collections.singletonList(new MapPropertySource("testconfigdatadatasource", stringStringMap)));
}
}
protected static class MyTestBean {
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2013-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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.test;
import java.util.Collections;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.core.env.MapPropertySource;
public class TestConfigDataLoader implements ConfigDataLoader<TestConfigDataResource> {
@Override
public ConfigData load(ConfigDataLoaderContext context, TestConfigDataResource resource)
throws ConfigDataResourceNotFoundException {
return new ConfigData(
Collections.singletonList(new MapPropertySource("testconfigdatadatasource", resource.props)));
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2013-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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.test;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.context.properties.bind.Bindable;
public class TestConfigDataLocationResolver implements ConfigDataLocationResolver<TestConfigDataResource> {
public static AtomicInteger count = new AtomicInteger(1);
public static Map<String, Object> config = new HashMap<>();
public static Object instance;
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
return location.hasPrefix("testdatasource:");
}
@Override
public List<TestConfigDataResource> resolve(ConfigDataLocationResolverContext context, ConfigDataLocation location)
throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException {
if (instance != null) {
BootstrapRegistry.InstanceSupplier<Object> supplier = BootstrapRegistry.InstanceSupplier.of(instance);
Class<Object> aClass = (Class<Object>) instance.getClass();
context.getBootstrapContext().registerIfAbsent(aClass, supplier);
}
String myplaceholder = context.getBinder().bind("myplaceholder", Bindable.of(String.class)).orElse("notfound");
HashMap<String, Object> props = new HashMap<>(config);
props.put(TestEnvPostProcessor.EPP_VALUE, count.get());
if (count.get() == 99 && myplaceholder.contains("${vcap")) {
throw new ConfigDataResourceNotFoundException(new TestConfigDataResource(props),
new IllegalArgumentException("placeholder not resolved"));
}
return Collections.singletonList(new TestConfigDataResource(props));
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2013-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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.test;
import java.util.HashMap;
import java.util.Objects;
import org.springframework.boot.context.config.ConfigDataResource;
public class TestConfigDataResource extends ConfigDataResource {
final HashMap<String, Object> props;
public TestConfigDataResource(HashMap<String, Object> props) {
this.props = props;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestConfigDataResource that = (TestConfigDataResource) o;
return Objects.equals(this.props, that.props);
}
@Override
public int hashCode() {
return Objects.hash(this.props);
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2013-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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.test;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
public class TestEnvPostProcessor implements EnvironmentPostProcessor, Ordered {
public static final String EPP_ENABLED = "configdatarefresh.epp.enabled";
public static final String EPP_VALUE = "configdatarefresh.epp.count";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
if (environment.getProperty(EPP_ENABLED, Boolean.class, false)) {
Map<String, Object> source = new HashMap<>();
source.put("spring.cloud.refresh.additional-property-sources-to-retain", getClass().getSimpleName());
source.put("spring.config.import", "testdatasource:");
MapPropertySource propertySource = new MapPropertySource(getClass().getSimpleName(), source);
environment.getPropertySources().addFirst(propertySource);
}
}
@Override
public int getOrder() {
return ConfigDataEnvironmentPostProcessor.ORDER - 1;
}
}