I am new in MongoDB. I want to do client side field level encryption.
Here is the full project link which one I am practicing: https://javascript.plainenglish.io/mongodb-client-side-field-level-encryption-csfle-for-beginners-5eed965d4ba3
Requirments: What I have Installed.
MongoDB Enterprise server 6.0, npm i mongodb-client-encryption uuid-base64 mongodb
I am getting this error when running node app.js
app.js
const fs = require("fs");
const mongodb = require("mongodb");
const { MongoClient, Binary } = mongodb;
const base64KeyId = "fFGm1x/fRIWlWPLWhvf1Ig==";
const buffer = Buffer.from(base64KeyId, "base64");
const keyIdBinary = new Binary(buffer, Binary.SUBTYPE_UUID);
const JSONSchemaCreator = require("./schema-creator");
const jsonSchemas = JSONSchemaCreator(keyIdBinary);
const connectionString = "mongodb://localhost:27017/";
const keyVaultNamespace = "encryption.__keyVault";
const path = "./master-key.txt";
const localMasterKey = fs.readFileSync(path);
const kmsProviders = {
local: {
key: localMasterKey,
},
};
const patientSchema = {
"medicalRecords.patients": jsonSchemas,
};
const extraOptions = {
mongocryptdURI: "mongodb://localhost:27020",
};
const secureClient = new MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
autoEncryption: {
keyVaultNamespace,
kmsProviders,
schemaMap: patientSchema,
extraOptions: extraOptions,
},
});
async function insertPatient(name, bloodType, ssn) {
try {
await secureClient.connect();
const keyDB = secureClient.db("medicalRecords");
const collection = keyDB.collection("patients");
const writeResult = await collection.insertOne({
name,
ssn,
bloodType,
});
console.log(writeResult);
} catch (writeError) {
console.error("writeError occurred:", writeError);
}
}
insertPatient(
'Jon Doe',
"O+",
'1234567',
);
all of my server connection is perfect. ** error:**
writeError occurred: MongoServerError: BSON field 'insert.jsonSchema' is an unknown field. This command may be meant for a mongocryptd process.
at Connection.onMessage (/home/shahed/Documents/encryptionDemo/node_modules/mongodb/lib/cmap/connection.js:230:30)
at MessageStream.<anonymous> (/home/shahed/Documents/encryptionDemo/node_modules/mongodb/lib/cmap/connection.js:61:60)
at MessageStream.emit (node:events:513:28)
at processIncomingData (/home/shahed/Documents/encryptionDemo/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
at MessageStream._write (/home/shahed/Documents/encryptionDemo/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at Socket.ondata (node:internal/streams/readable:766:22)
at Socket.emit (node:events:513:28) {
ok: new Double(0.0),
code: new Int32(4662500),
codeName: 'Location4662500',
[Symbol(errorLabels)]: Set(0) {}
}
Please help me out and thanks in advance
I have a jsonschema format which one need create and perform encryption push into db
schema-creator.js
module.exports = function (keyId) {
return {
bsonType: "object",
encryptMetadata: {
keyId,
},
properties: {
bloodType: {
encrypt: {
bsonType: "string",
algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
},
},
ssn: {
encrypt: {
bsonType: "int",
algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
},
},
},
};
};