Browse Source

Use relaxed property resolver in KeyCondition

Allows users to define properties like encrypt.key-store.password=letmein

fixes gh-191
pull/497/head
Spencer Gibb 8 years ago
parent
commit
7744b639af
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 12
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java
  2. 14
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java

12
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -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 { @@ -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;
}

14
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java

@ -19,6 +19,20 @@ public class EncryptionBootstrapConfigurationTests { @@ -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();
}
}

Loading…
Cancel
Save