2
2
Configuring Queryable Encryption
3
3
================================
4
4
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
+
5
14
.. _server-side-queryable-encryption :
6
15
7
16
Configuring Queryable Encryption in Django is similar to
8
17
:doc: `manual:core/queryable-encryption/quick-start ` but with some additional
9
18
steps required for Django.
10
19
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
-
23
20
Prerequisites
24
21
-------------
25
22
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.
29
27
30
28
pip install django-mongodb-backend[encryption]
31
29
32
- .. admonition :: Queryable Encryption Compatibility
30
+ .. _ server-side-queryable-encryption-settings :
33
31
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
+ --------------------------------
38
34
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.
40
39
41
40
Settings
42
41
--------
43
42
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.
47
47
48
- ::
48
+ Here's how to set it up in your Django settings ::
49
49
50
50
from django_mongodb_backend import parse_uri
51
51
from pymongo.encryption_options import AutoEncryptionOpts
@@ -55,26 +55,28 @@ database router. Here's how to set it up in your Django settings.
55
55
DATABASE_URL,
56
56
db_name="my_database",
57
57
),
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
+ ),
72
69
}
73
70
74
71
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
+
75
77
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 ":
78
80
return db == "encrypted"
79
81
# Don't create other app's models in the encrypted database.
80
82
if db == "encrypted":
@@ -87,7 +89,7 @@ database router. Here's how to set it up in your Django settings.
87
89
DATABASE_ROUTERS = [EncryptedRouter()]
88
90
89
91
You are now ready to use server-side :doc: `Queryable Encryption
90
- </topics/queryable-encryption>` in your Django project .
92
+ </topics/queryable-encryption>`.
91
93
92
94
.. admonition :: KMS providers and credentials
93
95
@@ -100,37 +102,35 @@ You are now ready to use server-side :doc:`Queryable Encryption
100
102
:doc: `manual:core/queryable-encryption/fundamentals/keys-key-vaults `
101
103
for information on creating and managing data encryption keys.
102
104
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
+
103
108
.. _client-side-queryable-encryption :
104
109
105
110
Client-side Queryable Encryption
106
111
--------------------------------
107
112
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.
113
117
114
118
Encrypted fields map
115
119
~~~~~~~~~~~~~~~~~~~~
116
120
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 ``.
121
124
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::
128
127
129
128
python manage.py showencryptedfieldsmap
130
129
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.
134
134
135
135
Settings
136
136
~~~~~~~~
@@ -162,6 +162,5 @@ Now include the generated schema map in your Django settings::
162
162
…
163
163
}
164
164
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