View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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    * http://www.opensource.org/licenses/ecl2.php
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  package org.kuali.common.util.property;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.Properties;
22  import java.util.Random;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.jasypt.util.text.TextEncryptor;
26  import org.kuali.common.util.EncUtils;
27  import org.kuali.common.util.EncryptionMode;
28  import org.kuali.common.util.EncryptionStrength;
29  import org.kuali.common.util.PropertyUtils;
30  import org.kuali.common.util.Str;
31  import org.kuali.common.util.property.processor.AddPrefixProcessor;
32  import org.kuali.common.util.property.processor.EndsWithDecryptProcessor;
33  import org.kuali.common.util.property.processor.EndsWithEncryptProcessor;
34  import org.kuali.common.util.property.processor.GlobalOverrideProcessor;
35  import org.kuali.common.util.property.processor.PropertyProcessor;
36  import org.kuali.common.util.property.processor.ReformatKeysAsEnvVarsProcessor;
37  import org.kuali.common.util.property.processor.ResolvePlaceholdersProcessor;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.util.Assert;
41  import org.springframework.util.PropertyPlaceholderHelper;
42  
43  public class DefaultPropertyContext implements PropertyContext {
44  
45  	private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyContext.class);
46  	private static final Random RANDOM = new Random(System.currentTimeMillis());
47  
48  	PropertyPlaceholderHelper helper = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
49  	String globalPropertiesMode = Constants.DEFAULT_GLOBAL_PROPERTIES_MODE.name();
50  	String resolvePlaceholders = Boolean.toString(Constants.DEFAULT_RESOLVE_PLACEHOLDERS);
51  	String style = PropertyStyle.NORMAL.name();
52  	String encryptionMode = EncryptionMode.NONE.name();
53  	String encryptionStrength = EncryptionStrength.BASIC.name();
54  	String encryptionPassword;
55  	String prefix;
56  	List<PropertyProcessor> processors;
57  	Properties properties;
58  
59  	protected List<PropertyProcessor> getDefaultProcessors() {
60  		List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
61  
62  		// Decrypt/encrypt as appropriate
63  		if (encryptionMode != null) {
64  			EncryptionMode mode = EncryptionMode.valueOf(encryptionMode);
65  			EncryptionStrength strength = EncryptionStrength.valueOf(encryptionStrength);
66  			processors.add(getEncProcessor(mode, strength, encryptionPassword));
67  		}
68  
69  		/**
70  		 * Remove the local reference to the encryption password now that the TextEncryptor has been created.<br>
71  		 * Note that the encryption password is VERY likely to be hanging around in memory even after being set to null locally.<br>
72  		 * Setting it to null here just makes it slightly tougher for someone to obtain the password.<br>
73  		 * Having a reference to this bean no longer does them any good, they'll have to search around in memory to find it.<br>
74  		 */
75  		this.encryptionPassword = null;
76  
77  		GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
78  
79  		// By default, system/environment properties override loaded properties
80  		processors.add(new GlobalOverrideProcessor(gpm));
81  
82  		// By default, all placeholders in the properties are resolved
83  		if (Boolean.parseBoolean(resolvePlaceholders)) {
84  			processors.add(new ResolvePlaceholdersProcessor(helper, gpm));
85  		}
86  
87  		// Add a prefix to the property keys if appropriate
88  		if (!StringUtils.isBlank(prefix)) {
89  			processors.add(new AddPrefixProcessor(prefix));
90  		}
91  
92  		// Reformat the keys in environment variable format if appropriate
93  		if (style != null) {
94  			processors.add(getStyleProcessor(style));
95  		}
96  
97  		// Return the list of processors
98  		return processors;
99  	}
100 
101 	protected PropertyProcessor getStyleProcessor(String style) {
102 		switch (PropertyStyle.valueOf(style)) {
103 		case NORMAL:
104 			return Constants.NO_OP_PROCESSOR;
105 		case ENVIRONMENT_VARIABLE:
106 			return new ReformatKeysAsEnvVarsProcessor();
107 		default:
108 			throw new IllegalArgumentException("Property style " + style + " is unknown");
109 		}
110 	}
111 
112 	protected PropertyProcessor getEncProcessor(EncryptionMode mode, EncryptionStrength strength, String password) {
113 		switch (mode) {
114 		case NONE:
115 			return Constants.NO_OP_PROCESSOR;
116 		case ENCRYPT:
117 			TextEncryptor encryptor = EncUtils.getTextEncryptor(strength, password);
118 			return new EndsWithEncryptProcessor(encryptor);
119 		case DECRYPT:
120 			TextEncryptor decryptor = EncUtils.getTextEncryptor(strength, password);
121 			return new EndsWithDecryptProcessor(decryptor);
122 		default:
123 			throw new IllegalArgumentException("Encryption mode '" + mode + "' is unknown");
124 		}
125 	}
126 
127 	protected void log() {
128 		if (!StringUtils.equals(EncryptionMode.NONE.name(), encryptionMode)) {
129 			logger.info("Encryption mode - " + StringUtils.trimToEmpty(encryptionMode));
130 			logger.info("Encryption strength - " + StringUtils.trimToEmpty(encryptionStrength));
131 			String displayPassword = null;
132 			if (!StringUtils.isBlank(encryptionPassword)) {
133 				displayPassword = StringUtils.repeat("*", Math.max(RANDOM.nextInt(16), 8));
134 			}
135 			logger.info("Encryption password - " + StringUtils.trimToEmpty(displayPassword));
136 		}
137 		if (!StringUtils.equals(PropertyStyle.NORMAL.name(), style)) {
138 			logger.info("Property style - " + StringUtils.trimToEmpty(style));
139 		}
140 		if (!StringUtils.isEmpty(prefix)) {
141 			logger.info("Property prefix - " + StringUtils.trimToEmpty(prefix));
142 		}
143 		if (!StringUtils.equals(Boolean.toString(Constants.DEFAULT_RESOLVE_PLACEHOLDERS), resolvePlaceholders)) {
144 			logger.info("Resolve placeholders - " + resolvePlaceholders);
145 		}
146 	}
147 
148 	@Override
149 	public void initialize(Properties properties) {
150 		GlobalPropertiesMode gpm = GlobalPropertiesMode.valueOf(globalPropertiesMode);
151 		Properties global = PropertyUtils.getProperties(properties, gpm);
152 		this.encryptionMode = resolve(encryptionMode, global);
153 		this.encryptionPassword = resolveAndRemove(encryptionPassword, global, properties);
154 		this.encryptionStrength = resolve(encryptionStrength, global);
155 		this.style = resolve(style, global);
156 		this.prefix = resolve(prefix, global);
157 		this.resolvePlaceholders = resolve(resolvePlaceholders, global);
158 		log();
159 		validate();
160 		addProcessors();
161 		logger.info("Proceeding with " + processors.size() + " processors.");
162 	}
163 
164 	protected void addProcessors() {
165 		List<PropertyProcessor> defaultProcessors = getDefaultProcessors();
166 		if (processors == null) {
167 			processors = defaultProcessors;
168 		} else {
169 			processors.addAll(0, defaultProcessors);
170 		}
171 	}
172 
173 	protected void validate() {
174 		EncryptionMode.valueOf(encryptionMode);
175 		EncryptionStrength.valueOf(encryptionStrength);
176 		PropertyStyle.valueOf(style);
177 		Boolean.parseBoolean(resolvePlaceholders);
178 	}
179 
180 	protected String getPlaceholderKey(String string) {
181 		String prefix = Constants.DEFAULT_PLACEHOLDER_PREFIX;
182 		String suffix = Constants.DEFAULT_PLACEHOLDER_SUFFIX;
183 		String separator = Constants.DEFAULT_VALUE_SEPARATOR;
184 		String key = StringUtils.substringBetween(string, prefix, separator);
185 		if (key == null) {
186 			return StringUtils.substringBetween(string, prefix, suffix);
187 		} else {
188 			return key;
189 		}
190 	}
191 
192 	protected void remove(String string, String resolvedString, Properties properties) {
193 		boolean placeholder = PropertyUtils.isSingleUnresolvedPlaceholder(string);
194 		boolean resolved = !StringUtils.equals(string, resolvedString);
195 		boolean irrelevant = Str.contains(Arrays.asList(Constants.NONE, Constants.NULL), resolvedString, false);
196 		boolean remove = placeholder && resolved && !irrelevant;
197 		if (remove) {
198 			String key = getPlaceholderKey(string);
199 			Assert.notNull(key, "key is null");
200 			if (properties.getProperty(key) != null) {
201 				logger.info("Removing property '" + key + "'");
202 				properties.remove(key);
203 			}
204 		}
205 	}
206 
207 	protected String resolveAndRemove(String string, Properties global, Properties properties) {
208 		String resolvedString = resolve(string, global);
209 		remove(string, resolvedString, properties);
210 		return resolvedString;
211 	}
212 
213 	protected String resolve(String string, Properties properties) {
214 		if (string == null) {
215 			return null;
216 		} else {
217 			return helper.replacePlaceholders(string, properties);
218 		}
219 	}
220 
221 	public String getPrefix() {
222 		return prefix;
223 	}
224 
225 	public void setPrefix(String prefix) {
226 		this.prefix = prefix;
227 	}
228 
229 	public String getStyle() {
230 		return style;
231 	}
232 
233 	public void setStyle(String style) {
234 		this.style = style;
235 	}
236 
237 	public PropertyPlaceholderHelper getHelper() {
238 		return helper;
239 	}
240 
241 	public void setHelper(PropertyPlaceholderHelper helper) {
242 		this.helper = helper;
243 	}
244 
245 	public String getEncryptionMode() {
246 		return encryptionMode;
247 	}
248 
249 	public void setEncryptionMode(String encryptionMode) {
250 		this.encryptionMode = encryptionMode;
251 	}
252 
253 	public String getEncryptionStrength() {
254 		return encryptionStrength;
255 	}
256 
257 	public void setEncryptionStrength(String encryptionStrength) {
258 		this.encryptionStrength = encryptionStrength;
259 	}
260 
261 	public String getEncryptionPassword() {
262 		return encryptionPassword;
263 	}
264 
265 	public void setEncryptionPassword(String encryptionPassword) {
266 		this.encryptionPassword = encryptionPassword;
267 	}
268 
269 	@Override
270 	public List<PropertyProcessor> getProcessors() {
271 		return processors;
272 	}
273 
274 	public void setProcessors(List<PropertyProcessor> processors) {
275 		this.processors = processors;
276 	}
277 
278 	public Properties getProperties() {
279 		return properties;
280 	}
281 
282 	public void setProperties(Properties properties) {
283 		this.properties = properties;
284 	}
285 
286 	public String getGlobalPropertiesMode() {
287 		return globalPropertiesMode;
288 	}
289 
290 	public void setGlobalPropertiesMode(String globalPropertiesMode) {
291 		this.globalPropertiesMode = globalPropertiesMode;
292 	}
293 
294 	public String getResolvePlaceholders() {
295 		return resolvePlaceholders;
296 	}
297 
298 	public void setResolvePlaceholders(String resolvePlaceholders) {
299 		this.resolvePlaceholders = resolvePlaceholders;
300 	}
301 }