Skip to content

Commit 78d10fc

Browse files
committed
Use relaxed property resolver in KeyCondition
Allows users to define properties like encrypt.key-store.password=letmein fixes spring-projectsgh-191 (cherry picked from commit 7744b63)
1 parent 61199a1 commit 78d10fc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
2323
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
24+
import org.springframework.boot.bind.RelaxedPropertyResolver;
2425
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2526
import org.springframework.cloud.bootstrap.encrypt.KeyProperties.KeyStore;
2627
import org.springframework.cloud.context.encrypt.EncryptorFactory;
@@ -109,21 +110,22 @@ public static class KeyCondition extends SpringBootCondition {
109110
public ConditionOutcome getMatchOutcome(ConditionContext context,
110111
AnnotatedTypeMetadata metadata) {
111112
Environment environment = context.getEnvironment();
112-
if (hasProperty(environment, "encrypt.keyStore.location")) {
113-
if (hasProperty(environment, "encrypt.keyStore.password")) {
113+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment);
114+
if (hasProperty(propertyResolver, environment, "encrypt.keyStore.location")) {
115+
if (hasProperty(propertyResolver, environment, "encrypt.keyStore.password")) {
114116
return ConditionOutcome.match("Keystore found in Environment");
115117
}
116118
return ConditionOutcome
117119
.noMatch("Keystore found but no password in Environment");
118120
}
119-
else if (hasProperty(environment, "encrypt.key")) {
121+
else if (hasProperty(propertyResolver, environment, "encrypt.key")) {
120122
return ConditionOutcome.match("Key found in Environment");
121123
}
122124
return ConditionOutcome.noMatch("Keystore nor key found in Environment");
123125
}
124126

125-
private boolean hasProperty(Environment environment, String key) {
126-
String value = environment.getProperty(key);
127+
private boolean hasProperty(RelaxedPropertyResolver propertyResolver, Environment environment, String key) {
128+
String value = propertyResolver.getProperty(key);
127129
if (value == null) {
128130
return false;
129131
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public void rsaKeyStore() {
1919
.run();
2020
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
2121
assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo")));
22+
context.close();
23+
}
24+
25+
@Test
26+
public void rsaKeyStoreWithRelaxedProperties() {
27+
ConfigurableApplicationContext context = new SpringApplicationBuilder(
28+
EncryptionBootstrapConfiguration.class).web(false).properties(
29+
"encrypt.key-store.location:classpath:/server.jks",
30+
"encrypt.key-store.password:letmein",
31+
"encrypt.key-store.alias:mytestkey", "encrypt.key-store.secret:changeme")
32+
.run();
33+
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
34+
assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo")));
35+
context.close();
2236
}
2337

2438
}

0 commit comments

Comments
 (0)