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.config.Config;
23 import net.brabenetz.lib.securedproperties.config.ConfigInitializer;
24 import net.brabenetz.lib.securedproperties.config.ConfigInitializers;
25 import net.brabenetz.lib.securedproperties.core.Algorithm;
26 import net.brabenetz.lib.securedproperties.core.SupportedAlgorithm;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.apache.commons.lang3.SystemUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.io.File;
33 import java.util.Arrays;
34
35
36
37
38 public class SecuredPropertiesConfig implements Config {
39
40 private static final Logger LOG = LoggerFactory.getLogger(SecuredPropertiesConfig.class);
41
42 private static final int DEFAULT_SALT_LENGTH = 11;
43
44
45 private File secretFile;
46
47 private int saltLength = DEFAULT_SALT_LENGTH;
48
49 private Algorithm[] allowedAlgorithm = new Algorithm[] {
50 SupportedAlgorithm.AES_256,
51 SupportedAlgorithm.AES_192,
52 SupportedAlgorithm.AES_128,
53 SupportedAlgorithm.DESede_168,
54 SupportedAlgorithm.DESede_112
55 };
56
57
58 private boolean autoCreateSecretKey = true;
59
60
61
62
63
64
65 public File getSecretFile() {
66 if (secretFile == null) {
67 final String secretFilePath = SystemUtils.USER_HOME + "/.secret/securedProperties.key";
68 LOG.debug("No secretFilePath configured. Use default location: {}", secretFilePath);
69 secretFile = new File(secretFilePath);
70 }
71 return secretFile;
72 }
73
74 public int getSaltLength() {
75 return saltLength;
76 }
77
78 public boolean isAutoCreateSecretKey() {
79 return autoCreateSecretKey;
80 }
81
82 public Algorithm[] getAllowedAlgorithm() {
83 return allowedAlgorithm;
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public SecuredPropertiesConfig initDefault() {
108 return init(ConfigInitializers.propertyFile(new File("./application.properties")),
109 ConfigInitializers.propertyFile(new File("./config/application.properties")),
110 ConfigInitializers.envProperties(),
111 ConfigInitializers.systemProperties());
112 }
113
114
115
116
117
118
119
120
121
122 public SecuredPropertiesConfig init(final ConfigInitializer... configInitializers) {
123 Arrays.asList(configInitializers).forEach(configInit -> configInit.init(this));
124 return this;
125 }
126
127
128
129
130
131
132
133 @Override
134 public SecuredPropertiesConfig withSecretFile(final File newSecretFile) {
135 secretFile = newSecretFile;
136 return this;
137 }
138
139 @Override
140 public SecuredPropertiesConfig withSaltLength(final int newSaltLength) {
141 saltLength = newSaltLength;
142 return this;
143 }
144
145 @Override
146 public SecuredPropertiesConfig withAllowedAlgorithm(final Algorithm... newAllowedAlgorithm) {
147 allowedAlgorithm = newAllowedAlgorithm;
148 return this;
149 }
150
151 public SecuredPropertiesConfig addAllowedAlgorithm(final Algorithm... addedAllowedAlgorithm) {
152 allowedAlgorithm = ArrayUtils.addAll(allowedAlgorithm, addedAllowedAlgorithm);
153 return this;
154 }
155
156 @Override
157 public SecuredPropertiesConfig withAutoCreateSecretKey(final boolean autoCreate) {
158 autoCreateSecretKey = autoCreate;
159 return this;
160 }
161
162 }