@@ -24,10 +24,9 @@ Installation
2424============
2525
2626In addition to the :doc: `installation </intro/install >` and :doc: `configuration
27- </intro/configure>` steps already required to use Django MongoDB Backend,
28- Queryable Encryption requires the installation of additional dependencies.
29- You can install these dependencies by using the ``encryption `` extra when
30- installing ``django-mongodb-backend ``:
27+ </intro/configure>` steps required to use Django MongoDB Backend, Queryable
28+ Encryption has additional dependencies. You can install these dependencies
29+ by using the ``encryption `` extra when installing ``django-mongodb-backend ``:
3130
3231.. code-block :: console
3332
@@ -39,13 +38,14 @@ Configuring the ``DATABASES`` setting
3938=====================================
4039
4140In addition to the :ref: `database settings <configuring-databases-setting >`
42- already required to use Django MongoDB Backend, you must configure an
43- encrypted database to store encrypted data.
41+ required to use Django MongoDB Backend, Queryable Encryption requires you to
42+ configure a separate encrypted database connection in your
43+ :setting: `django:DATABASES ` setting.
4444
4545.. admonition :: Encrypted database
4646
4747 An encrypted database is a separate database connection in your
48- :setting: `django:DATABASES ` setting that is configured to use
48+ :setting: `django:DATABASES ` setting that is configured to use PyMongo's
4949 :class: `automatic encryption
5050 <pymongo.encryption_options.AutoEncryptionOpts> `.
5151
@@ -102,15 +102,16 @@ provider and encryption keys stored in the ``encryption.__keyVault`` collection.
102102Configuring the ``DATABASE_ROUTERS `` setting
103103============================================
104104
105- Similar to configuring a :ref: `database router
106- <configuring-database-routers-setting>` for
105+ Similar to configuring the :ref: `DATABASE_ROUTERS
106+ <configuring-database-routers-setting>` setting for
107107:doc: `embedded models </topics/embedded-models >`, Queryable Encryption
108- requires a :setting: `database router <django:DATABASE_ROUTERS> ` to route
109- queries to the encrypted database.
108+ requires a :setting: `DATABASE_ROUTERS <django:DATABASE_ROUTERS> ` setting to
109+ route database operations to the encrypted database.
110110
111- The following example shows how to
112- configure a custom router for the "myapp" application that routes queries to the
113- encrypted database.
111+ The following example shows how to configure a router for the "myapp"
112+ application that routes database operations to the encrypted database for all
113+ models in that application. The router also specifies the :ref: `KMS provider
114+ <qe-configuring-kms>` to use.
114115
115116.. code-block :: python
116117
@@ -148,7 +149,11 @@ Then in your Django settings, add the custom router to the
148149Configuring the Key Management Service (KMS)
149150============================================
150151
151- To use Queryable Encryption, you must configure a Key Management Service (KMS).
152+ To use Queryable Encryption, you must configure a Key Management Service (KMS)
153+ to store and manage your encryption keys. Django MongoDB Backend allows you to
154+ configure multiple KMS providers and select the appropriate provider for each
155+ model using a custom database router.
156+
152157The KMS is responsible for managing the encryption keys used to encrypt and
153158decrypt data. The following table summarizes the available KMS configuration
154159options followed by an example of how to use them.
@@ -175,23 +180,7 @@ Example of KMS configuration with AWS KMS:
175180
176181 DATABASES = {
177182 " encrypted" : {
178- " ENGINE" : " django_mongodb_backend" ,
179- " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
180- " NAME" : " encrypted" ,
181- " USER" : " my_user" ,
182- " PASSWORD" : " my_password" ,
183- " PORT" : 27017 ,
184- " OPTIONS" : {
185- " auto_encryption_opts" : AutoEncryptionOpts(
186- key_vault_namespace = " encryption.__keyVault" ,
187- kms_providers = {
188- " aws" : {
189- " accessKeyId" : " your-access-key-id" ,
190- " secretAccessKey" : " your-secret-access-key" ,
191- }
192- },
193- )
194- },
183+ # ...
195184 " KMS_CREDENTIALS" : {
196185 " aws" : {
197186 " key" : os.getenv(" AWS_KEY_ARN" , " " ),
@@ -212,22 +201,22 @@ Example of KMS configuration with AWS KMS:
212201Configuring the ``encrypted_fields_map ``
213202========================================
214203
215- When you :ref: `configure an encrypted database connection
216- <qe-configuring-databases- setting>` without specifying an
204+ When you configure the :ref: `DATABASES < qe-configuring-databases-setting >`
205+ setting for Queryable Encryption * without * specifying an
217206``encrypted_fields_map ``, Django MongoDB Backend will create encrypted
218- collections for you when you run ``python manage.py migrate --database
219- encrypted ``.
207+ collections, including data keys, when you run ``python manage.py migrate
208+ --database encrypted ``.
220209
221- Encryption keys for encrypted fields are stored in the key vault
222- :ref: `specified in the Django settings <qe-configuring-kms >`. To see the keys
223- created by Django MongoDB Backend, along with the entire schema, you can run the
210+ Encryption keys for encrypted fields are stored in the key vault specified in
211+ the :ref: `DATABASES <qe-configuring-kms >` setting . To see the keys created by
212+ Django MongoDB Backend, along with the entire schema, you can run the
224213:djadmin: `showencryptedfieldsmap ` command::
225214
226215 $ python manage.py showencryptedfieldsmap --database encrypted
227216
228- Use the output of the :djadmin: `showencryptedfieldsmap ` command to set the
229- ``encrypted_fields_map `` in
230- :class: ` pymongo.encryption_options.AutoEncryptionOpts ` in your Django settings.
217+ Use the output of :djadmin: `showencryptedfieldsmap ` to set the
218+ ``encrypted_fields_map `` in :class: ` AutoEncryptionOpts
219+ < pymongo.encryption_options.AutoEncryptionOpts> ` in your Django settings.
231220
232221.. code-block :: python
233222
@@ -236,21 +225,10 @@ Use the output of the :djadmin:`showencryptedfieldsmap` command to set the
236225
237226 DATABASES = {
238227 " encrypted" : {
239- " ENGINE" : " django_mongodb_backend" ,
240- " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
241- " NAME" : " encrypted" ,
242- " USER" : " my_user" ,
243- " PASSWORD" : " my_password" ,
244- " PORT" : 27017 ,
228+ # ...
245229 " OPTIONS" : {
246230 " auto_encryption_opts" : AutoEncryptionOpts(
247- key_vault_namespace = " encryption.__keyVault" ,
248- kms_providers = {
249- " aws" : {
250- " accessKeyId" : " your-access-key-id" ,
251- " secretAccessKey" : " your-secret-access-key" ,
252- }
253- },
231+ # ...
254232 encrypted_fields_map = json_util.loads(
255233 """ {
256234 "encrypt_patient": {
@@ -277,6 +255,13 @@ Use the output of the :djadmin:`showencryptedfieldsmap` command to set the
277255 },
278256 }
279257
258+
259+ .. admonition :: Security consideration
260+
261+ Supplying an encrypted fields map provides more security than relying on an
262+ encrypted fields map obtained from the server. It protects against a
263+ malicious server advertising a false encrypted fields map.
264+
280265Configuring the Automatic Encryption Shared Library
281266===================================================
282267
@@ -288,60 +273,25 @@ You can :ref:`download the shared library
288273<manual:qe-csfle-shared-library-download>` from the
289274:ref: `manual:enterprise-official-packages ` and configure it in your Django
290275settings using the ``crypt_shared_lib_path `` option in
291- :class: `pymongo.encryption_options.AutoEncryptionOpts `. The following example
292- shows how to configure the shared library in your Django settings:
276+ :class: `AutoEncryptionOpts <pymongo.encryption_options.AutoEncryptionOpts> `.
277+
278+ The following example shows how to configure the shared library in your Django
279+ settings:
293280
294281.. code-block :: python
295282
296283 from pymongo.encryption_options import AutoEncryptionOpts
297284
298285 DATABASES = {
299286 " encrypted" : {
300- " ENGINE" : " django_mongodb_backend" ,
301- " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
302- " NAME" : " encrypted" ,
303- " USER" : " my_user" ,
304- " PASSWORD" : " my_password" ,
305- " PORT" : 27017 ,
287+ # ...
306288 " OPTIONS" : {
307289 " auto_encryption_opts" : AutoEncryptionOpts(
308- key_vault_namespace = " encryption.__keyVault" ,
309- kms_providers = {
310- " aws" : {
311- " accessKeyId" : " your-access-key-id" ,
312- " secretAccessKey" : " your-secret-access-key" ,
313- }
314- },
315- encrypted_fields_map = json_util.loads(
316- """ {
317- "encrypt_patient": {
318- "fields": [
319- {
320- "bsonType": "string",
321- "path": "patient_record.ssn",
322- "keyId": {
323- "$binary": {
324- "base64": "2MA29LaARIOqymYHGmi2mQ==",
325- "subType": "04"
326- }
327- },
328- "queries": {
329- "queryType": "equality"
330- }
331- },
332- ]
333- }
334- }"""
335- ),
290+ # ...
336291 crypt_shared_lib_path = " /path/to/mongo_crypt_shared_v1.dylib" ,
337292 )
338293 },
339- " KMS_CREDENTIALS" : {
340- " aws" : {
341- " key" : os.getenv(" AWS_KEY_ARN" , " " ),
342- " region" : os.getenv(" AWS_KEY_REGION" , " " ),
343- },
344- },
294+ # ...
345295 },
346296 }
347297
0 commit comments