M4-S3 — Field-encryption keys, backup and restore
Section titled “M4-S3 — Field-encryption keys, backup and restore”| Module | M4 — The operator side of the hand-offs |
| After this session you can | Keep encrypted data readable through any change of key provider, take a complete backup of a release, and prove it by restoring it. |
| Time | Facilitated: 50 min · On your own: 50 min |
| Before you start | M4-S2. From ACSA: protected values. The pre-upgrade.gz archive from M2-S3. |
| Go deeper | Field 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”How encrypted fields are protected
Section titled “How encrypted fields are protected”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.
Key providers
Section titled “Key providers”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.
| Choice | What it gives | What it costs |
|---|---|---|
local, mode wrap (default) | Nothing else to run | The key is a value you must guard and back up |
vault or azure-key-vault, mode wrap | The master key never leaves the key service; access is audited there | One call to the key service per data key, cached for 300 seconds |
vault or azure-key-vault, mode direct | Every value is enciphered by the key service; disabling the key there stops all reads and writes at once | One 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”| Part | Why | How |
|---|---|---|
| MongoDB: every database of the release | All records, definitions, users, providers, wrapped keys | A consistent dump of the whole replica set (mongodump --oplog), or your database team’s snapshots |
| The field-encryption key | Without it the restored encrypted values are unreadable | Your secret manager, stored with the backup’s date |
| Your values and secrets files | To rebuild the release exactly | Git for nw-values.yaml; your secret manager for the two others |
| Object storage (uploaded files) | Files are not in MongoDB | Your 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.
Restoring
Section titled “Restoring”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
- 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
-
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.gzkubectl -n datastores cp mongo-0:/tmp/nw-backup.gz ./nw-backup-$(date +%F).gz -
Record the backup with its key: copy
data.config.fieldEncryptionMasterKeyfromnw-secrets.yamlinto your password manager entry for this backup, with the file’s name and date.
Change something after the backup
- In Administration › Organization › Users, deactivate Drill Marker from its row menu (ACSA M1-S1). This is the change the restore should undo.
Restore
-
Stop the platform’s writers:
kubectl -n northwind scale deployment --all --replicas=0, then wait untilkubectl -n northwind get podsshows no running pods except finished Jobs. -
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.gzkubectl -n datastores exec mongo-0 -- mongorestore --drop --oplogReplay --gzip --archive=/tmp/nw-restore.gz -
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. -
Wait five minutes, then sign in as
nwoperatorand find Drill Marker in Administration: the user is active again. -
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.
-
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
mongorestoreran. -
helm upgradebrought 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 see | What 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 errors | The 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 deactivated | Your 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 pod | Jobs are not Deployments; a completed Job does not write. |
Model answer
Restore runbook (single-tenant release)
- Announce the downtime and the backup’s date: everything entered after it is lost.
- Check the master key recorded with the backup equals
data.config.fieldEncryptionMasterKeyin the values. kubectl -n <ns> scale deployment --all --replicas=0; wait until no release pod runs.- Copy the archive to the MongoDB primary;
mongorestore --drop --oplogReplay --gzip --archive=…. helm upgrade <release> <chart> -n <ns> <values> --wait --timeout 20m.- Wait five minutes for the gateway’s caches to expire.
- Run the smoke test; check one record changed after the backup is back to its old state.
- Restore uploaded files from object storage to the same point, if they are affected.
- Tell the administrator the platform is back and to which moment.
- 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”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?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
Which of these belong in a complete backup of a single-tenant release? Choose all that apply.
Check your answer
The correct answers are A, B and D. Open the explanation under each option to see why.
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?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
Why does the lab scale every Deployment to zero before mongorestore?
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.