1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.property.processor;
17
18 import java.util.List;
19 import java.util.Properties;
20
21 import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
22 import org.jasypt.util.text.TextEncryptor;
23 import org.kuali.common.util.Mode;
24 import org.kuali.common.util.PropertyUtils;
25 import org.kuali.common.util.property.Constants;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class EndsWithDecryptProcessor extends DecryptProcessor {
30
31 private static final Logger logger = LoggerFactory.getLogger(EndsWithDecryptProcessor.class);
32
33 String suffix = Constants.DEFAULT_ENCRYPTED_SUFFIX;
34 boolean removeEncryptedProperties = true;
35 Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
36
37 public EndsWithDecryptProcessor() {
38 this(null);
39 }
40
41 public EndsWithDecryptProcessor(TextEncryptor encryptor) {
42 super(encryptor);
43 }
44
45 @Override
46 public void process(Properties properties) {
47 List<String> keys = PropertyUtils.getEndsWithKeys(properties, suffix);
48 logger.info("Decrypting {} property values", keys.size());
49 for (String key : keys) {
50 logger.debug("Decrypting [{}]", key);
51 String encryptedValue = properties.getProperty(key);
52 String decryptedValue = decrypt(key, encryptedValue, encryptor);
53 int endIndex = key.length() - suffix.length();
54 String newKey = key.substring(0, endIndex);
55 PropertyUtils.addOrOverrideProperty(properties, newKey, decryptedValue, propertyOverwriteMode);
56 if (removeEncryptedProperties) {
57 logger.debug("Removing {}", key);
58 properties.remove(key);
59 }
60 }
61 }
62
63 protected String decrypt(String key, String encryptedValue, TextEncryptor encryptor) {
64 try {
65 return encryptor.decrypt(encryptedValue);
66 } catch (EncryptionOperationNotPossibleException e) {
67 throw new IllegalStateException("Unexpected error decrypting [" + key + "]=[" + encryptedValue + "]");
68 }
69 }
70
71 public String getSuffix() {
72 return suffix;
73 }
74
75 public void setSuffix(String suffix) {
76 this.suffix = suffix;
77 }
78
79 public boolean isRemoveEncryptedProperties() {
80 return removeEncryptedProperties;
81 }
82
83 public void setRemoveEncryptedProperties(boolean removeEncryptedProperties) {
84 this.removeEncryptedProperties = removeEncryptedProperties;
85 }
86
87 public Mode getPropertyOverwriteMode() {
88 return propertyOverwriteMode;
89 }
90
91 public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
92 this.propertyOverwriteMode = propertyOverwriteMode;
93 }
94
95 }