Skip to content

Commit 670d3af

Browse files
committed
Move key creation to top of csfle tutorial
1 parent 3fcb260 commit 670d3af

File tree

1 file changed

+66
-65
lines changed

1 file changed

+66
-65
lines changed

docs/tutorial/client-side-encryption.txt

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,61 @@ Client-Side Field Level Encryption allows administrators and developers to
1414
encrypt specific data fields in addition to other MongoDB encryption features.
1515

1616

17+
Creating an Encryption Key
18+
--------------------------
19+
20+
.. note::
21+
22+
The following examples use a local master key; however, other key providers
23+
such as AWS KMS are also an option. This master key is used to encrypt data
24+
keys that are stored locally. It is important that you keep this key secure.
25+
26+
To create an encryption key, create a :php:`MongoDB\\Driver\\ClientEncryption <class.mongodb-driver-clientencryption>`
27+
instance with encryption options and create a new data key. The method will
28+
return the key ID which can be used to reference the key later. You can also
29+
pass multiple alternate names for this key and reference the key by these names
30+
instead of the key ID. Creating a new data encryption key would typically be
31+
done on initial deployment, but depending on your use case you may want to use
32+
more than one encryption key or create them dynamically.
33+
34+
.. code-block:: php
35+
36+
<?php
37+
38+
use MongoDB\BSON\Binary;
39+
use MongoDB\Client;
40+
use MongoDB\Driver\ClientEncryption;
41+
42+
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
43+
44+
$clientEncryptionOpts = [
45+
'keyVaultNamespace' => 'encryption.__keyVault',
46+
'kmsProviders' => [
47+
'local' => ['key' => $localKey],
48+
],
49+
];
50+
51+
$client = new Client();
52+
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
53+
54+
// Create an encryption key with an alternate name
55+
// To store the key ID for later use, you can use serialize or var_export
56+
$keyId = $clientEncryption->createDataKey('local', ['keyAltNames' => ['my-encryption-key']]);
57+
58+
.. seealso:: :manual:`Encryption Key Management </csfle/fundamentals/manage-keys/>` in the MongoDB manual
59+
60+
1761
Automatic Encryption and Decryption
1862
-----------------------------------
1963

2064
.. note::
2165

2266
Auto encryption is an enterprise only feature.
2367

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
26-
encrypted on insertion and decrypted when querying on the client side.
68+
The following example sets up a collection with automatic encryption based on a
69+
``$jsonSchema`` validator. The data in the ``encryptedField`` field is
70+
automatically encrypted on insertion and decrypted when reading on the client
71+
side.
2772

2873
.. code-block:: php
2974

@@ -34,7 +79,6 @@ encrypted on insertion and decrypted when querying on the client side.
3479
use MongoDB\Driver\ClientEncryption;
3580

3681
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
37-
3882
$encryptionOpts = [
3983
'keyVaultNamespace' => 'encryption.__keyVault',
4084
'kmsProviders' => [
@@ -43,13 +87,13 @@ encrypted on insertion and decrypted when querying on the client side.
4387
];
4488

4589
$client = new Client();
46-
$clientEncryption = $client->createClientEncryption($encryptionOpts);
4790

4891
$database = $client->selectDatabase('test');
4992
$database->dropCollection('coll'); // remove old data
5093

51-
// Create new key in the key vault and store its ID for later use
52-
$keyId = $clientEncryption->createDataKey('local');
94+
// This uses the key ID from the first example. The key ID could be read from
95+
// configuration.
96+
$keyId = readDataKey();
5397

5498
$database->createCollection('coll', [
5599
'validator' => [
@@ -80,9 +124,8 @@ encrypted on insertion and decrypted when querying on the client side.
80124
Specifying an Explicit Schema for Encryption
81125
--------------------------------------------
82126

83-
The following example shows how to create a new key and store it in the key
84-
vault collection. The encrypted client configures an explicit schema for
85-
encryption using the newly created key.
127+
The following example uses the ``schemaMap`` encryption option to define
128+
encrypted fields.
86129

87130
.. note::
88131

@@ -101,18 +144,11 @@ encryption using the newly created key.
101144

102145
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
103146

104-
$clientEncryptionOpts = [
105-
'keyVaultNamespace' => 'encryption.__keyVault',
106-
'kmsProviders' => [
107-
'local' => ['key' => $localKey],
108-
],
109-
];
110-
111147
$client = new Client();
112-
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
113148

114-
// Create new key in the key vault and store its ID for later use
115-
$keyId = $clientEncryption->createDataKey('local');
149+
// This uses the key ID from the first example. The key ID could be read from
150+
// configuration.
151+
$keyId = readDataKey();
116152

117153
$autoEncryptionOpts = [
118154
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -148,10 +184,10 @@ encryption using the newly created key.
148184
Manually Encrypting and Decrypting Values
149185
-----------------------------------------
150186

151-
In the MongoDB Community Edition, you will have to manually encrypt and decrypt
152-
values before storing them in the database. The following example assumes that
153-
you have already created an encryption key in the key vault collection and
154-
explicitly encrypts and decrypts values in the document.
187+
In the MongoDB Community Edition, you will have to manually encrypt values
188+
before storing them in the database. The following example assumes that you have
189+
already created an encryption key in the key vault collection and explicitly
190+
encrypts and decrypts values in the document.
155191

156192
.. code-block:: php
157193

@@ -173,8 +209,9 @@ explicitly encrypts and decrypts values in the document.
173209
$client = new Client();
174210
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
175211

176-
// Create new key in the key vault and store its ID for later use
177-
$keyId = $clientEncryption->createDataKey('local');
212+
// This uses the key ID from the first example. The key ID could be read from
213+
// configuration.
214+
$keyId = readDataKey();
178215

179216
$collection = $client->selectCollection('test', 'coll');
180217
$collection->drop(); // clear old data
@@ -202,41 +239,6 @@ specified when creating the key. The following example creates an encryption key
202239
with an alternative name, which could be done when deploying the application.
203240
The software then encrypts data by referencing the key by its alternative name.
204241

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-
214-
.. code-block:: php
215-
216-
<?php
217-
218-
use MongoDB\BSON\Binary;
219-
use MongoDB\Client;
220-
use MongoDB\Driver\ClientEncryption;
221-
222-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
223-
224-
$clientEncryptionOpts = [
225-
'keyVaultNamespace' => 'encryption.__keyVault',
226-
'kmsProviders' => [
227-
'local' => ['key' => $localKey],
228-
],
229-
];
230-
231-
$client = new Client();
232-
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
233-
234-
// Create an encryption key with an alternate name
235-
$keyId = $clientEncryption->createDataKey('local', ['keyAltNames' => ['altname']]);
236-
237-
Using an Encryption Key by Alternate Name
238-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239-
240242
To use an alternate name when referencing an encryption key, use the
241243
``keyAltName`` option instead of ``keyId``.
242244

@@ -263,9 +265,10 @@ To use an alternate name when referencing an encryption key, use the
263265
$collection = $client->selectCollection('test', 'coll');
264266
$collection->drop(); // clear old data
265267

266-
// Reference the encryption key we created earlier by its alternative name
268+
// Reference the encryption key created in the first example by its
269+
// alternative name
267270
$encryptionOpts = [
268-
'keyAltName' => 'altname',
271+
'keyAltName' => 'my-encryption-key',
269272
'algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
270273
];
271274
$encryptedValue = $clientEncryption->encrypt('123456789', $encryptionOpts);
@@ -363,5 +366,3 @@ query on the ``encryptedIndexed`` field.
363366
$unencryptedCollection = $client->selectCollection('test', 'coll');
364367

365368
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)