2
2
Configuring Queryable Encryption
3
3
================================
4
4
5
- .. versionadded :: 5.2.0b2
5
+ .. versionadded :: 5.2.0rc1
6
6
7
- .. admonition :: Queryable Encryption Compatibility
7
+ .. admonition :: MongoDB requirements
8
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 .
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 .
13
13
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 `
17
15
18
- Prerequisites
19
- -------------
16
+ Installation
17
+ ============
20
18
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 ::
25
23
26
24
pip install django-mongodb-backend[encryption]
27
25
28
- Settings
29
- --------
26
+ Configuring the `` DATABASES `` setting
27
+ =====================================
30
28
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.
35
31
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
37
40
38
41
import os
39
42
40
43
from django_mongodb_backend import parse_uri
41
44
from pymongo.encryption_options import AutoEncryptionOpts
42
45
43
46
DATABASES = {
44
- ...
47
+ # ...
45
48
" encrypted" : parse_uri(
46
49
DATABASE_URL ,
47
50
options = {
48
51
" auto_encryption_opts" : AutoEncryptionOpts(
49
- key_vault_namespace="my_encrypted_database .keyvault",
52
+ key_vault_namespace = " keyvault .keyvault" ,
50
53
kms_providers = {" local" : {" key" : os.urandom(96 )}},
51
54
)
52
55
},
53
56
db_name = " encrypted" ,
54
57
),
55
58
}
56
59
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
+
57
73
class EncryptedRouter :
58
74
"""
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 .
61
77
"""
62
78
63
79
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.
65
82
if app_label == " patientdata" :
66
83
return db == " encrypted"
67
84
# 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::
72
89
def kms_provider (self , model , ** hints ):
73
90
return " local"
74
91
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 ``.
97
92
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]
149
94
150
- You are now ready to use :doc: ` Queryable Encryption
151
- </topics/queryable-encryption>`.
95
+ Configuring KMS Providers
96
+ =========================
0 commit comments