Skip to content

Commit 45c2930

Browse files
authored
Merge pull request #391 from praveenag/1.4
better exception messages when database is down.
2 parents 8935542 + dd710ec commit 45c2930

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

driver/src/main/java/org/neo4j/driver/internal/security/TLSSocketChannel.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class TLSSocketChannel implements ByteChannel
5353
{
5454
private final ByteChannel channel; // The real channel the data is sent to and read from
5555
private final Logger logger;
56+
private final BoltServerAddress address;
5657

5758
private SSLEngine sslEngine;
5859

@@ -65,17 +66,20 @@ public class TLSSocketChannel implements ByteChannel
6566

6667
private static final ByteBuffer DUMMY_BUFFER = ByteBuffer.allocate( 0 );
6768

68-
public static TLSSocketChannel create( BoltServerAddress address, SecurityPlan securityPlan, ByteChannel channel, Logger logger )
69+
public static TLSSocketChannel create( BoltServerAddress address, SecurityPlan securityPlan,
70+
ByteChannel channel, Logger logger )
6971
throws IOException
7072
{
7173
SSLEngine sslEngine = securityPlan.sslContext().createSSLEngine( address.host(), address.port() );
7274
sslEngine.setUseClientMode( true );
73-
return create( channel, logger, sslEngine );
75+
return create( channel, logger, sslEngine, address );
7476
}
7577

76-
public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEngine sslEngine ) throws IOException
78+
public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEngine sslEngine,
79+
BoltServerAddress address ) throws IOException
7780
{
78-
TLSSocketChannel tlsChannel = new TLSSocketChannel( channel, logger, sslEngine );
81+
82+
TLSSocketChannel tlsChannel = new TLSSocketChannel( channel, logger, sslEngine, address );
7983
try
8084
{
8185
tlsChannel.runHandshake();
@@ -87,9 +91,10 @@ public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEn
8791
return tlsChannel;
8892
}
8993

90-
TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine )
94+
TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine, BoltServerAddress address )
9195
throws IOException
9296
{
97+
this.address = address;
9398
this.logger = logger;
9499
this.channel = channel;
95100
this.sslEngine = sslEngine;
@@ -167,9 +172,10 @@ int channelRead( ByteBuffer toBuffer ) throws IOException
167172
{
168173
// best effort
169174
}
175+
170176
throw new ServiceUnavailableException(
171-
"SSL Connection terminated while receiving data. " +
172-
"This can happen due to network instabilities, or due to restarts of the database." );
177+
"Failed to receive any data from the connected address " + address + ". " +
178+
"Please ensure a working connection to the database." );
173179
}
174180
return read;
175181
}
@@ -193,8 +199,8 @@ int channelWrite( ByteBuffer fromBuffer ) throws IOException
193199
// best effort
194200
}
195201
throw new ServiceUnavailableException(
196-
"SSL Connection terminated while writing data. " +
197-
"This can happen due to network instabilities, or due to restarts of the database." );
202+
"Failed to send any data to the connected address " + address + ". " +
203+
"Please ensure a working connection to the database." );
198204
}
199205
return written;
200206
}

driver/src/test/java/org/neo4j/driver/internal/security/TLSSocketChannelTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.net.ssl.SSLHandshakeException;
2727
import javax.net.ssl.SSLSession;
2828

29+
import org.neo4j.driver.internal.net.BoltServerAddress;
2930
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
3031
import org.neo4j.driver.v1.exceptions.SecurityException;
3132

@@ -40,6 +41,7 @@
4041
import static org.mockito.Mockito.verify;
4142
import static org.mockito.Mockito.when;
4243
import static org.neo4j.driver.internal.logging.DevNullLogger.DEV_NULL_LOGGER;
44+
import static org.neo4j.driver.internal.net.BoltServerAddress.LOCAL_DEFAULT;
4345
import static org.neo4j.driver.internal.security.TLSSocketChannel.create;
4446

4547
public class TLSSocketChannelTest
@@ -59,7 +61,7 @@ public void shouldCloseConnectionIfFailedToRead() throws Throwable
5961
when( mockedSslSession.getPacketBufferSize() ).thenReturn( 10 );
6062

6163
// When
62-
TLSSocketChannel channel = new TLSSocketChannel( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine );
64+
TLSSocketChannel channel = new TLSSocketChannel( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine, LOCAL_DEFAULT );
6365

6466
try
6567
{
@@ -69,7 +71,8 @@ public void shouldCloseConnectionIfFailedToRead() throws Throwable
6971
catch( Exception e )
7072
{
7173
assertThat( e, instanceOf( ServiceUnavailableException.class ) );
72-
assertThat( e.getMessage(), startsWith( "SSL Connection terminated while receiving data. " ) );
74+
assertThat( e.getMessage(), startsWith( "Failed to receive any data from the connected address " +
75+
"localhost:7687. Please ensure a working connection to the database." ) );
7376
}
7477
// Then
7578
verify( mockedChannel ).close();
@@ -89,7 +92,7 @@ public void shouldCloseConnectionIfFailedToWrite() throws Throwable
8992
when( mockedSslSession.getPacketBufferSize() ).thenReturn( 10 );
9093

9194
// When
92-
TLSSocketChannel channel = new TLSSocketChannel( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine );
95+
TLSSocketChannel channel = new TLSSocketChannel( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine, LOCAL_DEFAULT );
9396

9497
try
9598
{
@@ -99,7 +102,8 @@ public void shouldCloseConnectionIfFailedToWrite() throws Throwable
99102
catch( Exception e )
100103
{
101104
assertThat( e, instanceOf( ServiceUnavailableException.class ) );
102-
assertThat( e.getMessage(), startsWith( "SSL Connection terminated while writing data. " ) );
105+
assertThat( e.getMessage(), startsWith( "Failed to send any data to the connected address localhost:7687. " +
106+
"Please ensure a working connection to the database." ) );
103107
}
104108

105109
// Then
@@ -123,7 +127,7 @@ public void shouldThrowUnauthorizedIfFailedToHandshake() throws Throwable
123127
// When & Then
124128
try
125129
{
126-
create( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine );
130+
create( mockedChannel, DEV_NULL_LOGGER, mockedSslEngine, LOCAL_DEFAULT );
127131
fail( "Should fail to run handshake" );
128132
}
129133
catch( Exception e )

driver/src/test/java/org/neo4j/driver/v1/integration/TLSSocketChannelReadFragmentationIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
import javax.net.ssl.SSLContext;
3030
import javax.net.ssl.SSLEngine;
3131

32+
import org.neo4j.driver.internal.net.BoltServerAddress;
3233
import org.neo4j.driver.internal.security.TLSSocketChannel;
3334

3435
import static org.hamcrest.core.IsEqual.equalTo;
3536
import static org.junit.Assert.assertThat;
3637
import static org.neo4j.driver.internal.logging.DevNullLogger.DEV_NULL_LOGGER;
38+
import static org.neo4j.driver.internal.net.BoltServerAddress.LOCAL_DEFAULT;
3739

3840
/**
3941
* This tests that the TLSSocketChannel handles every combination of network buffer sizes that we
@@ -58,7 +60,7 @@ protected void testForBufferSizes( byte[] blobOfData, int networkFrameSize, int
5860
SocketAddress address = new InetSocketAddress( serverSocket.getInetAddress(), serverSocket.getLocalPort() );
5961
ByteChannel ch = new LittleAtATimeChannel( SocketChannel.open( address ), networkFrameSize );
6062

61-
try ( TLSSocketChannel channel = TLSSocketChannel.create( ch, DEV_NULL_LOGGER, engine ) )
63+
try ( TLSSocketChannel channel = TLSSocketChannel.create( ch, DEV_NULL_LOGGER, engine, LOCAL_DEFAULT ) )
6264
{
6365
ByteBuffer readBuffer = ByteBuffer.allocate( blobOfData.length );
6466
while ( readBuffer.position() < readBuffer.capacity() )

driver/src/test/java/org/neo4j/driver/v1/integration/TLSSocketChannelWriteFragmentationIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import javax.net.ssl.SSLContext;
3131
import javax.net.ssl.SSLEngine;
3232

33+
import org.neo4j.driver.internal.net.BoltServerAddress;
3334
import org.neo4j.driver.internal.security.TLSSocketChannel;
3435

3536
import static org.hamcrest.core.IsEqual.equalTo;
3637
import static org.junit.Assert.assertEquals;
3738
import static org.junit.Assert.assertThat;
3839
import static org.neo4j.driver.internal.logging.DevNullLogger.DEV_NULL_LOGGER;
40+
import static org.neo4j.driver.internal.net.BoltServerAddress.LOCAL_DEFAULT;
3941

4042
/**
4143
* This tests that the TLSSocketChannel handles every combination of network buffer sizes that we
@@ -60,7 +62,7 @@ protected void testForBufferSizes( byte[] blobOfData, int networkFrameSize, int
6062
SocketAddress address = new InetSocketAddress( serverSocket.getInetAddress(), serverSocket.getLocalPort() );
6163
ByteChannel ch = new LittleAtATimeChannel( SocketChannel.open( address ), networkFrameSize );
6264

63-
try ( TLSSocketChannel channel = TLSSocketChannel.create( ch, DEV_NULL_LOGGER, engine ) )
65+
try ( TLSSocketChannel channel = TLSSocketChannel.create( ch, DEV_NULL_LOGGER, engine, LOCAL_DEFAULT ) )
6466
{
6567
ByteBuffer writeBuffer = ByteBuffer.wrap( blobOfData );
6668
while ( writeBuffer.position() < writeBuffer.capacity() )

0 commit comments

Comments
 (0)