Skip to content

Commit 4fe6a35

Browse files
committed
Code review fixes (6/6)
1 parent 621c44e commit 4fe6a35

File tree

9 files changed

+136
-21
lines changed

9 files changed

+136
-21
lines changed

django_mongodb_backend/fields/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .embedded_model import EmbeddedModelField
55
from .embedded_model_array import EmbeddedModelArrayField
66
from .encryption import (
7+
EncryptedBigIntegerField,
78
EncryptedBinaryField,
89
EncryptedBooleanField,
910
EncryptedCharField,
@@ -16,6 +17,7 @@
1617
EncryptedFloatField,
1718
EncryptedGenericIPAddressField,
1819
EncryptedIntegerField,
20+
EncryptedPositiveBigIntegerField,
1921
EncryptedPositiveIntegerField,
2022
EncryptedPositiveSmallIntegerField,
2123
EncryptedSmallIntegerField,
@@ -32,6 +34,7 @@
3234
"ArrayField",
3335
"EmbeddedModelArrayField",
3436
"EmbeddedModelField",
37+
"EncryptedBigIntegerField",
3538
"EncryptedBinaryField",
3639
"EncryptedBooleanField",
3740
"EncryptedCharField",
@@ -44,6 +47,7 @@
4447
"EncryptedFloatField",
4548
"EncryptedGenericIPAddressField",
4649
"EncryptedIntegerField",
50+
"EncryptedPositiveBigIntegerField",
4751
"EncryptedPositiveIntegerField",
4852
"EncryptedPositiveSmallIntegerField",
4953
"EncryptedSmallIntegerField",

django_mongodb_backend/fields/encryption.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class EncryptedBinaryField(EncryptedFieldMixin, models.BinaryField):
2727
pass
2828

2929

30+
class EncryptedBigIntegerField(EncryptedFieldMixin, models.BigIntegerField):
31+
pass
32+
33+
3034
class EncryptedBooleanField(EncryptedFieldMixin, models.BooleanField):
3135
pass
3236

@@ -67,6 +71,10 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
6771
pass
6872

6973

74+
class EncryptedPositiveBigIntegerField(EncryptedFieldMixin, models.PositiveBigIntegerField):
75+
pass
76+
77+
7078
class EncryptedPositiveIntegerField(EncryptedFieldMixin, models.PositiveIntegerField):
7179
pass
7280

django_mongodb_backend/management/commands/showencryptedfieldsmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def add_arguments(self, parser):
2222
Specifies the database to use. Defaults to ``default``.""",
2323
)
2424
parser.add_argument(
25-
"--create-new-keys",
25+
"--create-data-keys",
2626
action="store_true",
2727
help="""
2828
If specified, this option will create and show new encryption

docs/source/howto/queryable-encryption.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ Configuring Queryable Encryption
44

55
.. versionadded:: 5.2.0rc1
66

7-
This guide is similar to the
8-
:doc:`manual:core/queryable-encryption/quick-start` but with some additional
9-
steps required to configure Queryable Encryption with Django MongoDB Backend.
7+
Queryable Encryption is a powerful MongoDB feature that enables you to encrypt
8+
sensitive fields in your database while still allowing queries on that
9+
encrypted data.
10+
11+
This section will guide you through the process of configuring Queryable
12+
Encryption in your Django project using the Django MongoDB Backend.
1013

1114
.. admonition:: MongoDB requirements
1215

@@ -239,5 +242,5 @@ settings as follows:
239242
),
240243
}
241244
242-
You are now ready to :doc:`develop with Queryable Encryption
243-
</topics/queryable-encryption>` in Django MongoDB Backend!
245+
You are now ready to :doc:`start developing applications
246+
</topics/queryable-encryption>` with Queryable Encryption!

docs/source/ref/django-admin.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Available commands
3030

3131
Specifies the database to use. Defaults to ``default``.
3232

33-
.. django-admin-option:: --create-new-keys
33+
.. django-admin-option:: --create-data-keys
3434

3535
If specified, this option will create and show new encryption keys
3636
instead of showing existing keys from the configured key vault.

docs/source/ref/models/encrypted-fields.rst

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Queryable Encryption.
2020
+========================================+======================================================+
2121
| ``EncryptedBooleanField`` | :class:`~django.db.models.BooleanField` |
2222
+----------------------------------------+------------------------------------------------------+
23+
| ``EncryptedBigIntegerField`` | :class:`~django.db.models.BigIntegerField` |
24+
+----------------------------------------+------------------------------------------------------+
2325
| ``EncryptedCharField`` | :class:`~django.db.models.CharField` |
2426
+----------------------------------------+------------------------------------------------------+
2527
| ``EncryptedDateField`` | :class:`~django.db.models.DateField` |
@@ -38,6 +40,8 @@ Queryable Encryption.
3840
+----------------------------------------+------------------------------------------------------+
3941
| ``EncryptedPositiveIntegerField`` | :class:`~django.db.models.PositiveIntegerField` |
4042
+----------------------------------------+------------------------------------------------------+
43+
| ``EncryptedPositiveBigIntegerField`` | :class:`~django.db.models.PositiveBigIntegerField` |
44+
+----------------------------------------+------------------------------------------------------+
4145
| ``EncryptedPositiveSmallIntegerField`` | :class:`~django.db.models.PositiveSmallIntegerField` |
4246
+----------------------------------------+------------------------------------------------------+
4347
| ``EncryptedSmallIntegerField`` | :class:`~django.db.models.SmallIntegerField` |
@@ -67,20 +71,10 @@ supported by Queryable Encryption.
6771
MongoDB's Queryable Encryption.
6872

6973
To create a custom encrypted field, inherit from ``EncryptedFieldMixin`` and
70-
the desired Django field class. You can then specify the ``queries`` option
71-
to define how the field can be queried.
72-
73-
The ``queries`` option should be a dictionary that specifies the type of
74-
queries that can be performed on the field. The :ref:`available query types
75-
<manual:qe-fundamentals-encrypt-query>` are as follows:
76-
77-
- ``equality``: Supports equality queries.
78-
- ``range``: Supports range queries.
74+
the desired Django field class.
7975

80-
You can configure an encrypted field for either equality or range queries,
81-
but not both. Configure fields for the expected query type.
8276

83-
For example, to create a custom encrypted field that supports equality
77+
For example, to create a custom encrypted field that supports ``equality``
8478
queries, you can define it as follows:
8579

8680
.. code-block:: python

docs/source/topics/queryable-encryption.rst

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,98 @@ Queryable Encryption
44

55
.. versionadded:: 5.2.0rc1
66

7-
Use :doc:`/ref/models/encrypted-fields` to structure your sensitive data.
7+
Once you have configured your Django project and MongoDB deployment for
8+
Queryable Encryption, you’re ready to start developing applications that take
9+
advantage of these enhanced security features.
10+
11+
Encrypted fields
12+
================
13+
14+
You can use :doc:`encrypted fields </ref/models/encrypted-fields>` to structure
15+
your sensitive data.
16+
17+
18+
The basics
19+
----------
20+
21+
For example, you can define a model with encrypted fields
22+
like this:
23+
24+
.. code-block:: python
25+
26+
from django.db import models
27+
from django_mongodb_backend.fields import EncryptedCharField
28+
29+
30+
class Patient(models.Model):
31+
name = models.CharField(max_length=255)
32+
ssn = models.EncryptedCharField(max_length=11)
33+
34+
def __str__(self):
35+
return self.name
36+
37+
Once you have defined your model, created migrations with ``python manage.py
38+
makemigrations`` and run migrations with ``python manage.py migrate``, you can
39+
create and manipulate instances of the data just like any other Django model
40+
data. The fields will automatically handle encryption and decryption, ensuring
41+
that sensitive data is stored securely in the database.
42+
43+
From an encrypted client, you can access the data::
44+
45+
from myapp.models import Patient
46+
47+
>>> bob = Patient.objects.create(name="Bob", ssn="123-45-6789")
48+
>>> bob.ssn
49+
'123-45-6789'
50+
51+
From an unencrypted client, you can still access the data, but the sensitive
52+
fields will be encrypted. For example, if you try to access the ``ssn`` field
53+
from an unencrypted client, you will see the encrypted value::
54+
55+
from myapp.models import Patient
56+
57+
>>> bob = Patient.objects.get(name="Bob")
58+
>>> bob.ssn
59+
Binary(b'\x0e\x97sv\xecY\x19Jp\x81\xf1\\\x9cz\t1\r\x02...', 6)
60+
61+
Querying encrypted fields
62+
-------------------------
63+
64+
In order to query encrypted fields, you must define the queryable encryption
65+
query type in the model field definition. For example, if you want to query the
66+
``ssn`` field for equality, you can define it as follows:
67+
68+
.. code-block:: python
69+
70+
from django.db import models
71+
from django_mongodb_backend.fields import EncryptedCharField
72+
73+
74+
class Patient(models.Model):
75+
name = models.CharField(max_length=255)
76+
ssn = models.EncryptedCharField(max_length=11, queries={"equality": True})
77+
78+
def __str__(self):
79+
return self.name
80+
81+
Query types
82+
~~~~~~~~~~~
83+
84+
The ``queries`` option should be a dictionary that specifies the type of queries
85+
that can be performed on the field. The :ref:`available query types
86+
<manual:qe-fundamentals-encrypt-query>` are as follows:
87+
88+
- ``equality``: Supports equality queries.
89+
- ``range``: Supports range queries.
90+
91+
You can configure an encrypted field for either equality or range queries, but
92+
not both.
93+
94+
Now you can perform queries on the ``ssn`` field using the defined query type.
95+
For example, to find a patient by their SSN, you can do the following::
96+
97+
from myapp.models import Patient
98+
99+
>>> patient = Patient.objects.get(ssn="123-45-6789")
100+
>>> patient.name
101+
'Bob'

tests/encryption_/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,20 @@ class Meta:
7676

7777

7878
class EncryptedNumbers(models.Model):
79+
# Not tested elsewhere
7980
pos_smallint = EncryptedPositiveSmallIntegerField(queries=EQUALITY_QUERY)
8081
smallint = EncryptedSmallIntegerField(queries=EQUALITY_QUERY)
8182

8283
class Meta:
8384
required_db_features = {"supports_queryable_encryption"}
85+
86+
87+
class SensitiveData(models.Model):
88+
# Example from documentation
89+
name = EncryptedCharField(max_length=100)
90+
email = EncryptedEmailField()
91+
phone_number = EncryptedCharField(max_length=15)
92+
93+
sensitive_text = EncryptedTextField()
94+
sensitive_integer = EncryptedIntegerField()
95+
sensitive_date = EncryptedDateField()

tests/encryption_/test_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_create_new_keys(self):
7272
"showencryptedfieldsmap",
7373
"--database",
7474
"encrypted",
75-
"--create-new-keys",
75+
"--create-data-keys",
7676
verbosity=0,
7777
stdout=out,
7878
)

0 commit comments

Comments
 (0)