Skip to content

Commit 1af38d8

Browse files
committed
Remove EncryptedModel, check for encrypted fields
1 parent a8d2aa4 commit 1af38d8

File tree

17 files changed

+66
-59
lines changed

17 files changed

+66
-59
lines changed

django_mongodb_backend/encryption.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import os
44

5+
from .fields import has_encrypted_fields
6+
57
KMS_CREDENTIALS = {
68
"aws": {
79
"key": os.getenv("AWS_KEY_ARN", ""),
@@ -44,11 +46,11 @@
4446
class EncryptedRouter:
4547
def allow_migrate(self, db, app_label, model_name=None, model=None, **hints):
4648
if model:
47-
return db == ("other" if getattr(model, "encrypted", False) else "default")
49+
return db == ("other" if has_encrypted_fields(model) else "default")
4850
return db == "default"
4951

5052
def db_for_read(self, model, **hints):
51-
if getattr(model, "encrypted", False):
53+
if has_encrypted_fields(model):
5254
return "other"
5355
return "default"
5456

django_mongodb_backend/fields/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .duration import register_duration_field
44
from .embedded_model import EmbeddedModelField
55
from .embedded_model_array import EmbeddedModelArrayField
6-
from .encrypted_model import (
6+
from .encryption import (
77
EncryptedBigIntegerField,
88
EncryptedBinaryField,
99
EncryptedBooleanField,
@@ -19,11 +19,13 @@
1919
EncryptedTextField,
2020
EncryptedTimeField,
2121
EncryptedURLField,
22+
has_encrypted_fields,
2223
)
2324
from .json import register_json_field
2425
from .objectid import ObjectIdField
2526

2627
__all__ = [
28+
"has_encrypted_fields",
2729
"register_fields",
2830
"ArrayField",
2931
"EmbeddedModelArrayField",

django_mongodb_backend/fields/encrypted_model.py renamed to django_mongodb_backend/fields/encryption.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from django.db import models
22

33

4+
def has_encrypted_fields(model):
5+
return any(getattr(field, "encrypted", False) for field in model._meta.fields)
6+
7+
48
class EncryptedFieldMixin(models.Field):
59
encrypted = True
610

django_mongodb_backend/models.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,3 @@ def delete(self, *args, **kwargs):
1414

1515
def save(self, *args, **kwargs):
1616
raise NotSupportedError("EmbeddedModels cannot be saved.")
17-
18-
19-
class EncryptedModel(models.Model):
20-
encrypted = True
21-
22-
class Meta:
23-
abstract = True

django_mongodb_backend/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pymongo.encryption import ClientEncryption
66
from pymongo.operations import SearchIndexModel
77

8-
from .fields import EmbeddedModelField
8+
from .fields import EmbeddedModelField, has_encrypted_fields
99
from .indexes import SearchIndex
1010
from .query import wrap_database_errors
1111
from .utils import OperationCollector
@@ -432,7 +432,7 @@ def _create_collection(self, model):
432432
"""
433433
db = self.get_database()
434434
db_table = model._meta.db_table
435-
if getattr(model, "encrypted", False):
435+
if has_encrypted_fields(model):
436436
client = self.connection.connection
437437
options = getattr(client._options, "auto_encryption_opts", None)
438438
if options is not None:

docs/source/howto/encryption.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In addition to :doc:`installing </intro/install>` and
1919
:doc:`configuring </intro/configure>` Django MongoDB Backend,
2020
you will need to install PyMongo with Queryable Encryption support::
2121

22-
pip install pymongo[aws,encryption]
22+
pip install django-mongodb-backend[encryption]
2323

2424
Settings
2525
--------
@@ -28,7 +28,7 @@ Add an encrypted database, encrypted database router and KMS credentials to
2828
your Django settings.
2929

3030
.. note:: Use of the helpers provided in ``django_mongodb_backend.encryption``
31-
requires an encrypted database named "my_encrypted_database".
31+
requires an encrypted database named "other".
3232

3333
::
3434

@@ -40,21 +40,21 @@ your Django settings.
4040
MONGODB_URI,
4141
db_name="my_database",
4242
),
43-
"my_encrypted_database": parse_uri(
43+
"other": parse_uri(
4444
MONGODB_URI,
45-
db_name="my_encrypted_database",
45+
db_name="other",
4646
options={
4747
"auto_encryption_opts": AutoEncryptionOpts(
4848
kms_providers=encryption.KMS_PROVIDERS,
49-
key_vault_namespace="my_encrypted_database.keyvault",
49+
key_vault_namespace="other.keyvault",
5050
)
5151
},
5252
),
5353

54-
DATABASES["my_encrypted_database"]["KMS_CREDENTIALS"] = encryption.KMS_CREDENTIALS
54+
DATABASES["other"]["KMS_CREDENTIALS"] = encryption.KMS_CREDENTIALS
5555
DATABASE_ROUTERS = [encryption.EncryptedRouter()]
5656

57-
You are now ready to use :doc:`encrypted models </topics/encrypted-models>` in your Django project.
57+
You are now ready to use :doc:`encrypted fields </topics/encrypted-fields>` in your Django project.
5858

5959

6060
Helper classes and settings

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Models
4545
**Topic guides:**
4646

4747
- :doc:`topics/embedded-models`
48-
- :doc:`topics/encrypted-models`
48+
- :doc:`topics/encrypted-fields`
4949

5050
Forms
5151
=====

docs/source/intro/configure.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ present.)
175175
Queryable Encryption
176176
--------------------
177177

178-
If you intend to use :doc:`encrypted models </topics/encrypted-models>`, you may
178+
If you intend to use :doc:`encrypted fields </topics/encrypted-fields>`, you may
179179
optionally configure the :setting:`DATABASE_ROUTERS` setting so that collections
180-
for encrypted models are created in an encrypted database.
180+
for encrypted fields are created in an encrypted database.
181181

182182
`Router configuration <https://docs.djangoproject.com/en/stable/topics/db/multi-db/#database-routers>`__
183183
is unique to each project and beyond the scope of Django MongoDB Backend, but an
184184
example is included in the :doc:`encryption helpers </howto/encryption>`
185-
that routes collection operations for encrypted models to a database named
186-
"my_encrypted_database"::
185+
that routes collection operations for encrypted fields to a database named
186+
"other"::
187187

188188
DATABASE_ROUTERS = ["django_mongodb_backend.encryption.EncryptedRouter"]
189189

docs/source/ref/django-admin.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ Available commands
3333

3434
.. django-admin:: get_encrypted_fields_map
3535

36-
Creates a schema map for the encrypted fields in your encrypted models. This
36+
Creates a schema map for the encrypted fields in your models. This
3737
map can be provided to
3838
:class:`~pymongo.encryption_options.AutoEncryptionOpts` for use with
39-
production deployments of :class:`~pymongo.encryption.ClientEncryption`.
39+
:class:`~pymongo.encryption.ClientEncryption`.
4040

4141
.. django-admin-option:: --database DATABASE
4242

43-
Specifies the database to use to generate an encrypted fields map
44-
for all encrypted models. Defaults to ``default``.
43+
Specifies the database to use to generate an encrypted fields map.
44+
Defaults to ``default``.

docs/source/ref/models/fields.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ they encrypt the data before storing it in the database.
349349
| ``EncryptedURLField`` | :class:`~django.db.models.URLField` |
350350
+---------------------------------------+--------------------------------------------------+
351351

352-
.. _encrypted-model-unsupported-fields:
352+
.. _encrypted-fields-unsupported-fields:
353353

354354
Unsupported fields
355355
~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)