Skip to content

M4-S3 — Field-encryption keys, backup and restore

Section titled “M4-S3 — Field-encryption keys, backup and restore”
ModuleM4 — The operator side of the hand-offs
After this session you canKeep encrypted data readable through any change of key provider, take a complete backup of a release, and prove it by restoring it.
TimeFacilitated: 50 min · On your own: 50 min
Before you startM4-S2. From ACSA: protected values. The pre-upgrade.gz archive from M2-S3.
Go deeperField encryption keys and providers · Tenant backup, restore and export · Runbooks: restore a tenant

◆ Orientation (1 min) — The two things you cannot recreate

Section titled “◆ Orientation (1 min) — The two things you cannot recreate”

Pods, Services and values can all be recreated from Git and the chart. Two things cannot: the data in MongoDB, and the key that makes encrypted fields readable. Lose the first and Northwind loses its records; lose the second and the records survive but every encrypted value in them is noise. In ACSA the administrator decided who may read Northwind’s encrypted bank accounts. In this session you make sure anyone can read them at all, next year, after the worst day.

▤ Concept (7 min) — Keys, providers and complete backups

Section titled “▤ Concept (7 min) — Keys, providers and complete backups”

When a builder turns on Encrypt for a field, its values are stored enciphered (ACSA M2-S3). Behind that are two layers:

flowchart LR
  M["Master key<br/>(your value, or a key<br/>in Vault / Azure Key Vault)"] -->|"wraps"| D["Data key<br/>(stored in MongoDB,<br/>only in wrapped form)"]
  D -->|"enciphers"| V["Every encrypted value"]

What to notice: the database holds only the wrapped data key. Without the master key that wrapped it, the data key cannot be unwrapped, and no encrypted value can be read.

With the default local provider, the master key is data.config.fieldEncryptionMasterKey, one 32-byte value for the whole release, set at install (M2-S1). The migration creates the data key with it on the first install. The identity server uses the same root key to protect its stored sign-in keys (M4-S1). Three rules follow:

  • Never change it. There is no re-encryption: a different master key cannot unwrap the stored data key, so every encrypted value becomes permanently unreadable.
  • Back it up outside the cluster, with the same care as the database backups, and with them: a backup restored without its key is unreadable where it matters most.
  • The chart does not check it is set. An empty value renders; the services fail when they start or first need it.

Instead of a key in your values, the master key can live in a key service: HashiCorp Vault (its Transit engine) or Azure Key Vault. You choose with data.config.encryption.provider (local, vault or azure-key-vault) and its settings under data.config.encryption.

ChoiceWhat it givesWhat it costs
local, mode wrap (default)Nothing else to runThe key is a value you must guard and back up
vault or azure-key-vault, mode wrapThe master key never leaves the key service; access is audited thereOne call to the key service per data key, cached for 300 seconds
vault or azure-key-vault, mode directEvery value is enciphered by the key service; disabling the key there stops all reads and writes at onceOne call per value; for Azure, use RSA-OAEP-256 only (the chart refuses plain RSA-OAEP with direct)

Changing provider on a running release never re-encrypts anything. It provisions a new active key under the new provider for future writes, and old values stay readable through the old key, which must never be deleted. Because that is a deliberate act, the change is blocked unless you also set data.config.encryption.adoptProvider: true; the migration then provisions and activates the new key. With the opt-in missing, the upgrade fails on purpose, rather than leaving you believing your key service is in use while the old key keeps encrypting.

A complete backup of a single-tenant release

Section titled “A complete backup of a single-tenant release”
PartWhyHow
MongoDB: every database of the releaseAll records, definitions, users, providers, wrapped keysA consistent dump of the whole replica set (mongodump --oplog), or your database team’s snapshots
The field-encryption keyWithout it the restored encrypted values are unreadableYour secret manager, stored with the backup’s date
Your values and secrets filesTo rebuild the release exactlyGit for nw-values.yaml; your secret manager for the two others
Object storage (uploaded files)Files are not in MongoDBYour storage service’s own versioning or backups

Not in a backup: Redis, whose contents the platform rebuilds (the domain and space lists are written again when the services start), and RabbitMQ, whose queues hold work in flight. Backups do not run themselves: schedule them, keep them encrypted, restrict who can read them (they hold the single sign-on secrets, M4-S1), and rehearse a restore: a backup that was never restored is a hope, not a backup.

A restore of the whole release replaces every database at once, so the platform must not be writing while it runs: stop the release’s workloads, restore, start them again, then wait five minutes for the gateway’s caches to expire before you judge the result (M3-S2). Anything entered after the backup is lost, and messages that were queued describe a state that no longer exists.

Multi-tenant deployments can back up, restore and export one tenant at a time through the tenant administrator’s API, run by the platform’s cell-controller with Percona Backup for MongoDB agents you provide. It is not switched on by the chart, and a single-tenant release answers that it is not supported. M8 places it among the multi-tenant operations.

⛏ Lab (34 min) — Back up, break, restore

Section titled “⛏ Lab (34 min) — Back up, break, restore”

Goal. A consistent backup of the lab release, a deliberate change after it, and a restore that brings the release back to the backup’s state, proven in the product.

Before you start. The release healthy and you signed in as nwoperator. A restore stops the platform for a few minutes; that is expected.

Mark the moment

  1. In the portal, open Administration › Organization › Users › Add User and add a user Drill Marker (drill.marker@northwind.example.com), as you learned in ACSA M1-S1. Save it.

Back up

  1. Take a consistent dump of the whole replica set, then copy it out:

    Terminal window
    kubectl -n datastores exec mongo-0 -- mongodump --oplog --gzip --archive=/tmp/nw-backup.gz
    kubectl -n datastores cp mongo-0:/tmp/nw-backup.gz ./nw-backup-$(date +%F).gz
  2. Record the backup with its key: copy data.config.fieldEncryptionMasterKey from nw-secrets.yaml into your password manager entry for this backup, with the file’s name and date.

Change something after the backup

  1. In Administration › Organization › Users, deactivate Drill Marker from its row menu (ACSA M1-S1). This is the change the restore should undo.

Restore

  1. Stop the platform’s writers: kubectl -n northwind scale deployment --all --replicas=0, then wait until kubectl -n northwind get pods shows no running pods except finished Jobs.

  2. Copy the backup back in and restore it, replacing what is there:

    Terminal window
    kubectl -n datastores cp ./nw-backup-<date>.gz mongo-0:/tmp/nw-restore.gz
    kubectl -n datastores exec mongo-0 -- mongorestore --drop --oplogReplay --gzip --archive=/tmp/nw-restore.gz
  3. Start the platform again by re-applying the release: helm upgrade nw ./osos -n northwind $values --wait --timeout 20m. Helm sets every Deployment back to the replica count in its values.

  4. Wait five minutes, then sign in as nwoperator and find Drill Marker in Administration: the user is active again.

  5. Run the rest of your smoke test (M2-S2), and confirm an encrypted field still reads, if your lab has one, or that Administration opens without errors if not.

  6. Write a restore runbook of ten lines or fewer in inventory.md, from your steps.

Check.

  • The backup file exists and its date and key are recorded together in your password manager.
  • Drill Marker was deactivated before the restore and is active after it.
  • No pod of the release ran while mongorestore ran.
  • helm upgrade brought every workload back to ready.
  • The smoke test passes after the restore.
  • Your restore runbook says to stop the writers, restore, restart, wait for the caches, and test.

Pass condition. All six checks hold.

If something goes wrong

What you seeWhat to do
mongodump --oplog fails with “not a replica set”MongoDB is not running as a replica set; see M0-S2 step 6.
mongorestore reports duplicate key errors--drop was missing; run step 6 again with it.
Pods restart in a loop after step 7, logging encryption-key errorsThe release’s master key differs from the one in the backup. Put the recorded key back in nw-secrets.yaml and upgrade.
Drill Marker is still deactivatedYour backup was taken after step 4, or the gateway cache had not expired. Wait five minutes and check again; then check the backup’s time.
scale --all leaves the migration Job’s podJobs are not Deployments; a completed Job does not write.
Model answer

Restore runbook (single-tenant release)

  1. Announce the downtime and the backup’s date: everything entered after it is lost.
  2. Check the master key recorded with the backup equals data.config.fieldEncryptionMasterKey in the values.
  3. kubectl -n <ns> scale deployment --all --replicas=0; wait until no release pod runs.
  4. Copy the archive to the MongoDB primary; mongorestore --drop --oplogReplay --gzip --archive=….
  5. helm upgrade <release> <chart> -n <ns> <values> --wait --timeout 20m.
  6. Wait five minutes for the gateway’s caches to expire.
  7. Run the smoke test; check one record changed after the backup is back to its old state.
  8. Restore uploaded files from object storage to the same point, if they are affected.
  9. Tell the administrator the platform is back and to which moment.
  10. Record the incident: why, when, how long.

◇ Worked scenario (2 min) — The Vault migration that would not deploy

Section titled “◇ Worked scenario (2 min) — The Vault migration that would not deploy”

Northwind’s security team wants the master key in its HashiCorp Vault. A colleague adds the Vault address, the Transit key name and provider: vault under data.config.encryption, and upgrades. The upgrade fails: the migration Job reports that the configured provider differs from the active key’s, and stops.

Decide: is the platform broken? No. The migration refused on purpose. Changing provider re-keys every future write, so it needs the explicit opt-in. Nothing has been re-encrypted and nothing is lost; the workloads already rolled, and the release keeps working on the old key.

Decide: what next? First, back up (database and the current master key). Confirm with the security team that the pods can reach Vault and authenticate. Then set data.config.encryption.adoptProvider: true and upgrade again: the migration provisions a new data key wrapped by Vault and activates it. New values are encrypted under it; old values still decrypt through the old key.

Decide: can the old local master key now be thrown away? No. Every value written before the switch is still enciphered under the old data key, which only the old master key can unwrap. Keep it, and its backups, for as long as any of those values exists.

✓ Practice (5 min) — Check your understanding

Section titled “✓ Practice (5 min) — Check your understanding”
PracticeChoose one answer.

A disaster recovery test restores last night’s MongoDB backup into a fresh cluster with freshly generated secrets. Records appear, but every encrypted field shows an error. What went wrong?

  1. Explanation for A

    ✗ Incorrect

    Not this one: the values are in the database; they are simply unreadable without the right key. See How encrypted fields are protected.

  2. Explanation for B

    ✓ Correct

    Correct: the master key must travel with the backup; restore it into the values first. See A complete backup.

  3. Explanation for C

    ✗ Incorrect

    Not this one: they restore fine when the release has the same master key. See How encrypted fields are protected.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose every answer that applies.

Which of these belong in a complete backup of a single-tenant release? Choose all that apply.

  1. Explanation for A

    ✓ Correct

    Correct: records, definitions, users and the wrapped keys all live there. See A complete backup.

  2. Explanation for B

    ✓ Correct

    Correct: without it, encrypted values in the dump cannot be read. See A complete backup.

  3. Explanation for C

    ✗ Incorrect

    Not this one: the platform rebuilds Redis’s contents, such as the domain and space lists, when its services start. See A complete backup.

  4. Explanation for D

    ✓ Correct

    Correct: files are not in MongoDB, so the database dump does not contain them. See A complete backup.

Check your answer

The correct answers are A, B and D. Open the explanation under each option to see why.

PracticeChoose one answer.

After switching to Azure Key Vault with adoptProvider, a colleague proposes deleting the old key document from the database “to tidy up”. What do you answer?

  1. Explanation for A

    ✗ Incorrect

    Not this one: every value written before the switch still needs the old key to decrypt. See Key providers.

  2. Explanation for B

    ✓ Correct

    Correct: changing provider re-keys future writes only; retired keys must stay while their values exist. See Key providers.

  3. Explanation for C

    ✗ Incorrect

    Not this one: no migration ever re-encrypts old values under a new key. See Key providers.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

Why does the lab scale every Deployment to zero before mongorestore?

  1. Explanation for A

    ✓ Correct

    Correct: a whole-release restore needs the writers stopped, then started again afterwards. See Restoring.

  2. Explanation for B

    ✗ Incorrect

    Not this one: it would run; the result would just be inconsistent. See Restoring.

  3. Explanation for C

    ✗ Incorrect

    Not this one: the caches are in Redis and expire by themselves; that is why you wait afterwards. See Restoring.

Check your answer

The correct answer is A. Open the explanation under each option to see why.

↺ Recap (1 min) — After Module 4 you can

Section titled “↺ Recap (1 min) — After Module 4 you can”

After this module you can carry out the operator side of every hand-off the tenant administrator prepared: connect single sign-on and look after the identity server, run and prove the directory import, make every message channel deliver, set the brand, keep encrypted data readable through any change of key provider, and back up and restore the release.

Next. Module 5 makes the platform observable: health, metrics, logs and traces, so you see a problem before a user reports it.