1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.brabenetz.lib.securedproperties;
21
22 import net.brabenetz.lib.securedproperties.core.Encryption;
23 import net.brabenetz.lib.securedproperties.core.SecretContainer;
24 import net.brabenetz.lib.securedproperties.core.SecretContainerStore;
25 import net.brabenetz.lib.securedproperties.utils.SecuredPropertiesUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.lang3.tuple.Pair;
28
29 import java.io.File;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Properties;
34 import java.util.stream.Collectors;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public final class SecuredProperties {
73
74 private SecuredProperties() {
75 super();
76 }
77
78
79
80
81
82
83
84
85
86 public static String getSecretValue(final SecuredPropertiesConfig config, final File propertyFile, final String key) {
87 return getSecretValues(config, propertyFile, key).get(key);
88 }
89
90
91
92
93
94
95
96
97 public static String getSecretValue(final SecuredPropertiesConfig config, final File[] propertyFiles, final String key) {
98 return getSecretValues(config, propertyFiles, key).get(key);
99 }
100
101
102
103
104
105
106
107
108 public static Map<String, String> getSecretValues(
109 final SecuredPropertiesConfig config, final File propertyFile, final String... keys) {
110 return getSecretValues(config, new File[] {propertyFile}, keys);
111 }
112
113
114
115
116
117
118
119
120 public static Map<String, String> getSecretValues(
121 final SecuredPropertiesConfig config, final File[] propertyFiles, final String... keys) {
122
123 Map<String, String> result = new HashMap<>();
124 final SecretContainer secretContainer = getSecretContainer(config);
125
126 for (File propertyFile : propertyFiles) {
127 if (!propertyFile.exists()) {
128 continue;
129 }
130 final Properties properties = SecuredPropertiesUtils.readProperties(propertyFile);
131 for (String key : keys) {
132
133 String value = properties.getProperty(key);
134 if (Encryption.isEncryptedValue(value)) {
135
136 result.put(key, Encryption.decrypt(secretContainer.getAlgorithm(), secretContainer.getSecretKey(), config.getSaltLength(), value));
137 } else {
138 result.put(key, value);
139 }
140 }
141
142 }
143
144 return result;
145
146 }
147
148
149
150
151
152
153
154 public static void encryptNonEncryptedValues(
155 final SecuredPropertiesConfig config, final File propertyFile, final String... keys) {
156 encryptNonEncryptedValues(config, new File[] {propertyFile}, keys);
157 }
158
159
160
161
162
163
164
165 public static void encryptNonEncryptedValues(
166 final SecuredPropertiesConfig config, final File[] propertyFiles, final String... keys) {
167
168 final SecretContainer secretContainer = getSecretContainer(config);
169 for (File propertyFile : propertyFiles) {
170 if (!propertyFile.exists()) {
171 continue;
172 }
173 Map<String, String> unencryptedValues = new HashMap<>();
174 final Properties properties = SecuredPropertiesUtils.readProperties(propertyFile);
175 for (String key : keys) {
176
177 String value = properties.getProperty(key);
178 if (!Encryption.isEncryptedValue(value) && StringUtils.isNotEmpty(value)) {
179
180 unencryptedValues.put(key, value);
181 }
182 }
183
184 if (!unencryptedValues.isEmpty()) {
185 Map<String, String> encryptedValues = encryptValues(config, secretContainer, unencryptedValues);
186 Pair<String, String>[] newProperties = encryptedValues.entrySet().stream()
187 .map(e -> Pair.of(e.getKey(), e.getValue()))
188 .collect(Collectors.toSet())
189 .toArray(new Pair[encryptedValues.size()]);
190 SecuredPropertiesUtils.replaceSecretValue(propertyFile, newProperties);
191 }
192 }
193 }
194
195 private static Map<String, String> encryptValues(final SecuredPropertiesConfig config, final SecretContainer secretContainer,
196 final Map<String, String> unencryptedValues) {
197
198 Map<String, String> encryptedValues = new HashMap<>();
199 for (Entry<String, String> entry : unencryptedValues.entrySet()) {
200 encryptedValues.put(entry.getKey(), Encryption.encrypt(
201 secretContainer.getAlgorithm(), secretContainer.getSecretKey(), config.getSaltLength(), entry.getValue()));
202 }
203 return encryptedValues;
204 }
205
206
207
208
209 public static boolean isEncryptedValue(final String maybeEncryptedValue) {
210 return Encryption.isEncryptedValue(maybeEncryptedValue);
211 }
212
213
214
215
216
217
218
219
220
221
222 public static String encrypt(final SecuredPropertiesConfig config, final String plainTextValue) {
223 final SecretContainer secretContainer = getSecretContainer(config);
224
225 return Encryption.encrypt(secretContainer.getAlgorithm(), secretContainer.getSecretKey(), config.getSaltLength(), plainTextValue);
226 }
227
228
229
230
231
232
233
234
235 public static String decrypt(final SecuredPropertiesConfig config, final String encryptedPassword) {
236 final SecretContainer secretContainer = getSecretContainer(config);
237
238 return Encryption.decrypt(secretContainer.getAlgorithm(), secretContainer.getSecretKey(), config.getSaltLength(), encryptedPassword);
239
240 }
241
242 private static SecretContainer getSecretContainer(final SecuredPropertiesConfig config) {
243
244 return SecretContainerStore.getSecretContainer(config.getSecretFile(), config.isAutoCreateSecretKey(), config.getAllowedAlgorithm());
245 }
246 }