28
28
from __future__ import division
29
29
30
30
from collections import deque
31
+ import re
31
32
32
33
from .bolt import connect , Response , RUN , PULL_ALL
33
34
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
35
37
from .exceptions import CypherError , ProtocolError , ResultError
36
38
from .ssl_compat import SSL_AVAILABLE , SSLContext , PROTOCOL_SSLv23 , OP_NO_SSLv2 , CERT_REQUIRED
37
39
from .summary import ResultSummary
40
42
41
43
DEFAULT_MAX_POOL_SIZE = 50
42
44
45
+ localhost = re .compile (r"^(localhost|127(\.\d+){3})$" , re .IGNORECASE )
46
+
43
47
44
48
class AuthToken (object ):
45
49
""" Container for auth information
@@ -70,7 +74,40 @@ def driver(url, **config):
70
74
71
75
72
76
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
+
74
111
"""
75
112
76
113
def __init__ (self , address , ** config ):
@@ -91,13 +128,14 @@ def __init__(self, address, **config):
91
128
self .config = config
92
129
self .max_pool_size = config .get ("max_pool_size" , DEFAULT_MAX_POOL_SIZE )
93
130
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 :
97
133
_warn_about_insecure_default ()
98
- self .encrypted = encrypted = ENCRYPTED_DEFAULT
134
+ encrypted = ENCRYPTION_DEFAULT
135
+ self .encrypted = encrypted
99
136
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 ):
101
139
if not SSL_AVAILABLE :
102
140
raise RuntimeError ("Bolt over TLS is only available in Python 2.7.9+ and Python 3.3+" )
103
141
ssl_context = SSLContext (PROTOCOL_SSLv23 )
0 commit comments