Skip to content

Commit 2eb0df8

Browse files
authored
PYTHON-2879 Fix get_ssl_context for CSFLE and ocsptest.py (#713)
1 parent fa9531b commit 2eb0df8

File tree

8 files changed

+32
-56
lines changed

8 files changed

+32
-56
lines changed

pymongo/client_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def _parse_ssl_options(options):
109109
certfile,
110110
passphrase,
111111
ca_certs,
112-
allow_invalid_certificates,
113112
crlfile,
113+
allow_invalid_certificates,
114114
allow_invalid_hostnames,
115115
disable_ocsp_endpoint_check)
116116
return ctx, allow_invalid_hostnames

pymongo/encryption.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,11 @@
4949
from pymongo.mongo_client import MongoClient
5050
from pymongo.pool import _configured_socket, PoolOptions
5151
from pymongo.read_concern import ReadConcern
52-
from pymongo.ssl_support import get_ssl_context, HAVE_SSL
52+
from pymongo.ssl_support import get_ssl_context
5353
from pymongo.uri_parser import parse_host
5454
from pymongo.write_concern import WriteConcern
5555
from pymongo.daemon import _spawn_daemon
5656

57-
if HAVE_SSL:
58-
from ssl import CERT_REQUIRED
59-
else:
60-
CERT_REQUIRED = None
6157

6258
_HTTPS_PORT = 443
6359
_KMS_CONNECT_TIMEOUT = 10 # TODO: CDRIVER-3262 will define this value.
@@ -114,14 +110,13 @@ def kms_request(self, kms_context):
114110
# Enable strict certificate verification, OCSP, match hostname, and
115111
# SNI using the system default CA certificates.
116112
ctx = get_ssl_context(
117-
None, # certfile
118-
None, # keyfile
119-
None, # passphrase
120-
None, # ca_certs
121-
CERT_REQUIRED, # cert_reqs
122-
None, # crlfile
123-
True, # match_hostname
124-
True) # check_ocsp_endpoint
113+
None, # certfile
114+
None, # passphrase
115+
None, # ca_certs
116+
None, # crlfile
117+
False, # allow_invalid_certificates
118+
False, # allow_invalid_hostnames
119+
False) # disable_ocsp_endpoint_check
125120
opts = PoolOptions(connect_timeout=_KMS_CONNECT_TIMEOUT,
126121
socket_timeout=_KMS_CONNECT_TIMEOUT,
127122
ssl_context=ctx)

pymongo/pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def ssl_context(self):
401401

402402
@property
403403
def tls_allow_invalid_hostnames(self):
404-
"""Call ssl.match_hostname if cert_reqs is not ssl.CERT_NONE.
404+
"""If True skip ssl.match_hostname.
405405
"""
406406
return self.__tls_allow_invalid_hostnames
407407

pymongo/ssl_support.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,15 @@
3535
# at a high level. This is legacy behavior, but requires us to
3636
# import the ssl module even if we're only using it for this purpose.
3737
import ssl as _stdlibssl
38-
from ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
38+
from ssl import CERT_NONE, CERT_REQUIRED
3939
HAS_SNI = _ssl.HAS_SNI
4040
IPADDR_SAFE = _ssl.IS_PYOPENSSL or sys.version_info[:2] >= (3, 7)
4141
SSLError = _ssl.SSLError
4242

43-
def get_ssl_context(*args):
43+
def get_ssl_context(certfile, passphrase, ca_certs, crlfile,
44+
allow_invalid_certificates, allow_invalid_hostnames,
45+
disable_ocsp_endpoint_check):
4446
"""Create and return an SSLContext object."""
45-
(certfile,
46-
passphrase,
47-
ca_certs,
48-
allow_invalid_certificates,
49-
crlfile,
50-
allow_invalid_hostnames,
51-
disable_ocsp_endpoint_check) = args
5247
verify_mode = CERT_NONE if allow_invalid_certificates else CERT_REQUIRED
5348
ctx = _ssl.SSLContext(_ssl.PROTOCOL_SSLv23)
5449
# SSLContext.check_hostname was added in CPython 3.4.

test/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_keyword_arg_defaults(self):
115115
read_preference=ReadPreference.PRIMARY,
116116
ssl=False,
117117
tlsCertificateKeyFile=None,
118-
tlsAllowInvalidCertificates=True, # ssl.CERT_NONE
118+
tlsAllowInvalidCertificates=True,
119119
tlsCAFile=None,
120120
connect=False,
121121
serverSelectionTimeoutMS=12000)

test/test_ssl.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,13 @@ def test_cert_ssl_validation_hostname_matching(self):
291291
#
292292
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
293293
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
294-
ctx = get_ssl_context(
295-
None, None, None, True, None, True, False)
294+
ctx = get_ssl_context(None, None, None, None, True, True, False)
296295
self.assertFalse(ctx.check_hostname)
297-
ctx = get_ssl_context(
298-
None, None, None, True, None, False, False)
296+
ctx = get_ssl_context(None, None, None, None, True, False, False)
299297
self.assertFalse(ctx.check_hostname)
300-
ctx = get_ssl_context(
301-
None, None, None, False, None, True, False)
298+
ctx = get_ssl_context(None, None, None, None, False, True, False)
302299
self.assertFalse(ctx.check_hostname)
303-
ctx = get_ssl_context(
304-
None, None, None, False, None, False, False)
300+
ctx = get_ssl_context(None, None, None, None, False, False, False)
305301
if _PY37PLUS or _HAVE_PYOPENSSL:
306302
self.assertTrue(ctx.check_hostname)
307303
else:
@@ -424,8 +420,7 @@ def test_validation_with_system_ca_certs(self):
424420
**self.credentials))
425421

426422
def test_system_certs_config_error(self):
427-
ctx = get_ssl_context(
428-
None, None, None, ssl.CERT_NONE, None, True, False)
423+
ctx = get_ssl_context(None, None, None, None, True, True, False)
429424
if ((sys.platform != "win32"
430425
and hasattr(ctx, "set_default_verify_paths"))
431426
or hasattr(ctx, "load_default_certs")):
@@ -457,12 +452,12 @@ def test_certifi_support(self):
457452
# Force the test on Windows, regardless of environment.
458453
ssl_support.HAVE_WINCERTSTORE = False
459454
try:
460-
ctx = get_ssl_context(
461-
None, None, None, CA_PEM, ssl.CERT_REQUIRED, None, True, True)
455+
ctx = get_ssl_context(None, None, CA_PEM, None, False, False,
456+
False)
462457
ssl_sock = ctx.wrap_socket(socket.socket())
463458
self.assertEqual(ssl_sock.ca_certs, CA_PEM)
464459

465-
ctx = get_ssl_context(None, None, None, None, None, None, True, True)
460+
ctx = get_ssl_context(None, None, None, None, False, False, False)
466461
ssl_sock = ctx.wrap_socket(socket.socket())
467462
self.assertEqual(ssl_sock.ca_certs, ssl_support.certifi.where())
468463
finally:
@@ -479,12 +474,11 @@ def test_wincertstore(self):
479474
if not ssl_support.HAVE_WINCERTSTORE:
480475
raise SkipTest("Need wincertstore to test wincertstore.")
481476

482-
ctx = get_ssl_context(
483-
None, None, None, CA_PEM, ssl.CERT_REQUIRED, None, True, True)
477+
ctx = get_ssl_context(None, None, CA_PEM, None, False, False, False)
484478
ssl_sock = ctx.wrap_socket(socket.socket())
485479
self.assertEqual(ssl_sock.ca_certs, CA_PEM)
486480

487-
ctx = get_ssl_context(None, None, None, None, None, None, True, True)
481+
ctx = get_ssl_context(None, None, None, None, False, False, False)
488482
ssl_sock = ctx.wrap_socket(socket.socket())
489483
self.assertEqual(ssl_sock.ca_certs, ssl_support._WINCERTS.name)
490484

test/test_uri_parser.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
import sys
1919
import warnings
2020

21-
try:
22-
from ssl import CERT_NONE
23-
except ImportError:
24-
CERT_NONE = 0
25-
2621
sys.path[0:0] = [""]
2722

2823
from bson.binary import JAVA_LEGACY

tools/ocsptest.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import logging
1717
import socket
1818

19-
from ssl import CERT_REQUIRED
20-
2119
from pymongo.pyopenssl_context import SSLContext
2220
from pymongo.ssl_support import get_ssl_context
2321

@@ -28,14 +26,13 @@
2826

2927
def check_ocsp(host, port, capath):
3028
ctx = get_ssl_context(
31-
None, # certfile
32-
None, # keyfile
33-
None, # passphrase
34-
capath,
35-
CERT_REQUIRED,
36-
None, # crlfile
37-
True, # match_hostname
38-
True) # check_ocsp_endpoint
29+
None, # certfile
30+
None, # passphrase
31+
capath, # ca_certs
32+
None, # crlfile
33+
False, # allow_invalid_certificates
34+
False, # allow_invalid_hostnames
35+
False) # disable_ocsp_endpoint_check
3936

4037
# Ensure we're using pyOpenSSL.
4138
assert isinstance(ctx, SSLContext)

0 commit comments

Comments
 (0)