Skip to content

Commit ef718b5

Browse files
committed
PYTHON-2096 Validate that mongocryptd is not spawned if bypassAutoEncryption=true (#668)
(cherry picked from commit 98b64ee)
1 parent 73fcfb6 commit ef718b5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/test_encryption.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from bson.json_util import JSONOptions
3737
from bson.son import SON
3838

39+
from pymongo import encryption
3940
from pymongo.cursor import CursorType
4041
from pymongo.encryption import (Algorithm,
4142
ClientEncryption)
@@ -45,6 +46,7 @@
4546
EncryptionError,
4647
InvalidOperation,
4748
OperationFailure,
49+
ServerSelectionTimeoutError,
4850
WriteError)
4951
from pymongo.mongo_client import MongoClient
5052
from pymongo.operations import InsertOne
@@ -1582,5 +1584,51 @@ def test_case_8(self):
15821584
self.assertEqual(len(self.topology_listener.results['opened']), 1)
15831585

15841586

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+
15851633
if __name__ == "__main__":
15861634
unittest.main()

0 commit comments

Comments
 (0)