|
36 | 36 | from bson.json_util import JSONOptions
|
37 | 37 | from bson.son import SON
|
38 | 38 |
|
| 39 | +from pymongo import encryption |
39 | 40 | from pymongo.cursor import CursorType
|
40 | 41 | from pymongo.encryption import (Algorithm,
|
41 | 42 | ClientEncryption)
|
|
45 | 46 | EncryptionError,
|
46 | 47 | InvalidOperation,
|
47 | 48 | OperationFailure,
|
| 49 | + ServerSelectionTimeoutError, |
48 | 50 | WriteError)
|
49 | 51 | from pymongo.mongo_client import MongoClient
|
50 | 52 | from pymongo.operations import InsertOne
|
@@ -1582,5 +1584,51 @@ def test_case_8(self):
|
1582 | 1584 | self.assertEqual(len(self.topology_listener.results['opened']), 1)
|
1583 | 1585 |
|
1584 | 1586 |
|
| 1587 | +# https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#bypass-spawning-mongocryptd |
| 1588 | +class TestBypassSpawningMongocryptdProse(EncryptionIntegrationTest): |
| 1589 | + def test_mongocryptd_bypass_spawn(self): |
| 1590 | + # Lower the mongocryptd timeout to reduce the test run time. |
| 1591 | + self._original_timeout = encryption._MONGOCRYPTD_TIMEOUT_MS |
| 1592 | + encryption._MONGOCRYPTD_TIMEOUT_MS = 500 |
| 1593 | + def reset_timeout(): |
| 1594 | + encryption._MONGOCRYPTD_TIMEOUT_MS = self._original_timeout |
| 1595 | + self.addCleanup(reset_timeout) |
| 1596 | + |
| 1597 | + # Configure the encrypted field via the local schema_map option. |
| 1598 | + schemas = {'db.coll': json_data('external', 'external-schema.json')} |
| 1599 | + opts = AutoEncryptionOpts( |
| 1600 | + {'local': {'key': LOCAL_MASTER_KEY}}, |
| 1601 | + 'keyvault.datakeys', |
| 1602 | + schema_map=schemas, |
| 1603 | + mongocryptd_bypass_spawn=True, |
| 1604 | + mongocryptd_uri='mongodb://localhost:27027/', |
| 1605 | + mongocryptd_spawn_args=[ |
| 1606 | + '--pidfilepath=bypass-spawning-mongocryptd.pid', |
| 1607 | + '--port=27027'] |
| 1608 | + ) |
| 1609 | + client_encrypted = rs_or_single_client(auto_encryption_opts=opts) |
| 1610 | + self.addCleanup(client_encrypted.close) |
| 1611 | + with self.assertRaisesRegex(EncryptionError, 'Timeout'): |
| 1612 | + client_encrypted.db.coll.insert_one({'encrypted': 'test'}) |
| 1613 | + |
| 1614 | + def test_bypassAutoEncryption(self): |
| 1615 | + opts = AutoEncryptionOpts( |
| 1616 | + {'local': {'key': LOCAL_MASTER_KEY}}, |
| 1617 | + 'keyvault.datakeys', |
| 1618 | + bypass_auto_encryption=True, |
| 1619 | + mongocryptd_spawn_args=[ |
| 1620 | + '--pidfilepath=bypass-spawning-mongocryptd.pid', |
| 1621 | + '--port=27027'] |
| 1622 | + ) |
| 1623 | + client_encrypted = rs_or_single_client(auto_encryption_opts=opts) |
| 1624 | + self.addCleanup(client_encrypted.close) |
| 1625 | + client_encrypted.db.coll.insert_one({"unencrypted": "test"}) |
| 1626 | + # Validate that mongocryptd was not spawned: |
| 1627 | + mongocryptd_client = MongoClient( |
| 1628 | + 'mongodb://localhost:27027/?serverSelectionTimeoutMS=500') |
| 1629 | + with self.assertRaises(ServerSelectionTimeoutError): |
| 1630 | + mongocryptd_client.admin.command('ping') |
| 1631 | + |
| 1632 | + |
1585 | 1633 | if __name__ == "__main__":
|
1586 | 1634 | unittest.main()
|
0 commit comments