Data Security: Encryption, Keys, and RBAC
Protect Cosmos DB data with encryption at rest (service-managed and customer-managed keys), account keys, resource tokens, RBAC for data and management planes, and Always Encrypted for client-side protection.
Data security layers
Think of data security in layers, like a bank vault. Encryption at rest is the vault walls β data is scrambled when stored. Keys are the combinations to the vault β you need one to get in. RBAC is the security guard checking your badge β different people get different levels of access. Always Encrypted is a locked briefcase inside the vault β even the bank staff canβt read whatβs inside.
Marcusβs security checklist
βοΈ Marcus at FinSecure has SOC 2 requirements for data security:
- All data encrypted at rest with customer-controlled keys
- No shared master keys in application code
- Least-privilege access for each microservice
- PII fields (SSN, account numbers) encrypted client-side
Encryption at rest
Cosmos DB always encrypts data at rest β you cannot disable it.
| Aspect | Service-Managed Keys (default) | Customer-Managed Keys (CMK) |
|---|---|---|
| Key management | Microsoft manages keys | You manage keys in Azure Key Vault |
| Key rotation | Automatic by Microsoft | You control rotation schedule |
| Compliance | Meets most requirements | Required for some regulatory frameworks |
| Setup | Automatic β no configuration | Requires Key Vault + managed identity |
| Cost | Included | Key Vault charges apply |
| Revocation | Not possible | Revoke access by removing key permissions |
# Configure CMK with Azure Key Vault
az cosmosdb update --name finsecure-cosmos \
--resource-group rg-finsecure \
--key-uri "https://finsecure-vault.vault.azure.net/keys/cosmos-key/abc123"
Exam tip: CMK revocation
If you revoke the Key Vault permissions for a CMK-enabled Cosmos DB account, the account becomes inaccessible β all reads and writes fail. This is a powerful security control (you can lock out a compromised account) but also a risk (misconfigured Key Vault access can cause an outage).
The exam tests this: βWhat happens if the CMK is deleted from Key Vault?β β the account becomes inaccessible.
Authentication methods
1. Account keys (master keys)
// Full access β read, write, delete anything
CosmosClient client = new CosmosClient(endpoint, accountKey);
- Two keys: Primary and secondary (for rotation without downtime)
- Full access: Master keys grant complete control β never embed in client apps
- Rotation: Rotate using
az cosmosdb keys regenerate; switch apps to the secondary key first
2. Resource tokens (scoped access)
// Create a permission that grants read access to a specific partition
Permission permission = await user.CreatePermissionAsync(
new PermissionProperties(
id: "readOrders",
permissionMode: PermissionMode.Read,
container: ordersContainer,
resourcePartitionKey: new PartitionKey("customer-123")
),
tokenExpiry: 3600 // 1 hour
);
string resourceToken = permission.Resource.Token;
// Give this token to the client β they can only read customer-123's orders
CosmosClient scopedClient = new CosmosClient(endpoint, resourceToken);
- Scoped: Limit access to specific containers, partitions, or documents
- Temporary: Tokens expire (1-24 hours, default 1 hour)
- Per-user: Created via the Users and Permissions system
3. Microsoft Entra ID (RBAC)
The recommended approach β no keys or tokens to manage:
# Assign the built-in "Cosmos DB Built-in Data Reader" role
az cosmosdb sql role assignment create \
--account-name finsecure-cosmos \
--resource-group rg-finsecure \
--role-definition-name "Cosmos DB Built-in Data Reader" \
--principal-id "00000000-0000-0000-0000-000000000000" \
--scope "/dbs/orders/colls/transactions"
RBAC: data plane vs management plane
| Aspect | Data Plane RBAC | Management Plane RBAC |
|---|---|---|
| What it controls | Read/write/query operations on data | Account config, databases, containers |
| Roles | Cosmos DB Built-in Data Reader, Data Contributor | Azure built-in roles (Contributor, Reader) |
| Scope | Account, database, container, or partition | Subscription, resource group, or account |
| Authentication | Microsoft Entra ID tokens | Microsoft Entra ID tokens |
| Key operations | CRUD on items, run queries | Create/delete DBs, change throughput |
| Built-in Data Plane Role | Permissions |
|---|---|
| Cosmos DB Built-in Data Reader | Read items, execute queries |
| Cosmos DB Built-in Data Contributor | Read + write items, execute queries |
Exam tip: RBAC vs master keys
RBAC with Microsoft Entra ID is the recommended authentication method for production. Master keys should only be used for initial setup or legacy applications. Key advantages of RBAC:
- No secrets to rotate or leak
- Least-privilege access (specific roles, scoped to containers)
- Integration with Entra ID Conditional Access policies
- Audit trail in Entra ID logs
The exam often presents βa developer has the master key in their codeβ as a security anti-pattern, with RBAC as the correct alternative.
Always Encrypted (client-side)
For the most sensitive data, Always Encrypted provides client-side encryption:
- Data is encrypted before it leaves the application
- Cosmos DB stores and indexes ciphertext β it never sees plaintext
- Only the application with the encryption key can decrypt
- Supports deterministic encryption (allows equality queries on encrypted fields) and randomised encryption (no queries, maximum security)
// Always Encrypted configuration
ClientEncryptionPolicy encryptionPolicy = new ClientEncryptionPolicy(
new List<ClientEncryptionIncludedPath>
{
new ClientEncryptionIncludedPath
{
Path = "/ssn",
ClientEncryptionKeyId = "customer-dek",
EncryptionType = EncryptionType.Deterministic.ToString(),
EncryptionAlgorithm = "AEAD_AES_256_CBC_HMAC_SHA256"
}
}
);
Dianaβs tip: π Diana requires Always Encrypted for SSN and account number fields β even FinSecureβs DBAs cannot see plaintext values.
π¬ Video walkthrough
π¬ Video coming soon
Data Security β DP-420 Module 25
Data Security β DP-420 Module 25
~16 minFlashcards
Knowledge Check
Marcus needs to ensure that even FinSecure's database administrators cannot see customer SSN values stored in Cosmos DB. What should he implement?
A developer embeds the Cosmos DB master key in their web application's client-side JavaScript. What's the security risk and recommended fix?
Marcus needs to rotate the Cosmos DB master key without downtime. What's the correct process?
Next up: Cost Optimization β choosing between serverless, provisioned, and autoscale throughput to balance performance and cost.