From c4bc626feac5b98a69c9a8577faf57585ce20d5d Mon Sep 17 00:00:00 2001 From: "Christopher L. Shannon (cshannon)" Date: Wed, 7 Sep 2016 07:42:13 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6420 Properly copying wire format options map before applying to the transport when using auto transports so that the options do not get cleared and will be used for all future connections (cherry picked from commit 9ab94883a8ba88aeeb0a16e2310ab42a46de1d05) --- .../auto/AutoSslTransportFactory.java | 6 - .../auto/AutoTcpTransportServer.java | 11 +- .../auto/nio/AutoNIOSSLTransportServer.java | 2 +- .../activemq/transport/TransportFactory.java | 4 - .../transport/auto/AutoSslAuthTest.java | 21 ++- .../auto/AutoWireFormatConfigurationTest.java | 123 ++++++++++++++++++ 6 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoWireFormatConfigurationTest.java diff --git a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoSslTransportFactory.java b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoSslTransportFactory.java index 39c5c41281..824d3214f9 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoSslTransportFactory.java +++ b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoSslTransportFactory.java @@ -29,22 +29,16 @@ import javax.net.ssl.SSLServerSocketFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerServiceAware; -import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.TransportServer; import org.apache.activemq.transport.tcp.SslTransportFactory; -import org.apache.activemq.transport.tcp.SslTransportServer; import org.apache.activemq.transport.tcp.TcpTransport; import org.apache.activemq.util.IOExceptionSupport; import org.apache.activemq.util.IntrospectionSupport; import org.apache.activemq.util.URISupport; import org.apache.activemq.wireformat.WireFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class AutoSslTransportFactory extends SslTransportFactory implements BrokerServiceAware { - private static final Logger LOG = LoggerFactory.getLogger(AutoSslTransportFactory.class); - protected BrokerService brokerService; /* (non-Javadoc) diff --git a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoTcpTransportServer.java b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoTcpTransportServer.java index 308931b812..64162dd4dd 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoTcpTransportServer.java +++ b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/AutoTcpTransportServer.java @@ -22,6 +22,7 @@ import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; +import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -89,8 +90,14 @@ public class AutoTcpTransportServer extends TcpTransportServer { try { wff = (WireFormatFactory)WIREFORMAT_FACTORY_FINDER.newInstance(scheme); if (options != null) { - IntrospectionSupport.setProperties(wff, options.get(AutoTransportUtils.ALL)); - IntrospectionSupport.setProperties(wff, options.get(scheme)); + final Map wfOptions = new HashMap<>(); + if (options.get(AutoTransportUtils.ALL) != null) { + wfOptions.putAll(options.get(AutoTransportUtils.ALL)); + } + if (options.get(scheme) != null) { + wfOptions.putAll(options.get(scheme)); + } + IntrospectionSupport.setProperties(wff, wfOptions); } if (wff instanceof OpenWireFormatFactory) { protocolVerifiers.put(AutoTransportUtils.OPENWIRE, new OpenWireProtocolVerifier((OpenWireFormatFactory) wff)); diff --git a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/nio/AutoNIOSSLTransportServer.java b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/nio/AutoNIOSSLTransportServer.java index fff105592c..a8d4eb9749 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/transport/auto/nio/AutoNIOSSLTransportServer.java +++ b/activemq-broker/src/main/java/org/apache/activemq/transport/auto/nio/AutoNIOSSLTransportServer.java @@ -104,6 +104,7 @@ public class AutoNIOSSLTransportServer extends AutoTcpTransportServer { ExecutorService executor = Executors.newSingleThreadExecutor(); //The SSLEngine needs to be initialized and handshake done to get the first command and detect the format + //The wireformat doesn't need properties set here because we aren't using this format during the SSL handshake final AutoInitNioSSLTransport in = new AutoInitNioSSLTransport(wireFormatFactory.createWireFormat(), socket); if (context != null) { in.setSslContext(context); @@ -144,7 +145,6 @@ public class AutoNIOSSLTransportServer extends AutoTcpTransportServer { return new TransportInfo(format, transport, protocolInfo.detectedTransportFactory); } - } diff --git a/activemq-client/src/main/java/org/apache/activemq/transport/TransportFactory.java b/activemq-client/src/main/java/org/apache/activemq/transport/TransportFactory.java index 327ea423ac..acfc1b0389 100755 --- a/activemq-client/src/main/java/org/apache/activemq/transport/TransportFactory.java +++ b/activemq-client/src/main/java/org/apache/activemq/transport/TransportFactory.java @@ -18,19 +18,15 @@ package org.apache.activemq.transport; import java.io.IOException; import java.net.MalformedURLException; -import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; -import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executor; -import javax.net.ssl.SSLEngine; - import org.apache.activemq.util.FactoryFinder; import org.apache.activemq.util.IOExceptionSupport; import org.apache.activemq.util.IntrospectionSupport; diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoSslAuthTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoSslAuthTest.java index 14918527e5..be6043b363 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoSslAuthTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoSslAuthTest.java @@ -22,6 +22,7 @@ import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.Broker; @@ -29,6 +30,7 @@ import org.apache.activemq.broker.BrokerFilter; import org.apache.activemq.broker.BrokerPlugin; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.ConnectionContext; +import org.apache.activemq.broker.TransportConnection; import org.apache.activemq.broker.TransportConnector; import org.apache.activemq.command.ConnectionInfo; import org.junit.After; @@ -48,7 +50,7 @@ public class AutoSslAuthTest { private String uri; private final String protocol; - private boolean hasCertificate = false; + private AtomicInteger hasCertificateCount = new AtomicInteger(); private BrokerService brokerService; @Parameters(name="protocol={0}") @@ -74,6 +76,7 @@ public class AutoSslAuthTest { brokerService.setPersistent(false); TransportConnector connector = brokerService.addConnector(protocol + "://localhost:0?transport.needClientAuth=true"); + connector.setName("auto"); uri = connector.getPublishableConnectString(); ArrayList plugins = new ArrayList(); @@ -87,7 +90,9 @@ public class AutoSslAuthTest { @Override public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception { super.addConnection(context, info); - hasCertificate = info.getTransportContext() instanceof X509Certificate[]; + if (info.getTransportContext() instanceof X509Certificate[]) { + hasCertificateCount.getAndIncrement(); + } } }; } @@ -122,8 +127,16 @@ public class AutoSslAuthTest { public void testConnect() throws Exception { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); factory.setBrokerURL(uri); - factory.createConnection().start(); - assertTrue(hasCertificate); + //Create 5 connections to make sure all are properly set + for (int i = 0; i < 5; i++) { + factory.createConnection().start(); + } + + assertTrue(hasCertificateCount.get() == 5); + + for (TransportConnection connection : brokerService.getTransportConnectorByName("auto").getConnections()) { + assertTrue(connection.getTransport().getPeerCertificates() != null); + } } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoWireFormatConfigurationTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoWireFormatConfigurationTest.java new file mode 100644 index 0000000000..8f0e27ba8d --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/auto/AutoWireFormatConfigurationTest.java @@ -0,0 +1,123 @@ +/** + * 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.transport.auto; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collection; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.broker.TransportConnection; +import org.apache.activemq.broker.TransportConnector; +import org.apache.activemq.openwire.OpenWireFormat; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class AutoWireFormatConfigurationTest { + + public static final String KEYSTORE_TYPE = "jks"; + public static final String PASSWORD = "password"; + public static final String SERVER_KEYSTORE = "src/test/resources/server.keystore"; + public static final String TRUST_KEYSTORE = "src/test/resources/client.keystore"; + + private String uri; + private final String protocol; + private BrokerService brokerService; + //Use the scheme for applying wireformat options or apply to all wireformats if false + private final boolean onlyScheme; + + @Parameters(name="protocol={0},onlyScheme={1}") + public static Collection data() { + return Arrays.asList(new Object[][] { + {"auto", true}, + {"auto+nio", true}, + {"auto+nio+ssl", true}, + {"auto+ssl", true}, + {"auto", false}, + {"auto+nio", false}, + {"auto+nio+ssl", false}, + {"auto+ssl", false} + }); + } + + static { + System.setProperty("javax.net.ssl.trustStore", TRUST_KEYSTORE); + System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD); + System.setProperty("javax.net.ssl.trustStoreType", KEYSTORE_TYPE); + System.setProperty("javax.net.ssl.keyStore", SERVER_KEYSTORE); + System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD); + System.setProperty("javax.net.ssl.keyStoreType", KEYSTORE_TYPE); + } + + @Before + public void before() throws Exception { + BrokerService brokerService = new BrokerService(); + brokerService.setPersistent(false); + + String wireFormatSetting = onlyScheme ? + "wireFormat.default.cacheEnabled=false" : "wireFormat.cacheEnabled=false"; + TransportConnector connector = + brokerService.addConnector(protocol + "://localhost:0?" + wireFormatSetting); + connector.setName("auto"); + + uri = connector.getPublishableConnectString(); + + this.brokerService = brokerService; + brokerService.start(); + brokerService.waitUntilStarted(); + } + + @After + public void after() throws Exception { + if (brokerService != null) { + brokerService.stop(); + brokerService.waitUntilStopped(); + } + } + + /** + * @param isNio + */ + public AutoWireFormatConfigurationTest(String protocol, boolean onlyScheme) { + this.protocol = protocol; + this.onlyScheme = onlyScheme; + } + + @Test(timeout = 10000) + public void testConnect() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); + factory.setBrokerURL(uri); + + //Create 5 connections to make sure all are properly set + for (int i = 0; i < 5; i++) { + factory.createConnection().start(); + } + + for (TransportConnection connection : brokerService.getTransportConnectorByName("auto").getConnections()) { + //Cache should be disabled on the wire format + OpenWireFormat wireFormat = (OpenWireFormat) connection.getTransport().getWireFormat(); + assertEquals(false, wireFormat.isCacheEnabled()); + } + } +}