@@ -21,8 +21,8 @@ Automatic Encryption and Decryption
2121
2222 Auto encryption is an enterprise only feature.
2323
24- The following example uses a local key, however using AWS Key Management Service
25- is also an option. The data in the ``encryptedField`` field is automatically
24+ The following example uses a local key; however, other key providers such as AWS
25+ are also an option. The data in the ``encryptedField`` field is automatically
2626encrypted on insertion and decrypted when querying on the client side.
2727
2828.. code-block:: php
@@ -31,6 +31,7 @@ encrypted on insertion and decrypted when querying on the client side.
3131
3232 use MongoDB\BSON\Binary;
3333 use MongoDB\Client;
34+ use MongoDB\Driver\ClientEncryption;
3435
3536 $localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
3637
@@ -41,7 +42,7 @@ encrypted on insertion and decrypted when querying on the client side.
4142 ],
4243 ];
4344
44- $client = new Client('mongodb://127.0.0.1' );
45+ $client = new Client();
4546 $clientEncryption = $client->createClientEncryption($encryptionOpts);
4647
4748 $database = $client->selectDatabase('test');
@@ -134,7 +135,7 @@ encryption using the newly created key.
134135 ],
135136 ];
136137
137- $encryptedClient = new Client('mongodb://127.0.0.1' , [], ['autoEncryption' => $autoEncryptionOpts]);
138+ $encryptedClient = new Client(null , [], ['autoEncryption' => $autoEncryptionOpts]);
138139
139140 $collection = $encryptedClient->selectCollection('test', 'coll');
140141 $collection->drop(); // clear old data
@@ -239,3 +240,92 @@ The software then encrypts data by referencing the key by its alternative name.
239240
240241 $document = $collection->findOne();
241242 var_dump($clientEncryption->decrypt($document->encryptedField));
243+
244+
245+ Automatic Queryable Encryption
246+ ------------------------------
247+
248+ .. note::
249+
250+ Automatic queryable encryption is an enterprise only feature and requires
251+ MongoDB 6.0+.
252+
253+ The following example uses a local key; however, other key providers such as AWS
254+ are also an option. The data in the ``encryptedIndexed`` and
255+ ``encryptedUnindexed`` fields will be automatically encrypted on insertion and
256+ decrypted when querying on the client side. Additionally, it is possible to
257+ query on the ``encryptedIndexed`` field.
258+
259+ .. code-block:: php
260+
261+ <?php
262+
263+ use MongoDB\BSON\Binary;
264+ use MongoDB\Client;
265+
266+ $localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
267+
268+ $encryptionOpts = [
269+ 'keyVaultNamespace' => 'encryption.__keyVault',
270+ 'kmsProviders' => ['local' => ['key' => $localKey]],
271+ ];
272+
273+ $client = new Client();
274+ $clientEncryption = $client->createClientEncryption($encryptionOpts);
275+
276+ // Create two data keys, one for each encrypted field
277+ $dataKeyId1 = $clientEncryption->createDataKey('local');
278+ $dataKeyId2 = $clientEncryption->createDataKey('local');
279+
280+ $autoEncryptionOpts = [
281+ 'keyVaultNamespace' => 'encryption.__keyVault',
282+ 'kmsProviders' => ['local' => ['key' => $localKey]],
283+ 'encryptedFieldsMap' => [
284+ 'test.coll' => [
285+ 'fields' => [
286+ [
287+ 'path' => 'encryptedIndexed',
288+ 'bsonType' => 'string',
289+ 'keyId' => $dataKeyId1,
290+ 'queries' => ['queryType' => 'equality'],
291+ ],
292+ [
293+ 'path' => 'encryptedUnindexed',
294+ 'bsonType' => 'string',
295+ 'keyId' => $dataKeyId2,
296+ ],
297+ ],
298+ ],
299+ ],
300+ ];
301+
302+ $encryptedClient = new Client(null, [], ['autoEncryption' => $autoEncryptionOpts]);
303+
304+ /* Drop and create the collection under test. The createCollection() helper
305+ * will reference the client's encryptedFieldsMap and create additional,
306+ * internal collections automatically. */
307+ $encryptedClient->selectDatabase('test')->dropCollection('coll');
308+ $encryptedClient->selectDatabase('test')->createCollection('coll');
309+ $encryptedCollection = $encryptedClient->selectCollection('test', 'coll');
310+
311+ /* Using a client with auto encryption, insert a document with encrypted
312+ * fields and assert that those fields are automatically decrypted when
313+ * querying. The encryptedIndexed and encryptedUnindexed fields should both
314+ * be strings. */
315+ $indexedValue = 'indexedValue';
316+ $unindexedValue = 'unindexedValue';
317+
318+ $encryptedCollection->insertOne([
319+ '_id' => 1,
320+ 'encryptedIndexed' => $indexedValue,
321+ 'encryptedUnindexed' => $unindexedValue,
322+ ]);
323+
324+ var_dump($encryptedCollection->findOne(['encryptedIndexed' => $indexedValue]));
325+
326+ /* Using a client without auto encryption, query for the same document and
327+ * assert that encrypted data is returned. The encryptedIndexed and
328+ * encryptedUnindexed fields should both be Binary objects. */
329+ $unencryptedCollection = $client->selectCollection('test', 'coll');
330+
331+ var_dump($unencryptedCollection->findOne(['_id' => 1]));
0 commit comments