-
Notifications
You must be signed in to change notification settings - Fork 4
/
clients.js
74 lines (64 loc) · 2.12 KB
/
clients.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { readMasterKey, CsfleHelper } = require("./helpers");
const { connectionString, dataKey } = require("./config");
const localMasterKey = readMasterKey()
const csfleHelper = new CsfleHelper({
// The client expects a key management system to store and provide the application's master encryption key. For now, we will use a local master key, so they use the local KMS provider.
kmsProviders: {
local: {
key: localMasterKey
}
},
connectionString,
})
async function main() {
let regularClient = await csfleHelper.getRegularClient()
let schemeMap = csfleHelper.createJsonSchemaMap(dataKey)
let csfleClient = await csfleHelper.getCsfleEnabledClient(schemeMap)
let exampleDocument = {
name: "Jon Doe",
ssn: 241014209,
bloodType: "AB+",
medicalRecords: [
{
weight: 180,
bloodPressure: "120/80"
}
],
insurance: {
provider: "MaestCare",
policyNumber: 123142
}
}
const regularClientPatientsColl = regularClient
.db("medicalRecords")
.collection("patients")
const csfleClientPatientsColl = csfleClient
.db("medicalRecords")
.collection("patients")
// Performs the insert operation with the csfle-enabled client
// We're using an update with an upsert so that subsequent runs of this script
// don't insert new documents
await csfleClientPatientsColl.updateOne(
{ ssn: exampleDocument["ssn"] },
{ $set: exampleDocument },
{ upsert: true }
)
// Performs a read using the encrypted client, querying on an encrypted field
const csfleFindResult = await csfleClientPatientsColl.findOne({
ssn: exampleDocument["ssn"]
})
console.log(
"Document retreived with csfle enabled client:\n",
csfleFindResult
)
// Performs a read using the regular client. We must query on a field that is
// not encrypted.
// Try - query on the ssn field. What is returned?
const regularFindResult = await regularClientPatientsColl.findOne({
name: "Jon Doe"
})
console.log("Document retreived with regular client:\n", regularFindResult)
await regularClient.close()
await csfleClient.close()
}
main().catch(console.dir)