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