@@ -14,16 +14,61 @@ Client-Side Field Level Encryption allows administrators and developers to
14
14
encrypt specific data fields in addition to other MongoDB encryption features.
15
15
16
16
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
+
17
61
Automatic Encryption and Decryption
18
62
-----------------------------------
19
63
20
64
.. note::
21
65
22
66
Auto encryption is an enterprise only feature.
23
67
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.
27
72
28
73
.. code-block:: php
29
74
@@ -34,7 +79,6 @@ encrypted on insertion and decrypted when querying on the client side.
34
79
use MongoDB\Driver\ClientEncryption;
35
80
36
81
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
37
-
38
82
$encryptionOpts = [
39
83
'keyVaultNamespace' => 'encryption.__keyVault',
40
84
'kmsProviders' => [
@@ -43,13 +87,13 @@ encrypted on insertion and decrypted when querying on the client side.
43
87
];
44
88
45
89
$client = new Client();
46
- $clientEncryption = $client->createClientEncryption($encryptionOpts);
47
90
48
91
$database = $client->selectDatabase('test');
49
92
$database->dropCollection('coll'); // remove old data
50
93
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
+ // a configuration file.
96
+ $keyId = readDataKey();
53
97
54
98
$database->createCollection('coll', [
55
99
'validator' => [
@@ -80,9 +124,8 @@ encrypted on insertion and decrypted when querying on the client side.
80
124
Specifying an Explicit Schema for Encryption
81
125
--------------------------------------------
82
126
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.
86
129
87
130
.. note::
88
131
@@ -101,18 +144,11 @@ encryption using the newly created key.
101
144
102
145
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
103
146
104
- $clientEncryptionOpts = [
105
- 'keyVaultNamespace' => 'encryption.__keyVault',
106
- 'kmsProviders' => [
107
- 'local' => ['key' => $localKey],
108
- ],
109
- ];
110
-
111
147
$client = new Client();
112
- $clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
113
148
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
+ // a configuration file.
151
+ $keyId = readDataKey();
116
152
117
153
$autoEncryptionOpts = [
118
154
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -148,10 +184,10 @@ encryption using the newly created key.
148
184
Manually Encrypting and Decrypting Values
149
185
-----------------------------------------
150
186
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.
155
191
156
192
.. code-block:: php
157
193
@@ -173,8 +209,9 @@ explicitly encrypts and decrypts values in the document.
173
209
$client = new Client();
174
210
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
175
211
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
+ // a configuration file.
214
+ $keyId = readDataKey();
178
215
179
216
$collection = $client->selectCollection('test', 'coll');
180
217
$collection->drop(); // clear old data
@@ -196,12 +233,15 @@ Referencing Encryption Keys by an Alternative Name
196
233
197
234
While it is possible to create an encryption key every time data is encrypted,
198
235
this is not the recommended approach. Instead, you should create your encryption
199
- keys depending on your use- case, e.g. by creating a user-specific encryption
236
+ keys depending on your use case, e.g. by creating a user-specific encryption
200
237
key. To reference keys in your software, you can use the keyAltName attribute
201
238
specified when creating the key. The following example creates an encryption key
202
239
with an alternative name, which could be done when deploying the application.
203
240
The software then encrypts data by referencing the key by its alternative name.
204
241
242
+ To use an alternate name when referencing an encryption key, use the
243
+ ``keyAltName`` option instead of ``keyId``.
244
+
205
245
.. code-block:: php
206
246
207
247
<?php
@@ -222,16 +262,13 @@ The software then encrypts data by referencing the key by its alternative name.
222
262
$client = new Client();
223
263
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
224
264
225
- // Create an encryption key with an alternative name. This could be done when
226
- // deploying the application
227
- $keyId = $clientEncryption->createDataKey('local', ['keyAltNames' => ['altname']]);
228
-
229
265
$collection = $client->selectCollection('test', 'coll');
230
266
$collection->drop(); // clear old data
231
267
232
- // 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
233
270
$encryptionOpts = [
234
- 'keyAltName' => 'altname ',
271
+ 'keyAltName' => 'my-encryption-key ',
235
272
'algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
236
273
];
237
274
$encryptedValue = $clientEncryption->encrypt('123456789', $encryptionOpts);
0 commit comments