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 9ab94883a8)
This commit is contained in:
Christopher L. Shannon (cshannon) 2016-09-07 07:42:13 -04:00
parent c99eb1d600
commit c4bc626fea
6 changed files with 150 additions and 17 deletions

View File

@ -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)

View File

@ -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<String, Object> 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));

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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<BrokerPlugin> plugins = new ArrayList<BrokerPlugin>();
@ -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);
}
}
}

View File

@ -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<Object[]> 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());
}
}
}