Skip to content

Rewrap the secrets in the MDM command kwargs - #1810

Open
np5 wants to merge 5 commits into
mainfrom
20261120-rewrap-command-secrets
Open

np5 wants to merge 5 commits into
mainfrom
20261120-rewrap-command-secrets

Conversation

@np5

@np5 np5 commented Nov 20, 2026

Copy link
Copy Markdown
Member

Problem

The rewrap_secrets management command rewraps the objects of the models that have a rewrap_secrets() method. The MDM DeviceCommand and UserCommand models do not have one. Seven command classes keep a secret in their kwargs, encrypted with the secret engine:

Command Kwarg
AccountConfiguration admin_password
DeviceLock PIN
EraseDevice PIN
RotateFileVaultKey encryption_key
SetAutoAdminPassword new_password
SetFirmwarePassword new_password
SetRecoveryLock new_password

These secrets stay encrypted with the old secret engine. After the removal of this engine, Zentral cannot send or acknowledge a command that is still in the queue, and cannot decrypt the FileVault PRK that a device returns for a RotateFileVaultKey command. The secrets of the acknowledged commands also stay under the old key.

The command also saved all the fields of all the objects, so a rewrap set the updated_at field of each object to the time of the rewrap.

Changes

One commit for each step:

  1. rewrap_secrets compares the field values before and after rewrap_secrets(), and saves only the fields that changed, with update_fields. It does not save an object that has no change.
  2. A model can define a rewrap_secrets_queryset() class method to give the objects to rewrap. Without it, the command rewraps all the objects of the model, as before.
  3. The seven command classes use one get_secret_engine_kwargs(uuid, field) function from commands.base. Before, four modules had a copy of this function, and two modules wrote the context inline. The context does not change.
  4. audit_secret_kwargs is renamed secret_kwargs. It lists the encrypted kwargs, which the audit trail redacts and the rewrap rewraps.
  5. The abstract Command model gets rewrap_secrets() and rewrap_secrets_queryset(), so DeviceCommand and UserCommand both have them. The queryset gives only the commands whose class has secret kwargs, and whose kwargs have one of them. No user-channel command has a secret today, so the UserCommand queryset is empty.

Tests

  • The new tests fail without the changes of commits 1, 2 and 5.
  • One rewrap test for each of the seven commands, and one test that DeviceLock reads its PIN after a rewrap.
  • The audit test requires the same set of encrypted kwargs as the calls to get_secret_engine_kwargs(), so a regex that matches nothing fails.
  • The full suite passes in parallel, flake8 passes on the repository, and the tests cover each changed line.

🤖 Generated with Claude Code

np5 and others added 5 commits November 20, 2026 15:34
The command saved every field of every object, so a rewrap also rewrote
updated_at, and any other field, of all the rows of a model. It now
compares the field values before and after rewrap_secrets(), and saves
only the fields that changed, or nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A model can define a rewrap_secrets_queryset() class method, to return
only the objects that hold secrets. Without it, the command still
rewraps all the objects of the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Seven command classes encrypt a kwarg with the same context: the
mdm.devicecommand model, the kwarg name as the field, and the command
UUID. Four modules defined their own get_secret_engine_kwargs(), and two
wrote the context inline. They now all use get_secret_engine_kwargs()
from commands.base.

The audit test finds the encrypted kwargs from the calls to this
function, and now requires the same set as its list, so that a regex
that matches nothing fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The attribute lists the command kwargs that are encrypted with the
secret engine, not only the kwargs that are redacted in the audit
trail. The next commit also uses it to rewrap these kwargs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rewrap_secrets management command skipped the DeviceCommand and
UserCommand models, so the PINs, the passwords and the FileVault
encryption keys in the command kwargs stayed encrypted with the old
secret engine. Once that engine was removed, a command that was still
queued could not be sent or acknowledged, and the PRK returned for a
RotateFileVaultKey command could not be decrypted.

The abstract Command model now rewraps the secret_kwargs of its command
class. Its rewrap_secrets_queryset() only returns the commands that have
one of these kwargs, so that the rewrap does not load every command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 44756742642

Coverage increased (+0.04%) to 91.689%

Details

  • Coverage increased (+0.04%) from the base build.
  • Patch coverage: 56 of 56 lines across 10 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 60965
Covered Lines: 55898
Line Coverage: 91.69%
Coverage Strength: 0.92 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants