Skip to content

Commit 08b506a

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-6986): Create and document Node.js implementation of FLE
* created node.js snippets for CSFLE * added create a master key node step * changed variable name fileBytes to localMasterKey on node.js generate a local master key section * switched process.env.MONGO_URL -> connectionString * fixed spacing * fixed spacing * fixed spacing * fixed spacing and changed process.env.MONGO_URL -> connectionString * Fixed comment style * fixed single quotes -> double quotes & spacing * removed keyAltNames * switched more information tab to bottom of page & made it hidden * added Node.js data key generation example link * added specify location of encryption binary step for node.js * fixed RST typo * added helper code for js schema gen * made wording fixes * made wording fixes
1 parent cbcd304 commit 08b506a

File tree

4 files changed

+207
-13
lines changed

4 files changed

+207
-13
lines changed

source/includes/steps-create-data-encryption-key.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ content: |
2424
}
2525
2626
final byte[] localMasterKey = Arrays.copyOf(fileBytes, 96);
27+
.. tab::
28+
:tabid: nodejs
29+
30+
.. code-block:: javascript
31+
32+
const path = "./master-key.txt";
33+
let localMasterKey;
34+
fs.readFile(path, (err, data) => {
35+
if (err) throw err;
36+
localMasterKey = data;
37+
});
38+
2739
---
2840
title: Specify KMS Provider Settings
2941
ref: specify-kms-provider-settings
@@ -48,6 +60,16 @@ content: |
4860
put("key", localMasterKey);
4961
}});
5062
}};
63+
.. tab::
64+
:tabid: nodejs
65+
66+
.. code-block:: javascript
67+
68+
const kmsProviders = {
69+
local: {
70+
key: localMasterKey
71+
}
72+
}
5173
---
5274
title: Create a Data Encryption Key
5375
ref: create-a-data-encryption-key
@@ -77,6 +99,32 @@ content: |
7799
.build();
78100
79101
ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
102+
.. tab::
103+
:tabid: nodejs
104+
105+
.. code-block:: javascript
106+
107+
const connectionString = "mongodb://localhost:27017";
108+
const keyVaultNamespace = "encryption.__keyVault";
109+
const client = new MongoClient(connectionString, {
110+
useNewUrlParser: true,
111+
useUnifiedTopology: true
112+
});
113+
114+
client.connect().then((clientConnection)=>{
115+
const encryption = new ClientEncryption(client, {
116+
keyVaultNamespace,
117+
kmsProviders
118+
});
119+
encryption.createDataKey('local', (err, key) => {
120+
if (err) {
121+
console.log("dataKey creation error", err);
122+
} else {
123+
console.log("dataKey created", key)
124+
}
125+
})
126+
// ...
127+
})
80128
81129
.. note::
82130

source/includes/steps-fle-configure-the-mongodb-client.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ content: |
1313
.. code-block:: java
1414
1515
String keyVaultNamespace = "encryption.__keyVault";
16+
.. tab::
17+
:tabid: nodejs
18+
19+
.. code-block:: javascript
20+
21+
const keyVaultNamespace = "encryption.__keyVault";
1622
---
1723
title: Specify the Local Master Encryption Key
1824
ref: specify-the-local-master-encryption-key
@@ -35,6 +41,17 @@ content: |
3541
put("key", localMasterKey);
3642
}});
3743
}};
44+
.. tab::
45+
:tabid: nodejs
46+
47+
.. code-block:: javascript
48+
49+
const kmsProviders = {
50+
local: {
51+
key: localMasterKey
52+
}
53+
}
54+
3855
---
3956
title: Map the JSON Schema to the Patients Collection
4057
ref: map-the-json-schema-to-the-patients-collection
@@ -54,6 +71,15 @@ content: |
5471
HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>() {{
5572
put("medicalRecords.patients", BsonDocument.parse(jsonSchema));
5673
}}
74+
.. tab::
75+
:tabid: nodejs
76+
77+
.. code-block:: javascript
78+
79+
const dataNamespace = "medicalRecords.patients";
80+
const patientSchema = {
81+
[dataNamespace]: jsonSchema
82+
}
5783
---
5884
title: Specify the Location of the Encryption Binary
5985
ref: specify-the-location-of-the-encryption-binary
@@ -88,6 +114,29 @@ content: |
88114
final Map<String, Object> extraOptions = new HashMap<String, Object>() {{
89115
put("mongocryptdBypassSpawn", true);
90116
}};
117+
.. tab::
118+
:tabid: nodejs
119+
120+
.. code-block:: javascript
121+
:emphasize-lines: 2
122+
123+
const extraOptions = {
124+
mongocryptdPath: "/usr/local/bin/mongocryptd";
125+
}
126+
127+
.. admonition:: Encryption Binary Daemon
128+
:class: note
129+
130+
If the ``mongocryptd`` daemon is already running, you can
131+
configure the client to skip starting it by passing the
132+
following option:
133+
134+
.. code-block:: javascript
135+
:emphasize-lines: 2
136+
137+
const extraOptions = {
138+
mongocryptdBypassSpawn: true;
139+
}
91140
---
92141
title: Create the MongoClient
93142
ref: create-the-mongoclient
@@ -115,4 +164,20 @@ content: |
115164
.build();
116165
117166
MongoClient mongoClient = MongoClients.create(clientSettings);
167+
.. tab::
168+
:tabid: nodejs
169+
170+
.. code-block:: javascript
171+
172+
const secureClient = new MongoClient(connectionString, {
173+
useNewUrlParser: true,
174+
useUnifiedTopology: true,
175+
monitorCommands: true,
176+
autoEncryption: {
177+
keyVaultNamespace,
178+
kmsProviders,
179+
schemaMap: patientSchema,
180+
extraOptions: extraOptions
181+
}
182+
});
118183
...

source/includes/steps-fle-convert-to-a-remote-master-key.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ content: |
5858
put("secretAccessKey", awsSecretAccessKey);
5959
}});
6060
}};
61+
.. tab::
62+
:tabid: nodejs
63+
64+
.. code-block:: javascript
65+
66+
kmsProviders = {
67+
aws: {
68+
accessKeyId: "<IAM User Access Key ID>",
69+
secretAccessKey: "<IAM User Secret Access Key>",
70+
}
71+
}
6172
---
6273
title: Create a New Data Key
6374
ref: create-a-new-data-key
@@ -97,6 +108,21 @@ content: |
97108
98109
BsonBinary dataKeyId = clientEncryption.createDataKey("aws", dataKeyOptions);
99110
String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
111+
.. tab::
112+
:tabid: nodejs
113+
114+
.. code-block:: javascript
115+
116+
const encryption = new ClientEncryption(client, {
117+
keyVaultNamespace,
118+
kmsProviders
119+
});
120+
encryption.createDataKey("aws", {
121+
masterKey: {
122+
key: "<Master Key ARN>", // e.g. "arn:aws:kms:us-east-2:111122223333:alias/test-key"
123+
region: "<Master Key AWS Region>", // e.g. "us-east-1"
124+
}
125+
})
100126
---
101127
title: Update the JSON Schema
102128
ref: update-the-json-schema

source/use-cases/sensitive-data-encryption.txt

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,6 @@ compliant to data privacy regulations with MongoDB.
142142
Procedure
143143
---------
144144

145-
.. tabs-drivers::
146-
147-
.. tab::
148-
:tabid: java-sync
149-
150-
For more information, including 'full examples, see the `Client
151-
Side Encryption
152-
<https://mongodb.github.io/mongo-java-driver/3.11/driver/tutorials/client-side-encryption/>`_
153-
page in the official Java driver documentation.
154-
155-
156145
Requirements
157146
~~~~~~~~~~~~
158147

@@ -244,6 +233,23 @@ To begin development, MedcoMD engineers generate a local master key:
244233
}
245234
}
246235
}
236+
.. tab::
237+
:tabid: nodejs
238+
239+
The following script generates a 96-byte local master key and
240+
saves it to a file called ``master-key.txt`` in the directory
241+
from which the script is executed.
242+
243+
.. code-block:: javascript
244+
245+
const fs = require('fs');
246+
const secureRandom = require('secure-random');
247+
248+
const wstream = fs.createWriteStream('master-key.txt');
249+
const data = secureRandom.randomUint8Array(96);
250+
wstream.write(data);
251+
wstream.end();
252+
247253

248254
.. _fle-create-a-data-encryption-key:
249255

@@ -272,9 +278,16 @@ local master key.
272278
.. tab::
273279
:tabid: java-sync
274280

275-
You can also download the `complete code example on GitHub
281+
You can also download the `complete Java data key generation code
282+
example on GitHub
276283
<https://raw.githubusercontent.com/mongodb/docs-assets/DOCSP-csfle-data-encryption-key/DataEncryptionKeyGenerator.java>`_.
277-
284+
.. tab::
285+
:tabid: nodejs
286+
287+
You can also download the `complete Node.js data key generation
288+
code example on GitHub
289+
<https://raw.githubusercontent.com/mongodb/docs-assets/DOCSP-csfle-data-encryption-key/DataEncryptionKeyGenerator.js>`_.
290+
278291
.. include:: /includes/steps/create-data-encryption-key.rst
279292

280293
.. _fle-define-a-json-schema:
@@ -466,6 +479,11 @@ full `JSON Schema for the Medco Medical Management System
466479

467480
View the `helper code in Java
468481
<https://gist.github.com/ccho-mongodb/088176b1bed3b9e54cdc0c2c3c537d1b>`_.
482+
.. tab::
483+
:tabid: nodejs
484+
485+
View the `helper code in Javascript
486+
<https://gist.github.com/mongomoe/a94c59a8e3acdfc96b82d76eb5ea654d>`_.
469487

470488
D. Create the MongoDB Client
471489
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -516,6 +534,30 @@ MedcoMD engineers write a function to create a new patient record:
516534
.append("insurance", insurance);
517535
collection.insertOne(patient);
518536
}
537+
.. tab::
538+
:tabid: nodejs
539+
540+
.. code-block:: javascript
541+
542+
function insertPatient(collection, name, ssn, bloodType, medicalRecords, policyNumber, provider) {
543+
collection.insertOne({
544+
name: name,
545+
ssn: ssn,
546+
bloodType: bloodType,
547+
medicalRecords: medicalRecords,
548+
insurance: {
549+
policyNumber: policyNumber,
550+
provider: provider
551+
}
552+
})
553+
.then((writeResult) => {
554+
console.log('writeResult: \t', writeResult);
555+
})
556+
.catch((writeError) => {
557+
console.log('writeError occurred: \t', writeError);
558+
})
559+
}
560+
519561

520562
When a CSFLE-enabled client inserts a new patient record into the Medical Care
521563
Management System, it automatically encrypts the fields. This operation
@@ -688,3 +730,16 @@ check out the reference docs in the server manual:
688730
- :manual:`Client-Side Field Level Encryption </core/security-client-side-encryption>`
689731
- :manual:`Automatic Encryption JSON Schema Syntax </reference/security-client-side-automatic-json-schema>`
690732
- :manual:`Manage Client-Side Encryption Data Keys </tutorial/manage-client-side-encryption-data-keys>`
733+
734+
735+
.. tabs-drivers::
736+
:hidden: true
737+
738+
.. tab::
739+
:tabid: java-sync
740+
741+
For additional information on CSFLE, see the `official Java driver documentation <https://mongodb.github.io/mongo-java-driver/3.11/driver/tutorials/client-side-encryption/>`_
742+
.. tab::
743+
:tabid: nodejs
744+
745+
For additional information on CSFLE, see the `official Node.js driver documentation <https://www.npmjs.com/package/mongodb-client-encryption>`_

0 commit comments

Comments
 (0)