Skip to content

Commit b2fba28

Browse files
committed
Code review fixes (1/x)
- Planning to finish QE setup in the how-to doc then switch back to the topic doc to explain the workflow.
1 parent 2469f84 commit b2fba28

File tree

11 files changed

+135
-258
lines changed

11 files changed

+135
-258
lines changed

django_mongodb_backend/fields/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
from .embedded_model import EmbeddedModelField
55
from .embedded_model_array import EmbeddedModelArrayField
66
from .encryption import (
7-
EncryptedBigIntegerField,
87
EncryptedBinaryField,
98
EncryptedBooleanField,
109
EncryptedCharField,
1110
EncryptedDateField,
1211
EncryptedDateTimeField,
1312
EncryptedDecimalField,
13+
EncryptedDurationField,
1414
EncryptedEmailField,
1515
EncryptedFieldMixin,
1616
EncryptedFloatField,
1717
EncryptedGenericIPAddressField,
1818
EncryptedIntegerField,
19-
EncryptedPositiveBigIntegerField,
2019
EncryptedPositiveIntegerField,
2120
EncryptedPositiveSmallIntegerField,
2221
EncryptedSmallIntegerField,
@@ -33,19 +32,18 @@
3332
"ArrayField",
3433
"EmbeddedModelArrayField",
3534
"EmbeddedModelField",
36-
"EncryptedBigIntegerField",
3735
"EncryptedBinaryField",
3836
"EncryptedBooleanField",
3937
"EncryptedCharField",
4038
"EncryptedDateField",
4139
"EncryptedDateTimeField",
4240
"EncryptedDecimalField",
41+
"EncryptedDurationField",
4342
"EncryptedEmailField",
4443
"EncryptedFieldMixin",
4544
"EncryptedFloatField",
4645
"EncryptedGenericIPAddressField",
4746
"EncryptedIntegerField",
48-
"EncryptedPositiveBigIntegerField",
4947
"EncryptedPositiveIntegerField",
5048
"EncryptedPositiveSmallIntegerField",
5149
"EncryptedSmallIntegerField",

django_mongodb_backend/fields/encryption.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ def deconstruct(self):
2323
return name, path, args, kwargs
2424

2525

26-
class EncryptedBigIntegerField(EncryptedFieldMixin, models.BigIntegerField):
27-
pass
28-
29-
3026
class EncryptedBinaryField(EncryptedFieldMixin, models.BinaryField):
3127
pass
3228

@@ -51,6 +47,10 @@ class EncryptedDecimalField(EncryptedFieldMixin, models.DecimalField):
5147
pass
5248

5349

50+
class EncryptedDurationField(EncryptedFieldMixin, models.DurationField):
51+
pass
52+
53+
5454
class EncryptedEmailField(EncryptedFieldMixin, models.EmailField):
5555
pass
5656

@@ -67,10 +67,6 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
6767
pass
6868

6969

70-
class EncryptedPositiveBigIntegerField(EncryptedFieldMixin, models.PositiveBigIntegerField):
71-
pass
72-
73-
7470
class EncryptedPositiveIntegerField(EncryptedFieldMixin, models.PositiveIntegerField):
7571
pass
7672

django_mongodb_backend/management/commands/showencryptedfieldsmap.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@
77

88

99
class Command(BaseCommand):
10-
help = "Generate an `encrypted_fields_map` of encrypted fields for all encrypted"
11-
" models in the database for use with `AutoEncryptionOpts` in"
12-
" client configuration."
10+
help = """
11+
This command shows the mapping of encrypted fields to attributes
12+
including data type, data keys and query types. It can be used to set the
13+
``encrypted_fields_map`` in ``AutoEncryptionOpts``. Defaults to showing
14+
existing keys from the configured key vault.
15+
"""
1316

1417
def add_arguments(self, parser):
1518
parser.add_argument(
1619
"--database",
1720
default=DEFAULT_DB_ALIAS,
18-
help="Specify the database to use for generating the encrypted"
19-
"fields map. Defaults to the 'default' database.",
21+
help="""
22+
Specifies the database to use. Defaults to ``default``.""",
2023
)
2124
parser.add_argument(
2225
"--create-new-keys",
2326
action="store_true",
24-
help="Create the encrypted fields map.",
27+
help="""
28+
If specified, this option will create and show new encryption
29+
keys instead of showing existing keys from the configured key vault.
30+
""",
2531
)
2632

2733
def handle(self, *args, **options):

docs/source/howto/queryable-encryption.rst

Lines changed: 47 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,83 @@
22
Configuring Queryable Encryption
33
================================
44

5-
.. versionadded:: 5.2.0b2
5+
.. versionadded:: 5.2.0rc1
66

7-
.. admonition:: Queryable Encryption Compatibility
7+
.. admonition:: MongoDB requirements
88

9-
You can use Queryable Encryption on a MongoDB 7.0 or later replica
10-
set or sharded cluster, but not a standalone instance.
11-
:ref:`This table <manual:qe-compatibility-reference>` shows which MongoDB
12-
server products support which Queryable Encryption mechanisms.
9+
Queryable Encryption can be used with MongoDB replica sets or sharded
10+
clusters running version 7.0 or later. Standalone instances are not
11+
supported. The following table summarizes which MongoDB server products
12+
support each Queryable Encryption mechanism.
1313

14-
Configuring Queryable Encryption in Django is similar to
15-
:doc:`manual:core/queryable-encryption/quick-start` but with some additional
16-
steps required for Django.
14+
- :ref:`manual:qe-compatibility-reference`
1715

18-
Prerequisites
19-
-------------
16+
Installation
17+
============
2018

21-
In addition to :doc:`installing </intro/install>` and :doc:`configuring
22-
</intro/configure>` Django MongoDB Backend, you will need to install some
23-
additional packages to use Queryable Encryption. This can be done with the
24-
optional dependency ``encryption`` in the ``django-mongodb-backend`` package::
19+
In addition to the :doc:`installation </intro/install>` and :doc:`configuration
20+
</intro/configure>` steps for Django MongoDB Backend, enabling Queryable
21+
Encryption requires support for encryption and a Key Management Service (KMS).
22+
You can install these additional dependencies with the following command::
2523

2624
pip install django-mongodb-backend[encryption]
2725

28-
Settings
29-
--------
26+
Configuring the ``DATABASES`` setting
27+
=====================================
3028

31-
Due to a limited set of
32-
:doc:`manual:core/queryable-encryption/reference/supported-operations`, a second
33-
encrypted database and corresponding database router are needed to use Queryable
34-
Encryption in Django, as well as a KMS provider and credentials.
29+
In addition to :ref:`configuring-databases-setting`, you must also configure an
30+
encrypted database in your ``DATABASES`` setting.
3531

36-
Here's how to set it up in your Django settings::
32+
This database will be used to store encrypted fields in your models. The
33+
following example shows how to configure an encrypted database using the
34+
``AutoEncryptionOpts`` from the ``pymongo.encryption_options`` module.
35+
36+
This example uses a local KMS provider and a key vault namespace for storing
37+
encryption keys.
38+
39+
.. code-block:: python
3740
3841
import os
3942
4043
from django_mongodb_backend import parse_uri
4144
from pymongo.encryption_options import AutoEncryptionOpts
4245
4346
DATABASES = {
44-
...
47+
# ...
4548
"encrypted": parse_uri(
4649
DATABASE_URL,
4750
options={
4851
"auto_encryption_opts": AutoEncryptionOpts(
49-
key_vault_namespace="my_encrypted_database.keyvault",
52+
key_vault_namespace="keyvault.keyvault",
5053
kms_providers={"local": {"key": os.urandom(96)}},
5154
)
5255
},
5356
db_name="encrypted",
5457
),
5558
}
5659
60+
Configuring the ``DATABASE_ROUTERS`` setting
61+
============================================
62+
63+
Similar to :ref:`configuring-database-routers-setting` for using embedded
64+
models, to use Queryable Encryption, you must also configure the
65+
``DATABASE_ROUTERS`` setting to route queries to the encrypted database.
66+
67+
This is done by adding a custom router that routes queries to the encrypted
68+
database based on the model's metadata. The following example shows how to
69+
configure a custom router for Queryable Encryption:
70+
71+
.. code-block:: python
72+
5773
class EncryptedRouter:
5874
"""
59-
A database router for an encrypted database to be used with a
60-
"patientdata" app that contains models with encrypted fields.
75+
A router for routing queries to the encrypted database for Queryable
76+
Encryption.
6177
"""
6278
6379
def allow_migrate(self, db, app_label, model_name=None, **hints):
64-
# The patientdata app's models are only created in the encrypted database.
80+
# The patientdata app's models are only created in the encrypted
81+
# database.
6582
if app_label == "patientdata":
6683
return db == "encrypted"
6784
# Don't create other app's models in the encrypted database.
@@ -72,80 +89,8 @@ Here's how to set it up in your Django settings::
7289
def kms_provider(self, model, **hints):
7390
return "local"
7491
75-
DATABASE_ROUTERS = [EncryptedRouter()]
76-
77-
.. admonition:: KMS providers and credentials
78-
79-
The above example uses a local KMS provider with a randomly generated
80-
key. In a production environment, you should use a secure KMS provider
81-
such as AWS KMS, Azure Key Vault, or GCP KMS.
82-
83-
Please refer to :ref:`manual:qe-fundamentals-kms-providers`
84-
for more information on configuring KMS providers and credentials as well as
85-
:doc:`manual:core/queryable-encryption/fundamentals/keys-key-vaults`
86-
for information on creating and managing data encryption keys.
87-
88-
You can also refer to the `Python Queryable Encryption Tutorial
89-
<https://github.com/mongodb/docs/tree/adad2b1ae41ec81a6e5682842850030813adc1e5/source/includes/qe-tutorials/python>`_.
90-
91-
Encrypted fields map
92-
~~~~~~~~~~~~~~~~~~~~
93-
94-
In addition to the :ref:`settings described in the how-to guide
95-
<queryable-encryption-settings>` you will need to provide a
96-
``encrypted_fields_map`` to the ``AutoEncryptionOpts``.
9792
98-
You can use the :djadmin:`showencryptedfieldsmap` management command to generate
99-
the schema map for your encrypted fields and then use the results in your
100-
settings::
101-
102-
python manage.py showencryptedfieldsmap --database=encrypted
103-
104-
.. admonition:: Didn't work?
105-
106-
If you get the error ``Unknown command: 'showencryptedfieldsmap'``, ensure
107-
``"django_mongodb_backend"`` is in your :setting:`INSTALLED_APPS` setting.
108-
109-
Crypt shared library
110-
~~~~~~~~~~~~~~~~~~~~
111-
112-
Additionally, you will need to ensure that the :ref:`crypt shared library is
113-
available <manual:qe-reference-shared-library>` to your Python environment.
114-
The crypt shared library is recommended over mongocryptd for Queryable
115-
Encryption because it doesn't require an additional daemon process to run to
116-
facilitate Queryable Encryption operations.
117-
118-
Settings
119-
~~~~~~~~
120-
121-
Now include the crypt shared library path and generated schema map in your
122-
Django settings::
123-
124-
from bson.binary import Binary
125-
from pymongo.encryption_options import AutoEncryptionOpts
126-
127-
128-
DATABASES["encrypted"] = {
129-
...
130-
"OPTIONS": {
131-
"auto_encryption_opts": AutoEncryptionOpts(
132-
...
133-
crypt_shared_lib_path="/path/to/mongo_crypt_v1",
134-
encrypted_fields_map = {
135-
"encryption__patientrecord": {
136-
"fields": [
137-
{
138-
"bsonType": "string",
139-
"path": "ssn",
140-
"queries": {"queryType": "equality"},
141-
"keyId": Binary(b"\x14F\x89\xde\x8d\x04K7\xa9\x9a\xaf_\xca\x8a\xfb&", 4),
142-
},
143-
}
144-
},
145-
},
146-
),
147-
},
148-
}
93+
DATABASE_ROUTERS = [EncryptedRouter]
14994
150-
You are now ready to use :doc:`Queryable Encryption
151-
</topics/queryable-encryption>`.
95+
Configuring KMS Providers
96+
=========================

docs/source/index.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Having trouble? We’d like to help!
2323

2424
- Try the :doc:`faq` – it’s got answers to many common questions.
2525

26-
- The :doc:`howto/index` section has step-by-step guides for common tasks.
27-
2826
- Looking for specific information? Try the :ref:`genindex`, :ref:`modindex`,
2927
or the detailed :doc:`table of contents <contents>`.
3028

@@ -48,7 +46,7 @@ Models
4846
- :doc:`ref/database`
4947
- :doc:`ref/contrib/gis`
5048
- :doc:`ref/django-admin`
51-
- :doc:`ref/queryable-encryption`
49+
- :doc:`ref/models/encrypted-fields`
5250

5351
**Topic guides:**
5452

docs/source/ref/django-admin.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ Available commands
2222
.. django-admin:: showencryptedfieldsmap
2323

2424
This command shows the mapping of encrypted fields to attributes including
25-
data type, data keys and query types. Defaults to showing existing keys from
26-
the configured key vault.
25+
data type, data keys and query types. It can be used to set the
26+
``encrypted_fields_map`` in ``AutoEncryptionOpts``. Defaults to showing
27+
existing keys from the configured key vault.
2728

2829
.. django-admin-option:: --database DATABASE
2930

3031
Specifies the database to use. Defaults to ``default``.
3132

3233
.. django-admin-option:: --create-new-keys
3334

34-
If specified, this option will create new encryption keys for use in
35-
production workflows.
35+
If specified, this option will create and show new encryption keys
36+
instead of showing existing keys from the configured key vault.

docs/source/ref/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ API reference
99
forms
1010
contrib/index
1111
database
12-
queryable-encryption
12+
models/encrypted-fields
1313
django-admin
1414
utils
1515
settings

0 commit comments

Comments
 (0)