Skip to content

Commit 9d30dcc

Browse files
committed
Merge pull request #126 from zhenlineo/1.0-tls
Update config API for TLS to match DIP
2 parents f0b0504 + d99c21d commit 9d30dcc

24 files changed

+993
-387
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SSLContextFactory.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,34 @@
2727
import javax.net.ssl.TrustManagerFactory;
2828

2929
import org.neo4j.driver.v1.Config;
30+
import org.neo4j.driver.v1.exceptions.ClientException;
31+
import org.neo4j.driver.internal.spi.Logger;
3032

3133
import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;
3234

3335
class SSLContextFactory
3436
{
35-
3637
private final String host;
3738
private final int port;
38-
private final Config.TlsAuthenticationConfig authConfig;
39+
private final Config.TrustStrategy authConfig;
40+
private final Logger logger;
3941

40-
SSLContextFactory( String host, int port, Config.TlsAuthenticationConfig authConfig )
42+
SSLContextFactory( String host, int port, Config.TrustStrategy authConfig, Logger logger )
4143
{
4244
this.host = host;
4345
this.port = port;
4446
this.authConfig = authConfig;
47+
this.logger = logger;
4548
}
4649

4750
public SSLContext create()
4851
throws GeneralSecurityException, IOException
4952
{
5053
SSLContext sslContext = SSLContext.getInstance( "TLS" );
54+
TrustManager[] trustManagers;
5155

52-
// TODO Do we also want the server to verify the client's cert, a.k.a mutual authentication?
53-
// Ref: http://logicoy.com/blogs/ssl-keystore-truststore-and-mutual-authentication/
54-
KeyManager[] keyManagers = new KeyManager[0];
55-
TrustManager[] trustManagers = null;
56-
57-
if ( authConfig.isFullAuthEnabled() )
58-
{
56+
switch ( authConfig.strategy() ) {
57+
case TRUST_SIGNED_CERTIFICATES:
5958
// A certificate file is specified so we will load the certificates in the file
6059
// Init a in memory TrustedKeyStore
6160
KeyStore trustedKeyStore = KeyStore.getInstance( "JKS" );
@@ -68,13 +67,15 @@ public SSLContext create()
6867
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( "SunX509" );
6968
trustManagerFactory.init( trustedKeyStore );
7069
trustManagers = trustManagerFactory.getTrustManagers();
71-
}
72-
else
73-
{
74-
trustManagers = new TrustManager[]{new TrustOnFirstUseTrustManager( host, port, authConfig.certFile() )};
70+
break;
71+
case TRUST_ON_FIRST_USE:
72+
trustManagers = new TrustManager[]{new TrustOnFirstUseTrustManager( host, port, authConfig.certFile(), logger )};
73+
break;
74+
default:
75+
throw new ClientException( "Unknown TLS authentication strategy: " + authConfig.strategy().name() );
7576
}
7677

77-
sslContext.init( keyManagers, trustManagers, null );
78+
sslContext.init( new KeyManager[0], trustManagers, null );
7879
return sslContext;
7980
}
8081
}

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketClient.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,22 @@ public static ByteChannel create( String host, int port, Config config, Logger l
220220
soChannel.setOption( StandardSocketOptions.SO_KEEPALIVE, true );
221221
soChannel.connect( new InetSocketAddress( host, port ) );
222222

223-
ByteChannel channel = null;
223+
ByteChannel channel;
224224

225-
if( config.isTlsEnabled() )
225+
switch ( config.encryptionLevel() )
226226
{
227-
channel = new SSLSocketChannel( host, port, soChannel, logger, config.tlsAuthConfig() );
227+
case REQUIRED:
228+
{
229+
channel = new TLSSocketChannel( host, port, soChannel, logger, config.trustStrategy() );
230+
break;
228231
}
229-
else
232+
case NONE:
230233
{
231234
channel = new AllOrNothingChannel( soChannel );
235+
break;
236+
}
237+
default:
238+
throw new ClientException( "Unknown TLS Level: " + config.encryptionLevel() );
232239
}
233240

234241
if( logger.isTraceEnabled() )

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketConnector.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@
3030

3131
public class SocketConnector implements Connector
3232
{
33+
public static final String SCHEME = "bolt";
34+
public static final int DEFAULT_PORT = 7687;
35+
3336
@Override
3437
public boolean supports( String scheme )
3538
{
36-
return scheme.equals( Config.SCHEME );
39+
return scheme.equals( SCHEME );
3740
}
3841

3942
@Override
4043
public Connection connect( URI sessionURI, Config config ) throws ClientException
4144
{
42-
int port = sessionURI.getPort() == -1 ? Config.DEFAULT_PORT : sessionURI.getPort();
45+
int port = sessionURI.getPort() == -1 ? DEFAULT_PORT : sessionURI.getPort();
4346
SocketConnection conn = new SocketConnection( sessionURI.getHost(), port, config );
4447
conn.init( "bolt-java-driver/" + Version.driverVersion() );
4548
return conn;
@@ -48,6 +51,6 @@ public Connection connect( URI sessionURI, Config config ) throws ClientExceptio
4851
@Override
4952
public Collection<String> supportedSchemes()
5053
{
51-
return Collections.singletonList( Config.SCHEME );
54+
return Collections.singletonList( SCHEME );
5255
}
5356
}

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SSLSocketChannel.java renamed to driver/src/main/java/org/neo4j/driver/internal/connector/socket/TLSSocketChannel.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232

3333
import org.neo4j.driver.internal.spi.Logger;
3434
import org.neo4j.driver.internal.util.BytePrinter;
35-
import org.neo4j.driver.v1.Config.TlsAuthenticationConfig;
35+
import org.neo4j.driver.v1.Config.TrustStrategy;
3636
import org.neo4j.driver.v1.exceptions.ClientException;
3737

3838
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
3939
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
4040

4141
/**
42-
* A blocking SSL socket channel.
42+
* A blocking TLS socket channel.
4343
*
4444
* When debugging, we could enable JSSE system debugging by setting system property:
4545
* {@code -Djavax.net.debug=all} to value more information about handshake messages and other operations underway.
@@ -49,7 +49,7 @@
4949
* http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#SSLENG
5050
* http://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLEngine.html
5151
*/
52-
public class SSLSocketChannel implements ByteChannel
52+
public class TLSSocketChannel implements ByteChannel
5353
{
5454
private final SocketChannel channel; // The real channel the data is sent to and read from
5555
private final Logger logger;
@@ -64,25 +64,25 @@ public class SSLSocketChannel implements ByteChannel
6464
private ByteBuffer plainIn;
6565
private ByteBuffer plainOut;
6666

67-
public SSLSocketChannel( String host, int port, SocketChannel channel, Logger logger,
68-
TlsAuthenticationConfig authConfig )
67+
public TLSSocketChannel( String host, int port, SocketChannel channel, Logger logger,
68+
TrustStrategy trustStrategy )
6969
throws GeneralSecurityException, IOException
7070
{
7171
logger.debug( "TLS connection enabled" );
7272
this.logger = logger;
7373
this.channel = channel;
7474
this.channel.configureBlocking( true );
7575

76-
sslContext = new SSLContextFactory( host, port, authConfig ).create();
76+
sslContext = new SSLContextFactory( host, port, trustStrategy, logger ).create();
7777
createSSLEngine( host, port );
7878
createBuffers();
79-
runSSLHandShake();
79+
runHandshake();
8080
logger.debug( "TLS connection established" );
8181
}
8282

8383
/** Used in internal tests only */
84-
SSLSocketChannel( SocketChannel channel, Logger logger, SSLEngine sslEngine,
85-
ByteBuffer plainIn, ByteBuffer cipherIn, ByteBuffer plainOut, ByteBuffer cipherOut )
84+
TLSSocketChannel( SocketChannel channel, Logger logger, SSLEngine sslEngine,
85+
ByteBuffer plainIn, ByteBuffer cipherIn, ByteBuffer plainOut, ByteBuffer cipherOut )
8686
throws GeneralSecurityException, IOException
8787
{
8888
logger.debug( "Testing TLS buffers" );
@@ -109,7 +109,7 @@ public SSLSocketChannel( String host, int port, SocketChannel channel, Logger lo
109109
*
110110
* @throws IOException
111111
*/
112-
private void runSSLHandShake() throws IOException
112+
private void runHandshake() throws IOException
113113
{
114114
sslEngine.beginHandshake();
115115
HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();

0 commit comments

Comments
 (0)