1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.api.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 | 0 | private Log logger = LogFactory.getLog(getClass()); |
37 | |
private String namespace; |
38 | |
|
39 | |
public TlsPropertiesMapper(String namespace) |
40 | 0 | { |
41 | 0 | this.namespace = namespace; |
42 | 0 | } |
43 | |
|
44 | |
public void writeToProperties(Properties properties, TlsConfiguration configuration) |
45 | |
{ |
46 | 0 | writeTrustStoreToProperties(properties, configuration); |
47 | 0 | writeKeyStoreToProperties(properties, configuration); |
48 | 0 | } |
49 | |
|
50 | |
public void readFromProperties(TlsConfiguration configuration, Properties properties) throws IOException |
51 | |
{ |
52 | 0 | readTrustStoreFromProperties(configuration, properties); |
53 | 0 | readKeyStoreFromProperties(configuration, properties); |
54 | 0 | } |
55 | |
|
56 | |
private void writeTrustStoreToProperties(Properties properties, TlsConfiguration configuration) |
57 | |
{ |
58 | 0 | String trustStoreName = configuration.getTrustStore(); |
59 | 0 | String trustStorePassword = configuration.getTrustStorePassword(); |
60 | |
|
61 | 0 | if (null == trustStoreName && !configuration.isExplicitTrustStoreOnly()) |
62 | |
{ |
63 | 0 | logger.info("Defaulting " + namespace + " trust store to client Key Store"); |
64 | 0 | trustStoreName = configuration.getClientKeyStore(); |
65 | 0 | trustStorePassword = configuration.getClientKeyStorePassword(); |
66 | |
} |
67 | 0 | if (null != trustStoreName) |
68 | |
{ |
69 | 0 | synchronized (properties) |
70 | |
{ |
71 | 0 | setProperty(properties, TRUST_NAME_SUFFIX, trustStoreName); |
72 | 0 | setProperty(properties, TRUST_TYPE_SUFFIX, configuration.getTrustStoreType()); |
73 | 0 | setProperty(properties, TRUST_PASSWORD_SUFFIX, trustStorePassword); |
74 | 0 | setProperty(properties, TRUST_ALGORITHM_SUFFIX, configuration.getTrustManagerAlgorithm()); |
75 | 0 | } |
76 | 0 | logger.debug("Set Trust Store: " + namespace + TRUST_NAME_SUFFIX + " = " + trustStoreName); |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
private void readTrustStoreFromProperties(TlsConfiguration configuration, Properties properties) |
81 | |
throws IOException |
82 | |
{ |
83 | 0 | configuration.setTrustStore( |
84 | |
getProperty(properties, TRUST_NAME_SUFFIX, configuration.getTrustStore())); |
85 | 0 | configuration.setTrustStoreType( |
86 | |
getProperty(properties, TRUST_TYPE_SUFFIX, configuration.getTrustStoreType())); |
87 | 0 | configuration.setTrustStorePassword( |
88 | |
getProperty(properties, TRUST_PASSWORD_SUFFIX, configuration.getTrustStorePassword())); |
89 | 0 | configuration.setTrustManagerAlgorithm( |
90 | |
getProperty(properties, TRUST_ALGORITHM_SUFFIX, configuration.getTrustManagerAlgorithm())); |
91 | 0 | } |
92 | |
|
93 | |
private void writeKeyStoreToProperties(Properties properties, TlsConfiguration configuration) |
94 | |
{ |
95 | 0 | if (null != configuration.getClientKeyStore()) |
96 | |
{ |
97 | 0 | synchronized (properties) |
98 | |
{ |
99 | 0 | setProperty(properties, KEY_NAME_SUFFIX, configuration.getClientKeyStore()); |
100 | 0 | setProperty(properties, KEY_TYPE_SUFFIX, configuration.getClientKeyStoreType()); |
101 | 0 | setProperty(properties, KEY_PASSWORD_SUFFIX, configuration.getClientKeyStorePassword()); |
102 | 0 | } |
103 | 0 | logger.info("Set Key Store: " + namespace + KEY_NAME_SUFFIX + " = " + configuration.getClientKeyStore()); |
104 | |
} |
105 | 0 | } |
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 | 0 | configuration.setKeyStore( |
123 | |
getProperty(properties, KEY_NAME_SUFFIX, configuration.getKeyStore())); |
124 | 0 | configuration.setKeyStoreType( |
125 | |
getProperty(properties, KEY_TYPE_SUFFIX, configuration.getKeyStoreType())); |
126 | 0 | configuration.setKeyStorePassword( |
127 | |
getProperty(properties, KEY_PASSWORD_SUFFIX, configuration.getKeyStorePassword())); |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
private void setProperty(Properties properties, String suffix, String value) |
132 | |
{ |
133 | 0 | if (null != value) |
134 | |
{ |
135 | 0 | properties.setProperty(namespace + suffix, value); |
136 | 0 | if (logger.isDebugEnabled()) |
137 | |
{ |
138 | 0 | logger.debug(namespace + suffix + " <- " + value); |
139 | |
} |
140 | |
} |
141 | 0 | } |
142 | |
|
143 | |
private String getProperty(Properties properties, String suffix, String deflt) |
144 | |
{ |
145 | 0 | String value = properties.getProperty(namespace + suffix); |
146 | 0 | if (null == value) |
147 | |
{ |
148 | 0 | value = deflt; |
149 | |
} |
150 | 0 | if (logger.isDebugEnabled()) |
151 | |
{ |
152 | 0 | logger.debug(namespace + suffix + " -> " + value); |
153 | |
} |
154 | 0 | return value; |
155 | |
} |
156 | |
|
157 | |
} |
158 | |
|
159 | |
|