Skip to content

PYTHON-3017 Properly check for closed KMS connections #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pymongo/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def kms_request(self, kms_context):
conn.sendall(message)
while kms_context.bytes_needed > 0:
data = conn.recv(kms_context.bytes_needed)
if not data:
raise OSError('KMS connection closed')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was happening here is that the connection was closed, recv returns b'' and then we get stuck in an infinite loop.

kms_context.feed(data)
finally:
conn.close()
Expand Down
8 changes: 3 additions & 5 deletions test/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,9 +1775,6 @@ def test_invalid_hostname_in_kms_certificate(self):
class TestKmsTLSOptions(EncryptionIntegrationTest):
@unittest.skipUnless(any(AWS_CREDS.values()),
'AWS environment credentials are not set')
@unittest.skipIf(sys.version_info[:2] >= (3, 10) and
sys.platform == 'win32',
'These tests hang with Python 3.10 on Windows')
def setUp(self):
super(TestKmsTLSOptions, self).setUp()
# 1, create client with only tlsCAFile.
Expand Down Expand Up @@ -1822,15 +1819,16 @@ def setUp(self):
self.addCleanup(self.client_encryption_invalid_hostname.close)
# Errors when client has no cert, some examples:
# [SSL: TLSV13_ALERT_CERTIFICATE_REQUIRED] tlsv13 alert certificate required (_ssl.c:2623)
self.cert_error = 'certificate required|SSL handshake failed'
self.cert_error = ('certificate required|SSL handshake failed|'
'KMS connection closed')
# On Windows this error might be:
# [WinError 10054] An existing connection was forcibly closed by the remote host
if sys.platform == 'win32':
self.cert_error += '|forcibly closed'
# On Windows Python 3.10+ this error might be:
# EOF occurred in violation of protocol (_ssl.c:2384)
if sys.version_info[:2] >= (3, 10):
self.cert_error += '|forcibly closed'
self.cert_error += '|EOF'

def test_01_aws(self):
key = {
Expand Down