Skip to content

Commit 2d672f2

Browse files
committed
raise config error if no ae options + use other db
1 parent ec88df4 commit 2d672f2

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

django_mongodb_backend/encryption.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@
4444
class EncryptedRouter:
4545
def allow_migrate(self, db, app_label, model_name=None, model=None, **hints):
4646
if model:
47-
return db == (
48-
"my_encrypted_database" if getattr(model, "encrypted", False) else "default"
49-
)
47+
return db == ("other" if getattr(model, "encrypted", False) else "default")
5048
return db == "default"
5149

5250
def db_for_read(self, model, **hints):
5351
if getattr(model, "encrypted", False):
54-
return "my_encrypted_database"
52+
return "other"
5553
return "default"
5654

5755
db_for_write = db_for_read

django_mongodb_backend/schema.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.exceptions import ImproperlyConfigured
12
from django.db import router
23
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
34
from django.db.models import Index, UniqueConstraint
@@ -454,6 +455,12 @@ def _create_collection(self, model):
454455
provider,
455456
credentials,
456457
)
458+
else:
459+
raise ImproperlyConfigured(
460+
"The model has `encrypted=True`, but the connection does not have "
461+
"auto encryption options set. Please set `auto_encryption_opts` "
462+
"in the connection settings."
463+
)
457464
else:
458465
db.create_collection(db_table)
459466

tests/encryption_/routers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def allow_migrate(self, db, app_label, model_name=None, model=None, **hints):
1010

1111
def db_for_read(self, model, **hints):
1212
if getattr(model, "encrypted", False):
13-
return "my_encrypted_database"
13+
return "other"
1414
return None
1515

1616
db_for_write = db_for_read

tests/encryption_/tests.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def reload_module(module):
146146
)
147147
@override_settings(DATABASE_ROUTERS=[TestEncryptedRouter()])
148148
class EncryptedFieldTests(TransactionTestCase):
149-
databases = {"default", "my_encrypted_database"}
149+
databases = {"default", "other"}
150150
available_apps = ["django_mongodb_backend", "encryption_"]
151151

152152
def setUp(self):
@@ -216,7 +216,7 @@ def tearDownClass(cls):
216216

217217
def test_get_encrypted_fields_map_method(self):
218218
self.maxDiff = None
219-
with connections["my_encrypted_database"].schema_editor() as editor:
219+
with connections["other"].schema_editor() as editor:
220220
db_table = self.patient._meta.db_table
221221
self.assertCountEqual(
222222
{"fields": editor._get_encrypted_fields_map(self.patient)},
@@ -241,7 +241,7 @@ def test_get_encrypted_fields_map_command(self):
241241
call_command(
242242
"get_encrypted_fields_map",
243243
"--database",
244-
"my_encrypted_database",
244+
"other",
245245
verbosity=0,
246246
stdout=out,
247247
)
@@ -320,6 +320,18 @@ def test_patientrecord(self):
320320
self.assertFalse(PatientRecord.objects.filter(patient_age__gte=200).exists())
321321
self.assertTrue(PatientRecord.objects.filter(weight__gte=175.0).exists())
322322

323+
# Test encrypted patient record in unencrypted database.
324+
conn_params = connections["other"].get_connection_params()
325+
if conn_params.pop("auto_encryption_opts", False):
326+
# Call MongoClient instead of get_new_connection because
327+
# get_new_connection will return the encrypted connection
328+
# from the connection pool.
329+
connection = pymongo.MongoClient(**conn_params)
330+
patientrecords = connection["test_other"].patientrecord.find()
331+
ssn = patientrecords[0]["ssn"]
332+
self.assertTrue(isinstance(ssn, Binary))
333+
connection.close()
334+
323335
def test_patient(self):
324336
self.assertEqual(
325337
Patient.objects.get(patient_notes="patient notes " * 25).patient_notes,
@@ -337,22 +349,24 @@ def test_patient(self):
337349
)
338350

339351
# Test decrypted patient record in encrypted database.
340-
patients = connections["my_encrypted_database"].database.patient.find()
352+
patients = connections["other"].database.patient.find()
341353
self.assertEqual(len(list(patients)), 1)
342-
records = connections["my_encrypted_database"].database.patientrecord.find()
354+
records = connections["other"].database.patientrecord.find()
343355
self.assertTrue("__safeContent__" in records[0])
344356

345-
# Test encrypted patient record in unencrypted database.
346-
conn_params = connections["my_encrypted_database"].get_connection_params()
347-
if conn_params.pop("auto_encryption_opts", False):
348-
# Call MongoClient instead of get_new_connection because
349-
# get_new_connection will return the encrypted connection
350-
# from the connection pool.
351-
connection = pymongo.MongoClient(**conn_params)
352-
patientrecords = connection["test_my_encrypted_database"].patientrecord.find()
353-
ssn = patientrecords[0]["ssn"]
354-
self.assertTrue(isinstance(ssn, Binary))
355-
connection.close()
357+
def test_no_auto_encryption_opts(self):
358+
"""
359+
Ensure that the encrypted fields map is not set in the client
360+
when auto_encryption_opts is not set.
361+
"""
362+
Patient.objects.using("default").create(
363+
patient_id=2,
364+
patient_name="John Doe 2",
365+
patient_notes="patient notes " * 20,
366+
registration_date=datetime(2024, 10, 1, 12, 0, 0),
367+
is_active=True,
368+
369+
).save()
356370

357371

358372
class KMSCredentialsTests(TestCase):

0 commit comments

Comments
 (0)