@@ -4,73 +4,73 @@ Configuring Queryable Encryption
4
4
5
5
Configuring Queryable Encryption in Django is similar to
6
6
`configuring Queryable Encryption in Python <https://www.mongodb.com/docs/manual/core/queryable-encryption/quick-start/ >`_
7
- but with some additional steps to integrate with Django's operations. Below
8
- are the steps needed to set up Queryable Encryption in a Django project.
7
+ but with some additional steps required for Django.
8
+
9
+ .. note :: This section describes how to configure server side Queryable Encryption in Django.
10
+ For configuration of client side Queryable Encryption, please refer to this :ref: `FAQ question <queryable-encryption >`.
9
11
10
12
Prerequisites
11
13
-------------
12
14
13
- .. note :: You can use Queryable Encryption on a MongoDB 7.0 or later replica
14
- set or sharded cluster, but not a standalone instance.
15
- `This table <https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/compatibility/#std-label-qe-compatibility-reference >`_
16
- shows which MongoDB server products support which Queryable Encryption mechanisms.
17
-
18
15
In addition to :doc: `installing </intro/install >` and
19
16
:doc: `configuring </intro/configure >` Django MongoDB Backend,
20
17
you will need to install PyMongo with Queryable Encryption support::
21
18
22
19
pip install django-mongodb-backend[encryption]
23
20
21
+ .. note :: You can use Queryable Encryption on a MongoDB 7.0 or later replica
22
+ set or sharded cluster, but not a standalone instance.
23
+ `This table <https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/compatibility/#std-label-qe-compatibility-reference >`_
24
+ shows which MongoDB server products support which Queryable Encryption mechanisms.
25
+
26
+ .. _server-side-queryable-encryption-settings :
27
+
24
28
Settings
25
29
--------
26
30
27
- Add an encrypted database, encrypted database router and KMS credentials to
28
- your Django settings.
29
-
30
- .. note :: Use of the helpers provided in ``django_mongodb_backend.encryption``
31
- requires an encrypted database named "other".
31
+ Queryable Encryption in Django requires the use of an additional encrypted database
32
+ and Key Management Service (KMS) credentials as well as an encrypted database
33
+ router. Here's how to set it up in your Django settings.
32
34
33
35
::
34
36
35
- from django_mongodb_backend import encryption
36
- from pymongo.encryption import AutoEncryptionOpts
37
+ from django_mongodb_backend import parse_uri
38
+ from pymongo.encryption_options import AutoEncryptionOpts
37
39
38
40
DATABASES = {
39
41
"default": parse_uri(
40
- MONGODB_URI ,
42
+ DATABASE_URL ,
41
43
db_name="my_database",
42
44
),
43
- "other": parse_uri(
44
- MONGODB_URI,
45
- db_name="other",
46
- options={
47
- "auto_encryption_opts": AutoEncryptionOpts(
48
- kms_providers=encryption.KMS_PROVIDERS,
49
- key_vault_namespace="other.keyvault",
50
- )
51
- },
52
- ),
53
-
54
- DATABASES["other"]["KMS_CREDENTIALS"] = encryption.KMS_CREDENTIALS
55
- DATABASE_ROUTERS = [encryption.EncryptedRouter()]
56
-
57
- You are now ready to use :doc: `Queryable Encryption </topics/queryable-encryption >` in your Django project.
58
-
59
-
60
- Helper classes and settings
61
- ===========================
62
-
63
- ``KMS_CREDENTIALS ``
64
- -------------------
65
-
66
- ``KMS_PROVIDERS ``
67
- -----------------
68
-
69
- ``EncryptedRouter ``
70
- -------------------
71
-
72
- Query Types
73
- -----------
74
-
75
- - ``EqualityQuery ``
76
- - ``RangeQuery ``
45
+ }
46
+
47
+ DATABASES["encrypted"] = {
48
+ "ENGINE": "django_mongodb_backend",
49
+ "NAME": "my_encrypted_database",
50
+ "OPTIONS": {
51
+ "auto_encryption_opts": AutoEncryptionOpts(
52
+ key_vault_namespace="my_encrypted_database.keyvault",
53
+ kms_providers={"local": {"key": os.urandom(96)}},
54
+ ),
55
+ "directConnection": True,
56
+ },
57
+ "KMS_PROVIDERS": {},
58
+ "KMS_CREDENTIALS": {},
59
+ }
60
+
61
+ class EncryptedRouter:
62
+ def allow_migrate(self, db, app_label, model_name=None, **hints):
63
+ # The encryption_ app's models are only created in the encrypted database.
64
+ if app_label == "encryption_":
65
+ return db == "encrypted"
66
+ # Don't create other app's models in the encrypted database.
67
+ if db == "encrypted":
68
+ return False
69
+ return None
70
+
71
+ def kms_provider(self, model, **hints):
72
+ return "local"
73
+
74
+ DATABASE_ROUTERS = [EncryptedRouter()]
75
+
76
+ You are now ready to use server side :doc: `Queryable Encryption </topics/queryable-encryption >` in your Django project.
0 commit comments