|
19 | 19 | import os
|
20 | 20 | import traceback
|
21 | 21 | import socket
|
| 22 | +import ssl |
22 | 23 | import sys
|
23 | 24 | import textwrap
|
24 | 25 | import uuid
|
|
49 | 50 | WriteError)
|
50 | 51 | from pymongo.mongo_client import MongoClient
|
51 | 52 | from pymongo.operations import InsertOne
|
| 53 | +from pymongo.ssl_support import _ssl |
52 | 54 | from pymongo.write_concern import WriteConcern
|
53 | 55 |
|
54 | 56 | from test import unittest, IntegrationTest, PyMongoTestCase, client_context
|
|
60 | 62 | rs_or_single_client,
|
61 | 63 | wait_until)
|
62 | 64 | from test.utils_spec_runner import SpecRunner
|
| 65 | +from test.test_ssl import CA_PEM |
63 | 66 |
|
64 | 67 |
|
65 | 68 | def get_client_opts(client):
|
@@ -1624,5 +1627,59 @@ def test_bypassAutoEncryption(self):
|
1624 | 1627 | mongocryptd_client.admin.command('ping')
|
1625 | 1628 |
|
1626 | 1629 |
|
| 1630 | +# https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#kms-tls-tests |
| 1631 | +class TestKmsTLSProse(EncryptionIntegrationTest): |
| 1632 | + @unittest.skipIf(sys.platform == 'win32', |
| 1633 | + "Can't test system ca certs on Windows") |
| 1634 | + @unittest.skipIf(ssl.OPENSSL_VERSION.lower().startswith('libressl') and |
| 1635 | + sys.platform == 'darwin' and not _ssl.IS_PYOPENSSL, |
| 1636 | + "LibreSSL on OSX doesn't support setting CA certificates " |
| 1637 | + "using SSL_CERT_FILE environment variable.") |
| 1638 | + @unittest.skipUnless(any(AWS_CREDS.values()), |
| 1639 | + 'AWS environment credentials are not set') |
| 1640 | + def setUp(self): |
| 1641 | + self.original_certs = os.environ.get('SSL_CERT_FILE') |
| 1642 | + def restore_certs(): |
| 1643 | + if self.original_certs is None: |
| 1644 | + os.environ.pop('SSL_CERT_FILE') |
| 1645 | + else: |
| 1646 | + os.environ['SSL_CERT_FILE'] = self.original_certs |
| 1647 | + # Tell OpenSSL where CA certificates live. |
| 1648 | + os.environ['SSL_CERT_FILE'] = CA_PEM |
| 1649 | + self.addCleanup(restore_certs) |
| 1650 | + |
| 1651 | + self.client_encrypted = ClientEncryption( |
| 1652 | + {'aws': AWS_CREDS}, 'keyvault.datakeys', self.client, OPTS) |
| 1653 | + self.addCleanup(self.client_encrypted.close) |
| 1654 | + |
| 1655 | + def test_invalid_kms_certificate_expired(self): |
| 1656 | + key = { |
| 1657 | + "region": "us-east-1", |
| 1658 | + "key": "arn:aws:kms:us-east-1:579766882180:key/" |
| 1659 | + "89fcc2c4-08b0-4bd9-9f25-e30687b580d0", |
| 1660 | + "endpoint": "mongodb://127.0.0.1:8000", |
| 1661 | + } |
| 1662 | + # Some examples: |
| 1663 | + # certificate verify failed: certificate has expired (_ssl.c:1129) |
| 1664 | + # amazon1-2018 Python 3.6: certificate verify failed (_ssl.c:852) |
| 1665 | + with self.assertRaisesRegex( |
| 1666 | + EncryptionError, 'expired|certificate verify failed'): |
| 1667 | + self.client_encrypted.create_data_key('aws', master_key=key) |
| 1668 | + |
| 1669 | + def test_invalid_hostname_in_kms_certificate(self): |
| 1670 | + key = { |
| 1671 | + "region": "us-east-1", |
| 1672 | + "key": "arn:aws:kms:us-east-1:579766882180:key/" |
| 1673 | + "89fcc2c4-08b0-4bd9-9f25-e30687b580d0", |
| 1674 | + "endpoint": "mongodb://127.0.0.1:8001", |
| 1675 | + } |
| 1676 | + # Some examples: |
| 1677 | + # certificate verify failed: IP address mismatch, certificate is not valid for '127.0.0.1'. (_ssl.c:1129)" |
| 1678 | + # hostname '127.0.0.1' doesn't match 'wronghost.com' |
| 1679 | + with self.assertRaisesRegex( |
| 1680 | + EncryptionError, 'IP address mismatch|wronghost'): |
| 1681 | + self.client_encrypted.create_data_key('aws', master_key=key) |
| 1682 | + |
| 1683 | + |
1627 | 1684 | if __name__ == "__main__":
|
1628 | 1685 | unittest.main()
|
0 commit comments