Skip to content

Commit 6ab0a86

Browse files
committed
Fix db name on two encryption tests + doc updates
1 parent a6a12e7 commit 6ab0a86

File tree

7 files changed

+128
-59
lines changed

7 files changed

+128
-59
lines changed

docs/source/faq.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,71 @@ logging::
5252
If running ``manage.py dumpdata`` results in ``CommandError: Unable to
5353
serialize database: 'EmbeddedModelManager' object has no attribute using'``,
5454
see :ref:`configuring-database-routers-setting`.
55+
56+
.. _queryable-encryption:
57+
58+
Queryable Encryption
59+
====================
60+
61+
What about client side configuration?
62+
-------------------------------------
63+
64+
In the :doc:`Queryable Encryption how-to guide <howto/queryable-encryption>`,
65+
server side Queryable Encryption configuration is covered.
66+
67+
Client side Queryable Encryption configuration requires that the entire schema
68+
for encrypted fields is known at the time of client connection.
69+
70+
Schema Map
71+
~~~~~~~~~~
72+
73+
In addition to the
74+
:ref:`settings described in the how-to guide <server-side-queryable-encryption-settings>`,
75+
you will need to provide a ``schema_map`` to the ``AutoEncryptionOpts``.
76+
77+
Fortunately, this is easy to do with Django MongoDB Backend. You can use
78+
the ``showschemamap`` management command to generate the schema map
79+
for your encrypted fields, and then use the results in your settings.
80+
81+
To generate the schema map, run the following command in your Django project:
82+
::
83+
84+
python manage.py showschemamap
85+
86+
.. note:: The ``showschemamap`` command is only available if you have the
87+
``django_mongodb_backend`` app included in the :setting:`INSTALLED_APPS`
88+
setting.
89+
90+
Settings
91+
~~~~~~~~
92+
93+
Now include the generated schema map in your Django settings.
94+
95+
::
96+
97+
98+
DATABASES["encrypted"] = {
99+
100+
"OPTIONS": {
101+
"auto_encryption_opts": AutoEncryptionOpts(
102+
103+
schema_map= {
104+
"encryption__patientrecord": {
105+
"fields": [
106+
{
107+
"bsonType": "string",
108+
"path": "ssn",
109+
"queries": {"queryType": "equality"},
110+
"keyId": Binary(b"\x14F\x89\xde\x8d\x04K7\xa9\x9a\xaf_\xca\x8a\xfb&", 4),
111+
},
112+
}
113+
},
114+
# Add other models with encrypted fields here
115+
},
116+
),
117+
118+
},
119+
120+
}
121+
122+
You are now ready to use client side :doc:`Queryable Encryption </topics/queryable-encryption>` in your Django project.

docs/source/howto/queryable-encryption.rst

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,73 @@ Configuring Queryable Encryption
44

55
Configuring Queryable Encryption in Django is similar to
66
`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>`.
911

1012
Prerequisites
1113
-------------
1214

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-
1815
In addition to :doc:`installing </intro/install>` and
1916
:doc:`configuring </intro/configure>` Django MongoDB Backend,
2017
you will need to install PyMongo with Queryable Encryption support::
2118

2219
pip install django-mongodb-backend[encryption]
2320

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+
2428
Settings
2529
--------
2630

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.
3234

3335
::
3436

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
3739

3840
DATABASES = {
3941
"default": parse_uri(
40-
MONGODB_URI,
42+
DATABASE_URL,
4143
db_name="my_database",
4244
),
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.

docs/source/ref/models/fields.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,9 @@ they encrypt the data before storing it in the database.
419419

420420
.. _encrypted-fields-unsupported-fields:
421421

422-
Fields unsupported with Queryable Encryption
423-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422+
.. admonition:: Unsupported fields
424423

425-
The following fields are supported by Django MongoDB Backend but not by Queryable Encryption.
424+
The following fields are supported by Django MongoDB Backend but are not supported by
425+
Queryable Encryption.
426426

427-
- :class:`~django.db.models.DurationField`
428-
- :class:`~django.db.models.SlugField`
427+
:class:`~django.db.models.SlugField`

docs/source/ref/models/models.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Model reference
33

44
.. module:: django_mongodb_backend.models
55

6-
Two MongoDB-specific models are available in ``django_mongodb_backend.models``.
6+
One MongoDB-specific model is available in ``django_mongodb_backend.models``.
77

88
.. class:: EmbeddedModel
99

docs/source/topics/known-issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ backend rather than Django's built-in database cache backend,
106106
Queryable Encryption
107107
====================
108108

109-
TODO: Add Django core limitations that affect Queryable Encryption.
109+
.. TODO: Add Django core limitations that affect Queryable Encryption.

docs/source/topics/queryable-encryption.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ the patient data looks like this:
5050
ssn: Binary.createFromBase64('DkrbD67ejkt2u…', 6),
5151
}
5252
53+
.. admonition:: List of encrypted fields
54+
55+
See the full list of :ref:`encrypted fields <encrypted-fields>` in the :doc:`Model field reference </ref/models/fields>`.
56+
5357
Querying encrypted fields
5458
-------------------------
5559

tests/encryption_/tests.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ def test_patientrecord(self):
345345
# get_new_connection will return the encrypted connection
346346
# from the connection pool.
347347
with pymongo.MongoClient(**conn_params) as new_connection:
348-
patientrecords = new_connection[
349-
"test_django_encrypted"
350-
].encryption__patientrecord.find()
348+
patientrecords = new_connection["test_encrypted"].encryption__patientrecord.find()
351349
ssn = patientrecords[0]["ssn"]
352350
self.assertTrue(isinstance(ssn, Binary))
353351

0 commit comments

Comments
 (0)