Add failing test for decrypting and rebinding

See gh-269
This commit is contained in:
Dave Syer
2017-10-31 08:51:16 +00:00
parent 8f9dfd38e3
commit fb36b67913
4 changed files with 76 additions and 20 deletions

View File

@@ -25,6 +25,7 @@ import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.bootstrap.BootstrapApplicationListener;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
@@ -34,10 +35,10 @@ import org.springframework.core.Ordered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.security.crypto.encrypt.TextEncryptor;
/**
@@ -97,7 +98,7 @@ public class EnvironmentDecryptApplicationInitializer implements
// We have some decrypted properties
found.addAll(map.keySet());
insert(applicationContext,
new SystemEnvironmentPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map));
new MapPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map));
}
PropertySource<?> bootstrap = propertySources
.get(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME);
@@ -105,7 +106,7 @@ public class EnvironmentDecryptApplicationInitializer implements
map = decrypt(bootstrap);
if (!map.isEmpty()) {
found.addAll(map.keySet());
insert(applicationContext, new SystemEnvironmentPropertySource(
insert(applicationContext, new MapPropertySource(
DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME, map));
}
}
@@ -122,7 +123,7 @@ public class EnvironmentDecryptApplicationInitializer implements
}
private void insert(ApplicationContext applicationContext,
SystemEnvironmentPropertySource propertySource) {
MapPropertySource propertySource) {
ApplicationContext parent = applicationContext;
while (parent != null) {
if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
@@ -135,7 +136,7 @@ public class EnvironmentDecryptApplicationInitializer implements
}
private void insert(MutablePropertySources propertySources,
SystemEnvironmentPropertySource propertySource) {
MapPropertySource propertySource) {
if (propertySources
.contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME
@@ -214,8 +215,10 @@ public class EnvironmentDecryptApplicationInitializer implements
if (COLLECTION_PROPERTY.matcher(key).matches()) {
sourceHasDecryptedCollection = true;
}
} else if (COLLECTION_PROPERTY.matcher(key).matches()){
// put non-ecrypted properties so merging of index properties happens correctly
}
else if (COLLECTION_PROPERTY.matcher(key).matches()) {
// put non-ecrypted properties so merging of index properties
// happens correctly
otherCollectionProperties.put(key, value);
}
}

View File

@@ -18,7 +18,6 @@ import java.io.Serializable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -144,16 +143,6 @@ public class RefreshScope extends GenericScope
@ManagedOperation(description = "Dispose of the current instance of all beans in this scope and force a refresh on next method execution.")
public void refreshAll() {
super.destroy();
if (this.context != null && this.context.getBeanNamesForType(
ConfigurationPropertiesBindingPostProcessor.class).length == 1) {
try {
ConfigurationPropertiesBindingPostProcessor processor = context
.getBean(ConfigurationPropertiesBindingPostProcessor.class);
processor.afterPropertiesSet();
}
catch (Exception e) {
}
}
this.context.publishEvent(new RefreshScopeRefreshedEvent());
}

View File

@@ -12,6 +12,16 @@ import static org.junit.Assert.assertEquals;
public class EncryptionBootstrapConfigurationTests {
@Test
public void symmetric() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
EncryptionBootstrapConfiguration.class).web(WebApplicationType.NONE)
.properties("encrypt.key:pie").run();
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo")));
context.close();
}
@Test
public void rsaKeyStore() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
@@ -52,8 +62,8 @@ public class EncryptionBootstrapConfigurationTests {
"encrypt.key-store.alias:mytestkey",
"encrypt.key-store.secret:changeme")
.run();
assertThat(false)
.as("Should not create an application context with invalid keystore location")
assertThat(false).as(
"Should not create an application context with invalid keystore location")
.isTrue();
}
catch (Exception e) {

View File

@@ -0,0 +1,54 @@
package org.springframework.cloud.bootstrap.encrypt;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
public class EncryptionIntegrationTests {
@Test
public void symmetricPropertyValues() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:pie",
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95")
.run();
assertEquals("test", context.getEnvironment().getProperty("foo.password"));
}
@Test
public void symmetricConfigurationProperties() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:pie",
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95")
.run();
assertEquals("test", context.getBean(PasswordProperties.class).getPassword());
}
@Configuration
@EnableConfigurationProperties(PasswordProperties.class)
protected static class TestConfiguration {
}
@ConfigurationProperties("foo")
protected static class PasswordProperties {
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}