Skip to content

Commit 5d379da

Browse files
committed
PHPLIB-986: Split keyAltName example and link key management docs
1 parent 2e99cd8 commit 5d379da

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

docs/tutorial/client-side-encryption.txt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ specified when creating the key. The following example creates an encryption key
202202
with an alternative name, which could be done when deploying the application.
203203
The software then encrypts data by referencing the key by its alternative name.
204204

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+
205214
.. code-block:: php
206215

207216
<?php
@@ -222,10 +231,35 @@ The software then encrypts data by referencing the key by its alternative name.
222231
$client = new Client();
223232
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
224233

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.
227235
$keyId = $clientEncryption->createDataKey('local', ['keyAltNames' => ['altname']]);
228236

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+
229263
$collection = $client->selectCollection('test', 'coll');
230264
$collection->drop(); // clear old data
231265

@@ -329,3 +363,5 @@ query on the ``encryptedIndexed`` field.
329363
$unencryptedCollection = $client->selectCollection('test', 'coll');
330364

331365
var_dump($unencryptedCollection->findOne(['_id' => 1]));
366+
367+
.. seealso:: :manual:`Encryption Key Management </csfle/fundamentals/manage-keys/>` in the MongoDB manual

0 commit comments

Comments
 (0)