@@ -202,6 +202,15 @@ specified when creating the key. The following example creates an encryption key
202
202
with an alternative name, which could be done when deploying the application.
203
203
The software then encrypts data by referencing the key by its alternative name.
204
204
205
+ Creating the encryption key
206
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
207
+
208
+ To create the encryption key, create a client instance with encryption options
209
+ and create a new data key. You can pass multiple alternate names for this key
210
+ and later reference the key by these alternate names instead of the key ID.
211
+ Creating a new data encryption key would typically be done on initial deployment,
212
+ but depending on your use-case you may want to use more than one encryption key.
213
+
205
214
.. code-block:: php
206
215
207
216
<?php
@@ -222,10 +231,35 @@ The software then encrypts data by referencing the key by its alternative name.
222
231
$client = new Client();
223
232
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
224
233
225
- // Create an encryption key with an alternative name. This could be done when
226
- // deploying the application
234
+ // Create an encryption key with an alternate name.
227
235
$keyId = $clientEncryption->createDataKey('local', ['keyAltNames' => ['altname']]);
228
236
237
+ Using an encryption key by alternate name
238
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
+
240
+ To use an alternate name when referencing an encryption key, use the
241
+ ``keyAltName`` option instead of ``keyId``.
242
+
243
+ .. code-block:: php
244
+
245
+ <?php
246
+
247
+ use MongoDB\BSON\Binary;
248
+ use MongoDB\Client;
249
+ use MongoDB\Driver\ClientEncryption;
250
+
251
+ $localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
252
+
253
+ $clientEncryptionOpts = [
254
+ 'keyVaultNamespace' => 'encryption.__keyVault',
255
+ 'kmsProviders' => [
256
+ 'local' => ['key' => $localKey],
257
+ ],
258
+ ];
259
+
260
+ $client = new Client();
261
+ $clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
262
+
229
263
$collection = $client->selectCollection('test', 'coll');
230
264
$collection->drop(); // clear old data
231
265
@@ -329,3 +363,5 @@ query on the ``encryptedIndexed`` field.
329
363
$unencryptedCollection = $client->selectCollection('test', 'coll');
330
364
331
365
var_dump($unencryptedCollection->findOne(['_id' => 1]));
366
+
367
+ .. seealso:: :manual:`Encryption Key Management </csfle/fundamentals/manage-keys/>` in the MongoDB manual
0 commit comments