Skip to content

Commit 9f31307

Browse files
committed
PHPLIB-492: Create tutorial for client side encryption
1 parent 1ba76bf commit 9f31307

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

docs/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Tutorials
1010
/tutorial/commands
1111
/tutorial/custom-types
1212
/tutorial/decimal128
13+
/tutorial/fle
1314
/tutorial/gridfs
1415
/tutorial/indexes
1516
/tutorial/tailable-cursor

docs/tutorial/fle.txt

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
======================
2+
Client Side Encryption
3+
======================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Cliend Side Encryption allows administrators and developers to encrypt specific
14+
data fields in addition to other MongoDB encryption features.
15+
16+
17+
Automatic encryption and decryption
18+
-----------------------------------
19+
20+
.. note::
21+
22+
Auto encryption is an enterprise only feature.
23+
24+
The following example assumes the key and schema have already been created in
25+
MongoDB. The example uses a local key, however using AWS Key Management Service
26+
is also an option. The data in the ``encryptedField`` field is automatically
27+
encrypted on the insert and decrypted when using find on the client side.
28+
29+
.. code-block:: php
30+
31+
<?php
32+
33+
use MongoDB\BSON\Binary;
34+
use MongoDB\Client;
35+
36+
$localKey = new Binary('<binary key data>', Binary::TYPE_GENERIC);
37+
38+
$autoEncryptionOpts = [
39+
'keyVaultNamespace' => 'admin.datakeys',
40+
'kmsProviders' => [
41+
'local' => ['key' => $localKey],
42+
],
43+
];
44+
45+
$encryptedClient = new Client(null, [], ['autoEncryption' => $autoEncryptionOpts]);
46+
47+
$collection = $encryptedClient->selectCollection('test', 'coll');
48+
$collection->drop(); // clear old data
49+
50+
$collection->insertOne(['encryptedField' => '123456789']);
51+
52+
var_dump($collection->findOne([]));
53+
54+
55+
Specifying an explicit schema for encryption
56+
--------------------------------------------
57+
58+
The following example shows how to create a new key and store it in the key
59+
vault collection. The encrypted client configures an explicit schema for
60+
encryption using the newly created key.
61+
62+
.. note::
63+
64+
Supplying a ```schemaMap``` provides more security than relying on JSON
65+
schemas obtained from the server. It protects against a malicious server
66+
advertising a false JSON schema, which could trick the client into sending
67+
unencrypted data that should be encrypted.
68+
69+
.. code-block:: php
70+
71+
<?php
72+
73+
use MongoDB\BSON\Binary;
74+
use MongoDB\Client;
75+
use MongoDB\Driver\ClientEncryption;
76+
77+
$localKey = new Binary('<binary key data>', Binary::TYPE_GENERIC);
78+
79+
$clientEncryptionOpts = [
80+
'keyVaultNamespace' => 'admin.datakeys',
81+
'kmsProviders' => [
82+
'local' => ['key' => $localKey],
83+
],
84+
];
85+
86+
$client = new Client();
87+
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
88+
89+
// Create new key in the key vault and store its ID for later use
90+
$keyId = $clientEncryption->createDataKey('local');
91+
92+
$autoEncryptionOpts = [
93+
'keyVaultNamespace' => 'admin.datakeys',
94+
'kmsProviders' => [
95+
'local' => ['key' => $localKey],
96+
],
97+
'schemaMap' => [
98+
'test.coll' => [
99+
'bsonType' => 'object',
100+
'properties' => [
101+
'encryptedField' => [
102+
'encrypt' => [
103+
'keyId' => [$keyId],
104+
'bsonType' => 'string',
105+
'algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
106+
],
107+
],
108+
],
109+
],
110+
],
111+
];
112+
113+
$encryptedClient = new Client(null, [], ['autoEncryption' => $autoEncryptionOpts]);
114+
115+
$collection = $encryptedClient->selectCollection('test', 'coll');
116+
$collection->drop(); // clear old data
117+
118+
$collection->insertOne(['encryptedField' => '123456789']);
119+
120+
var_dump($collection->findOne([]));
121+
122+
123+
Manually encrypting and decrypting values
124+
-----------------------------------------
125+
126+
In the Community edition of MongoDB, you will have to manually encrypt and
127+
decrypt values before storing them in the database. The following example
128+
assumes that you have already created an encryption key in the key vault
129+
collection and explicitly encrypts and decrypts values in the document.
130+
131+
.. code-block:: php
132+
133+
<?php
134+
135+
use MongoDB\BSON\Binary;
136+
use MongoDB\Client;
137+
use MongoDB\Driver\ClientEncryption;
138+
139+
$localKey = new Binary('<binary key data>', Binary::TYPE_GENERIC);
140+
$keyId = new Binary('<UUID of encryption key', Binary::TYPE_UUID);
141+
142+
$clientEncryptionOpts = [
143+
'keyVaultNamespace' => 'admin.datakeys',
144+
'kmsProviders' => [
145+
'local' => ['key' => $localKey],
146+
],
147+
];
148+
149+
$client = new Client();
150+
$clientEncryption = $client->createClientEncryption($clientEncryptionOpts);
151+
152+
$collection = $client->selectCollection('test', 'coll');
153+
$collection->drop(); // clear old data
154+
155+
$encryptionOpts = [
156+
'keyId' => $keyId,
157+
'algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
158+
];
159+
$encryptedValue = $clientEncryption->encrypt('123456789', $encryptionOpts);
160+
161+
$collection->insertOne(['encryptedField' => $encryptedValue]);
162+
163+
$document = $collection->findOne();
164+
var_dump($clientEncryption->decrypt($document->encryptedField));

0 commit comments

Comments
 (0)