8787from pymongo .results import BulkWriteResult , DeleteResult
8888from pymongo .ssl_support import BLOCKING_IO_ERRORS , get_ssl_context
8989from pymongo .typings import _DocumentType , _DocumentTypeArg
90- from pymongo .uri_parser_shared import parse_host
90+ from pymongo .uri_parser_shared import _parse_kms_tls_options , parse_host
9191from pymongo .write_concern import WriteConcern
9292
9393if TYPE_CHECKING :
@@ -157,6 +157,7 @@ def __init__(
157157 self .mongocryptd_client = mongocryptd_client
158158 self .opts = opts
159159 self ._spawned = False
160+ self ._kms_ssl_contexts = opts ._kms_ssl_contexts (_IS_SYNC )
160161
161162 async def kms_request (self , kms_context : MongoCryptKmsContext ) -> None :
162163 """Complete a KMS request.
@@ -168,7 +169,7 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
168169 endpoint = kms_context .endpoint
169170 message = kms_context .message
170171 provider = kms_context .kms_provider
171- ctx = self .opts . _kms_ssl_contexts .get (provider )
172+ ctx = self ._kms_ssl_contexts .get (provider )
172173 if ctx is None :
173174 # Enable strict certificate verification, OCSP, match hostname, and
174175 # SNI using the system default CA certificates.
@@ -180,6 +181,7 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
180181 False , # allow_invalid_certificates
181182 False , # allow_invalid_hostnames
182183 False , # disable_ocsp_endpoint_check
184+ _IS_SYNC ,
183185 )
184186 # CSOT: set timeout for socket creation.
185187 connect_timeout = max (_csot .clamp_remaining (_KMS_CONNECT_TIMEOUT ), 0.001 )
@@ -396,6 +398,8 @@ def __init__(self, client: AsyncMongoClient[_DocumentTypeArg], opts: AutoEncrypt
396398 encrypted_fields_map = _dict_to_bson (opts ._encrypted_fields_map , False , _DATA_KEY_OPTS )
397399 self ._bypass_auto_encryption = opts ._bypass_auto_encryption
398400 self ._internal_client = None
401+ # parsing kms_ssl_contexts here so that parsing errors will be raised before internal clients are created
402+ opts ._kms_ssl_contexts (_IS_SYNC )
399403
400404 def _get_internal_client (
401405 encrypter : _Encrypter , mongo_client : AsyncMongoClient [_DocumentTypeArg ]
@@ -675,6 +679,7 @@ def __init__(
675679 kms_tls_options = kms_tls_options ,
676680 key_expiration_ms = key_expiration_ms ,
677681 )
682+ self ._kms_ssl_contexts = _parse_kms_tls_options (opts ._kms_tls_options , _IS_SYNC )
678683 self ._io_callbacks : Optional [_EncryptionIO ] = _EncryptionIO (
679684 None , key_vault_coll , None , opts
680685 )
0 commit comments