Browse Source

Fix property source for decrypted variables

Use SystemEnvironmentPropertySource so that env vars can be
encrypted as well as property values (i.e. FOO_BAR will bind to
@Value("foo.bar")).

Fixes gh-89, fixes gh-87
pull/94/head
tc 9 years ago committed by Dave Syer
parent
commit
bcb4e60448
  1. 10
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java
  2. 10
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java

10
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java

@ -33,10 +33,10 @@ import org.springframework.core.Ordered; @@ -33,10 +33,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;
/**
@ -96,7 +96,7 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -96,7 +96,7 @@ public class EnvironmentDecryptApplicationInitializer implements
// We have some decrypted properties
found.addAll(map.keySet());
insert(applicationContext,
new MapPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map));
new SystemEnvironmentPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, map));
}
PropertySource<?> bootstrap = propertySources
.get(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME);
@ -104,7 +104,7 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -104,7 +104,7 @@ public class EnvironmentDecryptApplicationInitializer implements
map = decrypt(bootstrap);
if (!map.isEmpty()) {
found.addAll(map.keySet());
insert(applicationContext, new MapPropertySource(
insert(applicationContext, new SystemEnvironmentPropertySource(
DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME, map));
}
}
@ -121,7 +121,7 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -121,7 +121,7 @@ public class EnvironmentDecryptApplicationInitializer implements
}
private void insert(ApplicationContext applicationContext,
MapPropertySource propertySource) {
SystemEnvironmentPropertySource propertySource) {
ApplicationContext parent = applicationContext;
while (parent != null) {
if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
@ -134,7 +134,7 @@ public class EnvironmentDecryptApplicationInitializer implements @@ -134,7 +134,7 @@ public class EnvironmentDecryptApplicationInitializer implements
}
private void insert(MutablePropertySources propertySources,
MapPropertySource propertySource) {
SystemEnvironmentPropertySource propertySource) {
if (propertySources
.contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME

10
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationListenerTests.java → spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java

@ -30,7 +30,7 @@ import static org.junit.Assert.assertEquals; @@ -30,7 +30,7 @@ import static org.junit.Assert.assertEquals;
* @author Dave Syer
*
*/
public class EnvironmentDecryptApplicationListenerTests {
public class EnvironmentDecryptApplicationInitializerTests {
private EnvironmentDecryptApplicationInitializer listener = new EnvironmentDecryptApplicationInitializer(
Encryptors.noOpText());
@ -43,6 +43,14 @@ public class EnvironmentDecryptApplicationListenerTests { @@ -43,6 +43,14 @@ public class EnvironmentDecryptApplicationListenerTests {
assertEquals("bar", context.getEnvironment().getProperty("foo"));
}
@Test
public void relaxedBinding() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "FOO_TEXT: {cipher}bar");
this.listener.initialize(context);
assertEquals("bar", context.getEnvironment().getProperty("foo.text"));
}
@Test
public void propertySourcesOrderedCorrectly() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
Loading…
Cancel
Save