@Kushagra_Kesav Thanks for the reply!
const { MongoClient, Binary } = require("mongodb");
const { getCredentials } = require("./your_credentials");
const credentials = getCredentials();
// start-key-vault
const eDB = "encryption";
const eKV = "__keyVault";
const keyVaultNamespace = `${eDB}.${eKV}`;
// end-key-vault
// start-kmsproviders
const fs = require("fs");
const provider = "local";
const path = "./master-key.txt";
// WARNING: Do not use a local key file in a production application
const localMasterKey = fs.readFileSync(path);
const kmsProviders = {
local: {
key: localMasterKey,
},
};
// end-kmsproviders
async function run() {
// start-schema
const uri = credentials.MONGODB_URI;
const unencryptedClient = new MongoClient(uri);
await unencryptedClient.connect();
const keyVaultClient = unencryptedClient.db(eDB).collection(eKV);
const dek1 = await keyVaultClient.findOne({ keyAltNames: "dataKey1" });
const dek2 = await keyVaultClient.findOne({ keyAltNames: "dataKey2" });
const dek3 = await keyVaultClient.findOne({ keyAltNames: "dataKey3" });
const dek4 = await keyVaultClient.findOne({ keyAltNames: "dataKey4" });
const secretDB = "medicalRecords";
const secretCollection = "patients";
const encryptedFieldsMap = {
[`${secretDB}.${secretCollection}`]: {
fields: [
{
keyId: dek1._id,
path: "patientId",
bsonType: "int",
queries: { queryType: "equality" },
},
{
keyId: dek2._id,
path: "medications",
bsonType: "array",
},
{
keyId: dek3._id,
path: "patientRecord.ssn",
bsonType: "string",
queries: { queryType: "equality" },
},
{
keyId: dek4._id,
path: "patientRecord.billing",
bsonType: "object",
},
],
},
};
// end-schema
console.log("dekq",encryptedFieldsMap)
// start-extra-options
const extraOptions = {
cryptSharedLibPath: credentials["SHARED_LIB_PATH"],
};
// end-extra-options
// start-client
const encryptedClient = new MongoClient(uri, {
autoEncryption: {
keyVaultNamespace:keyVaultNamespace ,
kmsProviders :kmsProviders,
extraOptions : extraOptions,
encryptedFieldsMap:encryptedFieldsMap,
},
});
await encryptedClient.connect().then(async()=>{
try {
const unencryptedColl = unencryptedClient
.db(secretDB)
.collection(secretCollection);
// start-insert
const encryptedColl = await encryptedClient.db(secretDB).collection(secretCollection);
console.log("encryptedColl",encryptedColl)
await encryptedColl.insertOne({
firstName: "Jon",
lastName: "Doe",
patientId: 12345678,
address: "157 Electric Ave.",
patientRecord: {
ssn: "987-65-4320",
billing: {
type: "Visa",
number: "4111111111111111",
},
},
// medications: ["Atorvastatin", "Levothyroxine"],
},
);
// end-insert
// start-find
console.log("Finding a document with regular (non-encrypted) client.");
// console.log(await unencryptedColl.findOne({ firstName: /Jon/ }));
console.log(
"Finding a document with encrypted client, searching on an encrypted field"
);
// console.log(
// await encryptedColl.findOne({ "patientRecord.ssn": "987-65-4320" })
// );
// end-find
} finally {
await unencryptedClient.close();
await encryptedClient.close();
}
})
// end-client
}
run().catch(console.dir)
I am using MongoDB enterprise 6.0.6 and Node v16.17.1
Yes, I am following Quick Start — MongoDB Manual to implement this.
I am attempting it for first time.