Skip to content

Commit 4705aae

Browse files
daprahamianmbroadst
authored andcommitted
docs(encryption): add driver-specific guide to client-side encryption
Fixes NODE-2265
1 parent 42f9e4d commit 4705aae

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
+++
2+
date = "2019-06-13T09:00:01+01:00"
3+
title = "Client Side Encryption"
4+
[menu.main]
5+
parent = "Reference"
6+
identifier = "Client Side Encryption"
7+
weight = 100
8+
pre = "<i class='fa'></i>"
9+
+++
10+
11+
# Client Side Encryption
12+
13+
New in MongoDB 4.2 client side encryption allows administrators and developers to encrypt specific data fields in
14+
addition to other MongoDB encryption features.
15+
16+
With field level encryption, developers can encrypt fields client side without any server-side
17+
configuration or directives. Client-side field level encryption supports workloads where applications must
18+
guarantee that unauthorized parties, including server administrators, cannot read the encrypted data.
19+
20+
## Installation
21+
22+
Using client side encryption requires installing the[`mongodb-client-encryption`](https://www.npmjs.com/package/mongodb-client-encryption) package:
23+
24+
```sh
25+
npm install mongodb-client-encryption
26+
```
27+
28+
### Prebuilds and manual compilation
29+
30+
`mongodb-client-encryption` has dependencies on the `libbson` and `libmongocrypt` C libraries, which means that a C++ toolchain is required in
31+
order to build the addon. In order to improve user experience we have leveraged the [`prebuild`](https://www.npmjs.com/package/prebuild) package
32+
to pre-compile the module during CI, so that in most cases you should be able to automatically install a pre-built version without needing to
33+
compile anything. However, if you are running on an architecture we have not created a prebuild for, or if you need to install without
34+
network access to github.com, you will need to manually build and compile these dependencies. Follow the instructions [here](https://github.com/mongodb/libmongocrypt/blob/master/README.md#building-libmongocrypt) for more information.
35+
36+
37+
### mongocryptd configuration
38+
39+
Client encryption requires the `mongocryptd` to function correctly. If the process has not started before encryption is requested, the driver
40+
will attempt to auto-spawn a `mongocrypt` instance. This means that `mongocrypt` should be in your PATH before running your application. You
41+
can also configure the driver to use an alternate URI to connect by setting the `autoEncryption.extraOptions.mongocryptdURI` to a valid
42+
connection string. There are more details on these options in the API documentation.
43+
44+
## Examples
45+
46+
The following is a sample app that assumes the **key** and **schema** have already been created in MongoDB. The example uses a local key,
47+
however using AWS Key Management Service is also an option. The data in the `encryptedField` field is automatically encrypted on the
48+
insert and decrypted when using find on the client side.
49+
50+
```js
51+
const { MongoClient } = require('mongodb');
52+
const crypto = require('crypto');
53+
54+
// This would have to be the same master key as was used to create the encryption key
55+
const localMasterKey = crypto.randomBytes(96);
56+
57+
const autoEncryption = {
58+
keyVaultNamespace: 'admin.datakeys',
59+
kmsProviders: {
60+
local: {
61+
key: localMasterKey
62+
}
63+
}
64+
};
65+
66+
const URL = 'mongodb://localhost:27017';
67+
const client = new MongoClient(URL, { autoEncryption, useUnifiedTopology: true });
68+
69+
main();
70+
71+
async function main() {
72+
try {
73+
await client.connect();
74+
75+
const db = mongoClient.db('test');
76+
await db.dropCollection('coll');
77+
78+
const collection = db.collection('coll');
79+
await collection.insertOne({ encryptedField: '123456789' });
80+
const result = await collection.findOne({});
81+
console.log(result);
82+
} finally {
83+
await client.close();
84+
}
85+
}
86+
```
87+
88+
{{% note %}}
89+
Auto encryption is an **enterprise** only feature.
90+
{{% /note %}}
91+
92+
The following example shows how to leverage a `ClientEncryption` instance to create a new key and use that key in the json schema map.
93+
94+
```js
95+
const crypto = require('crypto');
96+
const { MongoClient } = require('mongodb');
97+
const { ClientEncryption } = require('mongodb-client-encryption');
98+
99+
const keyVaultNamespace = 'admin.datakeys';
100+
// This would have to be the same master key as was used to create the encryption key
101+
const localMasterKey = crypto.randomBytes(96);
102+
const kmsProviders = { local: { key: localMasterKey } };
103+
104+
const URL = 'mongodb://localhost:27017';
105+
106+
main();
107+
108+
async function main() {
109+
const unencryptedClient = new MongoClient(URL, { useUnifiedTopology: true });
110+
try {
111+
await unencryptedClient.connect();
112+
const clientEncryption = new ClientEncryption(unencryptedClient, { kmsProviders, keyVaultNamespace });
113+
114+
const dataKeyId = await clientEncryption.createDataKey('local');
115+
116+
const dbName = 'test';
117+
const collName = 'coll';
118+
119+
const schemaMap = {
120+
[`${dbName}.${collName}`]: {
121+
properties: {
122+
encryptedField: {
123+
encrypt: {
124+
keyId: [ dataKeyId ],
125+
bsonType: 'string',
126+
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
127+
}
128+
}
129+
}
130+
}
131+
};
132+
133+
const encryptedClient = new MongoClient(URL, {
134+
useUnifiedTopology: true,
135+
autoEncryption: {
136+
keyVaultNamespace,
137+
kmsProviders,
138+
schemaMap
139+
}
140+
});
141+
142+
try {
143+
await encryptedClient.connect();
144+
// Do stuff here
145+
} finally {
146+
await encryptedClient.close();
147+
}
148+
} finally {
149+
await unencryptedClient.close();
150+
}
151+
}
152+
```
153+
154+
## Additional Resources
155+
- [Official Guide on Client-Side Encryption](https://docs.mongodb.com/manual/core/security-client-side-encryption/)

0 commit comments

Comments
 (0)