Skip to content

Commit 7fccdc9

Browse files
author
Zhen Li
committed
Default to unencrypted connection on the driver.
Removed insecure TrustAllCerts SecurityPlan. Default to always perform client host name verification when encryption is on. Deleted some code used to support server version earlier than 3.3.0.
1 parent 97b1532 commit 7fccdc9

File tree

58 files changed

+228
-803
lines changed

Some content is hidden

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

58 files changed

+228
-803
lines changed

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

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

37-
import static org.neo4j.driver.Config.TrustStrategy.trustAllCertificates;
3837
import static org.neo4j.driver.Logging.javaUtilLogging;
3938

4039
/**
@@ -249,8 +248,8 @@ public static class ConfigBuilder
249248
private long idleTimeBeforeConnectionTest = PoolSettings.DEFAULT_IDLE_TIME_BEFORE_CONNECTION_TEST;
250249
private long maxConnectionLifetimeMillis = PoolSettings.DEFAULT_MAX_CONNECTION_LIFETIME;
251250
private long connectionAcquisitionTimeoutMillis = PoolSettings.DEFAULT_CONNECTION_ACQUISITION_TIMEOUT;
252-
private boolean encrypted = true;
253-
private TrustStrategy trustStrategy = trustAllCertificates();
251+
private boolean encrypted = false;
252+
private TrustStrategy trustStrategy = TrustStrategy.trustSystemCertificates();
254253
private int routingFailureLimit = RoutingSettings.DEFAULT.maxRoutingFailures();
255254
private long routingRetryDelayMillis = RoutingSettings.DEFAULT.retryTimeoutDelay();
256255
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
@@ -439,7 +438,7 @@ public ConfigBuilder withoutEncryption()
439438

440439
/**
441440
* 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()}.
441+
* This defaults to {@link TrustStrategy#trustSystemCertificates()}.
443442
* See {@link TrustStrategy#trustCustomCertificateSignedBy(File)} for using certificate signatures instead to verify
444443
* trust.
445444
* <p>
@@ -688,16 +687,13 @@ public static class TrustStrategy
688687
*/
689688
public enum Strategy
690689
{
691-
TRUST_ALL_CERTIFICATES,
692-
693690
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES,
694-
695691
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
696692
}
697693

698694
private final Strategy strategy;
699695
private final File certFile;
700-
private boolean hostnameVerificationEnabled;
696+
private boolean hostnameVerificationEnabled = true;
701697

702698
private TrustStrategy( Strategy strategy )
703699
{
@@ -786,16 +782,5 @@ public static TrustStrategy trustSystemCertificates()
786782
{
787783
return new TrustStrategy( Strategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES );
788784
}
789-
790-
/**
791-
* Trust strategy for certificates that can be verified through the local system store.
792-
*
793-
* @return an authentication config
794-
* @since 1.1
795-
*/
796-
public static TrustStrategy trustAllCertificates()
797-
{
798-
return new TrustStrategy( Strategy.TRUST_ALL_CERTIFICATES );
799-
}
800785
}
801786
}

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

Lines changed: 2 additions & 10 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() )
@@ -312,8 +306,6 @@ private static SecurityPlan createSecurityPlanImpl( BoltServerAddress address, C
312306
return SecurityPlan.forCustomCASignedCertificates( trustStrategy.certFile(), hostnameVerificationEnabled );
313307
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
314308
return SecurityPlan.forSystemCASignedCertificates( hostnameVerificationEnabled );
315-
case TRUST_ALL_CERTIFICATES:
316-
return SecurityPlan.forAllCertificates( hostnameVerificationEnabled );
317309
default:
318310
throw new ClientException(
319311
"Unknown TLS authentication strategy: " + trustStrategy.strategy().name() );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void build( MessageFormat messageFormat, ChannelPipeline pipeline, Loggin
3939
pipeline.addLast( new InboundMessageHandler( messageFormat, logging ) );
4040

4141
// outbound handlers
42-
pipeline.addLast( OutboundMessageHandler.NAME, new OutboundMessageHandler( messageFormat, logging ) );
42+
pipeline.addLast( new OutboundMessageHandler( messageFormat, logging ) );
4343

4444
// last one - error handler
4545
pipeline.addLast( new ChannelErrorHandler( logging ) );

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636

3737
public class OutboundMessageHandler extends MessageToMessageEncoder<Message>
3838
{
39-
public static final String NAME = OutboundMessageHandler.class.getSimpleName();
40-
41-
private final MessageFormat messageFormat;
4239
private final ChunkAwareByteBufOutput output;
4340
private final MessageFormat.Writer writer;
4441
private final Logging logging;
@@ -47,14 +44,8 @@ public class OutboundMessageHandler extends MessageToMessageEncoder<Message>
4744

4845
public OutboundMessageHandler( MessageFormat messageFormat, Logging logging )
4946
{
50-
this( messageFormat, true, logging );
51-
}
52-
53-
private OutboundMessageHandler( MessageFormat messageFormat, boolean byteArraySupportEnabled, Logging logging )
54-
{
55-
this.messageFormat = messageFormat;
5647
this.output = new ChunkAwareByteBufOutput();
57-
this.writer = messageFormat.newWriter( output, byteArraySupportEnabled );
48+
this.writer = messageFormat.newWriter( output );
5849
this.logging = logging;
5950
}
6051

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

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: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.security.NoSuchAlgorithmException;
2626
import javax.net.ssl.KeyManager;
2727
import javax.net.ssl.SSLContext;
28-
import javax.net.ssl.TrustManager;
2928
import javax.net.ssl.TrustManagerFactory;
3029

3130
import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;
@@ -35,14 +34,6 @@
3534
*/
3635
public class SecurityPlan
3736
{
38-
public static SecurityPlan forAllCertificates( boolean requiresHostnameVerification ) throws GeneralSecurityException
39-
{
40-
SSLContext sslContext = SSLContext.getInstance( "TLS" );
41-
sslContext.init( new KeyManager[0], new TrustManager[]{new TrustAllTrustManager()}, null );
42-
43-
return new SecurityPlan( true, sslContext, true, requiresHostnameVerification );
44-
}
45-
4637
public static SecurityPlan forCustomCASignedCertificates( File certFile, boolean requiresHostnameVerification )
4738
throws GeneralSecurityException, IOException
4839
{
@@ -61,29 +52,27 @@ public static SecurityPlan forCustomCASignedCertificates( File certFile, boolean
6152
SSLContext sslContext = SSLContext.getInstance( "TLS" );
6253
sslContext.init( new KeyManager[0], trustManagerFactory.getTrustManagers(), null );
6354

64-
return new SecurityPlan( true, sslContext, true, requiresHostnameVerification );
55+
return new SecurityPlan( true, sslContext, requiresHostnameVerification );
6556
}
6657

6758
public static SecurityPlan forSystemCASignedCertificates( boolean requiresHostnameVerification ) throws NoSuchAlgorithmException
6859
{
69-
return new SecurityPlan( true, SSLContext.getDefault(), true, requiresHostnameVerification );
60+
return new SecurityPlan( true, SSLContext.getDefault(), requiresHostnameVerification );
7061
}
7162

7263
public static SecurityPlan insecure()
7364
{
74-
return new SecurityPlan( false, null, true, false );
65+
return new SecurityPlan( false, null, false );
7566
}
7667

7768
private final boolean requiresEncryption;
7869
private final SSLContext sslContext;
79-
private final boolean routingCompatible;
8070
private final boolean requiresHostnameVerification;
8171

82-
private SecurityPlan( boolean requiresEncryption, SSLContext sslContext, boolean routingCompatible, boolean requiresHostnameVerification )
72+
private SecurityPlan( boolean requiresEncryption, SSLContext sslContext, boolean requiresHostnameVerification )
8373
{
8474
this.requiresEncryption = requiresEncryption;
8575
this.sslContext = sslContext;
86-
this.routingCompatible = routingCompatible;
8776
this.requiresHostnameVerification = requiresHostnameVerification;
8877
}
8978

@@ -92,11 +81,6 @@ public boolean requiresEncryption()
9281
return requiresEncryption;
9382
}
9483

95-
public boolean isRoutingCompatible()
96-
{
97-
return routingCompatible;
98-
}
99-
10084
public SSLContext sslContext()
10185
{
10286
return sslContext;

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)