|
18 | 18 |
|
19 | 19 | from abc import ABCMeta |
20 | 20 | from collections.abc import Mapping |
21 | | -from warnings import warn |
22 | 21 |
|
| 22 | +from ._conf import ( |
| 23 | + TrustAll, |
| 24 | + TrustCustomCAs, |
| 25 | + TrustSystemCAs, |
| 26 | +) |
23 | 27 | from .api import ( |
24 | 28 | DEFAULT_DATABASE, |
25 | 29 | TRUST_ALL_CERTIFICATES, |
@@ -205,9 +209,9 @@ def __iter__(self): |
205 | 209 |
|
206 | 210 | def _trust_to_trusted_certificates(pool_config, trust): |
207 | 211 | if trust == TRUST_SYSTEM_CA_SIGNED_CERTIFICATES: |
208 | | - pool_config.trusted_certificates = None |
| 212 | + pool_config.trusted_certificates = TrustSystemCAs() |
209 | 213 | elif trust == TRUST_ALL_CERTIFICATES: |
210 | | - pool_config.trusted_certificates = [] |
| 214 | + pool_config.trusted_certificates = TrustAll() |
211 | 215 |
|
212 | 216 |
|
213 | 217 | class PoolConfig(Config): |
@@ -241,12 +245,13 @@ class PoolConfig(Config): |
241 | 245 | # Specify whether to use an encrypted connection between the driver and server. |
242 | 246 |
|
243 | 247 | #: SSL Certificates to Trust |
244 | | - trusted_certificates = None |
| 248 | + trusted_certificates = TrustSystemCAs() |
245 | 249 | # Specify how to determine the authenticity of encryption certificates |
246 | 250 | # provided by the Neo4j instance on connection. |
247 | | - # * None: Use system trust store. (default) |
248 | | - # * []: Trust any certificate. |
249 | | - # * ["<path>", ...]: Trust the specified certificate(s). |
| 251 | + # * `neo4j.TrustSystemCAs()`: Use system trust store. (default) |
| 252 | + # * `neo4j.TrustAll()`: Trust any certificate. |
| 253 | + # * `neo4j.TrustCustomCAs("<path>", ...)`: |
| 254 | + # Trust the specified certificate(s). |
250 | 255 |
|
251 | 256 | #: Custom SSL context to use for wrapping sockets |
252 | 257 | ssl_context = None |
@@ -296,26 +301,25 @@ def get_ssl_context(self): |
296 | 301 | ssl_context.options |= ssl.OP_NO_TLSv1 # Python 3.2 |
297 | 302 | ssl_context.options |= ssl.OP_NO_TLSv1_1 # Python 3.4 |
298 | 303 |
|
299 | | - if self.trusted_certificates is None: |
| 304 | + if isinstance(self.trusted_certificates, TrustAll): |
| 305 | + # trust any certificate |
| 306 | + ssl_context.check_hostname = False |
| 307 | + # https://docs.python.org/3.7/library/ssl.html#ssl.CERT_NONE |
| 308 | + ssl_context.verify_mode = ssl.CERT_NONE |
| 309 | + elif isinstance(self.trusted_certificates, TrustCustomCAs): |
| 310 | + # trust the specified certificate(s) |
| 311 | + ssl_context.check_hostname = True |
| 312 | + ssl_context.verify_mode = ssl.CERT_REQUIRED |
| 313 | + for cert in self.trusted_certificates.certs: |
| 314 | + ssl_context.load_verify_locations(cert) |
| 315 | + else: |
| 316 | + # default |
300 | 317 | # trust system CA certificates |
301 | 318 | ssl_context.check_hostname = True |
302 | 319 | ssl_context.verify_mode = ssl.CERT_REQUIRED |
303 | 320 | # Must be load_default_certs, not set_default_verify_paths to |
304 | 321 | # work on Windows with system CAs. |
305 | 322 | ssl_context.load_default_certs() |
306 | | - else: |
307 | | - self.trusted_certificates = tuple(self.trusted_certificates) |
308 | | - if not self.trusted_certificates: |
309 | | - # trust any certificate |
310 | | - ssl_context.check_hostname = False |
311 | | - # https://docs.python.org/3.7/library/ssl.html#ssl.CERT_NONE |
312 | | - ssl_context.verify_mode = ssl.CERT_NONE |
313 | | - else: |
314 | | - # trust the specified certificate(s) |
315 | | - ssl_context.check_hostname = True |
316 | | - ssl_context.verify_mode = ssl.CERT_REQUIRED |
317 | | - for cert in self.trusted_certificates: |
318 | | - ssl_context.load_verify_locations(cert) |
319 | 323 |
|
320 | 324 | return ssl_context |
321 | 325 |
|
|
0 commit comments