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

Merged
merged 1 commit into from
Oct 10, 2024

Conversation

dill0wn
Copy link
Collaborator

@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 95.39474% with 21 lines in your changes missing coverage. Please review.

Project coverage is 91.42%. Comparing base (637a033) to head (f335589).
Report is 22 commits behind head on master.

Files with missing lines Patch % Lines
ming/encryption.py 92.55% 7 Missing ⚠️
ming/validators.py 91.30% 6 Missing ⚠️
ming/datastore.py 90.47% 4 Missing ⚠️
ming/tests/test_encryption.py 99.01% 2 Missing ⚠️
ming/odm/mapper.py 87.50% 1 Missing ⚠️
ming/odm/property.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
+ Coverage   90.19%   91.42%   +1.22%     
==========================================
  Files          43       47       +4     
  Lines        6724     7146     +422     
==========================================
+ Hits         6065     6533     +468     
+ Misses        659      613      -46     
Flag Coverage Δ
tests-3.10 91.60% <95.39%> (+1.21%) ⬆️
tests-3.11 91.60% <95.39%> (+1.21%) ⬆️
tests-3.12 91.60% <95.39%> (?)
tests-3.7 ?
tests-3.8 ?
tests-3.9 91.41% <95.39%> (+1.22%) ⬆️
tests-pypy3.9 91.54% <95.39%> (+1.21%) ⬆️

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
Collaborator 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 Author

Choose a reason for hiding this comment

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

Fixed

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!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

@@ -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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed.

@brondsem brondsem merged commit 1c02d51 into TurboGears:master Oct 10, 2024
12 checks passed
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