Skip to content

Commit efe494d

Browse files
committed
add is_sync param
1 parent 3723edc commit efe494d

13 files changed

+279
-101
lines changed

pymongo/asynchronous/encryption.py

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
180180
False, # allow_invalid_certificates
181181
False, # allow_invalid_hostnames
182182
False, # disable_ocsp_endpoint_check
183+
_IS_SYNC,
183184
)
184185
# CSOT: set timeout for socket creation.
185186
connect_timeout = max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0.001)
@@ -674,6 +675,7 @@ def __init__(
674675
key_vault_namespace,
675676
kms_tls_options=kms_tls_options,
676677
key_expiration_ms=key_expiration_ms,
678+
is_sync=_IS_SYNC,
677679
)
678680
self._io_callbacks: Optional[_EncryptionIO] = _EncryptionIO(
679681
None, key_vault_coll, None, opts

pymongo/client_options.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def _parse_read_concern(options: Mapping[str, Any]) -> ReadConcern:
8484
return ReadConcern(concern)
8585

8686

87-
def _parse_ssl_options(options: Mapping[str, Any]) -> tuple[Optional[SSLContext], bool]:
87+
def _parse_ssl_options(
88+
options: Mapping[str, Any], is_sync: bool
89+
) -> tuple[Optional[SSLContext], bool]:
8890
"""Parse ssl options."""
8991
use_tls = options.get("tls")
9092
if use_tls is not None:
@@ -138,6 +140,7 @@ def _parse_ssl_options(options: Mapping[str, Any]) -> tuple[Optional[SSLContext]
138140
allow_invalid_certificates,
139141
allow_invalid_hostnames,
140142
disable_ocsp_endpoint_check,
143+
is_sync,
141144
)
142145
return ctx, allow_invalid_hostnames
143146
return None, allow_invalid_hostnames
@@ -167,7 +170,7 @@ def _parse_pool_options(
167170
compression_settings = CompressionSettings(
168171
options.get("compressors", []), options.get("zlibcompressionlevel", -1)
169172
)
170-
ssl_context, tls_allow_invalid_hostnames = _parse_ssl_options(options)
173+
ssl_context, tls_allow_invalid_hostnames = _parse_ssl_options(options, is_sync)
171174
load_balanced = options.get("loadbalanced")
172175
max_connecting = options.get("maxconnecting", common.MAX_CONNECTING)
173176
return PoolOptions(

pymongo/encryption_options.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(
5858
bypass_query_analysis: bool = False,
5959
encrypted_fields_map: Optional[Mapping[str, Any]] = None,
6060
key_expiration_ms: Optional[int] = None,
61+
is_sync: bool = True,
6162
) -> None:
6263
"""Options to configure automatic client-side field level encryption.
6364
@@ -236,7 +237,7 @@ def __init__(
236237
if not any("idleShutdownTimeoutSecs" in s for s in self._mongocryptd_spawn_args):
237238
self._mongocryptd_spawn_args.append("--idleShutdownTimeoutSecs=60")
238239
# Maps KMS provider name to a SSLContext.
239-
self._kms_ssl_contexts = _parse_kms_tls_options(kms_tls_options)
240+
self._kms_ssl_contexts = _parse_kms_tls_options(kms_tls_options, is_sync)
240241
self._bypass_query_analysis = bypass_query_analysis
241242
self._key_expiration_ms = key_expiration_ms
242243

pymongo/ssl_support.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
from pymongo.errors import ConfigurationError
2222

2323
HAVE_SSL = True
24+
HAVE_PYSSL = True
2425

2526
try:
26-
import pymongo.pyopenssl_context as _ssl
27+
import pymongo.pyopenssl_context as _pyssl
2728
except (ImportError, AttributeError) as exc:
29+
HAVE_PYSSL = False
2830
if isinstance(exc, AttributeError):
2931
warnings.warn(
3032
"Failed to use the installed version of PyOpenSSL. "
@@ -35,10 +37,10 @@
3537
UserWarning,
3638
stacklevel=2,
3739
)
38-
try:
39-
import pymongo.ssl_context as _ssl # type: ignore[no-redef]
40-
except ImportError:
41-
HAVE_SSL = False
40+
try:
41+
import pymongo.ssl_context as _ssl
42+
except ImportError:
43+
HAVE_SSL = False
4244

4345

4446
if HAVE_SSL:
@@ -65,8 +67,13 @@ def get_ssl_context(
6567
allow_invalid_certificates: bool,
6668
allow_invalid_hostnames: bool,
6769
disable_ocsp_endpoint_check: bool,
70+
is_sync: bool,
6871
) -> _ssl.SSLContext:
6972
"""Create and return an SSLContext object."""
73+
if is_sync and HAVE_PYSSL:
74+
ssl_in_use = _pyssl
75+
else:
76+
ssl_in_use = _ssl
7077
verify_mode = CERT_NONE if allow_invalid_certificates else CERT_REQUIRED
7178
ctx = _ssl.SSLContext(_ssl.PROTOCOL_SSLv23)
7279
if verify_mode != CERT_NONE:
@@ -80,21 +87,21 @@ def get_ssl_context(
8087
# up to date versions of MongoDB 2.4 and above already disable
8188
# SSLv2 and SSLv3, python disables SSLv2 by default in >= 2.7.7
8289
# and >= 3.3.4 and SSLv3 in >= 3.4.3.
83-
ctx.options |= _ssl.OP_NO_SSLv2
84-
ctx.options |= _ssl.OP_NO_SSLv3
85-
ctx.options |= _ssl.OP_NO_COMPRESSION
86-
ctx.options |= _ssl.OP_NO_RENEGOTIATION
90+
ctx.options |= ssl_in_use.OP_NO_SSLv2
91+
ctx.options |= ssl_in_use.OP_NO_SSLv3
92+
ctx.options |= ssl_in_use.OP_NO_COMPRESSION
93+
ctx.options |= ssl_in_use.OP_NO_RENEGOTIATION
8794
if certfile is not None:
8895
try:
8996
ctx.load_cert_chain(certfile, None, passphrase)
90-
except _ssl.SSLError as exc:
97+
except ssl_in_use.SSLError as exc:
9198
raise ConfigurationError(f"Private key doesn't match certificate: {exc}") from None
9299
if crlfile is not None:
93-
if _ssl.IS_PYOPENSSL:
100+
if ssl_in_use.IS_PYOPENSSL:
94101
raise ConfigurationError("tlsCRLFile cannot be used with PyOpenSSL")
95102
# Match the server's behavior.
96103
ctx.verify_flags = getattr( # type:ignore[attr-defined]
97-
_ssl, "VERIFY_CRL_CHECK_LEAF", 0
104+
ssl_in_use, "VERIFY_CRL_CHECK_LEAF", 0
98105
)
99106
ctx.load_verify_locations(crlfile)
100107
if ca_certs is not None:

pymongo/synchronous/encryption.py

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
179179
False, # allow_invalid_certificates
180180
False, # allow_invalid_hostnames
181181
False, # disable_ocsp_endpoint_check
182+
_IS_SYNC,
182183
)
183184
# CSOT: set timeout for socket creation.
184185
connect_timeout = max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0.001)
@@ -667,6 +668,7 @@ def __init__(
667668
key_vault_namespace,
668669
kms_tls_options=kms_tls_options,
669670
key_expiration_ms=key_expiration_ms,
671+
is_sync=_IS_SYNC,
670672
)
671673
self._io_callbacks: Optional[_EncryptionIO] = _EncryptionIO(
672674
None, key_vault_coll, None, opts

pymongo/uri_parser_shared.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ def _check_options(nodes: Sized, options: Mapping[str, Any]) -> None:
420420
raise ConfigurationError("Cannot specify replicaSet with loadBalanced=true")
421421

422422

423-
def _parse_kms_tls_options(kms_tls_options: Optional[Mapping[str, Any]]) -> dict[str, SSLContext]:
423+
def _parse_kms_tls_options(
424+
kms_tls_options: Optional[Mapping[str, Any]], is_sync
425+
) -> dict[str, SSLContext]:
424426
"""Parse KMS TLS connection options."""
425427
if not kms_tls_options:
426428
return {}
@@ -435,7 +437,7 @@ def _parse_kms_tls_options(kms_tls_options: Optional[Mapping[str, Any]]) -> dict
435437
opts = _handle_security_options(opts)
436438
opts = _normalize_options(opts)
437439
opts = cast(_CaseInsensitiveDictionary, validate_options(opts))
438-
ssl_context, allow_invalid_hostnames = _parse_ssl_options(opts)
440+
ssl_context, allow_invalid_hostnames = _parse_ssl_options(opts, is_sync)
439441
if ssl_context is None:
440442
raise ConfigurationError("TLS is required for KMS providers")
441443
if allow_invalid_hostnames:

test/asynchronous/test_client_bulk_write.py

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ async def test_returns_error_if_auto_encryption_configured(self):
545545
opts = AutoEncryptionOpts(
546546
key_vault_namespace="db.coll",
547547
kms_providers={"aws": {"accessKeyId": "foo", "secretAccessKey": "bar"}},
548+
is_sync=_IS_SYNC,
548549
)
549550
client = await self.async_rs_or_single_client(auto_encryption_opts=opts)
550551

0 commit comments

Comments
 (0)