-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from neo4j/1.1-crypt-non-local
Added EncryptionLevel.REQUIRED_NON_LOCAL
- Loading branch information
Showing
9 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
driver/src/main/java/org/neo4j/driver/internal/util/AddressUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.neo4j.driver.internal.util; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
public class AddressUtil | ||
{ | ||
/** | ||
* Return true if the host provided matches "localhost" or "127.x.x.x". | ||
* | ||
* @param host the host name to test | ||
* @return true if localhost, false otherwise | ||
*/ | ||
public static boolean isLocalHost( String host ) | ||
{ | ||
try | ||
{ | ||
// confirmed to work as desired with both "localhost" and "127.x.x.x" | ||
return InetAddress.getByName( host ).isLoopbackAddress(); | ||
} | ||
catch ( UnknownHostException e ) | ||
{ | ||
// if it's unknown, it's not local so we can safely return false | ||
return false; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
driver/src/test/java/org/neo4j/driver/internal/util/AddressUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.driver.internal.util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.junit.Assert.*; | ||
import static org.neo4j.driver.internal.util.AddressUtil.isLocalHost; | ||
|
||
public class AddressUtilTest | ||
{ | ||
@Test | ||
public void shouldWorkForVariantsOfLocalHost() throws Exception | ||
{ | ||
assertThat( isLocalHost( "localhost" ), equalTo( true ) ); | ||
assertThat( isLocalHost( "LocalHost" ), equalTo( true ) ); | ||
assertThat( isLocalHost( "LOCALHOST" ), equalTo( true ) ); | ||
assertThat( isLocalHost( "127.0.0.1" ), equalTo( true ) ); | ||
assertThat( isLocalHost( "127.5.6.7" ), equalTo( true ) ); | ||
assertThat( isLocalHost( "x" ), equalTo( false ) ); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
driver/src/test/java/org/neo4j/driver/v1/integration/EncryptionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.driver.v1.integration; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.neo4j.driver.v1.*; | ||
import org.neo4j.driver.v1.util.TestNeo4j; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.neo4j.driver.internal.util.AddressUtil.isLocalHost; | ||
import static org.neo4j.driver.v1.Config.EncryptionLevel.NONE; | ||
import static org.neo4j.driver.v1.Config.EncryptionLevel.REQUIRED; | ||
import static org.neo4j.driver.v1.Config.EncryptionLevel.REQUIRED_NON_LOCAL; | ||
|
||
public class EncryptionIT | ||
{ | ||
@Rule | ||
public TestNeo4j neo4j = new TestNeo4j(); | ||
|
||
@Test | ||
public void shouldOperateWithNoEncryption() throws Exception | ||
{ | ||
// Given | ||
Driver driver = GraphDatabase.driver( neo4j.address(), Config.build().withEncryptionLevel( NONE ).toConfig() ); | ||
|
||
// Then | ||
assertThat( driver.isEncrypted(), equalTo( false ) ); | ||
|
||
// When | ||
Session session = driver.session(); | ||
StatementResult result = session.run( "RETURN 1" ); | ||
|
||
// Then | ||
Record record = result.next(); | ||
int value = record.get( 0 ).asInt(); | ||
assertThat( value, equalTo( 1 ) ); | ||
|
||
// Finally | ||
session.close(); | ||
driver.close(); | ||
} | ||
|
||
@Test | ||
public void shouldOperateWithRequiredNonLocalEncryption() throws Exception | ||
{ | ||
// Given | ||
Driver driver = GraphDatabase.driver( neo4j.address(), Config.build().withEncryptionLevel( REQUIRED_NON_LOCAL ).toConfig() ); | ||
|
||
// Then | ||
assertThat( driver.isEncrypted(), equalTo( !isLocalHost( neo4j.host() ) ) ); | ||
|
||
// When | ||
Session session = driver.session(); | ||
StatementResult result = session.run( "RETURN 1" ); | ||
|
||
// Then | ||
Record record = result.next(); | ||
int value = record.get( 0 ).asInt(); | ||
assertThat( value, equalTo( 1 ) ); | ||
|
||
// Finally | ||
session.close(); | ||
driver.close(); | ||
} | ||
|
||
@Test | ||
public void shouldOperateWithRequiredEncryption() throws Exception | ||
{ | ||
// Given | ||
Driver driver = GraphDatabase.driver( neo4j.address(), Config.build().withEncryptionLevel( REQUIRED ).toConfig() ); | ||
|
||
// Then | ||
assertThat( driver.isEncrypted(), equalTo( true ) ); | ||
|
||
// When | ||
Session session = driver.session(); | ||
StatementResult result = session.run( "RETURN 1" ); | ||
|
||
// Then | ||
Record record = result.next(); | ||
int value = record.get( 0 ).asInt(); | ||
assertThat( value, equalTo( 1 ) ); | ||
|
||
// Finally | ||
session.close(); | ||
driver.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters