5555from neo4j .conf import (
5656 Config ,
5757 PoolConfig ,
58+ TRUST_ALL_CERTIFICATES ,
59+ TRUST_SYSTEM_CA_SIGNED_CERTIFICATES ,
5860)
5961from neo4j .meta import (
6062 experimental ,
@@ -88,29 +90,98 @@ def driver(cls, uri, *, auth=None, acquire_timeout=None, **config):
8890 concurrency.
8991
9092 :param uri:
93+
94+ bolt://host[:port]
95+ Settings: Direct driver with no encryption.
96+
97+ bolt+ssc://host[:port]
98+ Settings: Direct driver with encryption (accepts self signed certificates).
99+
100+ bolt+s://host[:port]
101+ Settings: Direct driver with encryption (accepts only certificates signed by an certificate authority), full certificate checks.
102+
103+ neo4j://host[:port][?routing_context]
104+ Settings: Routing driver with no encryption.
105+
106+ neo4j+ssc://host[:port][?routing_context]
107+ Settings: Routing driver with encryption (accepts self signed certificates).
108+
109+ neo4j+s://host[:port][?routing_context]
110+ Settings: Routing driver with encryption (accepts only certificates signed by an certificate authority), full certificate checks.
111+
91112 :param auth:
92- :param acquire_timeout:
113+ :param acquire_timeout: seconds
93114 :param config: connection configuration settings
94115 """
95- parsed = urlparse (uri )
96- if parsed .scheme == "bolt" :
116+
117+ from neo4j .api import (
118+ parse_neo4j_uri ,
119+ DRIVER_BOLT ,
120+ DRIVER_NEO4j ,
121+ SECURITY_TYPE_NOT_SECURE ,
122+ SECURITY_TYPE_SELF_SIGNED_CERTIFICATE ,
123+ SECURITY_TYPE_SECURE ,
124+ URI_SCHEME_BOLT ,
125+ URI_SCHEME_NEO4J ,
126+ URI_SCHEME_BOLT_SELF_SIGNED_CERTIFICATE ,
127+ URI_SCHEME_BOLT_SECURE ,
128+ URI_SCHEME_NEO4J_SELF_SIGNED_CERTIFICATE ,
129+ URI_SCHEME_NEO4J_SECURE ,
130+ )
131+ from neo4j .conf import (
132+ TRUST_ALL_CERTIFICATES ,
133+ TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
134+ )
135+
136+ driver_type , security_type , parsed = parse_neo4j_uri (uri )
137+
138+ if "trust" in config .keys ():
139+ if config .get ("trust" ) not in [TRUST_ALL_CERTIFICATES , TRUST_SYSTEM_CA_SIGNED_CERTIFICATES ]:
140+ from neo4j .exceptions import ConfigurationError
141+ raise ConfigurationError ("The config setting `trust` values are {!r}" .format (
142+ [
143+ TRUST_ALL_CERTIFICATES ,
144+ TRUST_SYSTEM_CA_SIGNED_CERTIFICATES ,
145+ ]
146+ ))
147+
148+ if security_type in [SECURITY_TYPE_SELF_SIGNED_CERTIFICATE , SECURITY_TYPE_SECURE ] and ("encrypted" in config .keys () or "trust" in config .keys ()):
149+ from neo4j .exceptions import ConfigurationError
150+ raise ConfigurationError ("The config settings 'encrypted' and 'trust' can only be used with the URI schemes {!r}. Use the other URI schemes {!r} for setting encryption settings." .format (
151+ [
152+ URI_SCHEME_BOLT ,
153+ URI_SCHEME_NEO4J ,
154+ ],
155+ [
156+ URI_SCHEME_BOLT_SELF_SIGNED_CERTIFICATE ,
157+ URI_SCHEME_BOLT_SECURE ,
158+ URI_SCHEME_NEO4J_SELF_SIGNED_CERTIFICATE ,
159+ URI_SCHEME_NEO4J_SECURE ,
160+ ]
161+ ))
162+
163+ if security_type == SECURITY_TYPE_SECURE :
164+ config ["encrypted" ] = True
165+ elif security_type == SECURITY_TYPE_SELF_SIGNED_CERTIFICATE :
166+ config ["encrypted" ] = True
167+ config ["trust" ] = TRUST_ALL_CERTIFICATES
168+
169+ if driver_type == DRIVER_BOLT :
97170 return cls .bolt_driver (parsed .netloc , auth = auth , acquire_timeout = acquire_timeout , ** config )
98- elif parsed . scheme == "neo4j" or parsed . scheme == "bolt+routing" :
171+ elif driver_type == DRIVER_NEO4j :
99172 rc = cls ._parse_routing_context (parsed .query )
100173 return cls .neo4j_driver (parsed .netloc , auth = auth , routing_context = rc , acquire_timeout = acquire_timeout , ** config )
101- else :
102- raise ValueError ("Unknown URI scheme {!r}" .format (parsed .scheme ))
103174
104175 @classmethod
105176 def bolt_driver (cls , target , * , auth = None , acquire_timeout = None , ** config ):
106177 """ Create a driver for direct Bolt server access that uses
107178 socket I/O and thread-based concurrency.
108179 """
109- from neo4j ._exceptions import BoltHandshakeError
180+ from neo4j ._exceptions import BoltHandshakeError , BoltSecurityError
110181
111182 try :
112183 return BoltDriver .open (target , auth = auth , acquire_timeout = acquire_timeout , ** config )
113- except BoltHandshakeError as error :
184+ except ( BoltHandshakeError , BoltSecurityError ) as error :
114185 from neo4j .exceptions import ServiceUnavailable
115186 raise ServiceUnavailable (str (error )) from error
116187
@@ -120,11 +191,11 @@ def neo4j_driver(cls, *targets, auth=None, routing_context=None, acquire_timeout
120191 """ Create a driver for routing-capable Neo4j service access
121192 that uses socket I/O and thread-based concurrency.
122193 """
123- from neo4j ._exceptions import BoltHandshakeError
194+ from neo4j ._exceptions import BoltHandshakeError , BoltSecurityError
124195
125196 try :
126197 return Neo4jDriver .open (* targets , auth = auth , routing_context = routing_context , acquire_timeout = acquire_timeout , ** config )
127- except BoltHandshakeError as error :
198+ except ( BoltHandshakeError , BoltSecurityError ) as error :
128199 from neo4j .exceptions import ServiceUnavailable
129200 raise ServiceUnavailable (str (error )) from error
130201
@@ -228,8 +299,8 @@ def __exit__(self, exc_type, exc_value, traceback):
228299 self .close ()
229300
230301 @property
231- def secure (self ):
232- return bool (self ._pool .config .secure )
302+ def encrypted (self ):
303+ return bool (self ._pool .config .encrypted )
233304
234305 def session (self , ** config ):
235306 """ Create a simple session.
@@ -278,8 +349,7 @@ def open(cls, target, *, auth=None, **config):
278349 from neo4j .io import BoltPool
279350 from neo4j .work import WorkspaceConfig
280351 address = cls .parse_target (target )
281- pool_config , default_workspace_config = Config .consume_chain (config , PoolConfig ,
282- WorkspaceConfig )
352+ pool_config , default_workspace_config = Config .consume_chain (config , PoolConfig , WorkspaceConfig )
283353 pool = BoltPool .open (address , auth = auth , ** pool_config )
284354 return cls (pool , default_workspace_config )
285355
@@ -323,8 +393,7 @@ def open(cls, *targets, auth=None, routing_context=None, **config):
323393 from neo4j .io import Neo4jPool
324394 from neo4j .work import WorkspaceConfig
325395 addresses = cls .parse_targets (* targets )
326- pool_config , default_workspace_config = Config .consume_chain (config , PoolConfig ,
327- WorkspaceConfig )
396+ pool_config , default_workspace_config = Config .consume_chain (config , PoolConfig , WorkspaceConfig )
328397 pool = Neo4jPool .open (* addresses , auth = auth , routing_context = routing_context , ** pool_config )
329398 return cls (pool , default_workspace_config )
330399
0 commit comments