Skip to content

Commit 8ab140f

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

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

docs/tutorial/client-side-encryption.txt

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

2063
.. note::
2164

2265
Auto encryption is an enterprise only feature.
2366

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

2872
.. code-block:: php
2973

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

3680
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
37-
3881
$encryptionOpts = [
3982
'keyVaultNamespace' => 'encryption.__keyVault',
4083
'kmsProviders' => [
@@ -43,13 +86,13 @@ encrypted on insertion and decrypted when querying on the client side.
4386
];
4487

4588
$client = new Client();
46-
$clientEncryption = $client->createClientEncryption($encryptionOpts);
4789

4890
$database = $client->selectDatabase('test');
4991
$database->dropCollection('coll'); // remove old data
5092

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

5497
$database->createCollection('coll', [
5598
'validator' => [
@@ -80,9 +123,8 @@ encrypted on insertion and decrypted when querying on the client side.
80123
Specifying an Explicit Schema for Encryption
81124
--------------------------------------------
82125

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.
126+
The following example uses the ``schemaMap`` encryption option to define
127+
encrypted fields.
86128

87129
.. note::
88130

@@ -101,18 +143,11 @@ encryption using the newly created key.
101143

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

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

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

117152
$autoEncryptionOpts = [
118153
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -148,10 +183,10 @@ encryption using the newly created key.
148183
Manually Encrypting and Decrypting Values
149184
-----------------------------------------
150185

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.
186+
In the MongoDB Community Edition, you will have to manually encrypt values
187+
before storing them in the database. The following example assumes that you have
188+
already created an encryption key in the key vault collection and explicitly
189+
encrypts and decrypts values in the document.
155190

156191
.. code-block:: php
157192

@@ -173,8 +208,9 @@ explicitly encrypts and decrypts values in the document.
173208
$client = new Client();
174209
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
175210

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

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

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-
240241
To use an alternate name when referencing an encryption key, use the
241242
``keyAltName`` option instead of ``keyId``.
242243

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

266-
// Reference the encryption key we created earlier by its alternative name
267+
// Reference the encryption key created in the first example by its
268+
// alternative name
267269
$encryptionOpts = [
268-
'keyAltName' => 'altname',
270+
'keyAltName' => 'my-encryption-key',
269271
'algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
270272
];
271273
$encryptedValue = $clientEncryption->encrypt('123456789', $encryptionOpts);
@@ -363,5 +365,3 @@ query on the ``encryptedIndexed`` field.
363365
$unencryptedCollection = $client->selectCollection('test', 'coll');
364366

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