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 protected static final Log logger = LogFactory.getLog(PlaceholderProcessor.class);
49
50 private static boolean strategiesLoaded = false;
51
52 private final Map types;
53 private final Map schemes = new HashMap();
54 private final TemplateParser parser = TemplateParser.createAntStyleParser();
55
56 public PlaceholderProcessor()
57 {
58 types = new HashMap();
59 types.put("PBE", PasswordBasedEncryptionStrategy.class.getName());
60 }
61
62 public PlaceholderProcessor(Map types)
63 {
64 this.types = types;
65 }
66
67 public Attributes processAttributes(Attributes attributes, String elementName)
68 throws ConfigurationException
69 {
70 AttributesImpl attribs = new AttributesImpl(attributes);
71 String value = null;
72
73 for (int i = 0; i < attribs.getLength(); i++)
74 {
75 value = attribs.getValue(i);
76 value = processValue(value);
77 if (value == null)
78 {
79 throw new ConfigurationException(
80 BuildersMessages.propertyTemplateMalformed(
81 "<" + elementName + attribs.getLocalName(i) + "='" + value + "' ...>"));
82 }
83 attribs.setValue(i, value);
84 }
85 return attribs;
86 }
87
88 public String processValue(String value) throws ConfigurationException
89 {
90 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 int x = value.indexOf("{encrypt:");
135 if (x > -1)
136 {
137 logger.debug("Value contains encrypted data.");
138 int y = value.indexOf("}");
139 if (y == -1)
140 {
141 logger.error("Encryption tag is malformed: " + value);
142 return null;
143 }
144 else
145 {
146 scheme = value.substring((x + 9), y);
147 logger.debug("look up encryption scheme: " + scheme);
148 try
149 {
150 UMOEncryptionStrategy strategy = getEncryptionStrategy(scheme);
151 String data = value.substring(y + 1);
152 byte[] decrypted = strategy.decrypt(data.getBytes(), null);
153 return new String(decrypted);
154 }
155 catch (Exception e)
156 {
157 throw new ConfigurationException(e);
158 }
159 }
160 }
161 else
162 {
163 return value;
164 }
165 }
166
167 public UMOEncryptionStrategy getEncryptionStrategy(String scheme) throws Exception
168 {
169 if (!strategiesLoaded)
170 {
171 loadStrategies();
172 }
173 return (UMOEncryptionStrategy)schemes.get(scheme);
174 }
175
176 private void loadStrategies() throws Exception
177 {
178 String path = System.getProperty(MULE_ENCRYPTION_PROPERTIES, MuleManager.getConfiguration()
179 .getWorkingDirectory()
180 + File.separator
181 + DEFAULT_ENCRYPTION_PROPERTIES_FILE);
182
183 logger.info("Attempting to load encryption properties from: " + path);
184 Properties props = PropertiesUtils.loadProperties(path, getClass());
185
186 Map names = new HashMap();
187 PropertiesUtils.getPropertiesWithPrefix(props, "name", names);
188 String name;
189 for (Iterator iterator = names.values().iterator(); iterator.hasNext();)
190 {
191 name = (String)iterator.next();
192 Map schemeConfig = new HashMap();
193 PropertiesUtils.getPropertiesWithPrefix(props, name + ".", schemeConfig);
194 schemeConfig = PropertiesUtils.removeNamespaces(schemeConfig);
195
196 String type = (String)schemeConfig.get("type");
197 String clazz = (String)types.get(type);
198 if (clazz == null)
199 {
200 throw new IllegalArgumentException("Unknown encryption type: " + type);
201 }
202 logger.debug("Found Class: " + clazz + " for type: " + type);
203 UMOEncryptionStrategy strat = (UMOEncryptionStrategy)ClassUtils.instanciateClass(clazz,
204 ClassUtils.NO_ARGS, PlaceholderProcessor.class);
205 BeanUtils.populateWithoutFail(strat, schemeConfig, true);
206 schemes.put(name, strat);
207 }
208 }
209 }