Skip to content

Commit 416adff

Browse files
committed
Code review fixes
1 parent 844abcc commit 416adff

File tree

9 files changed

+29
-22
lines changed

9 files changed

+29
-22
lines changed

django_mongodb_backend/management/commands/showencryptedfieldsmap.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from bson import json_util
22
from django.apps import apps
33
from django.core.management.base import BaseCommand
4-
from django.db import DEFAULT_DB_ALIAS, connections
4+
from django.db import DEFAULT_DB_ALIAS, connections, router
55

6-
from django_mongodb_backend.model_utils import has_encrypted_fields
6+
from django_mongodb_backend.model_utils import model_has_encrypted_fields
77

88

99
class Command(BaseCommand):
@@ -31,13 +31,9 @@ def handle(self, *args, **options):
3131
client = connection.connection
3232
encrypted_fields_map = {}
3333
auto_encryption_opts = getattr(client._options, "auto_encryption_opts", None)
34-
# FIXME:
35-
# TypeError: ConnectionRouter.get_migratable_models() missing 2 required
36-
# positional arguments: 'app_config' and 'db'
37-
# for app_config in router.get_migratable_models():
3834
for app_config in apps.get_app_configs():
39-
for model in app_config.get_models():
40-
if has_encrypted_fields(model):
35+
for model in router.get_migratable_models(app_config, db):
36+
if model_has_encrypted_fields(model):
4137
from_db = not create
4238
fields = connection.schema_editor()._get_encrypted_fields_map(
4339
model, client, auto_encryption_opts, from_db=from_db

django_mongodb_backend/model_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
# TODO: Move to models.utils
2-
def has_encrypted_fields(model):
1+
def model_has_encrypted_fields(model):
32
return any(getattr(field, "encrypted", False) for field in model._meta.fields)

django_mongodb_backend/routers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.core.exceptions import ImproperlyConfigured
33
from django.db.utils import ConnectionRouter
44

5-
from .model_utils import has_encrypted_fields
5+
from .model_utils import model_has_encrypted_fields
66

77

88
class MongoRouter:
@@ -30,7 +30,7 @@ def kms_provider(self, model, *args, **kwargs):
3030
result = func(model, *args, **kwargs)
3131
if result is not None:
3232
return result
33-
if has_encrypted_fields(model):
33+
if model_has_encrypted_fields(model):
3434
raise ImproperlyConfigured("No kms_provider found in database router.")
3535
return None
3636

django_mongodb_backend/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .fields import EmbeddedModelField
99
from .indexes import SearchIndex
10-
from .model_utils import has_encrypted_fields
10+
from .model_utils import model_has_encrypted_fields
1111
from .query import wrap_database_errors
1212
from .utils import OperationCollector
1313

@@ -431,7 +431,7 @@ def _create_collection(self, model):
431431
"""
432432
db = self.get_database()
433433
db_table = model._meta.db_table
434-
if has_encrypted_fields(model):
434+
if model_has_encrypted_fields(model):
435435
client = self.connection.connection
436436
auto_encryption_opts = getattr(client._options, "auto_encryption_opts", None)
437437
if not auto_encryption_opts:
@@ -485,7 +485,7 @@ def _get_encrypted_fields_map(self, model, client, auto_encryption_opts, from_db
485485
"path": field.column,
486486
"keyId": data_key,
487487
}
488-
if getattr(field, "queries", None):
488+
if field.queries:
489489
field_dict["queries"] = field.queries
490490
field_list.append(field_dict)
491491
return {"fields": field_list}

docs/source/howto/queryable-encryption.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,20 @@ database router. Here's how to set it up in your Django settings.
8686

8787
DATABASE_ROUTERS = [EncryptedRouter()]
8888

89-
You are now ready to use server side :doc:`Queryable Encryption
89+
You are now ready to use server-side :doc:`Queryable Encryption
9090
</topics/queryable-encryption>` in your Django project.
9191

92+
.. admonition:: KMS providers and credentials
93+
94+
The above example uses a local KMS provider with a randomly generated
95+
key. In a production environment, you should use a secure KMS provider
96+
such as AWS KMS, Azure Key Vault, or GCP KMS.
97+
98+
Please refer to :ref:`manual:qe-fundamentals-kms-providers`
99+
for more information on configuring KMS providers and credentials as well as
100+
:doc:`manual:core/queryable-encryption/fundamentals/keys-key-vaults`
101+
for information on creating and managing data encryption keys.
102+
92103
.. _client-side-queryable-encryption:
93104

94105
Client-side Queryable Encryption

tests/encryption_/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
EQUALITY_QUERY = {"queryType": "equality"}
2424
RANGE_QUERY = {"queryType": "range"}
25+
RANGE_QUERY_MIN_MAX = {"queryType": "range", "min": 0, "max": 100}
2526

2627

2728
class Appointment(models.Model):
@@ -52,7 +53,7 @@ class PatientRecord(models.Model):
5253
ssn = EncryptedCharField(max_length=11, queries=EQUALITY_QUERY)
5354
birth_date = EncryptedDateField(queries=RANGE_QUERY)
5455
profile_picture = EncryptedBinaryField(queries=EQUALITY_QUERY)
55-
patient_age = EncryptedIntegerField("patient_age", queries=RANGE_QUERY)
56+
patient_age = EncryptedIntegerField("patient_age", queries=RANGE_QUERY_MIN_MAX)
5657
weight = EncryptedFloatField(queries=RANGE_QUERY)
5758

5859
# TODO: Embed Billing model

tests/encryption_/routers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django_mongodb_backend.model_utils import has_encrypted_fields
1+
from django_mongodb_backend.model_utils import model_has_encrypted_fields
22

33

44
class TestEncryptedRouter:
@@ -10,11 +10,11 @@ class TestEncryptedRouter:
1010

1111
def allow_migrate(self, db, app_label, model_name=None, model=None, **hints):
1212
if model:
13-
return db == ("encrypted" if has_encrypted_fields(model) else "default")
13+
return db == ("encrypted" if model_has_encrypted_fields(model) else "default")
1414
return db == "default"
1515

1616
def db_for_read(self, model, **hints):
17-
if has_encrypted_fields(model):
17+
if model_has_encrypted_fields(model):
1818
return "encrypted"
1919
return "default"
2020

tests/encryption_/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_patientrecord(self):
163163
PatientRecord.objects.get(ssn="123-45-6789").profile_picture, b"image data"
164164
)
165165
self.assertTrue(PatientRecord.objects.filter(patient_age__gte=40).exists())
166-
self.assertFalse(PatientRecord.objects.filter(patient_age__gte=200).exists())
166+
self.assertFalse(PatientRecord.objects.filter(patient_age__gte=80).exists())
167167
self.assertTrue(PatientRecord.objects.filter(weight__gte=175.0).exists())
168168

169169
# Test encrypted patient record in unencrypted database.

tests/encryption_/test_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
{
4747
"bsonType": "int",
4848
"path": "patient_age",
49-
"queries": {"queryType": "range"},
49+
"queries": {"queryType": "range", "max": 100, "min": 0},
5050
},
5151
{
5252
"bsonType": "double",

0 commit comments

Comments
 (0)