-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added EncryptionLevel.REQUIRED_NON_LOCAL #191
Merged
+241
−6
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e5a7483
Added EncryptionLevel.REQUIRED_NON_LOCAL
technige 8f5abb9
Fixed license headers
technige 7462c02
Dagnamit license headers!
technige d6448ce
Third time lucky
technige 67b025b
Driver.encrypted->isEncrypted
technige d778329
Stupid auto-refactoring
technige 56b58c8
Better isLocalHost impl than using regex
technige 20f242b
Sigh. License headers.
technige File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
/** | ||
* 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.util.regex.Pattern; | ||
|
||
public class AddressUtil | ||
{ | ||
private static final Pattern LOCALHOST = Pattern.compile( "^(localhost|127(\\.\\d+){3})$", Pattern.CASE_INSENSITIVE ); | ||
|
||
/** | ||
* 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 ) | ||
{ | ||
return LOCALHOST.matcher( host ).matches(); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
import java.net.URI; | ||
|
||
import org.neo4j.driver.v1.Config.EncryptionLevel; | ||
import org.neo4j.driver.v1.exceptions.Neo4jException; | ||
|
||
/** | ||
|
@@ -71,6 +72,11 @@ | |
*/ | ||
public interface Driver extends AutoCloseable | ||
{ | ||
/** | ||
* Return a flag to indicate whether or not encryption is used for this driver. | ||
*/ | ||
boolean encrypted(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this method should be named |
||
|
||
/** | ||
* Establish a session | ||
* @return a session that could be used to run {@link Session#run(String) a statement} or | ||
|
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.encrypted(), 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.encrypted(), 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.encrypted(), 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good way to check for localhost. There can be multiple network cards, interfaces and what not. Plus regex matches things like
127.99999.99.123123123123
. ClassInetAddress
has methods like#isAnyLocalAddress()
,#isLoopbackAddress()
. It seems to be useful to check if address is defined on any interface like thisNetworkInterface.getByInetAddress(address) != null
. I guess we can adopt smth similar to http://stackoverflow.com/a/2406819/5517896There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple network cards don't apply here. We're talking about the address to which the client connects and whether that is equal to
localhost
or127.x.x.x
.I'm also not sure that
127.99999.99.123123123123
would actually be a valid IP address anyway. But I'll check out the utility methods you mention. We do need to make sure we catchlocalhost
here as well as the raw IP addresses so I'll work out which one(s) do that.