From bcb4e60448bedf37c068f24a05cd365a450e32a4 Mon Sep 17 00:00:00 2001 From: tc Date: Thu, 4 Feb 2016 15:25:24 -0200 Subject: [PATCH] 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 --- .../EnvironmentDecryptApplicationInitializer.java | 10 +++++----- ...EnvironmentDecryptApplicationInitializerTests.java} | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) rename spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/{EnvironmentDecryptApplicationListenerTests.java => EnvironmentDecryptApplicationInitializerTests.java} (89%) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java index 4930d25b..17788e42 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializer.java @@ -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 // 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 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 } 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 } private void insert(MutablePropertySources propertySources, - MapPropertySource propertySource) { + SystemEnvironmentPropertySource propertySource) { if (propertySources .contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) { if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationListenerTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java similarity index 89% rename from spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationListenerTests.java rename to spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java index 4e6ef446..208203fc 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationListenerTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EnvironmentDecryptApplicationInitializerTests.java @@ -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 { 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();