Skip to content

Commit 493fc2a

Browse files
authored
PYTHON-5014 Fix handling of async socket errors in kms request (#2054)
1 parent 6c9a20a commit 493fc2a

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

pymongo/asynchronous/encryption.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,14 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
219219
# Wrap I/O errors in PyMongo exceptions.
220220
if isinstance(exc, BLOCKING_IO_ERRORS):
221221
exc = socket.timeout("timed out")
222-
_raise_connection_failure(address, exc, timeout_details=_get_timeout_details(opts))
222+
# Async raises an OSError instead of returning empty bytes.
223+
if isinstance(exc, OSError):
224+
msg_prefix = "KMS connection closed"
225+
else:
226+
msg_prefix = None
227+
_raise_connection_failure(
228+
address, exc, msg_prefix=msg_prefix, timeout_details=_get_timeout_details(opts)
229+
)
223230
finally:
224231
conn.close()
225232
except MongoCryptError:

pymongo/synchronous/encryption.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,14 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
219219
# Wrap I/O errors in PyMongo exceptions.
220220
if isinstance(exc, BLOCKING_IO_ERRORS):
221221
exc = socket.timeout("timed out")
222-
_raise_connection_failure(address, exc, timeout_details=_get_timeout_details(opts))
222+
# Async raises an OSError instead of returning empty bytes.
223+
if isinstance(exc, OSError):
224+
msg_prefix = "KMS connection closed"
225+
else:
226+
msg_prefix = None
227+
_raise_connection_failure(
228+
address, exc, msg_prefix=msg_prefix, timeout_details=_get_timeout_details(opts)
229+
)
223230
finally:
224231
conn.close()
225232
except MongoCryptError:

test/asynchronous/test_encryption.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,8 @@ async def test_01_aws(self):
21622162
# 127.0.0.1:9001: ('Certificate does not contain any `subjectAltName`s.',)
21632163
key["endpoint"] = "127.0.0.1:9001"
21642164
with self.assertRaisesRegex(
2165-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2165+
EncryptionError,
2166+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
21662167
):
21672168
await self.client_encryption_invalid_hostname.create_data_key("aws", key)
21682169

@@ -2179,7 +2180,8 @@ async def test_02_azure(self):
21792180
await self.client_encryption_expired.create_data_key("azure", key)
21802181
# Invalid cert hostname error.
21812182
with self.assertRaisesRegex(
2182-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2183+
EncryptionError,
2184+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
21832185
):
21842186
await self.client_encryption_invalid_hostname.create_data_key("azure", key)
21852187

@@ -2196,7 +2198,8 @@ async def test_03_gcp(self):
21962198
await self.client_encryption_expired.create_data_key("gcp", key)
21972199
# Invalid cert hostname error.
21982200
with self.assertRaisesRegex(
2199-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2201+
EncryptionError,
2202+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
22002203
):
22012204
await self.client_encryption_invalid_hostname.create_data_key("gcp", key)
22022205

@@ -2210,7 +2213,8 @@ async def test_04_kmip(self):
22102213
await self.client_encryption_expired.create_data_key("kmip")
22112214
# Invalid cert hostname error.
22122215
with self.assertRaisesRegex(
2213-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2216+
EncryptionError,
2217+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
22142218
):
22152219
await self.client_encryption_invalid_hostname.create_data_key("kmip")
22162220

test/test_encryption.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,8 @@ def test_01_aws(self):
21542154
# 127.0.0.1:9001: ('Certificate does not contain any `subjectAltName`s.',)
21552155
key["endpoint"] = "127.0.0.1:9001"
21562156
with self.assertRaisesRegex(
2157-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2157+
EncryptionError,
2158+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
21582159
):
21592160
self.client_encryption_invalid_hostname.create_data_key("aws", key)
21602161

@@ -2171,7 +2172,8 @@ def test_02_azure(self):
21712172
self.client_encryption_expired.create_data_key("azure", key)
21722173
# Invalid cert hostname error.
21732174
with self.assertRaisesRegex(
2174-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2175+
EncryptionError,
2176+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
21752177
):
21762178
self.client_encryption_invalid_hostname.create_data_key("azure", key)
21772179

@@ -2188,7 +2190,8 @@ def test_03_gcp(self):
21882190
self.client_encryption_expired.create_data_key("gcp", key)
21892191
# Invalid cert hostname error.
21902192
with self.assertRaisesRegex(
2191-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2193+
EncryptionError,
2194+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
21922195
):
21932196
self.client_encryption_invalid_hostname.create_data_key("gcp", key)
21942197

@@ -2202,7 +2205,8 @@ def test_04_kmip(self):
22022205
self.client_encryption_expired.create_data_key("kmip")
22032206
# Invalid cert hostname error.
22042207
with self.assertRaisesRegex(
2205-
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
2208+
EncryptionError,
2209+
"IP address mismatch|wronghost|IPAddressMismatch|Certificate|SSL handshake failed",
22062210
):
22072211
self.client_encryption_invalid_hostname.create_data_key("kmip")
22082212

0 commit comments

Comments
 (0)