@@ -14,7 +14,7 @@ Encryption in your Django project.
14
14
.. admonition :: MongoDB requirements
15
15
16
16
Queryable Encryption can be used with MongoDB replica sets or sharded
17
- clusters running version 7 .0 or later. Standalone instances are not
17
+ clusters running version 8 .0 or later. Standalone instances are not
18
18
supported. The following table summarizes which MongoDB server products
19
19
support each Queryable Encryption mechanism.
20
20
@@ -51,21 +51,36 @@ encryption keys.
51
51
52
52
import os
53
53
54
- from django_mongodb_backend import parse_uri
55
54
from pymongo.encryption_options import AutoEncryptionOpts
56
55
57
56
DATABASES = {
58
- # ...
59
- " encrypted" : parse_uri(
60
- DATABASE_URL ,
61
- options = {
57
+ " default" : {
58
+ " ENGINE" : " django_mongodb_backend" ,
59
+ " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
60
+ " NAME" : " my_database" ,
61
+ " USER" : " my_user" ,
62
+ " PASSWORD" : " my_password" ,
63
+ " PORT" : 27017 ,
64
+ " OPTIONS" : {
65
+ " retryWrites" : " true" ,
66
+ " w" : " majority" ,
67
+ " tls" : " false" ,
68
+ },
69
+ },
70
+ " encrypted" : {
71
+ " ENGINE" : " django_mongodb_backend" ,
72
+ " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
73
+ " NAME" : " encrypted" ,
74
+ " USER" : " my_user" ,
75
+ " PASSWORD" : " my_password" ,
76
+ " PORT" : 27017 ,
77
+ " OPTIONS" : {
62
78
" auto_encryption_opts" : AutoEncryptionOpts(
63
- key_vault_namespace = " keyvault .keyvault" ,
79
+ key_vault_namespace = " encrypted .keyvault" ,
64
80
kms_providers = {" local" : {" key" : os.urandom(96 )}},
65
81
)
66
82
},
67
- db_name = " encrypted" ,
68
- ),
83
+ },
69
84
}
70
85
71
86
Configuring the ``DATABASE_ROUTERS `` setting
@@ -88,10 +103,15 @@ configure a custom router for Queryable Encryption:
88
103
Encryption.
89
104
"""
90
105
106
+ def db_for_read (self , model , ** hints ):
107
+ if model._meta.app_label == " myapp" :
108
+ return " encrypted"
109
+ return None
110
+
111
+ db_for_write = db_for_read
112
+
91
113
def allow_migrate (self , db , app_label , model_name = None , ** hints ):
92
- # The patientdata app's models are only created in the encrypted
93
- # database.
94
- if app_label == " patientdata" :
114
+ if app_label == " myapp" :
95
115
return db == " encrypted"
96
116
# Don't create other app's models in the encrypted database.
97
117
if db == " encrypted" :
@@ -132,15 +152,19 @@ Example of KMS configuration with AWS KMS:
132
152
133
153
.. code-block :: python
134
154
135
- from django_mongodb_backend import parse_uri
136
155
from pymongo.encryption_options import AutoEncryptionOpts
137
156
138
157
DATABASES = {
139
- " encrypted" : parse_uri(
140
- DATABASE_URL ,
141
- options = {
158
+ " encrypted" : {
159
+ " ENGINE" : " django_mongodb_backend" ,
160
+ " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
161
+ " NAME" : " encrypted" ,
162
+ " USER" : " my_user" ,
163
+ " PASSWORD" : " my_password" ,
164
+ " PORT" : 27017 ,
165
+ " OPTIONS" : {
142
166
" auto_encryption_opts" : AutoEncryptionOpts(
143
- key_vault_namespace = " keyvault .keyvault" ,
167
+ key_vault_namespace = " encrypted .keyvault" ,
144
168
kms_providers = {
145
169
" aws" : {
146
170
" accessKeyId" : " your-access-key-id" ,
@@ -149,14 +173,12 @@ Example of KMS configuration with AWS KMS:
149
173
},
150
174
)
151
175
},
152
- db_name = " encrypted" ,
153
- ),
154
- }
155
-
156
- DATABASES [" encrypted" ][" KMS_CREDENTIALS" ] = {
157
- " aws" : {
158
- " key" : os.getenv(" AWS_KEY_ARN" , " " ),
159
- " region" : os.getenv(" AWS_KEY_REGION" , " " ),
176
+ " KMS_CREDENTIALS" : {
177
+ " aws" : {
178
+ " key" : os.getenv(" AWS_KEY_ARN" , " " ),
179
+ " region" : os.getenv(" AWS_KEY_REGION" , " " ),
180
+ },
181
+ },
160
182
},
161
183
}
162
184
@@ -208,6 +230,57 @@ If you do not want to use the data keys created by Django MongoDB Backend (when
208
230
In this scenario, Django MongoDB Backend will use the newly created data keys
209
231
to create collections for models with encrypted fields.
210
232
233
+ Here is an example of how to configure the
234
+ ``encrypted_fields_map `` in your Django settings:
235
+
236
+ .. code-block :: python
237
+
238
+ from pymongo.encryption_options import AutoEncryptionOpts
239
+ from bson import json_util
240
+
241
+ DATABASES = {
242
+ " encrypted" : {
243
+ " ENGINE" : " django_mongodb_backend" ,
244
+ " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
245
+ " NAME" : " encrypted" ,
246
+ " USER" : " my_user" ,
247
+ " PASSWORD" : " my_password" ,
248
+ " PORT" : 27017 ,
249
+ " OPTIONS" : {
250
+ " auto_encryption_opts" : AutoEncryptionOpts(
251
+ key_vault_namespace = " encrypted.keyvault" ,
252
+ kms_providers = {
253
+ " aws" : {
254
+ " accessKeyId" : " your-access-key-id" ,
255
+ " secretAccessKey" : " your-secret-access-key" ,
256
+ }
257
+ },
258
+ encrypted_fields_map = json_util.loads(
259
+ """ {
260
+ "encrypt_patient": {
261
+ "fields": [
262
+ {
263
+ "bsonType": "string",
264
+ "path": "patient_record.ssn",
265
+ "keyId": {
266
+ "$binary": {
267
+ "base64": "2MA29LaARIOqymYHGmi2mQ==",
268
+ "subType": "04"
269
+ }
270
+ },
271
+ "queries": {
272
+ "queryType": "equality"
273
+ }
274
+ },
275
+ ]
276
+ }
277
+ }"""
278
+ ),
279
+ )
280
+ },
281
+ },
282
+ }
283
+
211
284
Configuring the Automatic Encryption Shared Library
212
285
===================================================
213
286
@@ -218,25 +291,62 @@ to perform automatic encryption.
218
291
You can :ref: `download the shared library
219
292
<manual:qe-csfle-shared-library-download>` from the
220
293
:ref: `manual:enterprise-official-packages ` and configure it in your Django
221
- settings as follows:
294
+ settings using the ``crypt_shared_lib_path `` option in
295
+ :class: `pymongo.encryption_options.AutoEncryptionOpts `. The following example
296
+ shows how to configure the shared library in your Django settings:
222
297
223
298
.. code-block :: python
224
299
225
- from django_mongodb_backend import parse_uri
226
300
from pymongo.encryption_options import AutoEncryptionOpts
227
301
228
302
DATABASES = {
229
- " encrypted" : parse_uri(
230
- DATABASE_URL ,
231
- options = {
303
+ " encrypted" : {
304
+ " ENGINE" : " django_mongodb_backend" ,
305
+ " HOST" : " mongodb+srv://cluster0.example.mongodb.net" ,
306
+ " NAME" : " encrypted" ,
307
+ " USER" : " my_user" ,
308
+ " PASSWORD" : " my_password" ,
309
+ " PORT" : 27017 ,
310
+ " OPTIONS" : {
232
311
" auto_encryption_opts" : AutoEncryptionOpts(
233
- key_vault_namespace = " keyvault.keyvault" ,
234
- kms_providers = {" local" : {" key" : os.urandom(96 )}},
312
+ key_vault_namespace = " encrypted.keyvault" ,
313
+ kms_providers = {
314
+ " aws" : {
315
+ " accessKeyId" : " your-access-key-id" ,
316
+ " secretAccessKey" : " your-secret-access-key" ,
317
+ }
318
+ },
319
+ encrypted_fields_map = json_util.loads(
320
+ """ {
321
+ "encrypt_patient": {
322
+ "fields": [
323
+ {
324
+ "bsonType": "string",
325
+ "path": "patient_record.ssn",
326
+ "keyId": {
327
+ "$binary": {
328
+ "base64": "2MA29LaARIOqymYHGmi2mQ==",
329
+ "subType": "04"
330
+ }
331
+ },
332
+ "queries": {
333
+ "queryType": "equality"
334
+ }
335
+ },
336
+ ]
337
+ }
338
+ }"""
339
+ ),
235
340
crypt_shared_lib_path = " /path/to/mongo_crypt_shared_v1.dylib" ,
236
341
)
237
342
},
238
- db_name = " encrypted" ,
239
- ),
343
+ " KMS_CREDENTIALS" : {
344
+ " aws" : {
345
+ " key" : os.getenv(" AWS_KEY_ARN" , " " ),
346
+ " region" : os.getenv(" AWS_KEY_REGION" , " " ),
347
+ },
348
+ },
349
+ },
240
350
}
241
351
242
352
You are now ready to :doc: `start developing applications
0 commit comments