Skip to content

Commit 77ace9a

Browse files
authored
PYTHON-3299 Add Automatic Queryable Encryption Example to Docs (#964)
1 parent 3e84878 commit 77ace9a

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

doc/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PyMongo 4.2 brings a number of improvements including:
1010

1111
- Support for MongoDB 6.0.
1212
- Support for the Queryable Encryption beta with MongoDB 6.0. Note that backwards-breaking
13-
changes may be made before the final release.
13+
changes may be made before the final release. See :ref:`automatic-queryable-client-side-encryption` for example usage.
1414
- Provisional (beta) support for :func:`pymongo.timeout` to apply a single timeout
1515
to an entire block of pymongo operations.
1616

@@ -41,6 +41,7 @@ in this release.
4141
.. _PYTHON-2885: https://jira.mongodb.org/browse/PYTHON-2885
4242
.. _PYTHON-3167: https://jira.mongodb.org/browse/PYTHON-3167
4343
.. _PyMongo 4.2 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=33196
44+
.. _Queryable Encryption: automatic-queryable-client-side-encryption
4445

4546
Changes in Version 4.1.1
4647
-------------------------

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
# so this link results in a 404.
8686
linkcheck_ignore = [
8787
"https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-monitoring.rst#requesting-an-immediate-check",
88+
"https://github.com/mongodb/libmongocrypt/blob/master/bindings/python/README.rst#installing-from-source",
8889
r"https://wiki.centos.org/[\w/]*",
8990
]
9091

doc/examples/encryption.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,79 @@ data key and create a collection with the
336336
if __name__ == "__main__":
337337
main()
338338

339+
.. _automatic-queryable-client-side-encryption:
340+
341+
Automatic Queryable Encryption (Beta)
342+
`````````````````````````````````````
343+
344+
PyMongo 4.2 brings beta support for Queryable Encryption with MongoDB 6.0.
345+
346+
Queryable Encryption is the second version of Client-Side Field Level Encryption.
347+
Data is encrypted client-side. Queryable Encryption supports indexed encrypted fields,
348+
which are further processed server-side.
349+
350+
You must have MongoDB 6.0rc8+ Enterprise to preview the capability.
351+
352+
Until PyMongo 4.2 release is finalized, it can be installed using::
353+
354+
pip install "pymongo@git+ssh://[email protected]/mongodb/[email protected]#egg=pymongo[encryption]"
355+
356+
Additionally, ``libmongocrypt`` must be installed from `source <https://github.com/mongodb/libmongocrypt/blob/master/bindings/python/README.rst#installing-from-source>`_.
357+
358+
Automatic encryption in Queryable Encryption is configured with an ``encrypted_fields`` mapping, as demonstrated by the following example::
359+
360+
import os
361+
from bson.codec_options import CodecOptions
362+
from pymongo import MongoClient
363+
from pymongo.encryption import Algorithm, ClientEncryption, QueryType
364+
from pymongo.encryption_options import AutoEncryptionOpts
365+
366+
367+
local_master_key = os.urandom(96)
368+
kms_providers = {"local": {"key": local_master_key}}
369+
key_vault_namespace = "keyvault.datakeys"
370+
key_vault_client = MongoClient()
371+
client_encryption = ClientEncryption(
372+
kms_providers, key_vault_namespace, key_vault_client, CodecOptions()
373+
)
374+
key_vault = key_vault_client["keyvault"]["datakeys"]
375+
key_vault.drop()
376+
key1_id = client_encryption.create_data_key("local", key_alt_names=["firstName"])
377+
key2_id = client_encryption.create_data_key("local", key_alt_names=["lastName"])
378+
379+
encrypted_fields_map = {
380+
"default.encryptedCollection": {
381+
"escCollection": "encryptedCollection.esc",
382+
"eccCollection": "encryptedCollection.ecc",
383+
"ecocCollection": "encryptedCollection.ecoc",
384+
"fields": [
385+
{
386+
"path": "firstName",
387+
"bsonType": "string",
388+
"keyId": key1_id,
389+
"queries": [{"queryType": "equality"}],
390+
},
391+
{
392+
"path": "lastName",
393+
"bsonType": "string",
394+
"keyId": key2_id,
395+
}
396+
]
397+
}
398+
}
399+
400+
auto_encryption_opts = AutoEncryptionOpts(
401+
kms_providers, key_vault_namespace, encrypted_fields_map=encrypted_fields_map)
402+
client = MongoClient(auto_encryption_opts=auto_encryption_opts)
403+
client.default.drop_collection('encryptedCollection')
404+
coll = client.default.create_collection('encryptedCollection')
405+
coll.insert_one({ "_id": 1, "firstName": "Jane", "lastName": "Doe" })
406+
docs = list(coll.find({"firstName": "Jane"}))
407+
print(docs)
408+
409+
In the above example, the ``firstName`` and ``lastName`` fields are
410+
automatically encrypted and decrypted.
411+
339412
.. _explicit-client-side-encryption:
340413

341414
Explicit Encryption

0 commit comments

Comments
 (0)