Skip to content

Commit e3827f5

Browse files
committed
Added non-local setting for encryption
1 parent d52e41e commit e3827f5

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

docs/source/index.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,29 @@ Session API
2727
.. autoclass:: neo4j.v1.StatementResult
2828
:members:
2929

30-
.. autoclass:: neo4j.v1.ResultSummary
30+
31+
Encryption Settings
32+
-------------------
33+
.. py:attribute:: neo4j.v1.ENCRYPTION_OFF
34+
.. py:attribute:: neo4j.v1.ENCRYPTION_ON
35+
.. py:attribute:: neo4j.v1.ENCRYPTION_NON_LOCAL
36+
.. py:attribute:: neo4j.v1.ENCRYPTION_DEFAULT
37+
38+
39+
Trust Settings
40+
--------------
41+
.. py:attribute:: neo4j.v1.TRUST_ON_FIRST_USE
42+
.. py:attribute:: neo4j.v1.TRUST_SIGNED_CERTIFICATES
43+
.. py:attribute:: neo4j.v1.TRUST_DEFAULT
44+
45+
46+
Query Summary Details
47+
---------------------
48+
49+
.. autoclass:: neo4j.v1.summary.ResultSummary
3150
:members:
3251

33-
.. autoclass:: neo4j.v1.SummaryCounters
52+
.. autoclass:: neo4j.v1.summary.SummaryCounters
3453
:members:
3554

3655

neo4j/v1/constants.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
MAGIC_PREAMBLE = 0x6060B017
3434

35-
ENCRYPTED_DEFAULT = SSL_AVAILABLE
35+
ENCRYPTION_OFF = 0
36+
ENCRYPTION_ON = 1
37+
ENCRYPTION_NON_LOCAL = 2
38+
ENCRYPTION_DEFAULT = ENCRYPTION_NON_LOCAL if SSL_AVAILABLE else ENCRYPTION_OFF
3639

3740
TRUST_ON_FIRST_USE = 0
3841
TRUST_SIGNED_CERTIFICATES = 1
39-
4042
TRUST_DEFAULT = TRUST_ON_FIRST_USE

neo4j/v1/session.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
from __future__ import division
2929

3030
from collections import deque
31+
import re
3132

3233
from .bolt import connect, Response, RUN, PULL_ALL
3334
from .compat import integer, string, urlparse
34-
from .constants import DEFAULT_PORT, ENCRYPTED_DEFAULT, TRUST_DEFAULT, TRUST_SIGNED_CERTIFICATES
35+
from .constants import DEFAULT_PORT, ENCRYPTION_DEFAULT, TRUST_DEFAULT, TRUST_SIGNED_CERTIFICATES, ENCRYPTION_ON, \
36+
ENCRYPTION_NON_LOCAL
3537
from .exceptions import CypherError, ProtocolError, ResultError
3638
from .ssl_compat import SSL_AVAILABLE, SSLContext, PROTOCOL_SSLv23, OP_NO_SSLv2, CERT_REQUIRED
3739
from .summary import ResultSummary
@@ -40,6 +42,8 @@
4042

4143
DEFAULT_MAX_POOL_SIZE = 50
4244

45+
localhost = re.compile(r"^(localhost|127(\.\d+){3})$", re.IGNORECASE)
46+
4347

4448
class AuthToken(object):
4549
""" Container for auth information
@@ -70,7 +74,40 @@ def driver(url, **config):
7074

7175

7276
class Driver(object):
73-
""" Accessor for a specific graph database resource.
77+
""" A :class:`.Driver` is an accessor for a specific graph database
78+
resource. It provides both a template for sessions and a container
79+
for the session pool. All configuration and authentication settings
80+
are collected by the `Driver` constructor; should different settings
81+
be required, a new `Driver` instance should be created.
82+
83+
:param address: address of the remote server as either a `bolt` URI
84+
or a `host:port` string
85+
:param config: configuration and authentication details (valid keys are listed below)
86+
87+
`auth`
88+
An authentication token for the server, for example
89+
``basic_auth("neo4j", "password")``.
90+
91+
`der_encoded_server_certificate`
92+
The server certificate in DER format, if required.
93+
94+
`encrypted`
95+
Encryption level: one of :attr:`.ENCRYPTION_ON`, :attr:`.ENCRYPTION_OFF`
96+
or :attr:`.ENCRYPTION_NON_LOCAL`. The default setting varies
97+
depending on whether SSL is available or not. If it is,
98+
:attr:`.ENCRYPTION_NON_LOCAL` is the default.
99+
100+
`max_pool_size`
101+
The maximum number of sessions to keep idle in the session
102+
pool.
103+
104+
`trust`
105+
Trust level: one of :attr:`.TRUST_ON_FIRST_USE` (default) or
106+
:attr:`.TRUST_SIGNED_CERTIFICATES`.
107+
108+
`user_agent`
109+
A custom user agent string, if required.
110+
74111
"""
75112

76113
def __init__(self, address, **config):
@@ -91,13 +128,14 @@ def __init__(self, address, **config):
91128
self.config = config
92129
self.max_pool_size = config.get("max_pool_size", DEFAULT_MAX_POOL_SIZE)
93130
self.session_pool = deque()
94-
try:
95-
self.encrypted = encrypted = config["encrypted"]
96-
except KeyError:
131+
encrypted = config.get("encrypted", None)
132+
if encrypted is None:
97133
_warn_about_insecure_default()
98-
self.encrypted = encrypted = ENCRYPTED_DEFAULT
134+
encrypted = ENCRYPTION_DEFAULT
135+
self.encrypted = encrypted
99136
self.trust = trust = config.get("trust", TRUST_DEFAULT)
100-
if encrypted:
137+
if encrypted == ENCRYPTION_ON or \
138+
encrypted == ENCRYPTION_NON_LOCAL and not localhost.match(host):
101139
if not SSL_AVAILABLE:
102140
raise RuntimeError("Bolt over TLS is only available in Python 2.7.9+ and Python 3.3+")
103141
ssl_context = SSLContext(PROTOCOL_SSLv23)

0 commit comments

Comments
 (0)