Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

dill0wn
Copy link
Contributor

@dill0wn dill0wn commented Sep 12, 2024

This implementation was guided largely by pymongo's examples on explicit, manual encryption.

These changes add a lot:

  • The Document class now supports automatic encryption and decryption of individual fields. It gains this ability by inheriting from ming.encryption.EncryptedDocumentMixin and leveraging the new quasi-field class ming.encryption.DecryptedField.

  • Encryption configuration options are handled by ming.encryption.EncryptionConfig

    • ming.configure automatically parses flattened config values like you would see in an ini. For example:
    ming.main.encryption.kms_providers.local.key = ENCR_KEY
    ming.main.encryption.key_vault_namespace = encryption_test.dataKeyVault
    ming.main.encryption.provider_options.local.key_alt_names = ["datakey_test1"]
    
  • A new formencode validator ming.validators.EncryptionConfigValidator has been added to validate config dict values.

  • For runtime usage, the EncryptionConfig is added to the DataStore instance as a new DataStore.encryption instance property. This is what individual Documents reference when performing encryption/decryption.

  • Added new package dependencies: pymongo[decryption] and cachetools

  • Added tests for new encryption features

  • Added new demo docs/presentations/demo_encryption.py

See pymongo's documentation on encryption for further details on the implementation and expected configuration.

Copy link

codecov bot commented Sep 12, 2024

Codecov Report

Attention: Patch coverage is 96.10984% with 17 lines in your changes missing coverage. Please review.

Project coverage is 91.45%. Comparing base (637a033) to head (5708d8b).
Report is 18 commits behind head on master.

Files with missing lines Patch % Lines
ming/validators.py 91.30% 6 Missing ⚠️
ming/datastore.py 90.47% 4 Missing ⚠️
ming/encryption.py 94.93% 4 Missing ⚠️
ming/tests/test_encryption.py 99.00% 2 Missing ⚠️
ming/odm/mapper.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
+ Coverage   90.19%   91.45%   +1.25%     
==========================================
  Files          43       47       +4     
  Lines        6724     7126     +402     
==========================================
+ Hits         6065     6517     +452     
+ Misses        659      609      -50     
Flag Coverage Δ
tests-3.10 91.63% <96.10%> (+1.24%) ⬆️
tests-3.11 91.63% <96.10%> (+1.24%) ⬆️
tests-3.12 91.63% <96.10%> (?)
tests-3.7 ?
tests-3.8 91.45% <96.10%> (+1.25%) ⬆️
tests-3.9 91.45% <96.10%> (+1.25%) ⬆️
tests-pypy3.9 91.57% <96.10%> (+1.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dill0wn
Copy link
Contributor Author

dill0wn commented Sep 12, 2024

This work is not ready to merge yet as it only covers Documents. We still need to support MappedClasses.

ming/encryption.py Outdated Show resolved Hide resolved
docs/presentations/demo_encryption.py Outdated Show resolved Hide resolved
ming/encryption.py Outdated Show resolved Hide resolved
ming/encryption.py Show resolved Hide resolved
ming/validators.py Outdated Show resolved Hide resolved
ming/validators.py Outdated Show resolved Hide resolved
ming/validators.py Outdated Show resolved Hide resolved
ming/validators.py Outdated Show resolved Hide resolved
ming/validators.py Outdated Show resolved Hide resolved
docs/encryption.rst Outdated Show resolved Hide resolved
@dill0wn dill0wn marked this pull request as ready for review October 3, 2024 18:55
def encryptor(self) -> ClientEncryption:
"""Creates and returns a :class:`pymongo.encryption.ClientEncryption` instance for the given ming datastore. It uses this to handle encryption/decryption using pymongo's native routines.

:param ming_ds: the :class:`ming.datastore.Datastore` for which this encryptor should be configured with.
Copy link
Collaborator

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!

Copy link
Collaborator

Choose a reason for hiding this comment

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

in def include_in_repr(self): lets add Binary types to the skip list. I tested repr(u) in one of the encryption tests to see what it'd look like. I don't think we want binary!

@@ -34,7 +34,7 @@
include_package_data=True,
zip_safe=True,
install_requires=[
"pymongo",
"pymongo[encryption]<4.9",
Copy link
Collaborator

@brondsem brondsem Oct 3, 2024

Choose a reason for hiding this comment

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

I'm ok with this for now, to get this MR merged. But we probably should make it work on 4.9 next, before making a release 😐 In case anyone's already using ming with a higher pymongo version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants