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