Use value prefix instead of key suffix

This commit is contained in:
Dave Syer
2014-08-10 07:35:58 -07:00
parent 845852b25f
commit 64b6b8d8b1
2 changed files with 5 additions and 6 deletions

View File

@@ -209,22 +209,21 @@ public class EncryptionController {
source.getSource());
for (Object key : map.keySet()) {
String name = key.toString();
if (name.endsWith(".secret")) {
Object value = map.get(key);
String value = map.get(key).toString();
if (value.startsWith("{cipher}")) {
map.remove(key);
name = name.substring(0, name.length() - ".secret".length());
if (encryptor == null) {
map.put(name, value);
}
else {
try {
value = value == null ? null : encryptor.decrypt(value
.toString());
.substring("{cipher}".length()));
}
catch (Exception e) {
value = "<n/a>";
name = "invalid." + name;
logger.warn("Cannot decode key: " + key + " (" + e.getClass()
logger.warn("Cannot decrypt key: " + key + " (" + e.getClass()
+ ": " + e.getMessage() + ")");
}
map.put(name, value);

View File

@@ -82,7 +82,7 @@ public class EncryptionControllerTests {
String cipher = controller.encrypt("foo");
Environment environment = new Environment("foo", "bar");
environment.add(new PropertySource("spam", Collections
.<Object, Object> singletonMap("my.secret", cipher)));
.<Object, Object> singletonMap("my", "{cipher}" + cipher)));
Environment result = controller.decrypt(environment);
assertEquals("foo", result.getPropertySources().get(0).getSource().get("my"));
}