Secure Cosmos DB resources from Accidental Deletion

Prashanth Madi
4 min readJul 14, 2023

--

Azure Cosmos DB is a fully managed NoSQL and relational database for modern app development.

In this blog, we will go over options to

  • Secure Cosmos DB resources
  • Audit deletion
  • Backup options and Restore process.

Below image shows hierarchy of resources inside Cosmos DB Account

Securing Account

  1. Utilize built-in/custom roles in Access control (IAM) to restrict access to certain users/operations. you can find more info on this @ Azure role-based access control in Azure Cosmos DB | Microsoft Learn

2. Restrict Access to certain private/public network using private endpoints/ IP Firewall.

3. To prevent accidental deletion of entire account you can also create a lock at Cosmos DB account level via Azure portal/PowerShell/cli.

While above option helps you with account deletion, it won’t stop in deleting internal resources(database/container) via account key.

Securing Internal Resources in Account

There are multiple ways to authenticate in Cosmos DB, I have updated image @ Restrict user access to data operations only with Azure Cosmos DB | Microsoft Learn to include RBAC along with ways to restrict key access.

If you are utilizing a key based authentication

As a Short-term solution (irrespective of API NoSQL/ Mongo),

  1. Utilize read-only Account keys whenever possible.
  2. set disableKeyBasedMetadataWriteAccess mentioned @ Azure role-based access control in Azure Cosmos DB | Microsoft Learn. this would prevent SDK’s delete internal resources(database/container) via Account Key access.

I have set this on one of my own Cosmos DB accounts and received below error while trying to delete a database using Account keys.

const { MongoClient } = require('mongodb');

// Connection URL
const url = '';
const client = new MongoClient(url);

// Database Name
const dbName = 'prdb3';

async function main() {
// Use connect method to connect to the server
await client.connect();

// delete above database
await client.db(dbName).dropDatabase();

return 'done.';
}

main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());

Long-term solution (requires 1|2-day effort depending on previous usage)

SQL API:

Mongo API:

Audit deletion:

Any operation performed from portal/PowerShell/cli are logged in Activity log (including deletion of internal resources database/collection)

If it's performed via SDK using keys, you can utilize diagnostic logs (requires previous setting to pipe logs into log analytics/ other telemetry system you utilize).

Here I’m utilizing log analytics and it has listed ip address that i have used to perform dropdatabase operation along with user agent.

Restore Deleted Resources:

Cosmos DB currently provides two different backup options. I would highly recommend switching over to Continuous backup for reasons listed below.

Periodic

  • Snapshot based.
  • Requires support request to restore.
  • Restore would always be done to new account.

Continuous

Cosmos Db doesn’t provide any SLA for the time it takes to restore. It took around 1hr for 4TB of data (providing this number so that you could plan things accordingly).

--

--