Skip to content

Commit 98b64ee

Browse files
authored
PYTHON-2096 Validate that mongocryptd is not spawned if bypassAutoEncryption=true (#668)
1 parent c8d920a commit 98b64ee

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
@@ -35,6 +35,7 @@
3535
from bson.json_util import JSONOptions
3636
from bson.son import SON
3737

38+
from pymongo import encryption
3839
from pymongo.cursor import CursorType
3940
from pymongo.encryption import (Algorithm,
4041
ClientEncryption)
@@ -44,6 +45,7 @@
4445
EncryptionError,
4546
InvalidOperation,
4647
OperationFailure,
48+
ServerSelectionTimeoutError,
4749
WriteError)
4850
from pymongo.mongo_client import MongoClient
4951
from pymongo.operations import InsertOne
@@ -1576,5 +1578,51 @@ def test_case_8(self):
15761578
self.assertEqual(len(self.topology_listener.results['opened']), 1)
15771579

15781580

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

0 commit comments

Comments
 (0)