Skip to content

Commit e2e4574

Browse files
authored
Merge pull request #218 from pontusmelke/1.0-cleanup-tls-opts
Deprecate TRUST_SIGNED_CERTIFICATES
2 parents 0f04591 + cf3dd8f commit e2e4574

File tree

6 files changed

+173
-73
lines changed

6 files changed

+173
-73
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import javax.net.ssl.TrustManagerFactory;
2828

2929
import org.neo4j.driver.v1.Config;
30-
import org.neo4j.driver.v1.exceptions.ClientException;
3130
import org.neo4j.driver.v1.Logger;
31+
import org.neo4j.driver.v1.exceptions.ClientException;
3232

3333
import static org.neo4j.driver.internal.util.CertificateTool.loadX509Cert;
3434

@@ -55,6 +55,10 @@ public SSLContext create()
5555

5656
switch ( authConfig.strategy() ) {
5757
case TRUST_SIGNED_CERTIFICATES:
58+
logger.warn( "Option `TRUST_SIGNED_CERTIFICATE` has been deprecated and will be removed in a future version " +
59+
"of the driver. Please switch to use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." );
60+
//intentional fallthrough
61+
case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
5862
// A certificate file is specified so we will load the certificates in the file
5963
// Init a in memory TrustedKeyStore
6064
KeyStore trustedKeyStore = KeyStore.getInstance( "JKS" );
@@ -67,7 +71,13 @@ public SSLContext create()
6771
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( "SunX509" );
6872
trustManagerFactory.init( trustedKeyStore );
6973
trustManagers = trustManagerFactory.getTrustManagers();
74+
7075
break;
76+
77+
//just rely on system defaults
78+
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
79+
return SSLContext.getDefault();
80+
7181
case TRUST_ON_FIRST_USE:
7282
trustManagers = new TrustManager[]{new TrustOnFirstUseTrustManager( host, port, authConfig.certFile(), logger )};
7383
break;

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.neo4j.driver.v1.util.Immutable;
2626

2727
import static java.lang.System.getProperty;
28-
import static org.neo4j.driver.v1.Config.TrustStrategy.*;
28+
import static org.neo4j.driver.v1.Config.TrustStrategy.trustOnFirstUse;
2929

3030
/**
3131
* A configuration class to config driver properties.
@@ -243,7 +243,7 @@ public ConfigBuilder withEncryptionLevel( EncryptionLevel level )
243243
/**
244244
* Specify how to determine the authenticity of an encryption certificate provided by the Neo4j instance we are connecting to.
245245
* This defaults to {@link TrustStrategy#trustOnFirstUse(File)}.
246-
* See {@link TrustStrategy#trustSignedBy(File)} for using certificate signatures instead to verify
246+
* See {@link TrustStrategy#trustCustomCertificateSignedBy(File)} for using certificate signatures instead to verify
247247
* trust.
248248
* <p>
249249
* This is an important setting to understand, because unless we know that the remote server we have an encrypted connection to
@@ -290,12 +290,20 @@ public static class TrustStrategy
290290
public enum Strategy
291291
{
292292
TRUST_ON_FIRST_USE,
293-
TRUST_SIGNED_CERTIFICATES
293+
@Deprecated
294+
TRUST_SIGNED_CERTIFICATES,
295+
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES,
296+
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
294297
}
295298

296299
private final Strategy strategy;
297300
private final File certFile;
298301

302+
private TrustStrategy( Strategy strategy )
303+
{
304+
this( strategy, null );
305+
}
306+
299307
private TrustStrategy( Strategy strategy, File certFile )
300308
{
301309
this.strategy = strategy;
@@ -316,6 +324,15 @@ public File certFile()
316324
return certFile;
317325
}
318326

327+
/**
328+
* Use {@link #trustCustomCertificateSignedBy(File)} instead.
329+
*/
330+
@Deprecated
331+
public static TrustStrategy trustSignedBy( File certFile )
332+
{
333+
return new TrustStrategy( Strategy.TRUST_SIGNED_CERTIFICATES, certFile );
334+
}
335+
319336
/**
320337
* Only encrypted connections to Neo4j instances with certificates signed by a trusted certificate will be accepted.
321338
* The file specified should contain one or more trusted X.509 certificates.
@@ -326,9 +343,14 @@ public File certFile()
326343
* @param certFile the trusted certificate file
327344
* @return an authentication config
328345
*/
329-
public static TrustStrategy trustSignedBy( File certFile )
346+
public static TrustStrategy trustCustomCertificateSignedBy( File certFile )
330347
{
331-
return new TrustStrategy( Strategy.TRUST_SIGNED_CERTIFICATES, certFile );
348+
return new TrustStrategy( Strategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES, certFile );
349+
}
350+
351+
public static TrustStrategy trustSystemCertifcates()
352+
{
353+
return new TrustStrategy( Strategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES );
332354
}
333355

334356
/**
@@ -339,7 +361,7 @@ public static TrustStrategy trustSignedBy( File certFile )
339361
* Each time we reconnect to a known host, we verify that its certificate remains the same, guarding against attackers intercepting our communication.
340362
* <p>
341363
* Note that this approach is vulnerable to man-in-the-middle attacks the very first time you connect to a new Neo4j instance.
342-
* If you do not trust the network you are connecting over, consider using {@link #trustSignedBy(File) signed certificates} instead, or manually adding the
364+
* If you do not trust the network you are connecting over, consider using {@link #trustCustomCertificateSignedBy(File)} signed certificates} instead, or manually adding the
343365
* trusted host line into the specified file.
344366
*
345367
* @param knownHostsFile a file where known certificates are stored.

driver/src/test/java/org/neo4j/driver/internal/ConfigTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public void shouldChangeToTrustedCert()
6969
{
7070
// Given
7171
File trustedCert = new File( "trusted_cert" );
72-
Config config = Config.build().withTrustStrategy( Config.TrustStrategy.trustSignedBy( trustedCert ) ).toConfig();
72+
Config config = Config.build().withTrustStrategy( Config.TrustStrategy.trustCustomCertificateSignedBy( trustedCert ) ).toConfig();
7373

7474
// When
7575
Config.TrustStrategy authConfig = config.trustStrategy();
7676

7777
// Then
78-
assertEquals( authConfig.strategy(), Config.TrustStrategy.Strategy.TRUST_SIGNED_CERTIFICATES );
78+
assertEquals( authConfig.strategy(), Config.TrustStrategy.Strategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES );
7979
assertEquals( trustedCert.getAbsolutePath(), authConfig.certFile().getAbsolutePath() );
8080
}
8181

@@ -86,7 +86,7 @@ public void shouldConfigureMinIdleTime() throws Throwable
8686
Config config = Config.build().withSessionLivenessCheckTimeout( 1337 ).toConfig();
8787

8888
// then
89-
assertThat( config.idleTimeBeforeConnectionTest(), equalTo( 1337l ) );
89+
assertThat( config.idleTimeBeforeConnectionTest(), equalTo( 1337L ) );
9090
}
9191

9292
public static void deleteDefaultKnownCertFileIfExists()

0 commit comments

Comments
 (0)