Skip to content

INTPYTHON-527 Add Queryable Encryption support #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 156 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 123 commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
bc52c8e
INTPYTHON-527 Add Queryable Encryption support
aclark4life Jun 25, 2025
38fb110
Fix test for unencrypted field not in field map
aclark4life Jun 27, 2025
65bd15a
Fix test for unencrypted field not in field map
aclark4life Jun 27, 2025
e08945b
Add comment about suppressing EncryptedCollectionError
aclark4life Jun 27, 2025
7b34b44
Don't rely on features to fall back to unencrypted
aclark4life Jun 27, 2025
8e83ada
Remove _nodb_cursor and disable version check
aclark4life Jun 28, 2025
4da895c
Don't surpress encrypted error
aclark4life Jun 28, 2025
ed54a9b
Rename get_encrypted_client -> get_client_encryption
aclark4life Jun 28, 2025
8a7766c
Add encryption router
aclark4life Jun 30, 2025
eab2f2e
Add "encryption" database to encryption tests
aclark4life Jun 30, 2025
10a361e
Move encrypted_fields_map to schema (1/2)
aclark4life Jul 1, 2025
01d5485
Move encrypted_fields_map to schema (2/x)
aclark4life Jul 1, 2025
db32487
Refactor helpers
aclark4life Jul 2, 2025
b2be223
Restore get_database_version functionality
aclark4life Jul 2, 2025
27d4b8e
Move encrypted router to tests
aclark4life Jul 2, 2025
c4d1c66
Fix router tests
aclark4life Jul 2, 2025
2772aff
Test feature `supports_queryable_encryption`
aclark4life Jul 2, 2025
d2ddf4e
Add path and bsonType to _get_encrypted_fields_map
aclark4life Jul 2, 2025
e25357e
Use the right database; rename some vars
aclark4life Jul 2, 2025
6487086
Refactor helpers again
aclark4life Jul 2, 2025
bc76db3
Allow user to customize some QE settings.
aclark4life Jul 2, 2025
4dbaa8f
Allow uer to customize KMS provider.
aclark4life Jul 2, 2025
9cc5ad2
Refactor
aclark4life Jul 2, 2025
c751b2d
Alpha sort helper functions
aclark4life Jul 2, 2025
b13a07f
Fix get_database_version
aclark4life Jul 3, 2025
534da6b
A better fix for using `buildInfo` command.
aclark4life Jul 3, 2025
13578ab
Add `queries` key to encrypted fields map
aclark4life Jul 4, 2025
3342d7f
Update django_mongodb_backend/schema.py
aclark4life Jul 7, 2025
9fd21e4
Update django_mongodb_backend/schema.py
aclark4life Jul 7, 2025
9bbe741
Update tests/encryption_/models.py
aclark4life Jul 7, 2025
d1eb737
Update tests/encryption_/models.py
aclark4life Jul 7, 2025
176f016
Fix conditional
aclark4life Jul 7, 2025
264b37a
Use column instead of name
aclark4life Jul 7, 2025
1771f56
Avoid double conditional
aclark4life Jul 7, 2025
819058a
Update tests and remove test router
aclark4life Jul 7, 2025
9a3c18e
Update django_mongodb_backend/fields/encryption.py
aclark4life Jul 7, 2025
071192e
Add deconstruct method for encryption fields
aclark4life Jul 7, 2025
b2a0534
Add setup & teardown for QE features test
aclark4life Jul 7, 2025
81cc887
Add query type classes and update test
aclark4life Jul 8, 2025
be3dd16
Add missing queries to deconstruct
aclark4life Jul 8, 2025
a2342e2
Add get_encrypted_fields_map management command
aclark4life Jul 8, 2025
05a7610
Add EncryptedRouter
aclark4life Jul 8, 2025
96b3fda
Optimistically add QE to release notes :-)
aclark4life Jul 8, 2025
1eb71d5
Fix label
aclark4life Jul 8, 2025
08209d3
Save encrypted models to encrypted db
aclark4life Jul 9, 2025
90fe562
Refactor and rename QueryTypes -> QueryType
aclark4life Jul 9, 2025
8c2b84c
Refactor, reword, alpha sort, add comments.
aclark4life Jul 9, 2025
ab680fd
Alpha-sort
aclark4life Jul 9, 2025
4a267f5
Document-driven design
aclark4life Jul 9, 2025
3fdc1f7
Document-driven design
aclark4life Jul 9, 2025
d562a76
Document-driven design
aclark4life Jul 9, 2025
163758d
Add encryption.rst
aclark4life Jul 9, 2025
b95c343
Make key_vault_namespace a required kwarg
aclark4life Jul 9, 2025
5205a0b
Reuse schema editor to create encrypted fields map
aclark4life Jul 9, 2025
b07c3e6
Add --database to get_encrypted_fields_map command
aclark4life Jul 9, 2025
e557632
Add WIP configuration docs
aclark4life Jul 9, 2025
c5f8888
Add check for mongodb 7.0
aclark4life Jul 9, 2025
a7bc5c5
Let's go with "Queryable Encryption" everywhere.
aclark4life Jul 9, 2025
09423bc
Update django_mongodb_backend/fields/encryption.py
aclark4life Jul 9, 2025
c756cf8
Update tests/encryption_/tests.py
aclark4life Jul 9, 2025
841797c
Update tests/encryption_/tests.py
aclark4life Jul 9, 2025
2386397
Remove gratuitous use of with and append
aclark4life Jul 10, 2025
d685d2a
Always use `assertRaisesMessage` for > precision
aclark4life Jul 10, 2025
08ea317
only include migratable models for given database
aclark4life Jul 10, 2025
3e839d7
Refactor QueryType, add encryptino_ migration
aclark4life Jul 10, 2025
75c6936
Refactor tests and fix schema test
aclark4life Jul 10, 2025
534452f
Remove migration, already tested by schema
aclark4life Jul 10, 2025
bf26a8a
Router & schema updates
aclark4life Jul 10, 2025
bf078ad
Re-add test routers
aclark4life Jul 10, 2025
2780e32
Fix test router
aclark4life Jul 10, 2025
31d3feb
Remove ENCRYPTED_DB_ALIAS, ENCRYPTED_APPS
aclark4life Jul 10, 2025
b005726
Get rid of more settings
aclark4life Jul 10, 2025
e7290e4
Remove router allow_relation
aclark4life Jul 10, 2025
76deec0
Use class method
aclark4life Jul 10, 2025
02ce21e
Remove ENCRYPTED_DB_ALIAS
aclark4life Jul 10, 2025
c8a5118
Rename Person to Patient to match tutorial
aclark4life Jul 11, 2025
39f1cbc
queries only takes a single object
timgraham Jul 11, 2025
e504fc5
Move kms_provder to monkeypatch'd ConnectionRouter
aclark4life Jul 11, 2025
0aa423f
Check settings for KMS_PROVIDER & add test.
aclark4life Jul 11, 2025
c27be37
Remove get_key_vault_namespace
aclark4life Jul 12, 2025
13de3bb
Remove get_kms_providers, get_customer_master_key
aclark4life Jul 12, 2025
7e3cd34
Update QE config docs
aclark4life Jul 12, 2025
4a9daa7
Add remaining KMS providers
aclark4life Jul 12, 2025
516642f
Look out for more credentials!
aclark4life Jul 12, 2025
a319e8e
Move encrypted db name back to router
aclark4life Jul 12, 2025
5807033
Remove comments
aclark4life Jul 12, 2025
37e7e06
Remove comments
aclark4life Jul 12, 2025
f19c901
Update comment
aclark4life Jul 12, 2025
528d503
I don't like `conn` either!
aclark4life Jul 14, 2025
c7c091b
Use correct verb style "Return" (per relevant PEP)
aclark4life Jul 14, 2025
b3a302b
Move connection router patching to routers.py
aclark4life Jul 14, 2025
acb0554
Update django_mongodb_backend/features.py
aclark4life Jul 14, 2025
67a640d
Update test models to match design doc
aclark4life Jul 14, 2025
9e76295
Refactor management command and fix test
aclark4life Jul 14, 2025
97196ed
Update Sphinx root_doc to index (the default value)
aclark4life Jul 10, 2025
1614919
Move kms_provider to user router
aclark4life Jul 14, 2025
a1bc5f3
Move kms_credentials to user router
aclark4life Jul 14, 2025
75c3cd1
Update docs
aclark4life Jul 14, 2025
a81d2ae
Move kms_credentials to settings
aclark4life Jul 15, 2025
e562718
Remove get_auto_encryption_opts
aclark4life Jul 15, 2025
3dca177
Remove get_client_encryption
aclark4life Jul 15, 2025
3432818
Define public helpers API
aclark4life Jul 15, 2025
cb7f153
Refactor test_auto_encryption_opts
aclark4life Jul 15, 2025
e0ef5b3
Assert the entire expected output via json.dumps
aclark4life Jul 15, 2025
ba4a6c8
Update docs
aclark4life Jul 15, 2025
1b9a714
Rename test methods
aclark4life Jul 15, 2025
3340ae7
Fix doc string
aclark4life Jul 15, 2025
c90406b
Rename tests -> test_schema & add charfield tests
aclark4life Jul 15, 2025
8a1f381
Add test_integerfield from django
aclark4life Jul 15, 2025
edb2fa6
Avoid reentrancy issue checking mongodb version
aclark4life Jul 15, 2025
0701160
Add encrypted Post model schema to expected schema
aclark4life Jul 15, 2025
08f7934
Re-add namespace to schema_map
aclark4life Jul 15, 2025
2c4d53b
Add a note about copying field tests from Django
aclark4life Jul 15, 2025
9919ce9
Add query type tests, remove django field tests
aclark4life Jul 15, 2025
45ea5b5
Restored documentation navigation
timgraham Jul 15, 2025
3e468e7
Test negative query type cases
aclark4life Jul 15, 2025
8869bc1
Refactor and start watching for bad schema maps
aclark4life Jul 16, 2025
8a05af8
Refactor and update helpers
aclark4life Jul 16, 2025
3353fd0
Update docs
aclark4life Jul 16, 2025
948d21c
Add billing model fields & tee command output
aclark4life Jul 16, 2025
aae8df9
Fix router
aclark4life Jul 16, 2025
9c7c82f
Add a fixme to test router for kms_provider
aclark4life Jul 16, 2025
cec0289
Use custom db table for tests
aclark4life Jul 16, 2025
43df16a
Code review fixes
aclark4life Jul 17, 2025
94ecbe1
Delete now-existing attribute in teardown
aclark4life Jul 17, 2025
041336e
Update django_mongodb_backend/schema.py
aclark4life Jul 17, 2025
e894fe1
Fix credentials for create_encrypted_collection
aclark4life Jul 17, 2025
a683a6c
One less import, use client.codec_options
aclark4life Jul 17, 2025
3c2bc97
Remove key vault helpers
aclark4life Jul 17, 2025
7f6971b
Add custom _router_func to require kms_provider
aclark4life Jul 17, 2025
14ad6a8
Fix import
aclark4life Jul 17, 2025
3fba90c
Remove comment
aclark4life Jul 17, 2025
b88b167
Add CI for QE
aclark4life Jul 17, 2025
48f26ea
Code review fixes
aclark4life Jul 18, 2025
68799fb
Code review fixes
aclark4life Jul 18, 2025
38332c4
Use TransactionTestCase, check db for data
aclark4life Jul 18, 2025
b303002
Check for decrypted content
aclark4life Jul 18, 2025
97c3f8d
WIP testing
aclark4life Jul 21, 2025
c1d38d5
Check encryption via unencrypted connection
aclark4life Jul 21, 2025
62df289
Add BigIntegerField and test billing model
aclark4life Jul 21, 2025
23abbbf
Add TextField and test notes field for equality
aclark4life Jul 21, 2025
61e5919
Add encrypted date field and birth_date field
aclark4life Jul 21, 2025
f6b2a17
Add registration_date field to test datetime
aclark4life Jul 21, 2025
e8389cb
Refactor tests
aclark4life Jul 21, 2025
98fdbe2
Add encrypted float field and test patient weight
aclark4life Jul 21, 2025
6645459
Add EncryptedDecimalField & account_balance field
aclark4life Jul 21, 2025
ea4599f
Add boolean field and test is_active
aclark4life Jul 21, 2025
d8192bc
Add docs
aclark4life Jul 22, 2025
ab82d7d
Fix evergreen config test label
aclark4life Jul 22, 2025
2974a4c
Remove schema validation todo comment
aclark4life Jul 22, 2025
938b55a
Add kms credentials and providers env var tests
aclark4life Jul 22, 2025
a2af6cc
Add and test binary field with profile_pic
aclark4life Jul 22, 2025
e06a96c
Move some patient fields to patient record
aclark4life Jul 22, 2025
fdfecf9
Code review fixes
aclark4life Jul 22, 2025
8bdb58c
Move KMS_CREDENTIALS to db settings
aclark4life Jul 22, 2025
0fea49c
Update docs
aclark4life Jul 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions django_mongodb_backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .indexes import register_indexes # noqa: E402
from .lookups import register_lookups # noqa: E402
from .query import register_nodes # noqa: E402
from .routers import register_routers # noqa: E402

__all__ = ["parse_uri"]

Expand All @@ -25,3 +26,4 @@
register_indexes()
register_lookups()
register_nodes()
register_routers()
5 changes: 4 additions & 1 deletion django_mongodb_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,7 @@ def validate_no_broken_transaction(self):

def get_database_version(self):
"""Return a tuple of the database's version."""
return tuple(self.connection.server_info()["versionArray"])
# Avoid PyMongo or require PyMongo>=4.14.0 which
# will contain a fix for the buildInfo command.
# https://jira.mongodb.org/browse/PYTHON-5429
return tuple(self.connection.admin.command("buildInfo")["versionArray"])
112 changes: 112 additions & 0 deletions django_mongodb_backend/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Queryable Encryption helper functions and constants for MongoDB
#
# These helper functions and constants are optional and Queryable
# Encryption can be used in Django without them. They are provided
# to make it easier configure Queryable Encryption in Django.

import base64
import os

KEY_VAULT_COLLECTION_NAME = "__keyVault"
KEY_VAULT_DATABASE_NAME = "keyvault"
KEY_VAULT_NAMESPACE = f"{KEY_VAULT_DATABASE_NAME}.{KEY_VAULT_COLLECTION_NAME}"
KMS_CREDENTIALS = {
"aws": {
"key": os.getenv("AWS_KEY_ARN", ""),
"region": os.getenv("AWS_KEY_REGION", ""),
},
"azure": {
"keyName": os.getenv("AZURE_KEY_NAME", ""),
"keyVaultEndpoint": os.getenv("AZURE_KEY_VAULT_ENDPOINT", ""),
},
"gcp": {
"projectId": os.getenv("GCP_PROJECT_ID", ""),
"location": os.getenv("GCP_LOCATION", ""),
"keyRing": os.getenv("GCP_KEY_RING", ""),
"keyName": os.getenv("GCP_KEY_NAME", ""),
},
"kmip": {},
"local": {},
}
Comment on lines +10 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some documentation we can link to help users know how to configure credentials, providers, etc? It doesn't feel like Django's job to document and maintain this sort of mapping.

I also read:

To enable the driver’s behavior to obtain credentials from the environment, add the appropriate key (“aws”, “gcp”, or “azure”) with an empty map to “kms_providers” in either AutoEncryptionOpts or ClientEncryption options.

so this won't work for that use case (I think).

I'd suggest trying to minimize the amount of "helpers" in this PR. We can always add things later if there are user pain points, but I feel these thing shouldn't be our focus for v1. Really, we should enhance MongoDB/pymongo docs if it's unclear how to construct the providers dictionary. I don't think a solution of "set these environment variables instead" is making things simpler.

Copy link
Collaborator Author

@aclark4life aclark4life Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some documentation we can link to help users know how to configure credentials, providers, etc? It doesn't feel like Django's job to document and maintain this sort of mapping.

It's definitely not Django's job but it may be Django MongoDB Backend's job since we are trying to support QE and we may need that mapping or something like it in the schema.

I also read:

To enable the driver’s behavior to obtain credentials from the environment, add the appropriate key (“aws”, “gcp”, or “azure”) with an empty map to “kms_providers” in either AutoEncryptionOpts or ClientEncryption options.

so this won't work for that use case (I think).

Good catch! Let me test some vendors with what I have now and if we can rely on PyMongo for some of this even better.

I'd suggest trying to minimize the amount of "helpers" in this PR. We can always add things later if there are user pain points, but I feel these thing shouldn't be our focus for v1. Really, we should enhance MongoDB/pymongo docs if it's unclear how to construct the providers dictionary. I don't think a solution of "set these environment variables instead" is making things simpler.

Agreed. I definitely don't want to be in the env var business but I do want to be in the "make this feature work with minimal effort" business.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think KMS_CREDENTIALS goes in DATABASES also. Referencing encryption.KMS_CREDENTIALS in schema editor doesn't look good to me for several reasons. It's "global state" as we discussed with kms providers, where it eliminates the possibility to use different provider credentials for different database aliases.

Also, the use of environment couples the list of providers ("aws", "azure", etc.) as well as each providers options ("accessKeyId", "secretAccessKey", etc.) to this package's release cycle. I remain unconvinced that the environment variables solution is useful and a step toward "making this feature work with minimal effort." How are environment variables less effort than something like this:

DATABASES = {
    "alias": {
        ....
        "KMS_CREDENTIALS": {
            "aws": {
                "accessKeyId": ...,
                "secretAccessKey": ...,
            },
        },
    },
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a helper like EncryptedRouter or QueryType so folks can do either:

DATABASES = {
    "alias": {
        ....
        "KMS_CREDENTIALS": {
            "aws": {
                "accessKeyId": ...,
                "secretAccessKey": ...,
            },
        },
    },
}

or

DATABASES = {
    "alias": {
        ....
        "KMS_CREDENTIALS": encryption.KMS_CREDENTIALS
        },
    },
}

The helper just happens to use env vars.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free try to gather some consensus on the design doc, but I've tried to lay out the reasons I believe encryption.KMS_CREDENTIALS adds an unnecessary layer of opaque, hardcoded inflexibility. All of these helpers are extra things to test and document for not much, if any, gain, in my opinion.

Jib agreed that the query type helper could be added later, if it's seen as a real pain point. (Incidentally, the current design differs from what I proposed.)

I feel you have some strong opinion that "dictionaries are ugly for configuration" or that using dictionaries is equated with "manual configuration of QE." For me, you've done a good job with the schema map part of this, and I think that's the main configuration thing to be automated, but feel free to get clarity from Jib about exactly what he meant by "manual configuration."

KMS_PROVIDERS = {
"aws": {
"accessKeyId": os.getenv("AWS_ACCESS_KEY_ID", "not an access key"),
"secretAccessKey": os.getenv("AWS_SECRET_ACCESS_KEY", "not a secret key"),
},
"azure": {
"tenantId": os.getenv("AZURE_TENANT_ID", "not a tenant ID"),
"clientId": os.getenv("AZURE_CLIENT_ID", "not a client ID"),
"clientSecret": os.getenv("AZURE_CLIENT_SECRET", "not a client secret"),
},
"gcp": {
"email": os.getenv("GCP_EMAIL", "not an email"),
"privateKey": os.getenv(
"GCP_PRIVATE_KEY",
base64.b64encode(b"not a private key").decode("ascii"),
),
},
"kmip": {
"endpoint": os.getenv("KMIP_KMS_ENDPOINT", "not a valid endpoint"),
},
"local": {
"key": bytes.fromhex(
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
)
},
}


class EncryptedRouter:
"""A sample database router for Django that routes encrypted
models to an encrypted database with a local KMS provider.
"""

def allow_migrate(self, db, app_label, model_name=None, model=None, **hints):
if model:
return db == ("encrypted" if getattr(model, "encrypted", False) else "default")
return db == "default"

def db_for_read(self, model, **hints):
if getattr(model, "encrypted", False):
return "encrypted"
return "default"

db_for_write = db_for_read

def kms_provider(self, model):
return "local"


class QueryType:
"""
Class that supports building encrypted equality and range queries
for MongoDB's Queryable Encryption.
"""

@classmethod
def equality(cls, *, contention=None):
query = {"queryType": "equality"}
if contention is not None:
query["contention"] = contention
return query

@classmethod
def range(
cls, *, contention=None, max=None, min=None, precision=None, sparsity=None, trimFactor=None
):
query = {"queryType": "range"}
options = {
"contention": contention,
"max": max,
"min": min,
"precision": precision,
"sparsity": sparsity,
"trimFactor": trimFactor,
}
query.update({k: v for k, v in options.items() if v is not None})
return query
26 changes: 25 additions & 1 deletion django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,17 @@ def django_test_expected_failures(self):
},
}

@cached_property
def mongodb_version(self):
return self.connection.get_database_version() # e.g., (6, 3, 0)

@cached_property
def is_mongodb_6_3(self):
return self.connection.get_database_version() >= (6, 3)
return self.mongodb_version >= (6, 3)

@cached_property
def is_mongodb_7_0(self):
return self.mongodb_version >= (7, 0)

@cached_property
def supports_atlas_search(self):
Expand Down Expand Up @@ -624,3 +632,19 @@ def supports_transactions(self):
hello = client.command("hello")
# a replica set or a sharded cluster
return "setName" in hello or hello.get("msg") == "isdbgrid"

@cached_property
def supports_queryable_encryption(self):
"""
Queryable Encryption is supported if the server is Atlas or Enterprise
and is configured as a replica set or sharded cluster.
"""
self.connection.ensure_connection()
client = self.connection.connection.admin
build_info = client.command("buildInfo")
is_enterprise = "enterprise" in build_info.get("modules")
# `supports_transactions` already checks if the server is a
# replica set or sharded cluster.
is_not_single = self.supports_transactions
# TODO: check if the server is Atlas
return is_enterprise and is_not_single and self.is_mongodb_7_0
3 changes: 3 additions & 0 deletions django_mongodb_backend/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .duration import register_duration_field
from .embedded_model import EmbeddedModelField
from .embedded_model_array import EmbeddedModelArrayField
from .encryption import EncryptedCharField, EncryptedIntegerField
from .json import register_json_field
from .objectid import ObjectIdField

Expand All @@ -11,6 +12,8 @@
"ArrayField",
"EmbeddedModelArrayField",
"EmbeddedModelField",
"EncryptedCharField",
"EncryptedIntegerField",
"ObjectIdAutoField",
"ObjectIdField",
]
Expand Down
31 changes: 31 additions & 0 deletions django_mongodb_backend/fields/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.db import models


class EncryptedFieldMixin(models.Field):
encrypted = True

def __init__(self, *args, queries=None, **kwargs):
self.queries = queries
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()

if self.queries is not None:
kwargs["queries"] = self.queries

if path.startswith("django_mongodb_backend.fields.encryption"):
path = path.replace(
"django_mongodb_backend.fields.encryption",
"django_mongodb_backend.fields",
)

return name, path, args, kwargs


class EncryptedCharField(EncryptedFieldMixin, models.CharField):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find in the docs if an encrypted collection could have an aggregate query. So my question is:
does it support all the lookups from CharField ?

Copy link
Collaborator Author

@aclark4life aclark4life Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No aggregation stages are supported and two tests from Django's test_charfield are failing, though only one is an aggregation stage failure:

======================================================================
ERROR: test_assignment_from_choice_enum (encryption_.test_charfield.TestEncryptedCharField.test_assignment_from_choice_enum)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 124, in _wrap_encryption_errors
    yield
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 466, in encrypt
    encrypted_cmd = self._auto_encrypter.encrypt(database, encoded_cmd)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pymongocrypt/synchronous/auto_encrypter.py", line 44, in encrypt
    return run_state_machine(ctx, self.callback)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pymongocrypt/synchronous/state_machine.py", line 136, in run_state_machine
    result = callback.mark_command(ctx.database, mongocryptd_cmd)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 286, in mark_command
    res = self.mongocryptd_client[database].command(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/_csot.py", line 125, in csot_wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/database.py", line 930, in command
    return self._command(
           ^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/database.py", line 770, in _command
    return conn.command(
           ^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/helpers.py", line 47, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/pool.py", line 414, in command
    return command(
           ^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/network.py", line 212, in command
    helpers_shared._check_command_response(
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/helpers_shared.py", line 250, in _check_command_response
    raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Comparison disallowed between fields where one is randomly encrypted; field 'title' is randomly encrypted., full error: RawBSONDocument(b"\xae\x00\x00\x00\x01ok\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02errmsg\x00k\x00\x00\x00Comparison disallowed between fields where one is randomly encrypted; field 'title' is randomly encrypted.\x00\x10code\x00\xb6y\x00\x00\x02codeName\x00\x0e\x00\x00\x00Location31158\x00\x00", codec_options=CodecOptions(document_class=<class 'bson.raw_bson.RawBSONDocument'>, tz_aware=False, uuid_representation=UuidRepresentation.UNSPECIFIED, unicode_decode_error_handler='strict', tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None), datetime_conversion=DatetimeConversion.DATETIME))

ERROR: test_lookup_integer_in_charfield (encryption_.test_charfield.TestEncryptedCharField.test_lookup_integer_in_charfield)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 124, in _wrap_encryption_errors
    yield
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 466, in encrypt
    encrypted_cmd = self._auto_encrypter.encrypt(database, encoded_cmd)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pymongocrypt/synchronous/auto_encrypter.py", line 44, in encrypt
    return run_state_machine(ctx, self.callback)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pymongocrypt/synchronous/state_machine.py", line 136, in run_state_machine
    result = callback.mark_command(ctx.database, mongocryptd_cmd)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/encryption.py", line 286, in mark_command
    res = self.mongocryptd_client[database].command(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/_csot.py", line 125, in csot_wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/database.py", line 930, in command
    return self._command(
           ^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/database.py", line 770, in _command
    return conn.command(
           ^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/helpers.py", line 47, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/pool.py", line 414, in command
    return command(
           ^^^^^^^^
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/synchronous/network.py", line 212, in command
    helpers_shared._check_command_response(
  File "/Users/alex.clark/Developer/django-mongodb-cli/src/mongo-python-driver/pymongo/helpers_shared.py", line 250, in _check_command_response
    raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Aggregation stage $internalFacetTeeConsumer is not allowed or supported with automatic encryption., full error: RawBSONDocument(b'\xa6\x00\x00\x00\x01ok\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02errmsg\x00c\x00\x00\x00Aggregation stage $internalFacetTeeConsumer is not allowed or supported with automatic encryption.\x00\x10code\x00#y\x00\x00\x02codeName\x00\x0e\x00\x00\x00Location31011\x00\x00', codec_options=CodecOptions(document_class=<class 'bson.raw_bson.RawBSONDocument'>, tz_aware=False, uuid_representation=UuidRepresentation.UNSPECIFIED, unicode_decode_error_handler='strict', tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None), datetime_conversion=DatetimeConversion.DATETIME))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, Are lookups needed? We could try to add something. But it won't be so easy given we cannot use aggregate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know! Probably not for v1. Equality and range queries seem to be working in the tests next up is to declare "done" for char and integer and move on to the rest of the fields. Most of the examples I've seen are for char and integer so we may need to experiment to see which additional fields will work without effort, which will work with some effort, and which will not work at all.

pass


class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json

from django.apps import apps
from django.core.management.base import BaseCommand
from django.db import DEFAULT_DB_ALIAS, connections, router


class Command(BaseCommand):
help = "Generate a `schema_map` of encrypted fields for all encrypted"
" models in the database for use with `AutoEncryptionOpts` in"
" production environments."

def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
help="Specify the database to use for generating the encrypted"
"fields map. Defaults to the 'default' database.",
)

def handle(self, *args, **options):
db = options["database"]
connection = connections[db]
schema_map = self.get_encrypted_fields_map(connection)
self.stdout.write(json.dumps(schema_map, indent=2))

def get_encrypted_fields_map(self, connection):
schema_map = {}
for app_config in apps.get_app_configs():
for model in router.get_migratable_models(
app_config, connection.alias, include_auto_created=False
):
if getattr(model, "encrypted", False):
fields = connection.schema_editor()._get_encrypted_fields_map(model)
schema_map[model._meta.db_table] = fields
return schema_map
8 changes: 8 additions & 0 deletions django_mongodb_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ def delete(self, *args, **kwargs):

def save(self, *args, **kwargs):
raise NotSupportedError("EmbeddedModels cannot be saved.")


class EncryptedModel(models.Model):
encrypted = True

class Meta:
abstract = True
required_db_features = {"supports_queryable_encryption"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think required_db_features is appropriate for EncryptedModel since that will silently cause encrypted models not to be created in user projects.

16 changes: 14 additions & 2 deletions django_mongodb_backend/routers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.apps import apps

from django_mongodb_backend.models import EmbeddedModel
from django.db.utils import ConnectionRouter


class MongoRouter:
Expand All @@ -9,10 +8,23 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
EmbeddedModels don't have their own collection and must be ignored by
dumpdata.
"""

if not model_name:
return None
try:
model = apps.get_model(app_label, model_name)
except LookupError:
return None

# Delay import for `register_routers` patching.
from django_mongodb_backend.models import EmbeddedModel

return False if issubclass(model, EmbeddedModel) else None


def register_routers():
"""
Patch the ConnectionRouter with methods to get KMS credentials and provider
from the SchemaEditor.
"""
ConnectionRouter.kms_provider = ConnectionRouter._router_func("kms_provider")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I think you'll have to write a custom function similar to _router_func instead of using it, since it falls back to returning DEFAULT_DB_ALIAS (which is the string "default") and that's not a useful behavior for kms_provider.

66 changes: 63 additions & 3 deletions django_mongodb_backend/schema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf import settings
from django.db import router
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models import Index, UniqueConstraint
from pymongo.encryption import ClientEncryption, CodecOptions
from pymongo.operations import SearchIndexModel

from django_mongodb_backend.indexes import SearchIndex

from .fields import EmbeddedModelField
from .indexes import SearchIndex
from .query import wrap_database_errors
from .utils import OperationCollector

Expand Down Expand Up @@ -41,7 +43,7 @@ def get_database(self):
@wrap_database_errors
@ignore_embedded_models
def create_model(self, model):
self.get_database().create_collection(model._meta.db_table)
self._create_collection(model)
self._create_model_indexes(model)
# Make implicit M2M tables.
for field in model._meta.local_many_to_many:
Expand Down Expand Up @@ -418,3 +420,61 @@ def _field_should_have_unique(self, field):
db_type = field.db_type(self.connection)
# The _id column is automatically unique.
return db_type and field.unique and field.column != "_id"

def _create_collection(self, model):
"""
If the model is encrypted create an encrypted collection with the
encrypted fields map else create a normal collection.
"""

def _create_collection(self, model):
"""
If the model is encrypted, create an encrypted collection with the
encrypted fields map; else, create a normal collection.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inadvertently repeated definition

db = self.get_database()
if getattr(model, "encrypted", False):
client = self.connection.connection
options = client._options.auto_encryption_opts
key_vault_namespace = options._key_vault_namespace
kms_providers = options._kms_providers
codec_options = CodecOptions()

ce = ClientEncryption(kms_providers, key_vault_namespace, client, codec_options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some example code that uses:

codec_options=client.codec_options

which might be more appropriate (though you wonder why codec_options and is a required argument if options can be retrieved from the also passed client).


# TODO: Validate schema! `create_encrypted_collection` appears to
# succeed no matter what you give it, as long as it's valid JSON.
# E.g. encrypted_fields_map = []
encrypted_fields_map = self._get_encrypted_fields_map(model)
provider = router.kms_provider(model)

# TODO: Remove ternary condition when `master_key` option is not
# inadvertently set to "default" somewhere, which then causes the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the database router issue I pointed out in another comment. However, perhaps the ternary is a useful condition to keep since think the user shouldn't have to define KMS_CREDENTIALS if they're using local, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it may have just been that I forgot to index the dict. It's working now with local and you are correct, they do not need to be set for local, but they do need to be set for other providers, hence the effort to set them for all providers in create_encrypted_collection.

# `master_key.copy` in libmongocrypt to fail.
credentials = settings.DATABASES[db].KMS_CREDENTIALS if provider != "local" else None

ce.create_encrypted_collection(
db,
model._meta.db_table,
encrypted_fields_map,
provider,
credentials,
)
else:
db.create_collection(model._meta.db_table)

def _get_encrypted_fields_map(self, model):
connection = self.connection
fields = model._meta.fields

return {
"fields": [
{
"bsonType": field.db_type(connection),
"path": field.column,
**({"queries": field.queries} if getattr(field, "queries", None) else {}),
}
for field in fields
if getattr(field, "encrypted", False)
]
}
2 changes: 0 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
"manual": ("https://www.mongodb.com/docs/manual/", None),
}

root_doc = "contents"

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

Expand Down
Loading