Skip to content

Commit 9ff205d

Browse files
committed
PYTHON-2578 Improve clarity of TLS settings for KMS requests (#567)
Note that cert_reqs=None and cert_reqs=CERT_REQUIRED are identical so this does not change any behavior. (cherry picked from commit c15028a)
1 parent 3997097 commit 9ff205d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pymongo/encryption.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@
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
52+
from pymongo.ssl_support import get_ssl_context, HAVE_SSL
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
5761

5862
_HTTPS_PORT = 443
5963
_KMS_CONNECT_TIMEOUT = 10 # TODO: CDRIVER-3262 will define this value.
@@ -107,7 +111,17 @@ def kms_request(self, kms_context):
107111
endpoint = kms_context.endpoint
108112
message = kms_context.message
109113
host, port = parse_host(endpoint, _HTTPS_PORT)
110-
ctx = get_ssl_context(None, None, None, None, None, None, True, True)
114+
# Enable strict certificate verification, OCSP, match hostname, and
115+
# SNI using the system default CA certificates.
116+
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
111125
opts = PoolOptions(connect_timeout=_KMS_CONNECT_TIMEOUT,
112126
socket_timeout=_KMS_CONNECT_TIMEOUT,
113127
ssl_context=ctx)

0 commit comments

Comments
 (0)