Skip to content

Commit f77a6ec

Browse files
committed
Creates DecryptEnvironmentPostProcessor.
This is used if bootstrap and legacy processing are not enabled. EnvironmentDecryptApplicationInitializer is only is if bootstrap and legacy processing are enabled. Fixes gh-815
1 parent 7211cdd commit f77a6ec

13 files changed

+510
-523
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/EncryptionBootstrapAutoConfiguration.java

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2013-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.bootstrap;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
22+
import org.springframework.boot.context.properties.bind.AbstractBindHandler;
23+
import org.springframework.boot.context.properties.bind.BindContext;
24+
import org.springframework.boot.context.properties.bind.Bindable;
25+
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
26+
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
27+
import org.springframework.security.crypto.encrypt.TextEncryptor;
28+
29+
/**
30+
* BindHandler that uses a TextEncryptor to decrypt text if properly prefixed with
31+
* {cipher}.
32+
*
33+
* @author Marcin Grzejszczak
34+
* @since 3.0.0
35+
*/
36+
class TextEncryptorBindHandler extends AbstractBindHandler {
37+
38+
private static final Log logger = LogFactory.getLog(TextEncryptorBindHandler.class);
39+
40+
/**
41+
* Prefix indicating an encrypted value.
42+
*/
43+
protected static final String ENCRYPTED_PROPERTY_PREFIX = "{cipher}";
44+
45+
private final TextEncryptor textEncryptor;
46+
47+
private final KeyProperties keyProperties;
48+
49+
TextEncryptorBindHandler(TextEncryptor textEncryptor, KeyProperties keyProperties) {
50+
this.textEncryptor = textEncryptor;
51+
this.keyProperties = keyProperties;
52+
}
53+
54+
@Override
55+
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
56+
if (result instanceof String && ((String) result).startsWith(ENCRYPTED_PROPERTY_PREFIX)) {
57+
return decrypt(name.toString(), (String) result);
58+
}
59+
return result;
60+
}
61+
62+
private String decrypt(String key, String original) {
63+
String value = original.substring(ENCRYPTED_PROPERTY_PREFIX.length());
64+
try {
65+
value = this.textEncryptor.decrypt(value);
66+
if (logger.isDebugEnabled()) {
67+
logger.debug("Decrypted: key=" + key);
68+
}
69+
return value;
70+
}
71+
catch (Exception e) {
72+
String message = "Cannot decrypt: key=" + key;
73+
if (logger.isDebugEnabled()) {
74+
logger.warn(message, e);
75+
}
76+
else {
77+
logger.warn(message);
78+
}
79+
if (this.keyProperties.isFailOnError()) {
80+
throw new IllegalStateException(message, e);
81+
}
82+
return "";
83+
}
84+
}
85+
86+
}

0 commit comments

Comments
 (0)