Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,7 @@ private static SecurityPlan createSecurityPlan( BoltServerAddress address, Confi
private static SecurityPlan createSecurityPlanImpl( BoltServerAddress address, Config config )
throws GeneralSecurityException, IOException
{
Config.EncryptionLevel encryptionLevel = config.encryptionLevel();
boolean requiresEncryption = encryptionLevel.equals( REQUIRED );

if ( requiresEncryption )
if ( config.encrypted() )
{
Logger logger = config.logging().getLog( "session" );
switch ( config.trustStrategy().strategy() )
Expand Down
42 changes: 36 additions & 6 deletions driver/src/main/java/org/neo4j/driver/v1/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public class Config
*/
private final long idleTimeBeforeConnectionTest;

/** Level of encryption we need to adhere to */
private final EncryptionLevel encryptionLevel;
/** Indicator for encrypted traffic */
private final boolean encrypted;

/** Strategy for how to trust encryption certificate */
private final TrustStrategy trustStrategy;
Expand All @@ -86,7 +86,7 @@ private Config( ConfigBuilder builder)
this.idleTimeBeforeConnectionTest = builder.idleTimeBeforeConnectionTest;
this.maxIdleConnectionPoolSize = builder.maxIdleConnectionPoolSize;

this.encryptionLevel = builder.encryptionLevel;
this.encrypted = builder.encrypted;
this.trustStrategy = builder.trustStrategy;
this.routingFailureLimit = builder.routingFailureLimit;
this.routingRetryDelayMillis = builder.routingRetryDelayMillis;
Expand Down Expand Up @@ -156,9 +156,18 @@ public int connectionTimeoutMillis()
/**
* @return the level of encryption required for all connections.
*/
@Deprecated
public EncryptionLevel encryptionLevel()
{
return encryptionLevel;
return encrypted ? EncryptionLevel.REQUIRED : EncryptionLevel.NONE;
}

/**
* @return indicator for encrypted communication.
*/
public boolean encrypted()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about isEncrypted name for this method?

{
return encrypted;
}

/**
Expand Down Expand Up @@ -205,7 +214,7 @@ public static class ConfigBuilder
private boolean logLeakedSessions;
private int maxIdleConnectionPoolSize = PoolSettings.DEFAULT_MAX_IDLE_CONNECTION_POOL_SIZE;
private long idleTimeBeforeConnectionTest = PoolSettings.DEFAULT_IDLE_TIME_BEFORE_CONNECTION_TEST;
private EncryptionLevel encryptionLevel = EncryptionLevel.REQUIRED;
private boolean encrypted = true;
private TrustStrategy trustStrategy = trustAllCertificates();
private int routingFailureLimit = 1;
private long routingRetryDelayMillis = TimeUnit.SECONDS.toMillis( 5 );
Expand Down Expand Up @@ -328,9 +337,30 @@ public ConfigBuilder withConnectionLivenessCheckTimeout( long value, TimeUnit un
* @param level the TLS level to use
* @return this builder
*/
@Deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EncryptionLevel enum should probably also be deprecated

public ConfigBuilder withEncryptionLevel( EncryptionLevel level )
{
this.encryptionLevel = level;
this.encrypted = level == EncryptionLevel.REQUIRED;
return this;
}

/**
* Set to use encrypted traffic.
* @return this builder
*/
public ConfigBuilder withEncryption()
{
this.encrypted = true;
return this;
}

/**
* Set to use unencrypted traffic.
* @return this builder
*/
public ConfigBuilder withoutEncryption()
{
this.encrypted = false;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class RoutingDriverBoltKitTest
public ExpectedException exception = ExpectedException.none();

private static final Config config = Config.build()
.withEncryptionLevel( Config.EncryptionLevel.NONE )
.withoutEncryption()
.withLogging( new ConsoleLogging( Level.INFO ) ).toConfig();

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void shouldDropBrokenOldSessions() throws Exception

Config config = Config.build()
.withConnectionLivenessCheckTimeout( livenessCheckTimeoutMinutes, TimeUnit.MINUTES )
.withEncryptionLevel( Config.EncryptionLevel.NONE )
.withoutEncryption()
.toConfig();

FakeClock clock = new FakeClock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void tearDown() throws Exception
protected Driver createDriver()
{
Config config = Config.build()
.withEncryptionLevel( Config.EncryptionLevel.NONE )
.withoutEncryption()
.withLogging( new ConsoleLogging( Level.OFF ) )
.toConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EncryptionIT
public void shouldOperateWithNoEncryption() throws Exception
{
// Given
Driver driver = GraphDatabase.driver( neo4j.uri(), Config.build().withEncryptionLevel( NONE ).toConfig() );
Driver driver = GraphDatabase.driver( neo4j.uri(), Config.build().withoutEncryption().toConfig() );

// Then
assertThat( driver.isEncrypted(), equalTo( false ) );
Expand All @@ -60,7 +60,7 @@ public void shouldOperateWithNoEncryption() throws Exception
public void shouldOperateWithRequiredEncryption() throws Exception
{
// Given
Driver driver = GraphDatabase.driver( neo4j.uri(), Config.build().withEncryptionLevel( REQUIRED ).toConfig() );
Driver driver = GraphDatabase.driver( neo4j.uri(), Config.build().withEncryption().toConfig() );

// Then
assertThat( driver.isEncrypted(), equalTo( true ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void shouldGetHelpfulErrorWhenTryingToConnectToHttpPort() throws Throwabl
// Given
//the http server needs some time to start up
Thread.sleep( 2000 );
Config config = Config.build().withEncryptionLevel( Config.EncryptionLevel.NONE ).toConfig();
Config config = Config.build().withoutEncryption().toConfig();
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:7474", config );
Session session = driver.session() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,24 @@ public class ServerKilledIT
@Parameters(name = "{0} connections")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "plaintext", Config.EncryptionLevel.NONE },
{ "tls encrypted", Config.EncryptionLevel.REQUIRED }
{ "plaintext", Config.build().withoutEncryption() },
{ "tls encrypted", Config.build().withEncryption() }
});
}

private Config.EncryptionLevel encryptionLevel;
private Config.ConfigBuilder config;

public ServerKilledIT( String testName, Config.EncryptionLevel encryptionLevel )
public ServerKilledIT( String testName, Config.ConfigBuilder config )
{
this.encryptionLevel = encryptionLevel;
this.config = config;
}

@Test
public void shouldRecoverFromServerRestart() throws Throwable
{
// Given
// config with sessionLivenessCheckTimeout not set, i.e. turned off
Config config = Config.build().withEncryptionLevel( encryptionLevel ).toConfig();
// Given config with sessionLivenessCheckTimeout not set, i.e. turned off

try ( Driver driver = GraphDatabase.driver( Neo4jRunner.DEFAULT_URI, config ) )
try ( Driver driver = GraphDatabase.driver( Neo4jRunner.DEFAULT_URI, config.toConfig() ) )
{
acquireAndReleaseConnections( 4, driver );

Expand Down Expand Up @@ -118,13 +116,11 @@ public void shouldDropBrokenOldSessions() throws Throwable
{
// config with set liveness check timeout
int livenessCheckTimeoutMinutes = 10;
Config config = Config.build().withEncryptionLevel( encryptionLevel )
.withConnectionLivenessCheckTimeout( livenessCheckTimeoutMinutes, TimeUnit.MINUTES )
.toConfig();
config.withConnectionLivenessCheckTimeout( livenessCheckTimeoutMinutes, TimeUnit.MINUTES );

FakeClock clock = new FakeClock();

try ( Driver driver = createDriver( clock, config ) )
try ( Driver driver = createDriver( clock, config.toConfig() ) )
{
acquireAndReleaseConnections( 5, driver );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void shouldPerformTLSHandshakeWithTheSameTrustedServerCert() throws Throw
public void shouldEstablishTLSConnection() throws Throwable
{

Config config = Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig();
Config config = Config.build().withEncryption().toConfig();

try( Driver driver = GraphDatabase.driver( Neo4jRunner.DEFAULT_URI, config );
Session session = driver.session() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void tearDown() throws Exception
public void shouldWorkFine() throws Throwable
{
Config config = Config.build()
.withEncryptionLevel( Config.EncryptionLevel.NONE )
.withoutEncryption()
.toConfig();

driver = driver( neo4j.uri(), config );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void aRunningNeo4jDatabaseUsingACertificateSignedByTheSameTrustedCertific
// give root certificate to driver
driver = GraphDatabase.driver(
Neo4jRunner.DEFAULT_URI,
Config.build().withEncryptionLevel( EncryptionLevel.REQUIRED )
Config.build().withEncryption()
.withTrustStrategy( trustCustomCertificateSignedBy( rootCert ) ).toConfig() );

// generate certificate signing request and get a certificate signed by the root private key
Expand All @@ -229,7 +229,7 @@ public void aRunningNeo4jDatabaseUsingThatExactTrustedCertificate()
{
driver = GraphDatabase.driver(
Neo4jRunner.DEFAULT_URI,
Config.build().withEncryptionLevel( EncryptionLevel.REQUIRED )
Config.build().withEncryption()
.withTrustStrategy( trustCustomCertificateSignedBy(
new File( HOME_DIR, DEFAULT_TLS_CERT_PATH ) ) )
.toConfig() );
Expand All @@ -245,7 +245,7 @@ public void aRunningNeo4jDatabaseUsingACertNotSignedByTheTrustedCertificates() t
// give root certificate to driver
driver = GraphDatabase.driver(
Neo4jRunner.DEFAULT_URI,
Config.build().withEncryptionLevel( EncryptionLevel.REQUIRED )
Config.build().withEncryption()
.withTrustStrategy( trustCustomCertificateSignedBy( cert ) ).toConfig() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class StubServer
private static final int SOCKET_CONNECT_ATTEMPTS = 20;

public static final Config INSECURE_CONFIG = Config.build()
.withEncryptionLevel( Config.EncryptionLevel.NONE ).toConfig();
.withoutEncryption().toConfig();

// This may be thrown if the driver has not been closed properly
public static class ForceKilled extends Exception {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private static Config driverConfig()
// try to build config for a very lightweight driver
return Config.build()
.withTrustStrategy( trustAllCertificates() )
.withEncryptionLevel( Config.EncryptionLevel.NONE )
.withEncryption()
.withMaxIdleSessions( 1 )
.withConnectionLivenessCheckTimeout( 1, TimeUnit.HOURS )
.toConfig();
Expand Down
8 changes: 4 additions & 4 deletions examples/src/main/java/org/neo4j/docs/driver/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static Driver requireEncryption() throws Exception
{
// tag::tls-require-encryption[]
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ),
Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig() );
Config.build().withEncryption().toConfig() );
// end::tls-require-encryption[]

return driver;
Expand All @@ -270,7 +270,7 @@ public static Driver trustOnFirstUse() throws Exception
// tag::tls-trust-on-first-use[]
Driver driver =
GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
.withEncryption()
.withTrustStrategy(
Config.TrustStrategy.trustOnFirstUse( new File( "/path/to/neo4j_known_hosts" ) ) )
.toConfig() );
Expand All @@ -284,7 +284,7 @@ public static Driver trustSignedCertificates() throws Exception
// tag::tls-signed[]
Driver driver =
GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
.withEncryption()
.withTrustStrategy( Config.TrustStrategy
.trustCustomCertificateSignedBy( new File( "/path/to/ca-certificate.pem" ) ) )
.toConfig() );
Expand All @@ -297,7 +297,7 @@ public static Driver connectWithAuthDisabled() throws Exception
{
// tag::connect-with-auth-disabled[]
Driver driver = GraphDatabase.driver( "bolt://localhost:7687",
Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig() );
Config.build().withEncryption().toConfig() );
// end::connect-with-auth-disabled[]

return driver;
Expand Down