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 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 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
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
126 changes: 126 additions & 0 deletions django_mongodb_backend/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Queryable Encryption helpers
#
# TODO: Decide if these helpers should even exist, and if so, find a permanent
# place for them.

from bson.binary import STANDARD
from bson.codec_options import CodecOptions
from pymongo.encryption import AutoEncryptionOpts, ClientEncryption

KEY_VAULT_DATABASE_NAME = "keyvault"
KEY_VAULT_COLLECTION_NAME = "__keyVault"
KMS_PROVIDER = "local" # e.g., "aws", "azure", "gcp", "kmip", or "local"


class EqualityQuery:
"""
Represents an encrypted equality query for encrypted fields in MongoDB's
Queryable Encryption.
"""

def __init__(self, contention=None):
self.queryType = "equality"
self.contention = contention

def to_dict(self):
query_type = {"queryType": self.queryType}
if self.contention is not None:
query_type["contention"] = self.contention
return [query_type]


class RangeQuery:
"""Represents an encrypted range query configuration for encrypted fields in
MongoDB's Queryable Encryption.
"""

def __init__(self, sparsity=None, precision=None, trimFactor=None):
self.queryType = "range"
self.sparsity = sparsity
self.precision = precision
self.trimFactor = trimFactor

def to_dict(self):
query_type = {"queryType": self.queryType}
if self.sparsity is not None:
query_type["sparsity"] = self.sparsity
if self.precision is not None:
query_type["precision"] = self.precision
if self.trimFactor is not None:
query_type["trimFactor"] = self.trimFactor
return query_type


class QueryTypes:
"""
Factory class for creating query type configurations for
MongoDB Queryable Encryption.
"""

def equality(self, *, contention=None):
return EqualityQuery(contention=contention)

def range(self, *, sparsity=None, precision=None, trimFactor=None):
return RangeQuery(sparsity=sparsity, precision=precision, trimFactor=trimFactor)


def get_auto_encryption_opts(
key_vault_namespace=None, crypt_shared_lib_path=None, kms_providers=None
):
"""
Returns an `AutoEncryptionOpts` instance for MongoDB Client-Side Field
Level Encryption (CSFLE) that can be used to create an encrypted connection.
"""
return AutoEncryptionOpts(
key_vault_namespace=key_vault_namespace,
kms_providers=kms_providers,
crypt_shared_lib_path=crypt_shared_lib_path,
)


def get_client_encryption(client, key_vault_namespace=None, kms_providers=None):
"""
Returns a `ClientEncryption` instance for MongoDB Client-Side Field Level
Encryption (CSFLE) that can be used to create an encrypted collection.
"""

codec_options = CodecOptions(uuid_representation=STANDARD)
return ClientEncryption(kms_providers, key_vault_namespace, client, codec_options)


def get_customer_master_key():
"""
Returns a 96-byte local master key for use with MongoDB Client-Side Field Level
Encryption (CSFLE). For local testing purposes only. In production, use a secure KMS
like AWS, Azure, GCP, or KMIP.
Returns:
bytes: A 96-byte key.
"""
# WARNING: This is a static key for testing only.
# Generate with: os.urandom(96)
return bytes.fromhex(
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
)


def get_key_vault_namespace(
key_vault_database_name=KEY_VAULT_DATABASE_NAME,
key_vault_collection_name=KEY_VAULT_COLLECTION_NAME,
):
return f"{key_vault_database_name}.{key_vault_collection_name}"


def get_kms_providers():
"""
Return supported KMS providers for MongoDB Client-Side Field Level Encryption (CSFLE).
"""
return {
"local": {
"key": get_customer_master_key(),
},
}
15 changes: 15 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,18 @@ 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
return is_enterprise and is_not_single
2 changes: 2 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
from .json import register_json_field
from .objectid import ObjectIdField

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


class EncryptedCharField(models.CharField):
encrypted = True
queries = []

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

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

# Add 'queries' to kwargs if it was set
if self.queries is not None:
kwargs["queries"] = self.queries

# Normalize path if needed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json

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


class Command(BaseCommand):
help = "Generate an encryptedFieldsMap for MongoDB automatic encryption"

def handle(self, *args, **options):
connection = connections[DEFAULT_DB_ALIAS]

schema_map = self.generate_encrypted_fields_schema_map(connection)

self.stdout.write(json.dumps(schema_map, indent=2))

def generate_encrypted_fields_schema_map(self, conn):
schema_map = {}

for model in apps.get_models():
encrypted_fields = self.get_encrypted_fields(model, conn)
if encrypted_fields:
collection = model._meta.db_table
schema_map[collection] = {"fields": encrypted_fields}

return schema_map

def get_encrypted_fields(self, model, conn):
fields = model._meta.fields
encrypted_fields = []

for field in fields:
if getattr(field, "encrypted", False):
field_map = {
"path": field.column,
"bsonType": field.db_type(conn),
}

if getattr(field, "queries", None):
field_map["queries"] = field.queries[0].to_dict()

encrypted_fields.append(field_map)

return encrypted_fields
7 changes: 7 additions & 0 deletions django_mongodb_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ 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
50 changes: 47 additions & 3 deletions django_mongodb_backend/schema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.conf import settings
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models import Index, UniqueConstraint
from pymongo.operations import SearchIndexModel

from django_mongodb_backend.indexes import SearchIndex

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

Expand Down Expand Up @@ -41,7 +42,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 +419,46 @@ 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 not encrypted, create a normal collection otherwise
create an encrypted collection with the encrypted fields map.
"""

db = self.get_database()
if getattr(model, "encrypted", False):
client = self.connection.connection
ce = get_client_encryption(
client,
key_vault_namespace=settings.KEY_VAULT_NAMESPACE,
kms_providers=settings.KMS_PROVIDERS,
)
ce.create_encrypted_collection(
db,
model._meta.db_table,
self._get_encrypted_fields_map(model),
settings.KMS_PROVIDER,
)
else:
db.create_collection(model._meta.db_table)

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

return {
"fields": [
{
"path": field.column,
"bsonType": field.db_type(conn),
**(
{"queries": field.queries[0].to_dict()}
if getattr(field, "queries", None)
else {}
),
}
for field in fields
if getattr(field, "encrypted", False)
]
}
22 changes: 22 additions & 0 deletions docs/source/topics/encrypted-models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Encrypted models
================

``EncryptedCharField``
----------------------

The basics
~~~~~~~~~~

Let's consider this example::

from django.db import models

from django_mongodb_backend.fields import EncryptedCharField
from django_mongodb_backend.models import EncryptedModel


class Person(EncryptedModel):
ssn = EncryptedCharField("ssn", max_length=11)

def __str__(self):
return self.ssn
1 change: 1 addition & 0 deletions docs/source/topics/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ know:

cache
embedded-models
encrypted-models
known-issues
18 changes: 18 additions & 0 deletions tests/backend_/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,21 @@ def mocked_command(command):

with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command):
self.assertIs(connection.features.supports_transactions, False)


class SupportsQueryableEncryptionTests(TestCase):
def setUp(self):
# Clear the cached property.
connection.features.__dict__.pop("supports_queryable_encryption", None)

def tearDown(self):
connection.features.__dict__.pop("supports_queryable_encryption", None)

def test_supports_queryable_encryption(self):
def mocked_command(command):
if command == "buildInfo":
return {"modules": ["enterprise"]}
raise Exception("Unexpected command")

with patch("pymongo.synchronous.database.Database.command", wraps=mocked_command):
self.assertIs(connection.features.supports_queryable_encryption, True)
Empty file added tests/encryption_/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/encryption_/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models

from django_mongodb_backend.encryption import QueryTypes
from django_mongodb_backend.fields import EncryptedCharField
from django_mongodb_backend.models import EncryptedModel

# Query types for encrypted fields with optional parameters
query_types = QueryTypes()
queries = [query_types.equality(contention=1), query_types.range(sparsity=2, precision=3)]


class Person(EncryptedModel):
name = models.CharField("name", max_length=100)
ssn = EncryptedCharField("ssn", max_length=11, queries=queries)

class Meta:
required_db_features = {"supports_queryable_encryption"}

def __str__(self):
return self.name
Loading
Loading