Decrypt after config data refresh
Fixes gh-962 Fixes gh-955
This commit is contained in:
committed by
spencergibb
parent
fd079e4ec9
commit
938fb861d0
@@ -20,6 +20,7 @@ import org.springframework.boot.ConfigurableBootstrapContext;
|
||||
import org.springframework.boot.DefaultBootstrapContext;
|
||||
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
|
||||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||
import org.springframework.cloud.bootstrap.encrypt.DecryptEnvironmentPostProcessor;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
@@ -33,6 +34,8 @@ import org.springframework.core.io.DefaultResourceLoader;
|
||||
*/
|
||||
public class ConfigDataContextRefresher extends ContextRefresher {
|
||||
|
||||
private final DecryptEnvironmentPostProcessor decryptEnvironmentPostProcessor = new DecryptEnvironmentPostProcessor();
|
||||
|
||||
@Deprecated
|
||||
public ConfigDataContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
|
||||
super(context, scope);
|
||||
@@ -83,6 +86,8 @@ public class ConfigDataContextRefresher extends ContextRefresher {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decryptEnvironmentPostProcessor.postProcessEnvironment(getContext().getEnvironment(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,12 @@ 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.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.security.crypto.encrypt.TextEncryptor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
@@ -118,6 +122,24 @@ public class EncryptionIntegrationTests {
|
||||
then(context.getBean(PasswordProperties.class).getPassword()).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decryptAfterRefresh() {
|
||||
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestAutoConfiguration.class)
|
||||
.web(WebApplicationType.NONE)
|
||||
.properties("encrypt.key:pie",
|
||||
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95",
|
||||
"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);
|
||||
refresher.refresh();
|
||||
then(env.getProperty("foo.password")).isEqualTo("newValue");
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(PasswordProperties.class)
|
||||
protected static class TestConfiguration {
|
||||
|
||||
Reference in New Issue
Block a user