Use relaxed property resolver in KeyCondition
Allows users to define properties like encrypt.key-store.password=letmein fixes gh-191
This commit is contained in:
@@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||||
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.bootstrap.encrypt.KeyProperties.KeyStore;
|
import org.springframework.cloud.bootstrap.encrypt.KeyProperties.KeyStore;
|
||||||
import org.springframework.cloud.context.encrypt.EncryptorFactory;
|
import org.springframework.cloud.context.encrypt.EncryptorFactory;
|
||||||
@@ -109,21 +110,22 @@ public class EncryptionBootstrapConfiguration {
|
|||||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||||
AnnotatedTypeMetadata metadata) {
|
AnnotatedTypeMetadata metadata) {
|
||||||
Environment environment = context.getEnvironment();
|
Environment environment = context.getEnvironment();
|
||||||
if (hasProperty(environment, "encrypt.keyStore.location")) {
|
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment);
|
||||||
if (hasProperty(environment, "encrypt.keyStore.password")) {
|
if (hasProperty(propertyResolver, environment, "encrypt.keyStore.location")) {
|
||||||
|
if (hasProperty(propertyResolver, environment, "encrypt.keyStore.password")) {
|
||||||
return ConditionOutcome.match("Keystore found in Environment");
|
return ConditionOutcome.match("Keystore found in Environment");
|
||||||
}
|
}
|
||||||
return ConditionOutcome
|
return ConditionOutcome
|
||||||
.noMatch("Keystore found but no password in Environment");
|
.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.match("Key found in Environment");
|
||||||
}
|
}
|
||||||
return ConditionOutcome.noMatch("Keystore nor key found in Environment");
|
return ConditionOutcome.noMatch("Keystore nor key found in Environment");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasProperty(Environment environment, String key) {
|
private boolean hasProperty(RelaxedPropertyResolver propertyResolver, Environment environment, String key) {
|
||||||
String value = environment.getProperty(key);
|
String value = propertyResolver.getProperty(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,20 @@ public class EncryptionBootstrapConfigurationTests {
|
|||||||
.run();
|
.run();
|
||||||
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
|
TextEncryptor encryptor = context.getBean(TextEncryptor.class);
|
||||||
assertEquals("foo", encryptor.decrypt(encryptor.encrypt("foo")));
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user