Merge remote-tracking branch 'origin/2.0.x'

This commit is contained in:
Ryan Baxter
2019-02-14 18:11:00 -05:00
2 changed files with 168 additions and 52 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.bootstrap.encrypt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -173,34 +172,20 @@ public class EnvironmentDecryptApplicationInitializer implements
sources.add(0, source);
}
for (PropertySource<?> source : sources) {
collectEncryptedProperties(source, overrides);
decrypt(source, overrides);
}
doDecrypt(overrides);
return overrides;
}
private Map<String, Object> decrypt(PropertySource<?> source) {
Map<String, Object> overrides = new LinkedHashMap<>();
collectEncryptedProperties(source, overrides);
doDecrypt(overrides);
decrypt(source, overrides);
return overrides;
}
private void collectEncryptedProperties(PropertySource<?> source,
Map<String, Object> overrides) {
private void decrypt(PropertySource<?> source, Map<String, Object> overrides) {
if (source instanceof CompositePropertySource) {
List<PropertySource<?>> propertySources = new ArrayList<>(
((CompositePropertySource) source).getPropertySources());
Collections.reverse(propertySources);
for (PropertySource<?> nested : propertySources) {
collectEncryptedProperties(nested, overrides);
}
}
else if (source instanceof EnumerablePropertySource) {
if (source instanceof EnumerablePropertySource) {
Map<String, Object> otherCollectionProperties = new LinkedHashMap<>();
boolean sourceHasDecryptedCollection = false;
@@ -210,19 +195,38 @@ public class EnvironmentDecryptApplicationInitializer implements
if (property != null) {
String value = property.toString();
if (value.startsWith("{cipher}")) {
value = value.substring("{cipher}".length());
try {
value = this.encryptor.decrypt(value);
if (logger.isDebugEnabled()) {
logger.debug("Decrypted: key=" + key);
}
}
catch (Exception e) {
String message = "Cannot decrypt: key=" + key;
if (this.failOnError) {
throw new IllegalStateException(message, e);
}
if (logger.isDebugEnabled()) {
logger.warn(message, e);
}
else {
logger.warn(message);
}
// Set value to empty to avoid making a password out of the
// cipher text
value = "";
}
overrides.put(key, value);
if (COLLECTION_PROPERTY.matcher(key).matches()) {
sourceHasDecryptedCollection = true;
}
}
else if (COLLECTION_PROPERTY.matcher(key).matches()) {
// put non-encrypted properties so merging of index properties
// put non-ecrypted properties so merging of index properties
// happens correctly
otherCollectionProperties.put(key, value);
}
else {
overrides.remove(key);
}
}
}
// copy all indexed properties even if not encrypted
@@ -231,37 +235,15 @@ public class EnvironmentDecryptApplicationInitializer implements
}
}
}
else if (source instanceof CompositePropertySource) {
private void doDecrypt(Map<String, Object> overrides) {
for (String key : overrides.keySet()) {
String value = overrides.get(key).toString();
if (value.startsWith("{cipher}")) {
value = value.substring("{cipher}".length());
try {
value = this.encryptor.decrypt(value);
if (logger.isDebugEnabled()) {
logger.debug("Decrypted: key=" + key);
}
}
catch (Exception e) {
String message = "Cannot decrypt: key=" + key;
if (this.failOnError) {
throw new IllegalStateException(message, e);
}
if (logger.isDebugEnabled()) {
logger.warn(message, e);
}
else {
logger.warn(message);
}
// Set value to empty to avoid making a password out of the
// cipher text
value = "";
}
overrides.put(key, value);
for (PropertySource<?> nested : ((CompositePropertySource) source)
.getPropertySources()) {
decrypt(nested, overrides);
}
}
}
}