-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3299 Add Automatic Queryable Encryption Example to Docs #964
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
54845fc
PYTHON-3299 Add Automatic Queryable Encryption Example to Docs
blink1073 8afdf73
fix title underline
blink1073 ff71b69
fix line length
blink1073 34028df
address review
blink1073 91f12f9
fix ref
blink1073 f8291e8
Merge branch 'master' of github.com:mongodb/mongo-python-driver into …
blink1073 04da41c
clean up example and add find
blink1073 19924fd
add link ignore
blink1073 8e74411
update for clarity
blink1073 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
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 |
---|---|---|
|
@@ -336,6 +336,79 @@ data key and create a collection with the | |
if __name__ == "__main__": | ||
main() | ||
|
||
.. _automatic-queryable-client-side-encryption: | ||
|
||
Automatic Queryable Encryption (Beta) | ||
````````````````````````````````````` | ||
|
||
PyMongo 4.2 brings beta support for Queryable Encryption with MongoDB 6.0. | ||
|
||
Queryable Encryption is the second version of Client-Side Field Level Encryption. | ||
Data is encrypted client-side. Queryable Encryption supports indexed encrypted fields, | ||
which are further processed server-side. | ||
|
||
You must have MongoDB 6.0rc8+ Enterprise to preview the capability. | ||
|
||
Until PyMongo 4.2 release is finalized, it can be installed using:: | ||
ShaneHarvey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pip install "pymongo@git+ssh://[email protected]/mongodb/[email protected]#egg=pymongo[encryption]" | ||
|
||
Additionally, ``libmongocrypt`` must be installed from `source <https://github.com/mongodb/libmongocrypt/blob/master/bindings/python/README.rst#installing-from-source>`_. | ||
|
||
Automatic encryption in Queryable Encryption is configured with an ``encrypted_fields`` mapping, as demonstrated by the following example:: | ||
|
||
import os | ||
from bson.codec_options import CodecOptions | ||
from pymongo import MongoClient | ||
from pymongo.encryption import Algorithm, ClientEncryption, QueryType | ||
from pymongo.encryption_options import AutoEncryptionOpts | ||
|
||
|
||
local_master_key = os.urandom(96) | ||
kms_providers = {"local": {"key": local_master_key}} | ||
key_vault_namespace = "keyvault.datakeys" | ||
key_vault_client = MongoClient() | ||
client_encryption = ClientEncryption( | ||
kms_providers, key_vault_namespace, key_vault_client, CodecOptions() | ||
) | ||
key_vault = key_vault_client["keyvault"]["datakeys"] | ||
key_vault.drop() | ||
key1_id = client_encryption.create_data_key("local", key_alt_names=["firstName"]) | ||
key2_id = client_encryption.create_data_key("local", key_alt_names=["lastName"]) | ||
|
||
encrypted_fields_map = { | ||
"default.encryptedCollection": { | ||
"escCollection": "encryptedCollection.esc", | ||
"eccCollection": "encryptedCollection.ecc", | ||
"ecocCollection": "encryptedCollection.ecoc", | ||
"fields": [ | ||
{ | ||
"path": "firstName", | ||
"bsonType": "string", | ||
"keyId": key1_id, | ||
"queries": [{"queryType": "equality"}], | ||
}, | ||
{ | ||
"path": "lastName", | ||
"bsonType": "string", | ||
"keyId": key2_id, | ||
} | ||
] | ||
} | ||
} | ||
|
||
auto_encryption_opts = AutoEncryptionOpts( | ||
kms_providers, key_vault_namespace, encrypted_fields_map=encrypted_fields_map) | ||
client = MongoClient(auto_encryption_opts=auto_encryption_opts) | ||
client.default.drop_collection('encryptedCollection') | ||
coll = client.default.create_collection('encryptedCollection') | ||
coll.insert_one({ "_id": 1, "firstName": "Jane", "lastName": "Doe" }) | ||
docs = list(coll.find({"firstName": "Jane"})) | ||
print(docs) | ||
|
||
In the above example, the ``firstName`` and ``lastName`` fields are | ||
automatically encrypted and decrypted. | ||
|
||
.. _explicit-client-side-encryption: | ||
|
||
Explicit Encryption | ||
|
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.