skip to content

In MongoDB in-use encryption, how do key-vault data keys relate to the customer master key, and what does deleting or rotating each do?

level: middleimportance: should knowfreq 26%

answer

  1. two levels of keys
  2. what a key vault read returns
  3. rewrapping leaves field ciphertext alone
  4. deletion has no undo

basics

~20 s

Data keys encrypt fields; the customer master key in a KMS encrypts the data keys kept in the key vault. Deleting either makes the affected fields permanently unreadable. Rotating the master key means rewrapping data keys, leaving field ciphertext untouched.

solid answer

~40 s

A data encryption key encrypts field values. It lives in a key vault collection, by convention `encryption.__keyVault`, wrapped by a customer master key held in your KMS, so MongoDB itself can never decrypt it. A client needs both read access to the vault and access to the master key before a data key is usable. Deleting a data key makes its fields permanently unreadable; deleting a master key does that for every data key it wraps. To rotate, you rotate the master key at the provider, then run `KeyVault.rewrapManyDataKey()`. That re-encrypts the matched data keys under the new master key, but the data keys themselves and all stored field ciphertext stay unchanged, so it does not address a leaked data key.

code

javascript · 7 lines
javascript
// mongosh, from a client configured with access to both master keys
const keyVault = db.getMongo().getKeyVault()

keyVault.rewrapManyDataKey(
  { keyAltNames: "card-token-key" },
  { provider: "kmip", masterKey: { keyId: "<new KMIP key identifier>" } }
)

go deeper

for a junior

Remember the chain: the master key wraps data keys and data keys encrypt fields. Know that the vault is an ordinary collection and that deleting a key means losing data.

for a middle

Explain why reading the vault is not enough to decrypt, what rewrapManyDataKey changes and leaves alone, and why the old master key must outlive the rewrap.

for a senior

Separate duties so DBAs hold the database and the application alone reaches the KMS. Back up the vault before rotating, and do not run rewraps alongside other key operations.

for a principal

Decide who may delete a master key and what stops them, because that one action is irreversible data loss. Weigh per-customer keys for shredding against what Queryable Encryption's single key per field allows.

## The two levels In-use encryption uses a key hierarchy, often called **envelope encryption**. - A **data encryption key (DEK)** encrypts field values. - A **customer master key (CMK)** encrypts DEKs. It sits at the top of the hierarchy in a key provider you control, and the manual says production use must keep it in a remote KMS. - A **key vault** is an ordinary MongoDB collection that stores DEKs in their encrypted form. Key management is client-side, so the server stores wrapped key material it cannot open. Stored ciphertext also carries the `_id` of the DEK that produced it, which is how a driver knows which key to fetch when decrypting. ## What is in the key vault By convention the namespace is `encryption.__keyVault`. Any namespace other than the `admin` database works, and the manual warns against `admin` because clients may lack the permissions to reach it. Each DEK is a BSON document with these fields: - `_id`, the key's UUID - `keyMaterial`, the encrypted key bytes - `masterKey`, which CMK wrapped it - `keyAltNames`, optional alternate names - `status`, `creationDate` and `updateDate` The collection must have a unique index on `keyAltNames`. The `mongosh` method `KeyVault.createKey()` creates it if it is missing, while an application using a driver has to create it before managing keys. Permission to read this collection is not permission to decrypt. Reading the vault with the `read` role returns wrapped keys, and only a client that can also reach the CMK can use them. That separation is what keeps a DBA out: give DBAs the database, and give the KMS key to the application alone. By default the vault shares a cluster with the data, but the manual recommends a separate deployment under tighter control, reached with the `keyVaultClient` option, which helps prevent unapproved changes to key documents, such as their KMS endpoint. ## Deleting | You delete | Effect | |---|---| | A DEK | Every field encrypted with that DEK becomes permanently unreadable | | A CMK | Every field encrypted with any DEK wrapped by that CMK becomes permanently unreadable | A vault backup can bring back a deleted DEK, and the manual tells you to back up the vault before rotating. It cannot save you from a lost CMK, because the backed-up DEKs are still wrapped by that CMK. Note that drivers cache unwrapped DEKs: in Node.js driver 7.8 and PyMongo 4.19, `keyExpirationMS` or `key_expiration_ms` defaults to 60000 ms, so a running client may keep using a deleted key briefly. Deliberate deletion has a use. In CSFLE, giving each user a DEK allows **crypto-shredding**: destroy one DEK, and that user's data becomes indecipherable wherever it is copied, including data backups, provided no vault backup still holds that DEK. CSFLE's `keyId` can be a JSON pointer to a key alternate name, which is how a per-document key is selected. Queryable Encryption works differently, because server validation rejects duplicate key IDs across a collection's fields and each field uses a single key. So deleting a QE key shreds that whole field for every document, not one customer. ## Rotating Rotation has steps that are easy to confuse: 1. You rotate the CMK at the provider, manually or automatically. MongoDB has no visibility of this. 2. New DEKs are wrapped with the new CMK. Existing DEKs stay wrapped by the prior CMK. 3. You call `KeyVault.rewrapManyDataKey(filter, options)`, or the driver's equivalent, to decrypt the matching DEKs and re-encrypt them under the CMK named in `options`. With no `masterKey`, each is rewrapped with the CMK named in its own metadata. 4. Only after this should the old CMK be retired, since it remains the only way to open any DEK not yet rewrapped. The operation is **not atomic** and should not run in parallel with other key management operations. The DEKs themselves do not change, so no document in the data collection is rewritten and the application is not interrupted. The same fact sets the limit. Rewrapping protects against a compromised CMK being used on future vault reads, but it does nothing about a DEK that has already leaked, since every field stays encrypted under the same DEK. ## What to take away The vault is data, the CMK is the root of trust, and they fail differently. Losing vault documents is a restore problem. Losing the CMK is permanent loss. Treat the KMS key's deletion controls and the vault's backups as part of the data's own recovery plan, and test both.

  • Does rewrapManyDataKey help if a data key itself has leaked?
    No. It changes the wrapping around the data key, not the key, so the leaked key still decrypts every field it encrypted. Recovering needs the values re-encrypted under a new data key, which in Queryable Encryption points to a new collection, since a field's key is fixed in its schema.
  • Why host the key vault on a separate MongoDB deployment?
    So that administrators of the data cluster cannot change key documents, such as their KMS endpoint configuration. The manual recommends tighter security on that deployment, and applications reach it through `keyVaultClient`. They then need access to both clusters to run encrypted operations.

Rewrapping is like changing the lock on a key cabinet: the keys inside stay the same, and so does every door they open.

  • Anyone with read access to the key vault collection can decrypt the fields.
  • Rotating the customer master key re-encrypts every encrypted field in the collection.
  • After rotating at the KMS, existing data keys automatically move to the new master key.
  • A lost customer master key can be recovered by restoring a backup of the key vault.
  • MongoDB keeps a copy of the data keys it can decrypt in an emergency.