-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add Support For MongoDB Client Side Field Level Encryption (CSFLE) #67
Open
dill0wn
wants to merge
6
commits into
TurboGears:master
Choose a base branch
from
dill0wn:dw/encryption
base: master
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
6 commits
Select commit
Hold shift + click to select a range
092d5bf
add support for mongodb Client Side Field Level Encryption (CSFLE)
dill0wn f60bb93
fixup! add support for mongodb Client Side Field Level Encryption (CS…
dill0wn ce93ad8
fixup! fixup! add support for mongodb Client Side Field Level Encrypt…
dill0wn 0818240
fixup! fixup! fixup! add support for mongodb Client Side Field Level …
dill0wn da7d5a0
fixup! fixup! fixup! fixup! add support for mongodb Client Side Field…
dill0wn 5708d8b
fixup! fixup! fixup! fixup! fixup! add support for mongodb Client Sid…
dill0wn 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 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 @@ | ||
:mod:`ming.encryption` module | ||
================================ | ||
|
||
|
||
.. automodule:: ming.encryption | ||
:members: | ||
:private-members: |
This file contains 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 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 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,89 @@ | ||
:tocdepth: 3 | ||
|
||
.. _odm-encryption: | ||
|
||
============================ | ||
Encrypting Sensitive Data | ||
============================ | ||
|
||
This section describes how Ming can be used to automatically encrypt and decrypt your document's fields. This is accomplished by leveraging MongoDB's `Client-Side Field Level Encryption (CSFLE)`_ feature. | ||
|
||
|
||
|
||
.. _Client-Side Field Level Encryption (CSFLE): https://pymongo.readthedocs.io/en/stable/examples/encryption.html#client-side-field-level-encryption | ||
|
||
|
||
Encryption at the Foundation Level | ||
================================== | ||
|
||
When declaratively working with models by subclassing :class:`~ming.declarative.Document` in the :ref:`ming_baselevel`, you can add field level encryption by pairing a :class:`~ming.encryption.DecryptedField` with a :class:`~ming.metadata.Field`. | ||
|
||
|
||
A simple example might look like the following. | ||
|
||
.. code-block:: python | ||
|
||
class UserEmail(Document): | ||
class __mongometa__: | ||
session = session | ||
name = 'user_emails' | ||
_id = Field(schema.ObjectId) | ||
|
||
email_encrypted = Field(S.Binary, if_missing=None) | ||
email = DecryptedField(str, 'email_encrypted') | ||
|
||
|
||
Breaking down DecryptedField | ||
---------------------------------- | ||
|
||
This approach requires that you follow a few conventions: | ||
|
||
#. The field storing the encrypted data should be configured in the following way: | ||
|
||
* It should be a :class:`~ming.metadata.Field`. | ||
* The Field should be of type :class:`~ming.schema.Binary`. | ||
* The Field's name should end with `_encrypted`. | ||
|
||
#. Next to this should be a corresponding :class:`~ming.encryption.DecryptedField` that will decrypt the data. | ||
|
||
* Its first argument should be the type that you expect the decrypted data to be (`str`, `int`, etc.). | ||
* The second argument should be the name of the encrypted field (e.g. `email_encrypted`). | ||
* The DecryptedField's name should be the same as the encrypted :class:`~ming.metadata.Field`, but without the `_encrypted` suffix (e.g. `email`). | ||
|
||
|
||
Encryption at the Declarative Level | ||
======================================== | ||
|
||
Similarly when working with the higher level of abstraction offered by :class:`~ming.odm.declarative.MappedClass`es, you can add field level encryption by pairing a :class:`~ming.odm.declarative.DecryptedProperty` with a :class:`~ming.odm.property.FieldProperty` | ||
|
||
|
||
A simple example might look like the following. | ||
|
||
.. code-block:: python | ||
|
||
class UserEmail(MappedClass): | ||
class __mongometa__: | ||
session = session | ||
name = 'user_emails' | ||
_id = FieldProperty(schema.ObjectId) | ||
|
||
email_encrypted = FieldProperty(S.Binary, if_missing=None) | ||
email = DecryptedProperty(str, 'email_encrypted') | ||
|
||
|
||
Breaking down DecryptedProperty | ||
---------------------------------- | ||
|
||
Similarly to the foundation level, this approach requires that you follow a few conventions: | ||
|
||
#. The field storing the encrypted data should be configured in the following way: | ||
|
||
* It should be a :class:`~ming.odm.property.FieldProperty`. | ||
* The FieldProperty should be of type :class:`~ming.schema.Binary`. | ||
* The FieldProperty's name should end with `_encrypted`. | ||
|
||
#. Next to this should be a :class:`~ming.odm.declarative.DecryptedProperty` that will decrypt the data. | ||
|
||
* Its first argument should be the type that you expect the decrypted data to be (`str`, `int`, etc.). | ||
* The second argument should be the name of the encrypted field (e.g. `email_encrypted`). | ||
* The DecryptedProperty's name should be the same as the encrypted :class:`~ming.odm.declarative.DecryptedProperty`, but without the `_encrypted` suffix (e.g. `email`). |
This file contains 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 |
---|---|---|
|
@@ -101,6 +101,7 @@ Documentation Content | |
polymorphism | ||
custom_properties | ||
baselevel | ||
encryption | ||
reference | ||
news | ||
|
||
|
This file contains 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,84 @@ | ||
import bson | ||
from ming import Session | ||
from ming.datastore import create_engine, create_datastore, DataStore | ||
from ming.encryption import EncryptionConfig | ||
import ming.schema as S | ||
from ming.tests import make_encryption_key | ||
|
||
bind: DataStore = create_datastore( | ||
'mongodb://localhost:27017/test_database', | ||
encryption=EncryptionConfig({ | ||
'kms_providers': { | ||
'local': { | ||
# Don't use this for production! This is just for demo purposes | ||
'key': make_encryption_key('demo_encryption'), | ||
}, | ||
}, | ||
'key_vault_namespace': 'demo_encryption_db.__keyVault', | ||
'provider_options': { | ||
'local': { | ||
'key_alt_names': ['datakeyName'], | ||
}, | ||
}, | ||
})) | ||
|
||
# clean up for our demo purposes | ||
bind.conn.drop_database('test_database') | ||
bind.conn.drop_database('demo_encryption_db') | ||
|
||
session = Session(bind) | ||
|
||
from ming import Field, Document, schema | ||
from ming.encryption import DecryptedField | ||
import datetime | ||
|
||
class UserEmail(Document): | ||
class __mongometa__: | ||
session = session | ||
name = 'user_emails' | ||
_id = Field(schema.ObjectId) | ||
|
||
# Encrypted fields should: | ||
# - Have '_encrypted' suffix | ||
# - Have type Binary | ||
email_encrypted = Field(S.Binary, if_missing=None) | ||
|
||
# Decrypted fields should: | ||
# - Have no suffix | ||
# - Have the actual type | ||
# - Provide the encrypted field's full name | ||
email = DecryptedField(str, 'email_encrypted') | ||
|
||
|
||
user_email = UserEmail.make({}) | ||
assert not user_email.email | ||
assert not user_email.email_encrypted | ||
|
||
# Can directly set DecryptedField and it will auto-populate and encrypt its counterpart | ||
user_email.email = 'rick@example.com' | ||
assert user_email.email_encrypted is not None | ||
assert user_email.email_encrypted != 'rick@example.com' | ||
assert isinstance(user_email.email_encrypted, bson.Binary) | ||
user_email.m.save() | ||
|
||
|
||
# Use .make_encr to properly create new instance with unencrypted data | ||
user_email2 = UserEmail.make_encr(dict( | ||
email='stacy@example.com')) | ||
|
||
|
||
assert user_email2.email_encrypted is not None | ||
assert user_email2.email_encrypted != 'stacy@example.com' | ||
assert isinstance(user_email2.email_encrypted, bson.Binary) | ||
blob1 = user_email2.email_encrypted | ||
|
||
user_email2.m.save() | ||
|
||
# updating the email updates the corresponding encrypted field | ||
user_email2.email = 'stacy+1@example.com' | ||
assert user_email2.email_encrypted != blob1 | ||
|
||
user_email2.m.save() | ||
|
||
bind.conn.drop_database('test_database') | ||
bind.conn.drop_database('demo_encryption_db') |
This file contains 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 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 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
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.
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.
can delete this line, since
self
is the datastore now!