Skip to content

Commit 981a046

Browse files
committed
fix the tests pt3
1 parent 4ed055e commit 981a046

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

pymongo/asynchronous/encryption.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __init__(
157157
self.mongocryptd_client = mongocryptd_client
158158
self.opts = opts
159159
self._spawned = False
160-
self._kms_ssl_contexts = _parse_kms_tls_options(opts._kms_tls_options, _IS_SYNC)
160+
self._kms_ssl_contexts = opts._kms_ssl_contexts(_IS_SYNC)
161161

162162
async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
163163
"""Complete a KMS request.
@@ -398,6 +398,7 @@ def __init__(self, client: AsyncMongoClient[_DocumentTypeArg], opts: AutoEncrypt
398398
encrypted_fields_map = _dict_to_bson(opts._encrypted_fields_map, False, _DATA_KEY_OPTS)
399399
self._bypass_auto_encryption = opts._bypass_auto_encryption
400400
self._internal_client = None
401+
opts._kms_ssl_contexts(_IS_SYNC)
401402

402403
def _get_internal_client(
403404
encrypter: _Encrypter, mongo_client: AsyncMongoClient[_DocumentTypeArg]

pymongo/encryption_options.py

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from typing import TYPE_CHECKING, Any, Mapping, Optional
2222

23+
from pymongo.uri_parser_shared import _parse_kms_tls_options
24+
2325
try:
2426
import pymongocrypt # type:ignore[import-untyped] # noqa: F401
2527

@@ -236,9 +238,21 @@ def __init__(
236238
self._mongocryptd_spawn_args.append("--idleShutdownTimeoutSecs=60")
237239
# Maps KMS provider name to a SSLContext.
238240
self._kms_tls_options = kms_tls_options
241+
self._sync_kms_ssl_contexts = None
242+
self._async_kms_ssl_contexts = None
239243
self._bypass_query_analysis = bypass_query_analysis
240244
self._key_expiration_ms = key_expiration_ms
241245

246+
def _kms_ssl_contexts(self, is_sync: bool):
247+
if is_sync:
248+
if self._sync_kms_ssl_contexts is None:
249+
self._sync_kms_ssl_contexts = _parse_kms_tls_options(self._kms_tls_options, True)
250+
return self._sync_kms_ssl_contexts
251+
else:
252+
if self._async_kms_ssl_contexts is None:
253+
self._async_kms_ssl_contexts = _parse_kms_tls_options(self._kms_tls_options, False)
254+
return self._async_kms_ssl_contexts
255+
242256

243257
class RangeOpts:
244258
"""Options to configure encrypted queries using the range algorithm."""

pymongo/synchronous/encryption.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(
156156
self.mongocryptd_client = mongocryptd_client
157157
self.opts = opts
158158
self._spawned = False
159-
self._kms_ssl_contexts = _parse_kms_tls_options(opts._kms_tls_options, _IS_SYNC)
159+
self._kms_ssl_contexts = opts._kms_ssl_contexts(_IS_SYNC)
160160

161161
def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
162162
"""Complete a KMS request.
@@ -395,6 +395,7 @@ def __init__(self, client: MongoClient[_DocumentTypeArg], opts: AutoEncryptionOp
395395
encrypted_fields_map = _dict_to_bson(opts._encrypted_fields_map, False, _DATA_KEY_OPTS)
396396
self._bypass_auto_encryption = opts._bypass_auto_encryption
397397
self._internal_client = None
398+
opts._kms_ssl_contexts(_IS_SYNC)
398399

399400
def _get_internal_client(
400401
encrypter: _Encrypter, mongo_client: MongoClient[_DocumentTypeArg]

test/asynchronous/test_encryption.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ async def test_init_kms_tls_options(self):
170170
# Error cases:
171171
opts = AutoEncryptionOpts({}, "k.d", kms_tls_options={"kmip": 1})
172172
with self.assertRaisesRegex(TypeError, r'kms_tls_options\["kmip"\] must be a dict'):
173-
client = AsyncMongoClient(auto_encryption_opts=opts)
174-
await client.close()
173+
AsyncMongoClient(auto_encryption_opts=opts)
175174

176175
tls_opts: Any
177176
for tls_opts in [
@@ -181,14 +180,12 @@ async def test_init_kms_tls_options(self):
181180
]:
182181
opts = AutoEncryptionOpts({}, "k.d", kms_tls_options=tls_opts)
183182
with self.assertRaisesRegex(ConfigurationError, "Insecure TLS options prohibited"):
184-
client = AsyncMongoClient(auto_encryption_opts=opts)
185-
await client.close()
183+
AsyncMongoClient(auto_encryption_opts=opts)
186184
opts = AutoEncryptionOpts(
187185
{}, "k.d", kms_tls_options={"kmip": {"tlsCAFile": "does-not-exist"}}
188186
)
189187
with self.assertRaises(FileNotFoundError):
190-
client = AsyncMongoClient(auto_encryption_opts=opts)
191-
await client.close()
188+
AsyncMongoClient(auto_encryption_opts=opts)
192189
# Success cases:
193190
tls_opts: Any
194191
for tls_opts in [None, {}]:

test/test_encryption.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ def test_init_kms_tls_options(self):
170170
# Error cases:
171171
opts = AutoEncryptionOpts({}, "k.d", kms_tls_options={"kmip": 1})
172172
with self.assertRaisesRegex(TypeError, r'kms_tls_options\["kmip"\] must be a dict'):
173-
client = MongoClient(auto_encryption_opts=opts)
174-
client.close()
173+
MongoClient(auto_encryption_opts=opts)
175174

176175
tls_opts: Any
177176
for tls_opts in [
@@ -181,14 +180,12 @@ def test_init_kms_tls_options(self):
181180
]:
182181
opts = AutoEncryptionOpts({}, "k.d", kms_tls_options=tls_opts)
183182
with self.assertRaisesRegex(ConfigurationError, "Insecure TLS options prohibited"):
184-
client = MongoClient(auto_encryption_opts=opts)
185-
client.close()
183+
MongoClient(auto_encryption_opts=opts)
186184
opts = AutoEncryptionOpts(
187185
{}, "k.d", kms_tls_options={"kmip": {"tlsCAFile": "does-not-exist"}}
188186
)
189187
with self.assertRaises(FileNotFoundError):
190-
client = MongoClient(auto_encryption_opts=opts)
191-
client.close()
188+
MongoClient(auto_encryption_opts=opts)
192189
# Success cases:
193190
tls_opts: Any
194191
for tls_opts in [None, {}]:

0 commit comments

Comments
 (0)