1
2
3
4
5
6
7
8
9
10
11 package org.mule.umo.security.tls;
12
13 import java.io.IOException;
14 import java.util.Properties;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19
20
21
22
23
24 public class TlsPropertiesMapper
25 {
26
27 private static final String TRUST_NAME_SUFFIX = ".ssl.trustStore";
28 private static final String TRUST_TYPE_SUFFIX = ".ssl.trustStoreType";
29 private static final String TRUST_PASSWORD_SUFFIX = ".ssl.trustStorePassword";
30 private static final String TRUST_ALGORITHM_SUFFIX = ".ssl.trustManagerAlgorithm";
31
32 private static final String KEY_NAME_SUFFIX = ".ssl.keyStore";
33 private static final String KEY_TYPE_SUFFIX = ".ssl.keyStoreType";
34 private static final String KEY_PASSWORD_SUFFIX = ".ssl.keyStorePassword";
35
36 private Log logger = LogFactory.getLog(getClass());
37 private String namespace;
38
39 public TlsPropertiesMapper(String namespace)
40 {
41 this.namespace = namespace;
42 }
43
44 public void writeToProperties(Properties properties, TlsConfiguration configuration)
45 {
46 writeTrustStoreToProperties(properties, configuration);
47 writeKeyStoreToProperties(properties, configuration);
48 }
49
50 public void readFromProperties(TlsConfiguration configuration, Properties properties) throws IOException
51 {
52 readTrustStoreFromProperties(configuration, properties);
53 readKeyStoreFromProperties(configuration, properties);
54 }
55
56 private void writeTrustStoreToProperties(Properties properties, TlsConfiguration configuration)
57 {
58 String trustStoreName = configuration.getTrustStore();
59 String trustStorePassword = configuration.getTrustStorePassword();
60
61 if (null == trustStoreName && !configuration.isExplicitTrustStoreOnly())
62 {
63 logger.info("Defaulting " + namespace + " trust store to client Key Store");
64 trustStoreName = configuration.getClientKeyStore();
65 trustStorePassword = configuration.getClientKeyStorePassword();
66 }
67 if (null != trustStoreName)
68 {
69 synchronized (properties)
70 {
71 setProperty(properties, TRUST_NAME_SUFFIX, trustStoreName);
72 setProperty(properties, TRUST_TYPE_SUFFIX, configuration.getTrustStoreType());
73 setProperty(properties, TRUST_PASSWORD_SUFFIX, trustStorePassword);
74 setProperty(properties, TRUST_ALGORITHM_SUFFIX, configuration.getTrustManagerAlgorithm());
75 }
76 logger.debug("Set Trust Store: " + namespace + TRUST_NAME_SUFFIX + " = " + trustStoreName);
77 }
78 }
79
80 private void readTrustStoreFromProperties(TlsConfiguration configuration, Properties properties)
81 throws IOException
82 {
83 configuration.setTrustStore(
84 getProperty(properties, TRUST_NAME_SUFFIX, configuration.getTrustStore()));
85 configuration.setTrustStoreType(
86 getProperty(properties, TRUST_TYPE_SUFFIX, configuration.getTrustStoreType()));
87 configuration.setTrustStorePassword(
88 getProperty(properties, TRUST_PASSWORD_SUFFIX, configuration.getTrustStorePassword()));
89 configuration.setTrustManagerAlgorithm(
90 getProperty(properties, TRUST_ALGORITHM_SUFFIX, configuration.getTrustManagerAlgorithm()));
91 }
92
93 private void writeKeyStoreToProperties(Properties properties, TlsConfiguration configuration)
94 {
95 if (null != configuration.getClientKeyStore())
96 {
97 synchronized (properties)
98 {
99 setProperty(properties, KEY_NAME_SUFFIX, configuration.getClientKeyStore());
100 setProperty(properties, KEY_TYPE_SUFFIX, configuration.getClientKeyStoreType());
101 setProperty(properties, KEY_PASSWORD_SUFFIX, configuration.getClientKeyStorePassword());
102 }
103 logger.info("Set Key Store: " + namespace + KEY_NAME_SUFFIX + " = " + configuration.getClientKeyStore());
104 }
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 private void readKeyStoreFromProperties(TlsConfiguration configuration, Properties properties)
120 throws IOException
121 {
122 configuration.setKeyStore(
123 getProperty(properties, KEY_NAME_SUFFIX, configuration.getKeyStore()));
124 configuration.setKeystoreType(
125 getProperty(properties, KEY_TYPE_SUFFIX, configuration.getKeystoreType()));
126 configuration.setStorePassword(
127 getProperty(properties, KEY_PASSWORD_SUFFIX, configuration.getStorePassword()));
128 }
129
130
131 private void setProperty(Properties properties, String suffix, String value)
132 {
133 if (null != value)
134 {
135 properties.setProperty(namespace + suffix, value);
136 if (logger.isDebugEnabled())
137 {
138 logger.debug(namespace + suffix + " <- " + value);
139 }
140 }
141 }
142
143 private String getProperty(Properties properties, String suffix, String deflt)
144 {
145 String value = properties.getProperty(namespace + suffix);
146 if (null == value)
147 {
148 value = deflt;
149 }
150 if (logger.isDebugEnabled())
151 {
152 logger.debug(namespace + suffix + " -> " + value);
153 }
154 return value;
155 }
156
157 }
158
159