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