1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.builders; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.ConfigurationException; |
15 | |
import org.mule.config.builders.i18n.BuildersMessages; |
16 | |
import org.mule.impl.security.PasswordBasedEncryptionStrategy; |
17 | |
import org.mule.umo.UMOEncryptionStrategy; |
18 | |
import org.mule.util.BeanUtils; |
19 | |
import org.mule.util.ClassUtils; |
20 | |
import org.mule.util.PropertiesUtils; |
21 | |
import org.mule.util.TemplateParser; |
22 | |
|
23 | |
import java.io.File; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Map; |
27 | |
import java.util.Properties; |
28 | |
|
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
import org.xml.sax.Attributes; |
32 | |
import org.xml.sax.helpers.AttributesImpl; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class PlaceholderProcessor |
41 | |
{ |
42 | |
public static final String MULE_ENCRYPTION_PROPERTIES = "org.mule.config.encryption.properties"; |
43 | |
public static final String DEFAULT_ENCRYPTION_PROPERTIES_FILE = "mule-encryption.properties"; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | protected static final Log logger = LogFactory.getLog(PlaceholderProcessor.class); |
49 | |
|
50 | 0 | private static boolean strategiesLoaded = false; |
51 | |
|
52 | |
private final Map types; |
53 | 0 | private final Map schemes = new HashMap(); |
54 | 0 | private final TemplateParser parser = TemplateParser.createAntStyleParser(); |
55 | |
|
56 | |
public PlaceholderProcessor() |
57 | 0 | { |
58 | 0 | types = new HashMap(); |
59 | 0 | types.put("PBE", PasswordBasedEncryptionStrategy.class.getName()); |
60 | 0 | } |
61 | |
|
62 | |
public PlaceholderProcessor(Map types) |
63 | 0 | { |
64 | 0 | this.types = types; |
65 | 0 | } |
66 | |
|
67 | |
public Attributes processAttributes(Attributes attributes, String elementName) |
68 | |
throws ConfigurationException |
69 | |
{ |
70 | 0 | AttributesImpl attribs = new AttributesImpl(attributes); |
71 | 0 | String value = null; |
72 | |
|
73 | 0 | for (int i = 0; i < attribs.getLength(); i++) |
74 | |
{ |
75 | 0 | value = attribs.getValue(i); |
76 | 0 | value = processValue(value); |
77 | 0 | if (value == null) |
78 | |
{ |
79 | 0 | throw new ConfigurationException( |
80 | |
BuildersMessages.propertyTemplateMalformed( |
81 | |
"<" + elementName + attribs.getLocalName(i) + "='" + value + "' ...>")); |
82 | |
} |
83 | 0 | attribs.setValue(i, value); |
84 | |
} |
85 | 0 | return attribs; |
86 | |
} |
87 | |
|
88 | |
public String processValue(String value) throws ConfigurationException |
89 | |
{ |
90 | 0 | return parser.parse(MuleManager.getInstance().getProperties(), value); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected String processEncryptedValue(String value) throws ConfigurationException |
132 | |
{ |
133 | |
String scheme; |
134 | 0 | int x = value.indexOf("{encrypt:"); |
135 | 0 | if (x > -1) |
136 | |
{ |
137 | 0 | logger.debug("Value contains encrypted data."); |
138 | 0 | int y = value.indexOf("}"); |
139 | 0 | if (y == -1) |
140 | |
{ |
141 | 0 | logger.error("Encryption tag is malformed: " + value); |
142 | 0 | return null; |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | scheme = value.substring((x + 9), y); |
147 | 0 | logger.debug("look up encryption scheme: " + scheme); |
148 | |
try |
149 | |
{ |
150 | 0 | UMOEncryptionStrategy strategy = getEncryptionStrategy(scheme); |
151 | 0 | String data = value.substring(y + 1); |
152 | 0 | byte[] decrypted = strategy.decrypt(data.getBytes(), null); |
153 | 0 | return new String(decrypted); |
154 | |
} |
155 | 0 | catch (Exception e) |
156 | |
{ |
157 | 0 | throw new ConfigurationException(e); |
158 | |
} |
159 | |
} |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | return value; |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
public UMOEncryptionStrategy getEncryptionStrategy(String scheme) throws Exception |
168 | |
{ |
169 | 0 | if (!strategiesLoaded) |
170 | |
{ |
171 | 0 | loadStrategies(); |
172 | |
} |
173 | 0 | return (UMOEncryptionStrategy)schemes.get(scheme); |
174 | |
} |
175 | |
|
176 | |
private void loadStrategies() throws Exception |
177 | |
{ |
178 | 0 | String path = System.getProperty(MULE_ENCRYPTION_PROPERTIES, MuleManager.getConfiguration() |
179 | |
.getWorkingDirectory() |
180 | |
+ File.separator |
181 | |
+ DEFAULT_ENCRYPTION_PROPERTIES_FILE); |
182 | |
|
183 | 0 | logger.info("Attempting to load encryption properties from: " + path); |
184 | 0 | Properties props = PropertiesUtils.loadProperties(path, getClass()); |
185 | |
|
186 | 0 | Map names = new HashMap(); |
187 | 0 | PropertiesUtils.getPropertiesWithPrefix(props, "name", names); |
188 | |
String name; |
189 | 0 | for (Iterator iterator = names.values().iterator(); iterator.hasNext();) |
190 | |
{ |
191 | 0 | name = (String)iterator.next(); |
192 | 0 | Map schemeConfig = new HashMap(); |
193 | 0 | PropertiesUtils.getPropertiesWithPrefix(props, name + ".", schemeConfig); |
194 | 0 | schemeConfig = PropertiesUtils.removeNamespaces(schemeConfig); |
195 | |
|
196 | 0 | String type = (String)schemeConfig.get("type"); |
197 | 0 | String clazz = (String)types.get(type); |
198 | 0 | if (clazz == null) |
199 | |
{ |
200 | 0 | throw new IllegalArgumentException("Unknown encryption type: " + type); |
201 | |
} |
202 | 0 | logger.debug("Found Class: " + clazz + " for type: " + type); |
203 | 0 | UMOEncryptionStrategy strat = (UMOEncryptionStrategy)ClassUtils.instanciateClass(clazz, |
204 | |
ClassUtils.NO_ARGS, PlaceholderProcessor.class); |
205 | 0 | BeanUtils.populateWithoutFail(strat, schemeConfig, true); |
206 | 0 | schemes.put(name, strat); |
207 | 0 | } |
208 | 0 | } |
209 | |
} |