From 11541e8608807d900f0fd66ef48bbc7c5590ee4c Mon Sep 17 00:00:00 2001 From: "Michael L. Bloom" Date: Mon, 31 Oct 2016 11:32:34 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6489 Add support for SSL configurations using JNDI. --- .../ActiveMQSslInitialContextFactory.java | 40 ++++++++++ .../ActiveMQSslInitialContextFactoryTest.java | 75 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 activemq-client/src/main/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactory.java create mode 100644 activemq-unit-tests/src/test/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactoryTest.java diff --git a/activemq-client/src/main/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactory.java b/activemq-client/src/main/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactory.java new file mode 100644 index 0000000000..d13ab67ad7 --- /dev/null +++ b/activemq-client/src/main/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactory.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.jndi; + +import java.net.URISyntaxException; +import java.util.Hashtable; +import java.util.Properties; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.ActiveMQSslConnectionFactory; + +public class ActiveMQSslInitialContextFactory extends ActiveMQInitialContextFactory { + + /** + * Factory method to create a new connection factory from the given + * environment + */ + @Override + protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) throws URISyntaxException { + ActiveMQConnectionFactory answer = new ActiveMQSslConnectionFactory(); + Properties properties = new Properties(); + properties.putAll(environment); + answer.setProperties(properties); + return answer; + } +} diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactoryTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactoryTest.java new file mode 100644 index 0000000000..e2962bfcdf --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/jndi/ActiveMQSslInitialContextFactoryTest.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.jndi; + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.NamingException; +import javax.naming.spi.InitialContextFactory; + +import org.apache.activemq.ActiveMQSslConnectionFactory; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class ActiveMQSslInitialContextFactoryTest { + + protected Context context; + + @Before + public void setUp() throws Exception { + InitialContextFactory factory = new ActiveMQSslInitialContextFactory(); + Hashtable environment = new Hashtable(); + environment.put("java.naming.provider.url", "vm://0"); + environment.put("connection.ConnectionFactory.userName", "user"); + environment.put("connection.ConnectionFactory.userPassword", "test"); + environment.put("connection.ConnectionFactory.keyStore", "keystore.jks"); + environment.put("connection.ConnectionFactory.keyStorePassword", "test"); + environment.put("connection.ConnectionFactory.keyStoreType", "JKS"); + environment.put("connection.ConnectionFactory.trustStore", "truststore.jks"); + environment.put("connection.ConnectionFactory.trustStorePassword", "test"); + environment.put("connection.ConnectionFactory.trustStoreType", "JKS"); + + context = factory.getInitialContext(environment); + assertTrue("No context created", context != null); + } + + @Test + public void testCreateConnectionFactory() throws NamingException { + assertTrue(context.lookup("ConnectionFactory") instanceof ActiveMQSslConnectionFactory); + } + + @Test + public void testAssertConnectionFactoryProperties() throws NamingException { + Object c = context.lookup("ConnectionFactory"); + if (c instanceof ActiveMQSslConnectionFactory) { + ActiveMQSslConnectionFactory factory = (ActiveMQSslConnectionFactory)c; + assertEquals(factory.getKeyStore(), "keystore.jks"); + assertEquals(factory.getKeyStorePassword(), "test"); + assertEquals(factory.getKeyStoreType(), "JKS"); + assertEquals(factory.getTrustStore(), "truststore.jks"); + assertEquals(factory.getTrustStorePassword(), "test"); + assertEquals(factory.getTrustStoreType(), "JKS"); + } else { + fail("Did not find an ActiveMQSslConnectionFactory"); + } + } + +}