How to Configure Cosmos DB PII

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up cosmos db pii controls by combining cloud role assignments with client-side field encryption. You also turn on diagnostic logs to track who reads your containers. Many teams start by sharing primary master keys across apps and developer laptops. Broad permissions let anyone with the key read every item in the database. If a developer runs a wide query, raw emails and phone numbers appear right on their screen. How native cosmos db pii controls work Azure provides three main tools t

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 pii controls by combining cloud role assignments with client-side field encryption. You also turn on diagnostic logs to track who reads your containers. Many teams start by sharing primary master keys across apps and developer laptops. Broad permissions let anyone with the key read every item in the database. If a developer runs a wide query, raw emails and phone numbers appear right on their screen.

How native cosmos db pii controls work

Azure provides three main tools to protect sensitive data in this database. First, you replace shared master keys with Azure role-based rules for your team. Data plane roles decide which users or apps can read or write items. Control plane roles decide who can manage the database account itself.

Second, the database encrypts all stored data by default using service-managed keys. Teams with strict rules often add customer-managed keys stored in a cloud key vault. This step lets you control key rotation and revoke storage keys if needed.

Third, you can set up client-side encryption for specific JSON fields before sending data. Your app uses the official client SDK to encrypt fields like card numbers or national IDs. The database stores only the cipher text and never holds the decryption key. Only apps with the right key vault permissions can turn that text back into plain text.

What native controls solve and where gaps remain

Native roles and encryption solve storage safety and basic login rules. They stop outsiders from reading stolen disks or logging in without an identity token. Client-side encryption also hides chosen fields from database admins who lack the vault key.

Still, a large gap remains during daily operations and live debugging. Native roles work at the container or database level, not the document field level. Once a user or support engineer gets read permissions, they can query whole documents. The database setup decides who logs in, not what individual queries return in flight.

End-to-End Encryption + PII in Logs Prevention: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Client-side encryption also adds heavy work for your engineering team. You must update every app service to use the right SDK and key rules. You cannot run rich queries or sort on fields that your app encrypted before storage. When engineers debug live issues, they often use shared read roles that manage those keys anyway. As a result, live queries still return raw emails, phone numbers, and card numbers in query results.

How to plan your data protection workflow

Start by mapping which containers hold sensitive fields like national IDs or phone numbers. Split high-risk records into separate containers so you can apply tighter read roles. Next, turn off local key logins so every user must log in through your identity provider.

Then, review how engineers and automated tools run queries during support tickets. Native diagnostic logs can record every query text and who ran it. Still, logs only tell you about a data leak after the query finishes. To protect fields during live queries without rewriting apps, teams look at runtime query controls. Masking rules at the query level rewrite matched fields like emails or card numbers in results.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits inline in the protocol beside the service as an open-source proxy, a sidecar for your database or API. On every production query an agent or human runs, hoop.dev decides whether to pass, mask, block, or escalate it for review.

Before a query runs, hoop.dev guardrails check the statement against your rules, an AI Analyzer you configure can score intent or hold risky calls for human approval, and hoop.dev data masking rewrites sensitive fields in responses in memory. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Does Azure Cosmos DB mask sensitive fields on the fly?

No, the database engine does not include built-in dynamic data masking for JSON documents. To hide fields natively, you must encrypt them in your app before writing the data.

Can I restrict read permissions to specific JSON properties?

No, native data plane roles apply to the account, database, or container scope. Any user with read rights on a container can read all properties inside its documents.

How do I stop developers from using primary master keys?

You can disable local key logins in your database account settings. After that change, every user and app must log in using identity tokens and role assignments.

The query runs. The emails stay hidden.