Skip to content

Commit cde37b8

Browse files
authored
Merge pull request #620 from zhenlineo/2.0-default-unencrypted
Default to unencrypted connection on the driver.
2 parents f609279 + a0857fd commit cde37b8

File tree

51 files changed

+169
-761
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+169
-761
lines changed

driver/src/main/java/org/neo4j/driver/Config.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.neo4j.driver.util.Immutable;
3535
import org.neo4j.driver.util.Resource;
3636

37-
import static org.neo4j.driver.Config.TrustStrategy.trustAllCertificates;
37+
import static org.neo4j.driver.Config.TrustStrategy.trustSystemCertificates;
3838
import static org.neo4j.driver.Logging.javaUtilLogging;
3939

4040
/**
@@ -249,8 +249,8 @@ public static class ConfigBuilder
249249
private long idleTimeBeforeConnectionTest = PoolSettings.DEFAULT_IDLE_TIME_BEFORE_CONNECTION_TEST;
250250
private long maxConnectionLifetimeMillis = PoolSettings.DEFAULT_MAX_CONNECTION_LIFETIME;
251251
private long connectionAcquisitionTimeoutMillis = PoolSettings.DEFAULT_CONNECTION_ACQUISITION_TIMEOUT;
252-
private boolean encrypted = true;
253-
private TrustStrategy trustStrategy = trustAllCertificates();
252+
private boolean encrypted = false;
253+
private TrustStrategy trustStrategy = trustSystemCertificates();
254254
private int routingFailureLimit = RoutingSettings.DEFAULT.maxRoutingFailures();
255255
private long routingRetryDelayMillis = RoutingSettings.DEFAULT.retryTimeoutDelay();
256256
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
@@ -439,7 +439,7 @@ public ConfigBuilder withoutEncryption()
439439

440440
/**
441441
* Specify how to determine the authenticity of an encryption certificate provided by the Neo4j instance we are connecting to.
442-
* This defaults to {@link TrustStrategy#trustAllCertificates()}.
442+
* This defaults to {@link TrustStrategy#trustSystemCertificates()}.
443443
* See {@link TrustStrategy#trustCustomCertificateSignedBy(File)} for using certificate signatures instead to verify
444444
* trust.
445445
* <p>
@@ -689,15 +689,13 @@ public static class TrustStrategy
689689
public enum Strategy
690690
{
691691
TRUST_ALL_CERTIFICATES,
692-
693692
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES,
694-
695693
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
696694
}
697695

698696
private final Strategy strategy;
699697
private final File certFile;
700-
private boolean hostnameVerificationEnabled;
698+
private boolean hostnameVerificationEnabled = true;
701699

702700
private TrustStrategy( Strategy strategy )
703701
{
@@ -788,7 +786,7 @@ public static TrustStrategy trustSystemCertificates()
788786
}
789787

790788
/**
791-
* Trust strategy for certificates that can be verified through the local system store.
789+
* Trust strategy for certificates that trust all certificates blindly. Suggested to only use this in tests.
792790
*
793791
* @return an authentication config
794792
* @since 1.1

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ protected InternalDriver createDirectDriver( SecurityPlan securityPlan, BoltServ
186186
protected InternalDriver createRoutingDriver( SecurityPlan securityPlan, BoltServerAddress address, ConnectionPool connectionPool,
187187
EventExecutorGroup eventExecutorGroup, RoutingSettings routingSettings, RetryLogic retryLogic, MetricsProvider metricsProvider, Config config )
188188
{
189-
if ( !securityPlan.isRoutingCompatible() )
190-
{
191-
throw new IllegalArgumentException( "The chosen security plan is not compatible with a routing driver" );
192-
}
193189
ConnectionProvider connectionProvider = createLoadBalancer( address, connectionPool, eventExecutorGroup,
194190
config, routingSettings );
195191
SessionFactory sessionFactory = createSessionFactory( connectionProvider, retryLogic, config );
@@ -285,7 +281,7 @@ private static SecurityPlan createSecurityPlan( BoltServerAddress address, Confi
285281
{
286282
try
287283
{
288-
return createSecurityPlanImpl( address, config );
284+
return createSecurityPlanImpl( config );
289285
}
290286
catch ( GeneralSecurityException | IOException ex )
291287
{
@@ -297,13 +293,11 @@ private static SecurityPlan createSecurityPlan( BoltServerAddress address, Confi
297293
* Establish a complete SecurityPlan based on the details provided for
298294
* driver construction.
299295
*/
300-
@SuppressWarnings( "deprecation" )
301-
private static SecurityPlan createSecurityPlanImpl( BoltServerAddress address, Config config )
296+
private static SecurityPlan createSecurityPlanImpl( Config config )
302297
throws GeneralSecurityException, IOException
303298
{
304299
if ( config.encrypted() )
305300
{
306-
Logger logger = config.logging().getLog( "SecurityPlan" );
307301
Config.TrustStrategy trustStrategy = config.trustStrategy();
308302
boolean hostnameVerificationEnabled = trustStrategy.isHostnameVerificationEnabled();
309303
switch ( trustStrategy.strategy() )

driver/src/main/java/org/neo4j/driver/internal/async/outbound/OutboundMessageHandler.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
public class OutboundMessageHandler extends MessageToMessageEncoder<Message>
3838
{
3939
public static final String NAME = OutboundMessageHandler.class.getSimpleName();
40-
41-
private final MessageFormat messageFormat;
4240
private final ChunkAwareByteBufOutput output;
4341
private final MessageFormat.Writer writer;
4442
private final Logging logging;
@@ -47,14 +45,8 @@ public class OutboundMessageHandler extends MessageToMessageEncoder<Message>
4745

4846
public OutboundMessageHandler( MessageFormat messageFormat, Logging logging )
4947
{
50-
this( messageFormat, true, logging );
51-
}
52-
53-
private OutboundMessageHandler( MessageFormat messageFormat, boolean byteArraySupportEnabled, Logging logging )
54-
{
55-
this.messageFormat = messageFormat;
5648
this.output = new ChunkAwareByteBufOutput();
57-
this.writer = messageFormat.newWriter( output, byteArraySupportEnabled );
49+
this.writer = messageFormat.newWriter( output );
5850
this.logging = logging;
5951
}
6052

@@ -98,9 +90,4 @@ protected void encode( ChannelHandlerContext ctx, Message msg, List<Object> out
9890
BoltProtocolUtil.writeMessageBoundary( messageBuf );
9991
out.add( messageBuf );
10092
}
101-
102-
public OutboundMessageHandler withoutByteArraySupport()
103-
{
104-
return new OutboundMessageHandler( messageFormat, false, logging );
105-
}
10693
}

driver/src/main/java/org/neo4j/driver/internal/handlers/InitResponseHandler.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public void onSuccess( Map<String,Value> metadata )
5050
{
5151
ServerVersion serverVersion = extractNeo4jServerVersion( metadata );
5252
setServerVersion( channel, serverVersion );
53-
updatePipelineIfNeeded( serverVersion, channel.pipeline() );
5453
connectionInitializedPromise.setSuccess();
5554
}
5655
catch ( Throwable error )
@@ -71,16 +70,4 @@ public void onRecord( Value[] fields )
7170
{
7271
throw new UnsupportedOperationException();
7372
}
74-
75-
private static void updatePipelineIfNeeded( ServerVersion serverVersion, ChannelPipeline pipeline )
76-
{
77-
if ( serverVersion.lessThan( ServerVersion.v3_2_0 ) )
78-
{
79-
OutboundMessageHandler outboundHandler = pipeline.get( OutboundMessageHandler.class );
80-
if ( outboundHandler != null )
81-
{
82-
pipeline.replace( outboundHandler, OutboundMessageHandler.NAME, outboundHandler.withoutByteArraySupport() );
83-
}
84-
}
85-
}
8673
}

driver/src/main/java/org/neo4j/driver/internal/messaging/MessageFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface Reader
3535
void read( ResponseMessageHandler handler ) throws IOException;
3636
}
3737

38-
Writer newWriter( PackOutput output, boolean byteArraySupportEnabled );
38+
Writer newWriter( PackOutput output );
3939

4040
Reader newReader( PackInput input );
4141
}

driver/src/main/java/org/neo4j/driver/internal/messaging/v1/MessageFormatV1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class MessageFormatV1 implements MessageFormat
3232
public static final int NODE_FIELDS = 3;
3333

3434
@Override
35-
public MessageFormat.Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
35+
public MessageFormat.Writer newWriter( PackOutput output )
3636
{
37-
return new MessageWriterV1( output, byteArraySupportEnabled );
37+
return new MessageWriterV1( output );
3838
}
3939

4040
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/v1/MessageWriterV1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838

3939
public class MessageWriterV1 extends AbstractMessageWriter
4040
{
41-
public MessageWriterV1( PackOutput output, boolean byteArraySupportEnabled )
41+
public MessageWriterV1( PackOutput output )
4242
{
43-
this( new ValuePackerV1( output, byteArraySupportEnabled ) );
43+
this( new ValuePackerV1( output ) );
4444
}
4545

4646
protected MessageWriterV1( ValuePacker packer )

driver/src/main/java/org/neo4j/driver/internal/messaging/v1/ValuePackerV1.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ public class ValuePackerV1 implements ValuePacker
3131
{
3232
protected final PackStream.Packer packer;
3333

34-
private final boolean byteArraySupportEnabled;
35-
36-
public ValuePackerV1( PackOutput output, boolean byteArraySupportEnabled )
34+
public ValuePackerV1( PackOutput output )
3735
{
3836
this.packer = new PackStream.Packer( output );
39-
this.byteArraySupportEnabled = byteArraySupportEnabled;
4037
}
4138

4239
@Override
@@ -89,11 +86,6 @@ protected void packInternalValue( InternalValue value ) throws IOException
8986
break;
9087

9188
case BYTES:
92-
if ( !byteArraySupportEnabled )
93-
{
94-
throw new PackStream.UnPackable(
95-
"Packing bytes is not supported as the current server this driver connected to does not support unpack bytes." );
96-
}
9789
packer.pack( value.asByteArray() );
9890
break;
9991

driver/src/main/java/org/neo4j/driver/internal/messaging/v2/MessageFormatV2.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ public class MessageFormatV2 extends MessageFormatV1
5050
public static final int POINT_3D_STRUCT_SIZE = 4;
5151

5252
@Override
53-
public Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
53+
public Writer newWriter( PackOutput output )
5454
{
55-
if ( !byteArraySupportEnabled )
56-
{
57-
throw new IllegalArgumentException( "Bolt V2 should support byte arrays" );
58-
}
5955
return new MessageWriterV2( output );
6056
}
6157

driver/src/main/java/org/neo4j/driver/internal/messaging/v2/ValuePackerV2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class ValuePackerV2 extends ValuePackerV1
5959
{
6060
public ValuePackerV2( PackOutput output )
6161
{
62-
super( output, true );
62+
super( output );
6363
}
6464

6565
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/v3/MessageFormatV3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class MessageFormatV3 implements MessageFormat
2727
{
2828
@Override
29-
public Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
29+
public Writer newWriter( PackOutput output )
3030
{
3131
return new MessageWriterV3( output );
3232
}

driver/src/main/java/org/neo4j/driver/internal/messaging/v4/MessageFormatV4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class MessageFormatV4 implements MessageFormat
2727
{
2828
@Override
29-
public Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
29+
public Writer newWriter( PackOutput output )
3030
{
3131
return new MessageWriterV4( output );
3232
}

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import java.security.GeneralSecurityException;
2424
import java.security.KeyStore;
2525
import java.security.NoSuchAlgorithmException;
26+
import java.security.cert.CertificateException;
27+
import java.security.cert.X509Certificate;
2628
import javax.net.ssl.KeyManager;
2729
import javax.net.ssl.SSLContext;
2830
import javax.net.ssl.TrustManager;
2931
import javax.net.ssl.TrustManagerFactory;
32+
import javax.net.ssl.X509TrustManager;
3033

3134
import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;
3235

@@ -40,7 +43,7 @@ public static SecurityPlan forAllCertificates( boolean requiresHostnameVerificat
4043
SSLContext sslContext = SSLContext.getInstance( "TLS" );
4144
sslContext.init( new KeyManager[0], new TrustManager[]{new TrustAllTrustManager()}, null );
4245

43-
return new SecurityPlan( true, sslContext, true, requiresHostnameVerification );
46+
return new SecurityPlan( true, sslContext, requiresHostnameVerification );
4447
}
4548

4649
public static SecurityPlan forCustomCASignedCertificates( File certFile, boolean requiresHostnameVerification )
@@ -61,29 +64,27 @@ public static SecurityPlan forCustomCASignedCertificates( File certFile, boolean
6164
SSLContext sslContext = SSLContext.getInstance( "TLS" );
6265
sslContext.init( new KeyManager[0], trustManagerFactory.getTrustManagers(), null );
6366

64-
return new SecurityPlan( true, sslContext, true, requiresHostnameVerification );
67+
return new SecurityPlan( true, sslContext, requiresHostnameVerification );
6568
}
6669

6770
public static SecurityPlan forSystemCASignedCertificates( boolean requiresHostnameVerification ) throws NoSuchAlgorithmException
6871
{
69-
return new SecurityPlan( true, SSLContext.getDefault(), true, requiresHostnameVerification );
72+
return new SecurityPlan( true, SSLContext.getDefault(), requiresHostnameVerification );
7073
}
7174

7275
public static SecurityPlan insecure()
7376
{
74-
return new SecurityPlan( false, null, true, false );
77+
return new SecurityPlan( false, null, false );
7578
}
7679

7780
private final boolean requiresEncryption;
7881
private final SSLContext sslContext;
79-
private final boolean routingCompatible;
8082
private final boolean requiresHostnameVerification;
8183

82-
private SecurityPlan( boolean requiresEncryption, SSLContext sslContext, boolean routingCompatible, boolean requiresHostnameVerification )
84+
private SecurityPlan( boolean requiresEncryption, SSLContext sslContext, boolean requiresHostnameVerification )
8385
{
8486
this.requiresEncryption = requiresEncryption;
8587
this.sslContext = sslContext;
86-
this.routingCompatible = routingCompatible;
8788
this.requiresHostnameVerification = requiresHostnameVerification;
8889
}
8990

@@ -92,11 +93,6 @@ public boolean requiresEncryption()
9293
return requiresEncryption;
9394
}
9495

95-
public boolean isRoutingCompatible()
96-
{
97-
return routingCompatible;
98-
}
99-
10096
public SSLContext sslContext()
10197
{
10298
return sslContext;
@@ -106,4 +102,22 @@ public boolean requiresHostnameVerification()
106102
{
107103
return requiresHostnameVerification;
108104
}
105+
106+
private static class TrustAllTrustManager implements X509TrustManager
107+
{
108+
public void checkClientTrusted( X509Certificate[] chain, String authType ) throws CertificateException
109+
{
110+
throw new CertificateException( "All client connections to this client are forbidden." );
111+
}
112+
113+
public void checkServerTrusted( X509Certificate[] chain, String authType ) throws CertificateException
114+
{
115+
// all fine, pass through
116+
}
117+
118+
public X509Certificate[] getAcceptedIssuers()
119+
{
120+
return new X509Certificate[0];
121+
}
122+
}
109123
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)