Skip to content

Commit 3007c4a

Browse files
author
Zhen
committed
Fix the error code for ssl connections
If failing to auth server to client/client to server, then fail with clientException If failing to establish connection with the server, then fail with ServiceUnavailableException If the connects between the server and the client breaks, then fail with ServiceUnavailableException
1 parent 4b99a13 commit 3007c4a

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

driver/src/main/java/org/neo4j/driver/internal/net/ChannelFactory.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ static ByteChannel create( BoltServerAddress address, SecurityPlan securityPlan,
4040
soChannel.setOption( StandardSocketOptions.SO_KEEPALIVE, true );
4141
connect( soChannel, address, timeoutMillis );
4242

43-
ByteChannel channel;
43+
ByteChannel channel = soChannel;
4444

4545
if ( securityPlan.requiresEncryption() )
4646
{
4747
channel = TLSSocketChannel.create( address, securityPlan, soChannel, log );
4848
}
49-
else
50-
{
51-
channel = soChannel;
52-
}
5349

5450
if ( log.isTraceEnabled() )
5551
{

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.net.ssl.SSLEngineResult;
2626
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
2727
import javax.net.ssl.SSLEngineResult.Status;
28+
import javax.net.ssl.SSLHandshakeException;
2829

2930
import org.neo4j.driver.internal.net.BoltServerAddress;
3031
import org.neo4j.driver.v1.Logger;
@@ -70,7 +71,8 @@ public static TLSSocketChannel create( BoltServerAddress address, SecurityPlan s
7071
return new TLSSocketChannel( channel, logger, sslEngine );
7172
}
7273

73-
public TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine ) throws IOException
74+
public TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine )
75+
throws IOException
7476
{
7577
this.logger = logger;
7678
this.channel = channel;
@@ -79,7 +81,21 @@ public TLSSocketChannel( ByteChannel channel, Logger logger, SSLEngine sslEngine
7981
this.cipherIn = ByteBuffer.allocate( sslEngine.getSession().getPacketBufferSize() );
8082
this.plainOut = ByteBuffer.allocate( sslEngine.getSession().getApplicationBufferSize() );
8183
this.cipherOut = ByteBuffer.allocate( sslEngine.getSession().getPacketBufferSize() );
82-
runHandshake();
84+
85+
try
86+
{
87+
runHandshake();
88+
}
89+
catch ( SSLHandshakeException e )
90+
{
91+
Throwable error = e;
92+
if( error.getCause() != null ) // get the real exception
93+
{
94+
error = e.getCause();
95+
}
96+
throw new ClientException( "Failed to establish secured connection with the server: " + error.getMessage(),
97+
error.getCause() );
98+
}
8399
}
84100

85101
/**
@@ -164,6 +180,14 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
164180
*/
165181
if ( channel.read( cipherIn ) < 0 )
166182
{
183+
try
184+
{
185+
channel.close();
186+
}
187+
catch( IOException e )
188+
{
189+
// best effort
190+
}
167191
throw new ServiceUnavailableException(
168192
"SSL Connection terminated while receiving data. " +
169193
"This can happen due to network instabilities, or due to restarts of the database." );
@@ -289,14 +313,19 @@ private HandshakeStatus wrap( ByteBuffer buffer ) throws IOException
289313
// flush as much data as possible
290314
cipherOut.flip();
291315
int written = channel.write( cipherOut );
292-
if (written == 0)
316+
if (written <= 0)
293317
{
294-
throw new ClientException(
295-
String.format(
296-
"Failed to enlarge network buffer from %s to %s. This is either because the " +
297-
"new size is however less than the old size, or because the application " +
298-
"buffer size %s is so big that the application data still cannot fit into the " +
299-
"new network buffer.", curNetSize, netSize, buffer.capacity() ) );
318+
try
319+
{
320+
channel.close();
321+
}
322+
catch( IOException e )
323+
{
324+
// best effort
325+
}
326+
throw new ServiceUnavailableException(
327+
"SSL Connection terminated while writing data. " +
328+
"This can happen due to network instabilities, or due to restarts of the database." );
300329
}
301330
cipherOut.compact();
302331
logger.debug( "Network output buffer couldn't be enlarged, flushing data to the channel instead." );

0 commit comments

Comments
 (0)