How to Configure Cosmos DB Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up cosmos db least privilege by moving from master keys to identity roles. You then assign the smallest set of data actions to each user or app. Many teams start by sharing primary account keys across multiple services and users. Those master keys grant full control over every database and container in the account. If an app or user holds a broad role, one mistake can wipe out production data. A stolen key also gives an attacker full read and write power right away. How cosmos db least

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up cosmos db least privilege by moving from master keys to identity roles. You then assign the smallest set of data actions to each user or app. Many teams start by sharing primary account keys across multiple services and users. Those master keys grant full control over every database and container in the account. If an app or user holds a broad role, one mistake can wipe out production data. A stolen key also gives an attacker full read and write power right away.

How cosmos db least privilege works in Azure

Azure splits database permissions into two clear planes. The control plane manages the account, throughput settings, and network rules. The data plane handles reading, writing, and deleting items inside containers. You must secure both planes to keep your data safe.

First, turn off local key access on the database account if your workload supports it. This step stops anyone from using static master keys to read or change data. Instead, every user and app must log in through Azure identity services.

Next, define custom roles for the data plane. Built-in roles often grant read or write rights across the entire account. A custom role lets you pick exact actions, such as reading items or running queries. You can omit delete actions or container creation rights from daily developer roles.

Finally, bind each role to a narrow scope. Do not assign roles at the root account level unless the app needs every database. Scope the role to one specific database or a single container. Use managed identities for apps so nobody stores secrets in config files.

Separating human users from app workloads

App workloads and human engineers need very different access rules. An app runs the same set of queries every day. You can give its managed identity a static role scoped to one container. That role never needs to change unless the app code changes.

Human engineers need access only when they fix a bug or check data. Leaving permanent write roles on engineer accounts creates a major risk. Instead, give engineers zero standing write permissions in production. Grant temporary read roles only when an incident occurs, and remove them right after.

Least Privilege Principle + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Where native database roles fall short

Native role assignments solve the problem of who can log in to a container. They also block unauthorized users from changing account settings or deleting whole databases. Still, container-level roles cannot inspect what happens inside an active database session.

Once a user has read access to a container, they can read every field in every document. Native roles cannot hide sensitive fields like emails, phone numbers, card numbers, or national IDs. If a document holds private customer details, a standard read role returns the full document. Masking tools must step in to rewrite matched fields in results before they reach the screen.

Role checks also stop at the container door. They do not inspect individual queries in flight to block heavy scans or risky bulk updates. To control exact queries and protect private fields, teams must enforce rules at the query level.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev sits inline in the wire protocol beside those services as an open-source proxy, a sidecar for your Postgres or API. On every query an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run, and hoop.dev data masking rewrites matched sensitive values in responses in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Why should teams avoid Cosmos DB master keys?

Master keys grant full admin rights to all data and cannot be tied to one person. If a key leaks, anyone can read or delete every container until you rotate the key.

What is the lowest scope for a Cosmos DB role assignment?

You can scope a data plane role down to a single container inside a database. Azure does not support native role assignments at the single document or field level.

How do you audit who ran a query in Cosmos DB?

You must use Azure identity logins and turn on diagnostic data plane logs for the account. These logs record the identity ID and the operation type for each request.

The query runs. The emails stay hidden.