2
2
sidebar_position : 2
3
3
---
4
4
5
- # RUN : Queryable Encryption challenge
5
+ # 👐 RUN : Queryable Encryption challenge
6
6
7
- > Hint! Remember to add the ` --projectId {project_id} `
7
+ :::tip
8
+ Remember to add the ` --projectId {project_id} `
9
+ :::
8
10
11
+ :::info
9
12
> Docs : [ CSFLE] ( https://www.mongodb.com/docs/atlas/app-services/data-api/csfle/ )
13
+ :::
10
14
11
15
### 1. Install the necessary packages.
12
16
@@ -89,17 +93,6 @@ client = MongoClient(new_connection)
89
93
``` python
90
94
# CODE_BLOCK_19
91
95
92
- import os
93
- from pymongo import MongoClient
94
- from pymongo.encryption import Algorithm, ClientEncryption, QueryType
95
- from pymongo.encryption_options import AutoEncryptionOpts
96
- from bson.codec_options import CodecOptions
97
- from bson import json_util
98
- import json
99
- import requests
100
- import platform
101
- import tempfile
102
-
103
96
local_master_key = os.urandom(96 )
104
97
kms_providers = {" local" : {" key" : local_master_key}}
105
98
key_vault_namespace = " encryption.__keyVault"
@@ -119,60 +112,54 @@ auto_encryption_options = AutoEncryptionOpts(
119
112
encrypted_client = MongoClient(
120
113
new_connection, auto_encryption_opts = auto_encryption_options)
121
114
122
- # TODO CODE_BLOCK_19
123
-
124
- encrypted_fields_map = ...
125
-
126
115
client_encryption = ClientEncryption(
127
116
kms_providers = kms_providers,
128
117
key_vault_namespace = key_vault_namespace,
129
118
key_vault_client = encrypted_client,
130
119
codec_options = CodecOptions()
131
120
)
121
+ ```
122
+
123
+ ### 5. Consider the following patient document:
124
+
125
+ ``` python
126
+ patient_document = {
127
+ " patientName" : " Jon Doe" ,
128
+ " patientId" : 12345678 ,
129
+ " patientRecord" : {
130
+ " ssn" : " 987-65-4320" ,
131
+ " billing" : {
132
+ " type" : " Visa" ,
133
+ " number" : " 4111111111111111" ,
134
+ },
135
+ },
136
+ }
137
+ ```
138
+ ### 6. Create an encrypted collection based on the following requirements:
139
+ - 'patientId' and 'billing' must be encrypted
140
+ - patients will be queried by 'patientId'
141
+
142
+ ``` python
143
+ # TODO CODE_BLOCK_19
144
+
145
+ encrypted_fields_map = < CODE_BLOCK_19 >
132
146
133
147
# TODO CODE_BLOCK_19
134
148
135
149
client_encryption.create_encrypted_collection(
136
150
encrypted_client[encrypted_database_name],
137
151
encrypted_collection_name,
138
- ... ,
152
+ < CODE_BLOCK_19 > ,
139
153
kms_provider_name,
140
154
{},
141
155
)
142
156
143
157
```
158
+
159
+ :::tip
144
160
<details >
145
161
<summary > Answer </summary >
146
162
```python
147
- import os
148
- from pymongo import MongoClient
149
- from pymongo .encryption import Algorithm , ClientEncryption , QueryType
150
- from pymongo .encryption_options import AutoEncryptionOpts
151
- from bson .codec_options import CodecOptions
152
- from bson import json_util
153
- import json
154
- import requests
155
- import platform
156
- import tempfile
157
-
158
- local_master_key = os .urandom (96)
159
- kms_providers = {"local" : {"key" : local_master_key }}
160
- key_vault_namespace = " encryption.__keyVault"
161
- kms_provider_name =" local"
162
- key_vault_database_name = " encryption"
163
- key_vault_collection_name = " __keyVault"
164
- key_vault_namespace = f " {key_vault_database_name}.{key_vault_collection_name}"
165
- encrypted_database_name = " medicalRecords"
166
- encrypted_collection_name = " patients"
167
-
168
- auto_encryption_options = AutoEncryptionOpts (
169
- kms_providers ,
170
- key_vault_namespace ,
171
- crypt_shared_lib_path =crypt_shared_lib_path
172
- )
173
-
174
- encrypted_client = MongoClient (
175
- new_connection , auto_encryption_opts =auto_encryption_options )
176
163
177
164
encrypted_fields_map = {
178
165
" fields" : [
@@ -187,14 +174,6 @@ encrypted_fields_map = {
187
174
}
188
175
]
189
176
}
190
-
191
- client_encryption = ClientEncryption (
192
- kms_providers =kms_providers ,
193
- key_vault_namespace =key_vault_namespace ,
194
- key_vault_client =encrypted_client ,
195
- codec_options =CodecOptions ()
196
- )
197
-
198
177
client_encryption.create_encrypted_collection(
199
178
encrypted_client[ encrypted_database_name] ,
200
179
encrypted_collection_name,
@@ -204,59 +183,44 @@ client_encryption.create_encrypted_collection(
204
183
)
205
184
```
206
185
</details>
186
+ :::
187
+
188
+ ### 7. Insert an encrypted document.
207
189
208
- ### 5. Insert an encrypted document.
209
190
210
191
```python
211
- # TODO CODE_BLOCK_20
212
192
213
- patient_document = {
214
- "patientName" : "Jon Doe" ,
215
- "patientId" : 12345678,
216
- "patientRecord" : {
217
- "ssn" : "987-65-4320" ,
218
- "billing" : {
219
- "type" : "Visa" ,
220
- "number" : "4111111111111111" ,
221
- },
222
- },
223
- }
224
193
225
194
# TODO CODE_BLOCK_20
226
195
227
- encrypted_collection = # encrypted_client ...
228
- result = encrypted_collection . insert_one (...)
196
+ encrypted_collection = encrypted_client[encrypted_database_name][encrypted_collection_name]
197
+ result = <CODE_BLOCK_20>
229
198
print(f"Inserted document ID: {result.inserted_id}")
230
199
```
200
+ :::tip
231
201
<details >
232
202
<summary > Answer </summary >
233
203
```python
234
- patient_document = {
235
- "patientName" : "Jon Doe" ,
236
- "patientId" : 12345678,
237
- "patientRecord" : {
238
- "ssn" : "987-65-4320" ,
239
- "billing" : {
240
- "type" : "Visa" ,
241
- "number" : "4111111111111111" ,
242
- },
243
- },
244
- }
204
+
245
205
encrypted_collection = encrypted_client[ encrypted_database_name] [ encrypted_collection_name ]
246
206
result = encrypted_collection.insert_one(patient_document)
247
207
print(f"Inserted document ID: { result .inserted_id } ")
248
208
```
209
+
210
+
249
211
</details>
212
+ :::
250
213
251
214
### 6. Query the encrypted collection.
252
215
253
216
```python
254
217
# TODO CODE_BLOCK_21
255
218
256
- find_result = encrypted_collection . find_one (...)
219
+ find_result = <CODE_BLOCK_21>
257
220
print(find_result)
258
221
...
259
222
```
223
+ :::tip
260
224
<details >
261
225
<summary > Answer </summary >
262
226
```python
@@ -266,6 +230,7 @@ find_result = encrypted_collection.find_one({
266
230
print(find_result)
267
231
```
268
232
</details>
233
+ :::
269
234
270
235
### 7. Query the collection without encryption.
271
236
@@ -285,6 +250,7 @@ print("\nAll documents in the collection:")
285
250
for doc in all_docs:
286
251
print(doc)
287
252
```
253
+ :::tip
288
254
<details >
289
255
<summary > Answer </summary >
290
256
```python
@@ -303,6 +269,7 @@ for doc in all_docs:
303
269
print(doc)
304
270
```
305
271
</details>
272
+ :::
306
273
307
274
## Next Steps
308
275
0 commit comments