From 7744b639aff84a753b524c1321629bfe96539f6b Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 3 Apr 2017 19:49:39 -0600 Subject: [PATCH] Use relaxed property resolver in KeyCondition Allows users to define properties like encrypt.key-store.password=letmein fixes gh-191 --- .../encrypt/EncryptionBootstrapConfiguration.java | 12 +++++++----- .../EncryptionBootstrapConfigurationTests.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java index 5996f263..17b53742 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.bootstrap.encrypt.KeyProperties.KeyStore; import org.springframework.cloud.context.encrypt.EncryptorFactory; @@ -109,21 +110,22 @@ public class EncryptionBootstrapConfiguration { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); - if (hasProperty(environment, "encrypt.keyStore.location")) { - if (hasProperty(environment, "encrypt.keyStore.password")) { + RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment); + if (hasProperty(propertyResolver, environment, "encrypt.keyStore.location")) { + if (hasProperty(propertyResolver, environment, "encrypt.keyStore.password")) { return ConditionOutcome.match("Keystore found in Environment"); } return ConditionOutcome .noMatch("Keystore found but no password in Environment"); } - else if (hasProperty(environment, "encrypt.key")) { + else if (hasProperty(propertyResolver, environment, "encrypt.key")) { return ConditionOutcome.match("Key found in Environment"); } return ConditionOutcome.noMatch("Keystore nor key found in Environment"); } - private boolean hasProperty(Environment environment, String key) { - String value = environment.getProperty(key); + private boolean hasProperty(RelaxedPropertyResolver propertyResolver, Environment environment, String key) { + String value = propertyResolver.getProperty(key); if (value == null) { return false; } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java index 52c654ba..91d67cdc 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java @@ -19,6 +19,20 @@ public class EncryptionBootstrapConfigurationTests { .run(); TextEncryptor encryptor = context.getBean(TextEncryptor.class); assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo"))); + context.close(); + } + + @Test + public void rsaKeyStoreWithRelaxedProperties() { + ConfigurableApplicationContext context = new SpringApplicationBuilder( + EncryptionBootstrapConfiguration.class).web(false).properties( + "encrypt.key-store.location:classpath:/server.jks", + "encrypt.key-store.password:letmein", + "encrypt.key-store.alias:mytestkey", "encrypt.key-store.secret:changeme") + .run(); + TextEncryptor encryptor = context.getBean(TextEncryptor.class); + assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo"))); + context.close(); } }