Skip to content

Commit 282d608

Browse files
authored
DOCSP-6987 Make Python CSFLE examples more idiomatic (#564)
1 parent a2626bf commit 282d608

4 files changed

+37
-47
lines changed

source/includes/steps-fle-configure-the-mongodb-client.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ content: |
232232
.. code-block:: python
233233
:emphasize-lines: 2-5
234234
235-
fle_opts = AutoEncryptionOpts(
236-
kms_providers,
235+
fle_opts = AutoEncryptionOpts(
236+
kms_providers,
237237
key_vault_namespace,
238-
schema_map = patient_schema,
238+
schema_map=patient_schema,
239239
**extra_options
240240
)
241-
client = MongoClient(connection_string, auto_encryption_opts = fle_opts)
241+
client = MongoClient(connection_string, auto_encryption_opts=fle_opts)

source/includes/steps-fle-convert-to-a-remote-master-key.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ content: |
147147
import pymongo
148148
from pymongo import MongoClient
149149
from pymongo.encryption_options import AutoEncryptionOpts
150+
from bson.binary import STANDARD
150151
from bson.codec_options import CodecOptions
151-
from bson.binary import (STANDARD)
152152
153153
connection_string = "mongodb://localhost:27017"
154154
key_vault_namespace = "encryption.__keyVault"
@@ -160,16 +160,16 @@ content: |
160160
161161
client_encryption = pymongo.encryption.ClientEncryption(
162162
{
163-
"aws": {
164-
"accessKeyId": "<IAM User Access Key ID>",
165-
"secretAccessKey": "<IAM User Secret Access Key>"
166-
}
163+
"aws": {
164+
"accessKeyId": "<IAM User Access Key ID>",
165+
"secretAccessKey": "<IAM User Secret Access Key>"
166+
}
167167
},
168168
key_vault_namespace,
169169
client,
170170
CodecOptions(uuid_representation=STANDARD)
171171
)
172-
data_key = client_encryption.create_data_key("aws")
172+
data_key_id = client_encryption.create_data_key("aws")
173173
174174
---
175175
title: Update the JSON Schema

source/includes/steps-fle-create-data-encryption-key.yaml

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ content: |
4040
:tabid: python
4141
4242
.. code-block:: python
43-
:emphasize-lines: 4
44-
45-
from bson import binary
43+
:emphasize-lines: 3
4644
4745
path = "./master-key.txt"
48-
local_master_key = binary.Binary(open(path, "rb").read(96))
46+
with open(path, "rb") as f:
47+
local_master_key = f.read()
4948
5049
---
5150
title: Specify KMS Provider Settings
@@ -94,9 +93,9 @@ content: |
9493
:emphasize-lines: 2,3
9594
9695
kms_providers = {
97-
"local": {
98-
"key": local_master_key # local_master_key variable from the previous step
99-
},
96+
"local": {
97+
"key": local_master_key # local_master_key variable from the previous step
98+
},
10099
}
101100
102101
---
@@ -183,7 +182,7 @@ content: |
183182
:tabid: python
184183
185184
.. code-block:: python
186-
:emphasize-lines: 13,14,20,30
185+
:emphasize-lines: 13,21
187186
188187
from pymongo import MongoClient
189188
from pymongo.encryption_options import AutoEncryptionOpts
@@ -195,14 +194,7 @@ content: |
195194
connection_string = "mongodb://localhost:27017"
196195
key_vault_namespace = "encryption.__keyVault"
197196
198-
fle_opts = AutoEncryptionOpts(
199-
kms_providers, # pass in the kms_providers variable from the previous step
200-
key_vault_namespace
201-
)
202-
client = MongoClient(
203-
connection_string,
204-
auto_encryption_opts=fle_opts
205-
)
197+
client = MongoClient(connection_string)
206198
client_encryption = ClientEncryption(
207199
kms_providers, # pass in the kms_providers variable from the previous step
208200
key_vault_namespace,
@@ -212,16 +204,15 @@ content: |
212204
213205
214206
def create_data_encryption_key():
215-
216-
data_key = client_encryption.create_data_key("local")
217-
uuid_data_key_id = UUID(bytes=data_key)
218-
base_64_data_key_id = base64.b64encode(data_key)
207+
data_key_id = client_encryption.create_data_key("local")
208+
uuid_data_key_id = UUID(bytes=data_key_id)
209+
base_64_data_key_id = base64.b64encode(data_key_id)
219210
print("DataKeyId [UUID]: ", str(uuid_data_key_id))
220211
print("DataKeyId [base64]: ", base_64_data_key_id)
221-
return data_key
212+
return data_key_id
222213
223214
224-
data_key = create_data_encryption_key()
215+
data_key_id = create_data_encryption_key()
225216
226217
227218
The ``_id`` field of the data encryption key is represented as a **UUID**
@@ -361,19 +352,19 @@ content: |
361352
:tabid: python
362353
363354
.. code-block:: python
355+
:emphasize-lines: 9,10
364356
365357
from pprint import pprint
366358
connection_string = "mongodb://localhost:27017"
367-
key_vault_namespace = "encryption.__keyVault"
368-
369-
def verify_data_key_created(data_key, client, client_encryption, fle_opts):
370-
db = client["encryption"]
371-
collection = db["__keyVault"]
372-
key = collection.find_one({"_id": data_key})
373-
pprint(key)
374-
375-
# pass in fle_opts, client, client_encryption, data_key variables created in previous section
376-
verify_data_key_created(data_key, client, client_encryption, fle_opts)
359+
key_vault_db = "encryption"
360+
key_vault_coll = "__keyVault"
361+
362+
client = MongoClient(connection_string)
363+
key_vault = client[key_vault_db][key_vault_coll]
364+
365+
# Pass in the data_key_id created in previous section
366+
key = key_vault.find_one({"_id": data_key_id})
367+
pprint(key)
377368
378369
379370
This code example should print a retrieved document that resembles the

source/use-cases/client-side-field-level-encryption-guide.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,9 @@ To begin development, MedcoMD engineers generate a locally-managed master key:
302302
import os
303303

304304
path = "master-key.txt"
305-
file_bytes = os.urandom(96).strip()
306-
f = open(path, "wb")
307-
f.write(file_bytes.strip())
308-
f.close()
305+
file_bytes = os.urandom(96)
306+
with open(path, "wb") as f:
307+
f.write(file_bytes)
309308

310309

311310
.. _fle-create-a-data-encryption-key:
@@ -655,7 +654,7 @@ MedcoMD engineers write a function to create a new patient record:
655654
}
656655
doc = {
657656
'name': name,
658-
'ssn': ssn,
657+
'ssn': ssn,
659658
'bloodType': blood_type,
660659
'medicalRecords': medical_records,
661660
'insurance': insurance

0 commit comments

Comments
 (0)