Skip to content

Commit ee70e32

Browse files
authored
DOCSP-23618 adds mongosh quickstart (via includes) (#1905) (#1968)
* DOCSP-23618 includes updates * adds js files for literalincludes
1 parent 0a71874 commit ee70e32

File tree

16 files changed

+1360
-0
lines changed

16 files changed

+1360
-0
lines changed

source/core/queryable-encryption/quick-start.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Before You Get Started
4646

4747
.. tabs::
4848

49+
.. tab:: mongosh
50+
:tabid: shell
51+
52+
`Complete mongosh Application <{+sample-app-url-qe+}/mongosh/local/reader/>`__
53+
4954
.. tab:: Node.js
5055
:tabid: nodejs
5156

@@ -96,6 +101,12 @@ Procedure
96101

97102
.. tabs-drivers::
98103

104+
.. tab::
105+
:tabid: shell
106+
107+
To view the complete code for making a {+dek-long+}, see
108+
`our Github repository <{+sample-app-url-qe+}/mongosh/local/reader/make_data_key.js>`__.
109+
99110
.. tab::
100111
:tabid: nodejs
101112

@@ -139,6 +150,12 @@ Procedure
139150

140151
.. tabs-drivers::
141152

153+
.. tab::
154+
:tabid: shell
155+
156+
To view the complete code for inserting an encrypted document, see
157+
`our Github repository <{+sample-app-url-qe+}/mongosh/local/reader/insert_encrypted_document.js>`__.
158+
142159
.. tab::
143160
:tabid: nodejs
144161

@@ -178,6 +195,12 @@ Procedure
178195

179196
.. tabs-drivers::
180197

198+
.. tab::
199+
:tabid: shell
200+
201+
To view the complete code for finding an encrypted document, see
202+
`our Github repository <{+sample-app-url-qe+}/mongosh/local/reader/insert_encrypted_document.js>`__.
203+
181204
.. tab::
182205
:tabid: nodejs
183206

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// start-key-vault
2+
const keyVaultDB = "encryption";
3+
const keyVaultColl = "__keyVault";
4+
const keyVaultNamespace = `${keyVaultDB}.${keyVaultColl}`;
5+
const secretDB = "medicalRecords";
6+
const secretCollection = "patients";
7+
// end-key-vault
8+
9+
// start-kmsproviders
10+
const kmsProviders = {
11+
aws: {
12+
accessKeyId: "<Your AWS Access Key ID>",
13+
secretAccessKey: "<Your AWS Secret Access Key>",
14+
},
15+
};
16+
// end-kmsproviders
17+
18+
async function run() {
19+
// start-schema
20+
const uri = "<Your Connection String>";
21+
const unencryptedClient = Mongo(uri);
22+
const autoEncryptionOpts = { kmsProviders, keyVaultNamespace };
23+
24+
const encClient = Mongo(uri, autoEncryptionOpts);
25+
const keyVault = encClient.getKeyVault();
26+
const keyVaultClient = unencryptedClient
27+
.getDB(keyVaultDB)
28+
.getCollection(keyVaultColl);
29+
30+
const dek1 = keyVaultClient.findOne({ keyAltNames: "dataKey1" });
31+
const dek2 = keyVaultClient.findOne({ keyAltNames: "dataKey2" });
32+
const dek3 = keyVaultClient.findOne({ keyAltNames: "dataKey3" });
33+
const dek4 = keyVaultClient.findOne({ keyAltNames: "dataKey4" });
34+
35+
const secretDB = "medicalRecords";
36+
const secretColl = "patients";
37+
38+
const encryptedFieldsMap = {
39+
[`${secretDB}.${secretColl}`]: {
40+
fields: [
41+
{
42+
keyId: dek1._id,
43+
path: "patientId",
44+
bsonType: "int",
45+
queries: { queryType: "equality" },
46+
},
47+
{
48+
keyId: dek2._id,
49+
path: "medications",
50+
bsonType: "array",
51+
},
52+
{
53+
keyId: dek3._id,
54+
path: "patientRecord.ssn",
55+
bsonType: "string",
56+
queries: { queryType: "equality" },
57+
},
58+
{
59+
keyId: dek4._id,
60+
path: "patientRecord.billing",
61+
bsonType: "object",
62+
},
63+
],
64+
},
65+
};
66+
// end-schema
67+
68+
// start-extra-options
69+
// end-extra-options
70+
71+
// start-client
72+
const autoEncryptionOptions = {
73+
keyVaultNamespace: keyVaultNamespace,
74+
kmsProviders: kmsProviders,
75+
bypassQueryAnalysis: false,
76+
encryptedFieldsMap: encryptedFieldsMap,
77+
};
78+
79+
const encryptedClient = Mongo(uri, autoEncryptionOptions);
80+
const encryptedColl = encryptedClient
81+
.getDB(secretDB)
82+
.getCollection(secretColl);
83+
const unencryptedColl = unencryptedClient
84+
.getDB(secretDB)
85+
.getCollection(secretColl);
86+
// end-client
87+
88+
try {
89+
// start-insert
90+
encryptedColl.insertOne({
91+
firstName: "Jon",
92+
lastName: "Doe",
93+
patientId: 12345678,
94+
address: "157 Electric Ave.",
95+
patientRecord: {
96+
ssn: "987-65-4320",
97+
billing: {
98+
type: "Visa",
99+
number: "4111111111111111",
100+
},
101+
},
102+
medications: ["Atorvastatin", "Levothyroxine"],
103+
});
104+
// end-insert
105+
106+
// start-find
107+
console.log("Finding a document with regular (non-encrypted) client.");
108+
console.log(unencryptedColl.findOne({ firstName: /Jon/ }));
109+
console.log(
110+
"Finding a document with encrypted client, searching on an encrypted field"
111+
);
112+
console.log(encryptedColl.findOne({ "patientRecord.ssn": "987-65-4320" }));
113+
// end-find
114+
} catch (error) {
115+
console.log(error);
116+
throw new Error(error);
117+
}
118+
}
119+
120+
run().catch(console.dir);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
const keyVaultDatabase = "encryption";
2+
const keyVaultCollection = "__keyVault";
3+
const keyVaultNamespace = `${keyVaultDatabase}.${keyVaultCollection}`;
4+
const secretDB = "medicalRecords";
5+
const secretCollection = "patients";
6+
7+
// start-kmsproviders
8+
const provider = "aws";
9+
const kmsProviders = {
10+
aws: {
11+
accessKeyId: "<Your AWS Access Key ID>",
12+
secretAccessKey: "<Your AWS Secret Access Key>",
13+
},
14+
};
15+
// end-kmsproviders
16+
17+
// start-datakeyopts
18+
const masterKey = {
19+
key: "<Your AWS Key ARN>",
20+
region: "<Your AWS Key Region>",
21+
};
22+
// end-datakeyopts
23+
24+
async function run() {
25+
// start-create-index
26+
const uri = "<Your Connection String>";
27+
const keyVaultClient = Mongo(uri);
28+
const keyVaultDB = keyVaultClient.getDB(keyVaultDatabase);
29+
// Drop the Key Vault Collection in case you created this collection
30+
// in a previous run of this application.
31+
keyVaultDB.dropDatabase();
32+
keyVaultDB.createCollection(keyVaultCollection);
33+
34+
const keyVaultColl = keyVaultDB.getCollection(keyVaultCollection);
35+
keyVaultColl.createIndex(
36+
{ keyAltNames: 1 },
37+
{
38+
unique: true,
39+
partialFilterExpression: { keyAltNames: { $exists: true } },
40+
}
41+
);
42+
// end-create-index
43+
44+
// start-create-dek
45+
const autoEncryptionOpts = {
46+
keyVaultNamespace: keyVaultNamespace,
47+
kmsProviders: kmsProviders,
48+
};
49+
50+
// start-create-dek
51+
const encClient = Mongo(uri, autoEncryptionOpts);
52+
const keyVault = encClient.getKeyVault();
53+
54+
const dek1 = keyVault.createKey(provider, {
55+
masterKey: masterKey,
56+
keyAltNames: ["dataKey1"],
57+
});
58+
const dek2 = keyVault.createKey(provider, {
59+
masterKey: masterKey,
60+
keyAltNames: ["dataKey2"],
61+
});
62+
const dek3 = keyVault.createKey(provider, {
63+
masterKey: masterKey,
64+
keyAltNames: ["dataKey3"],
65+
});
66+
const dek4 = keyVault.createKey(provider, {
67+
masterKey: masterKey,
68+
keyAltNames: ["dataKey4"],
69+
});
70+
// end-create-dek
71+
72+
// start-create-enc-collection
73+
const encryptedFieldsMap = {
74+
[`${secretDB}.${secretCollection}`]: {
75+
fields: [
76+
{
77+
keyId: dek1,
78+
path: "patientId",
79+
bsonType: "int",
80+
queries: { queryType: "equality" },
81+
},
82+
{
83+
keyId: dek2,
84+
path: "medications",
85+
bsonType: "array",
86+
},
87+
{
88+
keyId: dek3,
89+
path: "patientRecord.ssn",
90+
bsonType: "string",
91+
queries: { queryType: "equality" },
92+
},
93+
{
94+
keyId: dek4,
95+
path: "patientRecord.billing",
96+
bsonType: "object",
97+
},
98+
],
99+
},
100+
};
101+
102+
try {
103+
const autoEncryptionOptions = {
104+
keyVaultNamespace: keyVaultNamespace,
105+
kmsProviders: kmsProviders,
106+
encryptedFieldsMap: encryptedFieldsMap,
107+
};
108+
109+
const encClient = Mongo(uri, autoEncryptionOptions);
110+
const newEncDB = encClient.getDB(secretDB);
111+
// Drop the encrypted collection in case you created this collection
112+
// in a previous run of this application.
113+
newEncDB.dropDatabase();
114+
newEncDB.createCollection(secretCollection);
115+
console.log("Created encrypted collection!");
116+
// end-create-enc-collection
117+
} catch (error) {
118+
console.log(error);
119+
throw new Error(error);
120+
}
121+
}
122+
123+
run().catch(console.dir);

0 commit comments

Comments
 (0)