22Configuring Queryable Encryption
33================================
44
5+ .. versionadded :: 5.2.0b2
6+
7+ .. admonition :: Queryable Encryption Compatibility
8+
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.
13+
514.. _server-side-queryable-encryption :
615
716Configuring Queryable Encryption in Django is similar to
817:doc: `manual:core/queryable-encryption/quick-start ` but with some additional
918steps required for Django.
1019
11- Server-side Queryable Encryption
12- --------------------------------
13-
14- Server-side Queryable Encryption allows you to begin developing applications
15- without needing to define the encrypted fields map at the time of connection
16- to the database.
17-
18- .. admonition :: What about client-side Queryable Encryption?
19-
20- For configuration of client-side Queryable Encryption,
21- please refer to this :ref: `see below <client-side-queryable-encryption >`.
22-
2320Prerequisites
2421-------------
2522
26- In addition to :doc: `installing </intro/install >` and
27- :doc: `configuring </intro/configure >` Django MongoDB Backend,
28- you will need to install PyMongo with Queryable Encryption support::
23+ In addition to :doc: `installing </intro/install >` and :doc: `configuring
24+ </intro/configure>` Django MongoDB Backend, you will need to install some
25+ additional packages to use Queryable Encryption. This can be done with the
26+ optional dependency ``encryption `` in the ``django-mongodb-backend `` package.
2927
3028 pip install django-mongodb-backend[encryption]
3129
32- .. admonition :: Queryable Encryption Compatibility
30+ .. _ server-side-queryable-encryption-settings :
3331
34- You can use Queryable Encryption on a MongoDB 7.0 or later replica
35- set or sharded cluster, but not a standalone instance.
36- :ref: `This table <manual:qe-compatibility-reference >` shows which MongoDB
37- server products support which Queryable Encryption mechanisms.
32+ Server-side Queryable Encryption
33+ --------------------------------
3834
39- .. _server-side-queryable-encryption-settings :
35+ Queryable Encryption is considered "server-side" when the encrypted fields map
36+ is not known at the time of connection to the database and this approach allows
37+ you to begin developing applications without needing to define the encrypted
38+ fields map at the time of connection to the database.
4039
4140Settings
4241--------
4342
44- Queryable Encryption in Django requires the use of an additional encrypted
45- database and Key Management Service (KMS) credentials as well as an encrypted
46- database router. Here's how to set it up in your Django settings.
43+ Due to a limited set of
44+ :doc: `manual:core/queryable-encryption/reference/supported-operations `, a second
45+ encrypted database and corresponding database router are needed to use Queryable
46+ Encryption in Django, as well as a KMS provider and credentials.
4747
48- ::
48+ Here's how to set it up in your Django settings ::
4949
5050 from django_mongodb_backend import parse_uri
5151 from pymongo.encryption_options import AutoEncryptionOpts
@@ -55,26 +55,28 @@ database router. Here's how to set it up in your Django settings.
5555 DATABASE_URL,
5656 db_name="my_database",
5757 ),
58- }
59-
60- DATABASES["encrypted"] = {
61- "ENGINE": "django_mongodb_backend",
62- "NAME": "my_encrypted_database",
63- "OPTIONS": {
64- "auto_encryption_opts": AutoEncryptionOpts(
65- key_vault_namespace="my_encrypted_database.keyvault",
66- kms_providers={"local": {"key": os.urandom(96)}},
67- ),
68- "directConnection": True,
69- },
70- "KMS_PROVIDERS": {},
71- "KMS_CREDENTIALS": {},
58+ "encrypted": parse_uri(
59+ DATABASE_URL,
60+ options: {
61+ "auto_encryption_opts": AutoEncryptionOpts(
62+ key_vault_namespace="my_encrypted_database.keyvault",
63+ kms_providers={"local": {"key": os.urandom(96)}},
64+ ),
65+ },
66+ db_name="my_encrypted_database",
67+ "KMS_CREDENTIALS": {},
68+ ),
7269 }
7370
7471 class EncryptedRouter:
72+ """
73+ A database router for an encrypted database to be used with a
74+ "patientdata" app that contains models with encrypted fields.
75+ """
76+
7577 def allow_migrate(self, db, app_label, model_name=None, **hints):
76- # The encryption_ app's models are only created in the encrypted database.
77- if app_label == "encryption_ ":
78+ # The patientdata app's models are only created in the encrypted database.
79+ if app_label == "patientdata ":
7880 return db == "encrypted"
7981 # Don't create other app's models in the encrypted database.
8082 if db == "encrypted":
@@ -87,7 +89,7 @@ database router. Here's how to set it up in your Django settings.
8789 DATABASE_ROUTERS = [EncryptedRouter()]
8890
8991You are now ready to use server-side :doc: `Queryable Encryption
90- </topics/queryable-encryption>` in your Django project .
92+ </topics/queryable-encryption>`.
9193
9294.. admonition :: KMS providers and credentials
9395
@@ -100,37 +102,35 @@ You are now ready to use server-side :doc:`Queryable Encryption
100102 :doc: `manual:core/queryable-encryption/fundamentals/keys-key-vaults `
101103 for information on creating and managing data encryption keys.
102104
105+ You can also refer to the `Python Queryable Encryption Tutorial
106+ <https://github.com/mongodb/docs/tree/adad2b1ae41ec81a6e5682842850030813adc1e5/source/includes/qe-tutorials/python> `_.
107+
103108.. _client-side-queryable-encryption :
104109
105110Client-side Queryable Encryption
106111--------------------------------
107112
108- In the :ref: `section above <server-side-queryable-encryption-settings >`,
109- server-side Queryable Encryption configuration is covered.
110-
111- Client side Queryable Encryption configuration requires that the entire
112- encrypted fields map be known at the time of client connection.
113+ Queryable Encryption is considered "client-side" when the encrypted fields map
114+ is known at the time of connection to the database. This approach can be used
115+ when you have finished development and you have a fixed set of encrypted fields
116+ that you want to use in your production environment.
113117
114118Encrypted fields map
115119~~~~~~~~~~~~~~~~~~~~
116120
117- In addition to the
118- :ref: `settings described in the how-to guide <server-side-queryable-encryption-settings >`,
119- you will need to provide a ``encrypted_fields_map `` to the
120- ``AutoEncryptionOpts ``.
121+ In addition to the :ref: `settings described in the how-to guide
122+ <server-side-queryable-encryption-settings>` you will need to provide a
123+ ``encrypted_fields_map `` to the ``AutoEncryptionOpts ``.
121124
122- Fortunately, this is easy to do with Django MongoDB Backend. You can use
123- the ``showencryptedfieldsmap `` management command to generate the schema map
124- for your encrypted fields, and then use the results in your settings.
125-
126- To generate the encrypted fields map, run the following command in your Django
127- project::
125+ You can use the ``showencryptedfieldsmap `` management command to generate the
126+ schema map for your encrypted fields and then use the results in your settings::
128127
129128 python manage.py showencryptedfieldsmap
130129
131- .. note :: The ``showencryptedfieldsmap`` command is only available if you
132- have the ``django_mongodb_backend `` app included in the
133- :setting: `INSTALLED_APPS ` setting.
130+ .. admonition :: Didn't work?
131+
132+ If you get the error ``Unknown command: 'createcachecollection' ``, ensure
133+ ``"django_mongodb_backend" `` is in your :setting: `INSTALLED_APPS ` setting.
134134
135135Settings
136136~~~~~~~~
@@ -162,6 +162,5 @@ Now include the generated schema map in your Django settings::
162162 …
163163 }
164164
165- You are now ready to use client-side
166- :doc: `Queryable Encryption </topics/queryable-encryption >`
167- in your Django project.
165+ You are now ready to use client-side :doc: `Queryable Encryption
166+ </topics/queryable-encryption>`.
0 commit comments