INT-4183: Add SSL Handshake Timeout for TCP
JIRA: https://jira.spring.io/browse/INT-4183 Previously, this was hard-coded to 30 seconds. * Fix typos according PR comments
This commit is contained in:
committed by
Artem Bilan
parent
61e77435ab
commit
4e5d9016b3
@@ -71,6 +71,7 @@
|
||||
apply-sequence="true"
|
||||
using-nio="true"
|
||||
ssl-context-support="sslContextSupport"
|
||||
ssl-handshake-timeout="43"
|
||||
/>
|
||||
|
||||
<bean id="sslContextSupport" class="org.springframework.integration.ip.tcp.connection.DefaultTcpSSLContextSupport">
|
||||
@@ -89,6 +90,17 @@
|
||||
socket-support="socketSupport"
|
||||
socket-factory-support="socketFactorySupport" />
|
||||
|
||||
<ip:tcp-connection-factory id="secureServerNio"
|
||||
type="server"
|
||||
port="#{tcpIpUtils.findAvailableServerSocket(5250)}"
|
||||
lookup-host="false"
|
||||
apply-sequence="true"
|
||||
using-nio="true"
|
||||
ssl-context-support="sslContextSupport"
|
||||
ssl-handshake-timeout="34"
|
||||
socket-support="socketSupport"
|
||||
socket-factory-support="socketFactorySupport" />
|
||||
|
||||
<bean id="socketSupport" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.integration.ip.tcp.connection.TcpSocketSupport" />
|
||||
</bean>
|
||||
|
||||
@@ -256,6 +256,9 @@ public class ParserUnitTests {
|
||||
@Autowired
|
||||
TcpNetServerConnectionFactory secureServer;
|
||||
|
||||
@Autowired
|
||||
TcpNioServerConnectionFactory secureServerNio;
|
||||
|
||||
@Autowired
|
||||
TcpSocketFactorySupport socketFactorySupport;
|
||||
|
||||
@@ -343,6 +346,7 @@ public class ParserUnitTests {
|
||||
Object connectionSupport = TestUtils.getPropertyValue(cfS1Nio, "tcpNioConnectionSupport");
|
||||
assertTrue(connectionSupport instanceof DefaultTcpNioSSLConnectionSupport);
|
||||
assertNotNull(TestUtils.getPropertyValue(connectionSupport, "sslContext"));
|
||||
assertEquals(43, TestUtils.getPropertyValue(this.cfS1Nio, "sslHandshakeTimeout"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -656,6 +660,7 @@ public class ParserUnitTests {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(secureServer);
|
||||
assertSame(socketFactorySupport, dfa.getPropertyValue("tcpSocketFactorySupport"));
|
||||
assertSame(socketSupport, dfa.getPropertyValue("tcpSocketSupport"));
|
||||
assertEquals(34, TestUtils.getPropertyValue(this.secureServerNio, "sslHandshakeTimeout"));
|
||||
}
|
||||
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
import javax.net.SocketFactory;
|
||||
@@ -295,6 +296,9 @@ Certificate fingerprints:
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
assertEquals("Hello, world!", new String((byte[]) messages.get(0).getPayload()));
|
||||
assertNotNull(messages.get(0).getHeaders().get("cipher"));
|
||||
|
||||
client.stop();
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -330,12 +334,16 @@ Certificate fingerprints:
|
||||
connection.send(new GenericMessage<String>("Hello, world!"));
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
assertEquals("Hello, world!", new String((byte[]) messages.get(0).getPayload()));
|
||||
|
||||
client.stop();
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNioClientAndServerSSL() throws Exception {
|
||||
System.setProperty("javax.net.debug", "all"); // SSL activity in the console
|
||||
TcpNioServerConnectionFactory server = new TcpNioServerConnectionFactory(0);
|
||||
server.setSslHandshakeTimeout(43);
|
||||
DefaultTcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport("test.ks",
|
||||
"test.truststore.ks", "secret", "secret");
|
||||
sslContextSupport.setProtocol("SSL");
|
||||
@@ -351,19 +359,36 @@ Certificate fingerprints:
|
||||
return false;
|
||||
});
|
||||
server.setMapper(new SSLMapper());
|
||||
final AtomicReference<String> serverConnectionId = new AtomicReference<>();
|
||||
server.setApplicationEventPublisher(e -> {
|
||||
if (e instanceof TcpConnectionOpenEvent) {
|
||||
serverConnectionId.set(((TcpConnectionEvent) e).getConnectionId());
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
TestingUtilities.waitListening(server, null);
|
||||
|
||||
TcpNioClientConnectionFactory client = new TcpNioClientConnectionFactory("localhost", server.getPort());
|
||||
client.setSslHandshakeTimeout(34);
|
||||
client.setTcpNioConnectionSupport(tcpNioConnectionSupport);
|
||||
client.registerListener(message -> false);
|
||||
client.setApplicationEventPublisher(e -> { });
|
||||
client.start();
|
||||
|
||||
TcpConnection connection = client.getConnection();
|
||||
assertEquals(34, TestUtils.getPropertyValue(connection, "handshakeTimeout"));
|
||||
connection.send(new GenericMessage<String>("Hello, world!"));
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
assertEquals("Hello, world!", new String((byte[]) messages.get(0).getPayload()));
|
||||
assertNotNull(messages.get(0).getHeaders().get("cipher"));
|
||||
|
||||
Map<?, ?> connections = TestUtils.getPropertyValue(server, "connections", Map.class);
|
||||
Object serverConnection = connections.get(serverConnectionId.get());
|
||||
assertNotNull(serverConnection);
|
||||
assertEquals(43, TestUtils.getPropertyValue(serverConnection, "handshakeTimeout"));
|
||||
|
||||
client.stop();
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -394,6 +419,12 @@ Certificate fingerprints:
|
||||
ByteArrayCrLfSerializer deserializer = new ByteArrayCrLfSerializer();
|
||||
deserializer.setMaxMessageSize(120000);
|
||||
server.setDeserializer(deserializer);
|
||||
final AtomicReference<String> serverConnectionId = new AtomicReference<>();
|
||||
server.setApplicationEventPublisher(e -> {
|
||||
if (e instanceof TcpConnectionOpenEvent) {
|
||||
serverConnectionId.set(((TcpConnectionEvent) e).getConnectionId());
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
TestingUtilities.waitListening(server, null);
|
||||
|
||||
@@ -410,9 +441,11 @@ Certificate fingerprints:
|
||||
return false;
|
||||
});
|
||||
client.setDeserializer(deserializer);
|
||||
client.setApplicationEventPublisher(e -> { });
|
||||
client.start();
|
||||
|
||||
TcpConnection connection = client.getConnection();
|
||||
assertEquals(30, TestUtils.getPropertyValue(connection, "handshakeTimeout"));
|
||||
byte[] bytes = new byte[100000];
|
||||
connection.send(new GenericMessage<String>("Hello, world!" + new String(bytes)));
|
||||
assertTrue(latch.await(60, TimeUnit.SECONDS));
|
||||
@@ -422,6 +455,14 @@ Certificate fingerprints:
|
||||
payload = (byte[]) messages.get(1).getPayload();
|
||||
assertEquals(13 + bytes.length, payload.length);
|
||||
assertEquals("Hello, world!", new String(payload).substring(0, 13));
|
||||
|
||||
Map<?, ?> connections = TestUtils.getPropertyValue(server, "connections", Map.class);
|
||||
Object serverConnection = connections.get(serverConnectionId.get());
|
||||
assertNotNull(serverConnection);
|
||||
assertEquals(30, TestUtils.getPropertyValue(serverConnection, "handshakeTimeout"));
|
||||
|
||||
client.stop();
|
||||
server.stop();
|
||||
}
|
||||
|
||||
private static class Replier implements TcpSender {
|
||||
|
||||
Reference in New Issue
Block a user