This repository provides a code sample in .NET and Java on how to use MongoDB Client-Side Field Level Encryption with Azure Cosmos DB Mongo API 4.2 (Supported features for version 4.2).
- MongoDB Explict Encryption
- MongoDB Automatic Decryption
- MongoDB Encryption Key Management
- MongoDB Fields and Encryption Types
- MongoDB Use Automatic Client-Side Field Level Encryption with Azure
It's recommended to create all the resources within the same region.
-
Create a service principal and secret. Store TenantId, ClientId and the secret temporarely for next step reuse.
-
Create a Function App. Choose the Runtime stack accordingly (sample code provided in .NET 6 or Java 11).
-
Configure Key Vault
- Assign access policies
- Assign
Get, Create and RotateKey Permissions andGetSecret Permissions to your Function Managed Identity - Assign
Unwrap Key and Wrap KeyKey Permissions to your service principal
- Assign
- Create Secrets
- encryptionPrincipalTenantId: Service principal Tenant Id.
- encryptionPrincipalClientId: Service principal Client/Application Id.
- encryptionPrincipalClientSecret: Service principal Secret.
- mongoOrderConnectionString: Primary Connection String of your Cosmos DB Mongo API (Get MongoDB connection string).
- mongoVaultConnectionString: Same value as mongoOrderConnectionString.
- Assign access policies
-
Create a database and collections.
- Create
orderscollection: selectSharded, providecustomerIdas the Shard key, selectAutoscaleand provide1000as Collection Max RU/s. - Create
keyscollection: selectUnsharded, selectAutoscaleand provide1000as Collection Max RU/s.
- Create
-
Configure application settings
- collectionName:
orders - databaseName:
<database name> - encryptionPrincipalTenantId:
@Microsoft.KeyVault(VaultName=<key vault name>;SecretName=encryptionPrincipalTenantId) - encryptionPrincipalClientId:
@Microsoft.KeyVault(VaultName=<key vault name>;SecretName=encryptionPrincipalClientId) - encryptionPrincipalClientSecret:
@Microsoft.KeyVault(VaultName=<key vault name>;SecretName=encryptionPrincipalClientSecret) - mongoOrderConnectionString:
@Microsoft.KeyVault(VaultName=<key vault name>;SecretName=mongoOrderConnectionString) - mongoVaultConnectionString:
@Microsoft.KeyVault(VaultName=<key vault name>;SecretName=mongoVaultConnectionString) - mongoVaultNamespace:
<database name>.keys - orderKeyName:
OrdersCMK - vaultEndpoint:
<key vault name>.vault.azure.net(do not include https://)
- collectionName:
-
Deploy Function application to Azure (Deploy using VS Code).
You can call Function APIs from Azure Portal or your favorite tool.
-
Call CreateKey function
curl --request PUT 'https://<function app name>.azurewebsites.net/api/createkey/OrdersCMK?code=<function code>' -
Check Key Vault Key and keys collection on Cosmos
Cosmos DB key reference
Key Vault key reference
-
Call CreateOrder function
curl --request POST 'https://<function app name>.azurewebsites.net/api/orders/create?code=<function code>' \ --header 'Content-Type: application/json' \ --data-raw '{ "customerId": 1, "customerName": "John Doe", "shippingAddress": "John Doe's address", "amount": 200.00, "itens": 2 }' -
Call GetOrders function with different autoDecrypt values
-- Returns Orders by CustomerId with Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerId=1&autoDecrypt=true' -- Returns Orders by CustomerId without Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerId=1&autoDecrypt=false' -- Returns Orders by CustomerName with Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerName=John Doe&autoDecrypt=true' -- Returns Orders by CustomerName without Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerName=John Doe&autoDecrypt=false' -
Call RotateKey function
curl --request PUT 'https://<function app name>.azurewebsites.net/api/rotatekey/OrdersCMK?code=<function code>' -
Check new version of Key Vault Key and keys collection on Cosmos
Cosmos DB key reference (new version)
Key Vault key reference (new version)
-
Call GetOrders function with different autoDecrypt values
-- Returns Orders by CustomerId with Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerId=1&autoDecrypt=true' -- Returns Orders by CustomerId without Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerId=1&autoDecrypt=false' -- Returns Orders by CustomerName with Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerName=John Doe&autoDecrypt=true' -- Returns Orders by CustomerName without Auto Decryption curl --request GET 'https://<function app name>.azurewebsites.net/api/orders?code=<function code>&customerName=John Doe&autoDecrypt=false'
If you find any errors or have suggestions for changes, please be part of this project!
- Create your branch:
git checkout -b my-new-feature - Add your changes:
git add . - Commit your changes:
git commit -m '<message>' - Push your branch to Github:
git push origin my-new-feature - Create a new Pull Request 😄



