-
Notifications
You must be signed in to change notification settings - Fork 21
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
8
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.
Open
Changes from all commits
Commits
Show all changes
8 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 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_client_encryption(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): | ||
""" | ||
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, | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd think this could be a class-level variable. |
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 |
---|---|---|
|
@@ -14,3 +14,26 @@ def delete(self, *args, **kwargs): | |
|
||
def save(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be saved.") | ||
|
||
|
||
class EncryptedModelBase(models.base.ModelBase): | ||
def __new__(cls, name, bases, attrs, **kwargs): | ||
new_class = super().__new__(cls, name, bases, attrs, **kwargs) | ||
|
||
# Build a map of encrypted fields | ||
encrypted_fields = { | ||
"fields": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add query conditions |
||
field.name: field.__class__.__name__ | ||
for field in new_class._meta.fields | ||
if getattr(field, "encrypted", False) | ||
} | ||
} | ||
|
||
# Store it as a class-level attribute | ||
new_class.encrypted_fields_map = encrypted_fields | ||
return new_class | ||
|
||
|
||
class EncryptedModel(models.Model, metaclass=EncryptedModelBase): | ||
class Meta: | ||
abstract = True |
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): | ||
name = models.CharField("name", max_length=100) | ||
ssn = EncryptedCharField("ssn", max_length=11) | ||
|
||
def __str__(self): | ||
return self.name |
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) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crypt_shared
library is in thepymongocrypt
wheel, which is much easier than downloading separately and tellingMongoClient
where it is.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More to this story:
libmongocrypt
is in thepymongocrypt
wheel, notcrypt_shared
which must always be downloaded and configured manually.libmongocrypt
works becausemongocryptd
is running on enterprise.We should document this.
(via @ShaneHarvey, thanks!)