]> git.argeo.org Git - lgpl/argeo-commons.git/blob - SecuredActiveMqConnectionFactory.java
4afbd1d8d818bc41a8b0208f57d98eb72591f96e
[lgpl/argeo-commons.git] / SecuredActiveMqConnectionFactory.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.security.activemq;
18
19 import java.io.InputStream;
20 import java.security.KeyStore;
21 import java.security.SecureRandom;
22
23 import javax.jms.Connection;
24 import javax.jms.ConnectionFactory;
25 import javax.jms.JMSException;
26 import javax.net.ssl.KeyManagerFactory;
27 import javax.net.ssl.TrustManagerFactory;
28 import javax.swing.UIManager;
29 import javax.swing.UnsupportedLookAndFeelException;
30 import javax.swing.plaf.metal.MetalLookAndFeel;
31
32 import org.apache.activemq.ActiveMQSslConnectionFactory;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.argeo.ArgeoException;
36 import org.springframework.beans.factory.DisposableBean;
37 import org.springframework.beans.factory.InitializingBean;
38 import org.springframework.core.io.Resource;
39 import org.springframework.jms.connection.CachingConnectionFactory;
40 import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
41
42 public class SecuredActiveMqConnectionFactory implements ConnectionFactory,
43 InitializingBean, DisposableBean {
44
45 public final static String AUTHMODE_UI = "ui";
46 public final static String AUTHMODE_OS = "os";
47 public final static String AUTHMODE_DEFAULT = AUTHMODE_OS;
48 // private final static String LOGIN_CONFIG_PROPERTY =
49 // "java.security.auth.login.config";
50
51 private final static Log log = LogFactory
52 .getLog(SecuredActiveMqConnectionFactory.class);
53
54 private String keyStorePassword;
55 private Resource keyStore;
56 private String keyStoreType = "JKS";// "PKCS12"
57 private String brokerURL;
58
59 private String authenticationMode;
60
61 private CachingConnectionFactory cachingConnectionFactory;
62
63 public Connection createConnection() throws JMSException {
64 return cachingConnectionFactory.createConnection();
65 }
66
67 public Connection createConnection(String userName, String password)
68 throws JMSException {
69 throw new UnsupportedOperationException();
70 }
71
72 public void afterPropertiesSet() throws Exception {
73 ActiveMQSslConnectionFactory activeMQSslConnectionFactory = new ActiveMQSslConnectionFactory();
74 prepareActiveMqSslConnectionFactory(activeMQSslConnectionFactory);
75 activeMQSslConnectionFactory.setBrokerURL(brokerURL);
76 UserCredentialsConnectionFactoryAdapter uccfa = new UserCredentialsConnectionFactoryAdapter();
77 uccfa.setTargetConnectionFactory(activeMQSslConnectionFactory);
78 cachingConnectionFactory = new CachingConnectionFactory();
79 cachingConnectionFactory.setTargetConnectionFactory(uccfa);
80 cachingConnectionFactory.setCacheConsumers(false);
81
82 initConnectionFactoryCredentials(uccfa);
83 cachingConnectionFactory.initConnection();
84 log.info("Connected to " + brokerURL);
85 uccfa.setUsername(null);
86 uccfa.setPassword(null);
87
88 }
89
90 protected void initConnectionFactoryCredentials(
91 final UserCredentialsConnectionFactoryAdapter uccfa) {
92 if (authenticationMode == null)
93 authenticationMode = AUTHMODE_DEFAULT;
94
95 if (AUTHMODE_OS.equals(authenticationMode)) {
96 // if (false) {
97 // // Cache previous value of login conf location
98 // String oldLoginConfLocation = System
99 // .getProperty(LOGIN_CONFIG_PROPERTY);
100 // // Find OS family
101 // String osName = System.getProperty("os.name");
102 // final String auth;
103 // if (osName.startsWith("Windows"))
104 // auth = "Windows";
105 // else if (osName.startsWith("SunOS")
106 // || osName.startsWith("Solaris"))
107 // auth = "Solaris";
108 // else
109 // auth = "Unix";
110 //
111 // Subject subject;
112 // // see http://old.nabble.com/osgi-and-jaas-td23485885.html
113 // ClassLoader ccl = Thread.currentThread()
114 // .getContextClassLoader();
115 // try {
116 // Thread.currentThread().setContextClassLoader(
117 // getClass().getClassLoader());
118 // URL url = getClass().getResource(
119 // "/org/argeo/security/activemq/osLogin.conf");
120 //
121 // System.setProperty(LOGIN_CONFIG_PROPERTY, url.toString());
122 // LoginContext lc = new LoginContext(auth);
123 // lc.login();
124 // subject = lc.getSubject();
125 // } catch (LoginException le) {
126 // throw new ArgeoException("OS authentication failed", le);
127 // } finally {
128 // if (oldLoginConfLocation != null)
129 // System.setProperty(LOGIN_CONFIG_PROPERTY,
130 // oldLoginConfLocation);
131 // Thread.currentThread().setContextClassLoader(ccl);
132 // }
133 // // Extract user name
134 // String osUsername = null;
135 // for (Principal principal : subject.getPrincipals()) {
136 // String className = principal.getClass().getName();
137 // if ("Unix".equals(auth)
138 // && "com.sun.security.auth.UnixPrincipal"
139 // .equals(className))
140 // osUsername = principal.getName();
141 // else if ("Windows".equals(auth)
142 // && "com.sun.security.auth.NTUserPrincipal"
143 // .equals(className))
144 // osUsername = principal.getName();
145 // else if ("Solaris".equals(auth)
146 // && "com.sun.security.auth.SolarisPrincipal"
147 // .equals(className))
148 // osUsername = principal.getName();
149 // }
150 //
151 // if (osUsername == null)
152 // throw new ArgeoException("Could not find OS user name");
153 // }
154
155 uccfa.setUsername(System.getProperty("user.name"));
156 uccfa.setPassword(null);
157
158 } else if (AUTHMODE_UI.equals(authenticationMode)) {
159 try {
160 UIManager.setLookAndFeel(new MetalLookAndFeel());
161 } catch (UnsupportedLookAndFeelException e) {
162 throw new ArgeoException("Cannot load look and feel", e);
163 }
164 UIManager.put("ClassLoader", getClass().getClassLoader());
165 UserPasswordDialog dialog = new UserPasswordDialog() {
166 private static final long serialVersionUID = -891646559691412088L;
167
168 protected void useCredentials(String username, char[] password) {
169 uccfa.setUsername(username);
170 uccfa.setPassword(new String(password));
171 }
172 };
173 dialog.setVisible(true);
174 } else {
175 throw new ArgeoException("Authentication mode '"
176 + authenticationMode + "' is not supported");
177 }
178
179 }
180
181 protected void prepareActiveMqSslConnectionFactory(
182 ActiveMQSslConnectionFactory connectionFactory) {
183 try {
184 KeyStore keyStoreKs = KeyStore.getInstance(keyStoreType);
185
186 InputStream keyInput = keyStore.getInputStream();
187 keyStoreKs.load(keyInput,
188 keyStorePassword != null ? keyStorePassword.toCharArray()
189 : null);
190 keyInput.close();
191
192 TrustManagerFactory tmf = TrustManagerFactory
193 .getInstance(TrustManagerFactory.getDefaultAlgorithm());
194 tmf.init(keyStoreKs);
195
196 KeyManagerFactory keyManagerFactory = KeyManagerFactory
197 .getInstance(KeyManagerFactory.getDefaultAlgorithm());
198 keyManagerFactory.init(keyStoreKs, keyStorePassword.toCharArray());
199
200 connectionFactory.setKeyAndTrustManagers(
201 keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(),
202 new SecureRandom());
203 } catch (Exception e) {
204 throw new ArgeoException(
205 "Cannot initialize JMS connection factory", e);
206 }
207
208 }
209
210 public void destroy() throws Exception {
211 if (cachingConnectionFactory != null)
212 cachingConnectionFactory.destroy();
213 }
214
215 public void setKeyStorePassword(String keyStorePassword) {
216 this.keyStorePassword = keyStorePassword;
217 }
218
219 public void setKeyStore(Resource keyStore) {
220 this.keyStore = keyStore;
221 }
222
223 public void setKeyStoreType(String keyStoreType) {
224 this.keyStoreType = keyStoreType;
225 }
226
227 public void setBrokerURL(String brokerUrl) {
228 this.brokerURL = brokerUrl;
229 }
230
231 public void setAuthenticationMode(String authenticationMode) {
232 this.authenticationMode = authenticationMode;
233 }
234
235 }