-
Notifications
You must be signed in to change notification settings - Fork 25
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
aclark4life
wants to merge
116
commits into
mongodb:main
Choose a base branch
from
aclark4life:INTPYTHON-527
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+550
−14
Open
Changes from 2 commits
Commits
Show all changes
116 commits
Select commit
Hold shift + click to select a range
bc52c8e
INTPYTHON-527 Add Queryable Encryption support
aclark4life 38fb110
Fix test for unencrypted field not in field map
aclark4life 65bd15a
Fix test for unencrypted field not in field map
aclark4life e08945b
Add comment about suppressing EncryptedCollectionError
aclark4life 7b34b44
Don't rely on features to fall back to unencrypted
aclark4life 8e83ada
Remove _nodb_cursor and disable version check
aclark4life 4da895c
Don't surpress encrypted error
aclark4life ed54a9b
Rename get_encrypted_client -> get_client_encryption
aclark4life 8a7766c
Add encryption router
aclark4life eab2f2e
Add "encryption" database to encryption tests
aclark4life 10a361e
Move encrypted_fields_map to schema (1/2)
aclark4life 01d5485
Move encrypted_fields_map to schema (2/x)
aclark4life db32487
Refactor helpers
aclark4life b2be223
Restore get_database_version functionality
aclark4life 27d4b8e
Move encrypted router to tests
aclark4life c4d1c66
Fix router tests
aclark4life 2772aff
Test feature `supports_queryable_encryption`
aclark4life d2ddf4e
Add path and bsonType to _get_encrypted_fields_map
aclark4life e25357e
Use the right database; rename some vars
aclark4life 6487086
Refactor helpers again
aclark4life bc76db3
Allow user to customize some QE settings.
aclark4life 4dbaa8f
Allow uer to customize KMS provider.
aclark4life 9cc5ad2
Refactor
aclark4life c751b2d
Alpha sort helper functions
aclark4life b13a07f
Fix get_database_version
aclark4life 534da6b
A better fix for using `buildInfo` command.
aclark4life 13578ab
Add `queries` key to encrypted fields map
aclark4life 3342d7f
Update django_mongodb_backend/schema.py
aclark4life 9fd21e4
Update django_mongodb_backend/schema.py
aclark4life 9bbe741
Update tests/encryption_/models.py
aclark4life d1eb737
Update tests/encryption_/models.py
aclark4life 176f016
Fix conditional
aclark4life 264b37a
Use column instead of name
aclark4life 1771f56
Avoid double conditional
aclark4life 819058a
Update tests and remove test router
aclark4life 9a3c18e
Update django_mongodb_backend/fields/encryption.py
aclark4life 071192e
Add deconstruct method for encryption fields
aclark4life b2a0534
Add setup & teardown for QE features test
aclark4life 81cc887
Add query type classes and update test
aclark4life be3dd16
Add missing queries to deconstruct
aclark4life a2342e2
Add get_encrypted_fields_map management command
aclark4life 05a7610
Add EncryptedRouter
aclark4life 96b3fda
Optimistically add QE to release notes :-)
aclark4life 1eb71d5
Fix label
aclark4life 08209d3
Save encrypted models to encrypted db
aclark4life 90fe562
Refactor and rename QueryTypes -> QueryType
aclark4life 8c2b84c
Refactor, reword, alpha sort, add comments.
aclark4life ab680fd
Alpha-sort
aclark4life 4a267f5
Document-driven design
aclark4life 3fdc1f7
Document-driven design
aclark4life d562a76
Document-driven design
aclark4life 163758d
Add encryption.rst
aclark4life b95c343
Make key_vault_namespace a required kwarg
aclark4life 5205a0b
Reuse schema editor to create encrypted fields map
aclark4life b07c3e6
Add --database to get_encrypted_fields_map command
aclark4life e557632
Add WIP configuration docs
aclark4life c5f8888
Add check for mongodb 7.0
aclark4life a7bc5c5
Let's go with "Queryable Encryption" everywhere.
aclark4life 09423bc
Update django_mongodb_backend/fields/encryption.py
aclark4life c756cf8
Update tests/encryption_/tests.py
aclark4life 841797c
Update tests/encryption_/tests.py
aclark4life 2386397
Remove gratuitous use of with and append
aclark4life d685d2a
Always use `assertRaisesMessage` for > precision
aclark4life 08ea317
only include migratable models for given database
aclark4life 3e839d7
Refactor QueryType, add encryptino_ migration
aclark4life 75c6936
Refactor tests and fix schema test
aclark4life 534452f
Remove migration, already tested by schema
aclark4life bf26a8a
Router & schema updates
aclark4life bf078ad
Re-add test routers
aclark4life 2780e32
Fix test router
aclark4life 31d3feb
Remove ENCRYPTED_DB_ALIAS, ENCRYPTED_APPS
aclark4life b005726
Get rid of more settings
aclark4life e7290e4
Remove router allow_relation
aclark4life 76deec0
Use class method
aclark4life 02ce21e
Remove ENCRYPTED_DB_ALIAS
aclark4life c8a5118
Rename Person to Patient to match tutorial
aclark4life 39f1cbc
queries only takes a single object
timgraham e504fc5
Move kms_provder to monkeypatch'd ConnectionRouter
aclark4life 0aa423f
Check settings for KMS_PROVIDER & add test.
aclark4life c27be37
Remove get_key_vault_namespace
aclark4life 13de3bb
Remove get_kms_providers, get_customer_master_key
aclark4life 7e3cd34
Update QE config docs
aclark4life 4a9daa7
Add remaining KMS providers
aclark4life 516642f
Look out for more credentials!
aclark4life a319e8e
Move encrypted db name back to router
aclark4life 5807033
Remove comments
aclark4life 37e7e06
Remove comments
aclark4life f19c901
Update comment
aclark4life 528d503
I don't like `conn` either!
aclark4life c7c091b
Use correct verb style "Return" (per relevant PEP)
aclark4life b3a302b
Move connection router patching to routers.py
aclark4life acb0554
Update django_mongodb_backend/features.py
aclark4life 67a640d
Update test models to match design doc
aclark4life 9e76295
Refactor management command and fix test
aclark4life 97196ed
Update Sphinx root_doc to index (the default value)
aclark4life 1614919
Move kms_provider to user router
aclark4life a1bc5f3
Move kms_credentials to user router
aclark4life 75c3cd1
Update docs
aclark4life a81d2ae
Move kms_credentials to settings
aclark4life e562718
Remove get_auto_encryption_opts
aclark4life 3dca177
Remove get_client_encryption
aclark4life 3432818
Define public helpers API
aclark4life cb7f153
Refactor test_auto_encryption_opts
aclark4life e0ef5b3
Assert the entire expected output via json.dumps
aclark4life ba4a6c8
Update docs
aclark4life 1b9a714
Rename test methods
aclark4life 3340ae7
Fix doc string
aclark4life c90406b
Rename tests -> test_schema & add charfield tests
aclark4life 8a1f381
Add test_integerfield from django
aclark4life edb2fa6
Avoid reentrancy issue checking mongodb version
aclark4life 0701160
Add encrypted Post model schema to expected schema
aclark4life 08f7934
Re-add namespace to schema_map
aclark4life 2c4d53b
Add a note about copying field tests from Django
aclark4life 9919ce9
Add query type tests, remove django field tests
aclark4life 45ea5b5
Restored documentation navigation
timgraham 3e468e7
Test negative query type cases
aclark4life 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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 | ||
|
||
|
||
def get_encrypted_client(auto_encryption_opts, encrypted_connection): | ||
""" | ||
Returns a `ClientEncryption` instance for MongoDB Client-Side Field Level | ||
Encryption (CSFLE) that can be used to create an encrypted collection. | ||
""" | ||
|
||
key_vault_namespace = auto_encryption_opts._key_vault_namespace | ||
kms_providers = auto_encryption_opts._kms_providers | ||
codec_options = CodecOptions(uuid_representation=STANDARD) | ||
return ClientEncryption(kms_providers, key_vault_namespace, encrypted_connection, codec_options) | ||
|
||
|
||
def get_auto_encryption_opts(crypt_shared_lib_path=None, kms_providers=None): | ||
aclark4life marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Returns an `AutoEncryptionOpts` instance for MongoDB Client-Side Field | ||
Level Encryption (CSFLE) that can be used to create an encrypted connection. | ||
""" | ||
key_vault_database_name = "encryption" | ||
key_vault_collection_name = "__keyVault" | ||
key_vault_namespace = f"{key_vault_database_name}.{key_vault_collection_name}" | ||
return AutoEncryptionOpts( | ||
key_vault_namespace=key_vault_namespace, | ||
aclark4life marked this conversation as resolved.
Show resolved
Hide resolved
|
||
kms_providers=kms_providers, | ||
crypt_shared_lib_path=crypt_shared_lib_path, | ||
) | ||
|
||
|
||
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_kms_providers(): | ||
""" | ||
Return supported KMS providers for MongoDB Client-Side Field Level Encryption (CSFLE). | ||
""" | ||
return { | ||
"local": { | ||
"key": get_customer_master_key(), | ||
}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.db import models | ||
|
||
|
||
class EncryptedCharField(models.CharField): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.encrypted = True | ||
aclark4life marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ know: | |
|
||
cache | ||
embedded-models | ||
encrypted-models | ||
known-issues |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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) | ||
name = models.CharField("name", max_length=100) | ||
|
||
aclark4life marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __str__(self): | ||
return self.ssn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.test import TestCase | ||
|
||
from .models import Person | ||
|
||
|
||
class EncryptedModelTests(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.objs = [Person.objects.create()] | ||
|
||
def test_encrypted_fields_map_on_class(self): | ||
expected = { | ||
"fields": { | ||
"ssn": "EncryptedCharField", | ||
} | ||
} | ||
self.assertEqual(Person.encrypted_fields_map, expected) | ||
|
||
def test_encrypted_fields_map_on_instance(self): | ||
instance = Person(ssn="123-45-6789") | ||
expected = { | ||
"fields": { | ||
"ssn": "EncryptedCharField", | ||
} | ||
} | ||
self.assertEqual(instance.encrypted_fields_map, expected) | ||
|
||
def test_non_encrypted_fields_not_included(self): | ||
aclark4life marked this conversation as resolved.
Show resolved
Hide resolved
|
||
encrypted_field_names = Person.encrypted_fields_map.get("fields").keys() | ||
self.assertNotIn("name", encrypted_field_names) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.