Skip to content

Commit 5ed3cc5

Browse files
authored
Fix Bolt handshake write handling and timeout management (#1528)
1 parent 8d2416d commit 5ed3cc5

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/connection/ChannelConnectedListener.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package org.neo4j.driver.internal.async.connection;
1818

1919
import static java.lang.String.format;
20-
import static org.neo4j.driver.internal.async.connection.BoltProtocolUtil.handshakeBuf;
2120
import static org.neo4j.driver.internal.async.connection.BoltProtocolUtil.handshakeString;
2221

2322
import io.netty.channel.ChannelFuture;
2423
import io.netty.channel.ChannelFutureListener;
2524
import io.netty.channel.ChannelPromise;
25+
import javax.net.ssl.SSLHandshakeException;
2626
import org.neo4j.driver.Logger;
2727
import org.neo4j.driver.Logging;
28+
import org.neo4j.driver.exceptions.SecurityException;
2829
import org.neo4j.driver.exceptions.ServiceUnavailableException;
2930
import org.neo4j.driver.internal.BoltServerAddress;
3031
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
@@ -57,7 +58,18 @@ public void operationComplete(ChannelFuture future) {
5758
var pipeline = channel.pipeline();
5859
pipeline.addLast(new HandshakeHandler(pipelineBuilder, handshakeCompletedPromise, logging));
5960
log.debug("C: [Bolt Handshake] %s", handshakeString());
60-
channel.writeAndFlush(handshakeBuf(), channel.voidPromise());
61+
channel.writeAndFlush(BoltProtocolUtil.handshakeBuf()).addListener(f -> {
62+
if (!f.isSuccess()) {
63+
var error = f.cause();
64+
if (error instanceof SSLHandshakeException) {
65+
error = new SecurityException("Failed to establish secured connection with the server", error);
66+
} else {
67+
error = new ServiceUnavailableException(
68+
String.format("Unable to write Bolt handshake to %s.", this.address), error);
69+
}
70+
this.handshakeCompletedPromise.setFailure(error);
71+
}
72+
});
6173
} else {
6274
handshakeCompletedPromise.setFailure(databaseUnavailableError(address, future.cause()));
6375
}

driver/src/main/java/org/neo4j/driver/internal/async/connection/ChannelConnectorImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ private void installHandshakeCompletedListeners(
142142

143143
// remove timeout handler from the pipeline once TLS and Bolt handshakes are completed. regular protocol
144144
// messages will flow next and we do not want to have read timeout for them
145-
handshakeCompleted.addListener(future -> pipeline.remove(ConnectTimeoutHandler.class));
145+
handshakeCompleted.addListener(future -> {
146+
if (future.isSuccess()) {
147+
pipeline.remove(ConnectTimeoutHandler.class);
148+
}
149+
});
146150

147151
// add listener that sends an INIT message. connection is now fully established. channel pipeline if fully
148152
// set to send/receive messages for a selected protocol version

driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.net.ServerSocket;
3030
import java.net.URI;
31+
import org.junit.jupiter.api.Disabled;
3132
import org.junit.jupiter.api.Test;
3233
import org.neo4j.driver.exceptions.ServiceUnavailableException;
3334
import org.neo4j.driver.internal.security.StaticAuthTokenManager;
@@ -92,6 +93,7 @@ void shouldFailToCreateUnencryptedDriverWhenServerDoesNotRespond() throws IOExce
9293
}
9394

9495
@Test
96+
@Disabled("TLS actually fails, the test setup is not valid")
9597
void shouldFailToCreateEncryptedDriverWhenServerDoesNotRespond() throws IOException {
9698
testFailureWhenServerDoesNotRespond(true);
9799
}

driver/src/test/java/org/neo4j/driver/integration/ChannelConnectorImplIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.concurrent.TimeUnit;
4141
import org.junit.jupiter.api.AfterEach;
4242
import org.junit.jupiter.api.BeforeEach;
43+
import org.junit.jupiter.api.Disabled;
4344
import org.junit.jupiter.api.Test;
4445
import org.junit.jupiter.api.extension.RegisterExtension;
4546
import org.neo4j.driver.AuthTokenManager;
@@ -158,6 +159,7 @@ void shouldFailWhenProtocolNegotiationTakesTooLong() throws Exception {
158159
}
159160

160161
@Test
162+
@Disabled("TLS actually fails, the test setup is not valid")
161163
void shouldFailWhenTLSHandshakeTakesTooLong() throws Exception {
162164
// run with TLS so that TLS handshake is the very first operation after connection is established
163165
testReadTimeoutOnConnect(trustAllCertificates());

driver/src/test/java/org/neo4j/driver/integration/EncryptionIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void shouldOperateWithEncryptionWhenItIsOptionalInTheDatabase() {
5353

5454
@Test
5555
void shouldFailWithoutEncryptionWhenItIsRequiredInTheDatabase() {
56-
testMismatchingEncryption(BoltTlsLevel.REQUIRED, false);
56+
testMismatchingEncryption(BoltTlsLevel.REQUIRED, false, "Connection to the database terminated");
5757
}
5858

5959
@Test
@@ -68,7 +68,7 @@ void shouldOperateWithEncryptionWhenConfiguredUsingBoltSscURI() {
6868

6969
@Test
7070
void shouldFailWithEncryptionWhenItIsDisabledInTheDatabase() {
71-
testMismatchingEncryption(BoltTlsLevel.DISABLED, true);
71+
testMismatchingEncryption(BoltTlsLevel.DISABLED, true, "Unable to write Bolt handshake to");
7272
}
7373

7474
@Test
@@ -104,7 +104,7 @@ var record = result.next();
104104
}
105105
}
106106

107-
private void testMismatchingEncryption(BoltTlsLevel tlsLevel, boolean driverEncrypted) {
107+
private void testMismatchingEncryption(BoltTlsLevel tlsLevel, boolean driverEncrypted, String errorMessage) {
108108
Map<String, String> tlsConfig = new HashMap<>();
109109
tlsConfig.put(Neo4jSettings.BOLT_TLS_LEVEL, tlsLevel.toString());
110110
neo4j.deleteAndStartNeo4j(tlsConfig);
@@ -115,7 +115,7 @@ private void testMismatchingEncryption(BoltTlsLevel tlsLevel, boolean driverEncr
115115
neo4j.uri(), neo4j.authTokenManager(), config)
116116
.verifyConnectivity());
117117

118-
assertThat(e.getMessage(), startsWith("Connection to the database terminated"));
118+
assertThat(e.getMessage(), startsWith(errorMessage));
119119
}
120120

121121
private static Config newConfig(boolean withEncryption) {

driver/src/test/java/org/neo4j/driver/internal/async/connection/ChannelConnectedListenerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.neo4j.driver.internal.async.connection;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2022
import static org.junit.jupiter.api.Assertions.assertNotNull;
2123
import static org.junit.jupiter.api.Assertions.assertThrows;
2224
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -27,7 +29,9 @@
2729

2830
import io.netty.channel.ChannelPromise;
2931
import io.netty.channel.embedded.EmbeddedChannel;
32+
import io.netty.util.concurrent.Future;
3033
import java.io.IOException;
34+
import java.util.concurrent.CompletableFuture;
3135
import org.junit.jupiter.api.AfterEach;
3236
import org.junit.jupiter.api.Test;
3337
import org.neo4j.driver.exceptions.ServiceUnavailableException;
@@ -70,6 +74,25 @@ void shouldWriteHandshakeWhenChannelConnected() {
7074
assertEquals(handshakeBuf(), channel.readOutbound());
7175
}
7276

77+
@Test
78+
void shouldCompleteHandshakePromiseExceptionallyOnWriteFailure() {
79+
var handshakeCompletedPromise = channel.newPromise();
80+
var listener = newListener(handshakeCompletedPromise);
81+
var channelConnectedPromise = channel.newPromise();
82+
channelConnectedPromise.setSuccess();
83+
channel.close();
84+
85+
listener.operationComplete(channelConnectedPromise);
86+
87+
assertTrue(handshakeCompletedPromise.isDone());
88+
var future = new CompletableFuture<Future<?>>();
89+
handshakeCompletedPromise.addListener(future::complete);
90+
var handshakeFuture = future.join();
91+
assertTrue(handshakeFuture.isDone());
92+
assertFalse(handshakeFuture.isSuccess());
93+
assertInstanceOf(ServiceUnavailableException.class, handshakeFuture.cause());
94+
}
95+
7396
private static ChannelConnectedListener newListener(ChannelPromise handshakeCompletedPromise) {
7497
return new ChannelConnectedListener(
7598
LOCAL_DEFAULT, new ChannelPipelineBuilderImpl(), handshakeCompletedPromise, DEV_NULL_LOGGING);

0 commit comments

Comments
 (0)