I am implementing manual client-side encryption of specific properties in my document. The MongoDB documentation suggests that I can specify a schema that can be used to carry out server-side validation, I’m trying to set this up in order to ensure that the properties are correctly encrypted by the client code.
I’m specifying the schema when the collection is being created.
var schemaMap = new Dictionary<string, BsonDocument> { { collectionNamespace.CollectionName, schema } };
var options = new AutoEncryptionOptions(
keyVaultNamespace,
kmsProviders,
schemaMap: schemaMap,
bypassAutoEncryption: true);
var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.AutoEncryptionOptions = options;
var client = new MongoClient(settings);
var encryptedDatabase = client.GetDatabase(collectionNamespace.DatabaseNamespace.DatabaseName);
encryptedDatabase.CreateCollection(collectionNamespace.CollectionName);
And then again when creating the MongoDB Client that is used to Insert
the documents.
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
var autoEncryptionOptions = new AutoEncryptionOptions(
keyVaultNamespace: keyVaultNamespace,
kmsProviders: kmsProviders,
schemaMap: schemaMap,
bypassAutoEncryption: true
);
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
var secureClient = new MongoClient(clientSettings);
When I then insert a document that intentionally doesn’t comply with the configured schema, there is no exceptions thrown to identify the issue.
secureCollection.InsertOne(sampleDocument);
The schema being used is based on the MongoDB example code
I’m working with a Azure Cosmos DB for MongoDB account, server version 4.2. The MongoDB.Driver version is 2.20.0.
Have I mis-understood what the schema validation is capable of doing? Or have I missed a step?
Any pointers would be useful, thanks.
2
Answers
Whilst the original attempt at schema validation using
AutoEncryptionOptions
was incorrect (as helpfully pointed out by @dododo) further investigation into how to implement server-side schema validation led to defining aValidator
when creating a Collection.This approach still didn't work, but this is due to the database being used, rather than the implementation.The solution being worked on is using the MongoDB API to access an Azure CosmosDB. An answer from Microsoft states that this functionality isn't supported by CosmosDB
Closer examination of the Microsoft Azure Cosmos DB for MongoDB vs MongoDB Atlas documentation reveals that 'JSON schema for data governance controls' is 'Currently in development'
See below:
bypassAutoEncryption=true
, then you skip all encryption logic, so only decryption could happen if the field was encrypted previously.schemaMap
is to provide a path to the encrypted field, it doesn’t validate the document and if the path leads to not existed field – then the whole encryption logic is skipped.