From 4084624d125b17e1b0bd0f5b5fdcd6e7514a58db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Fri, 20 Nov 2026 15:29:20 +0100 Subject: [PATCH 1/5] rewrap_secrets: save only the fields that were rewrapped 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 --- CHANGELOG.md | 2 + .../management/commands/rewrap_secrets.py | 12 ++- .../test_management_command_rewrap_secrets.py | 80 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/server_base/test_management_command_rewrap_secrets.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c4301a88ec..ab6e528ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -639,6 +639,8 @@ An anonymous request to a page of the web console goes to the login page before The MDM check-in, the OTA enrollment and the DEP web enrollment endpoints refuse a malformed request with a 4xx error now, and not with a 500 error. A signature with no signer, with a signer that matches no certificate, or with a signer that is identified by its subject key identifier, is refused as a signature that is not valid. Before, the first one was read as a verified signature with no certificate, and the other two gave an error. The DEP web enrollment custom views and profile answer 404 to a request without an enrollment in the session. +`rewrap_secrets` saves only the fields that it rewraps now, and does not save an object that has no change. Before, it saved all the fields of all the objects, and set the `updated_at` field of each object to the time of the rewrap. + ## 2026.5 diff --git a/server/base/management/commands/rewrap_secrets.py b/server/base/management/commands/rewrap_secrets.py index 06af64bc1b..d5408b8130 100644 --- a/server/base/management/commands/rewrap_secrets.py +++ b/server/base/management/commands/rewrap_secrets.py @@ -8,6 +8,10 @@ logger = logging.getLogger("zentral.server.base.management.commands.rewrap_secrets") +def get_field_values(obj): + return {field.name: field.value_from_object(obj) for field in obj._meta.concrete_fields} + + class Command(BaseCommand): help = 'Rewrap the DB secrets using the default secret engine' @@ -25,5 +29,11 @@ def handle(self, **options): if not dry_run: with transaction.atomic(): for obj in tqdm(qs.iterator(), total=qs.count()): + # rewrap_secrets() assigns new values to the fields, it does not mutate them, + # so the old values are compared to find the fields to save. updated_at is not one of them. + old_values = get_field_values(obj) obj.rewrap_secrets() - super(model_class, obj).save() + update_fields = [name for name, value in get_field_values(obj).items() + if value != old_values[name]] + if update_fields: + super(model_class, obj).save(update_fields=update_fields) diff --git a/tests/server_base/test_management_command_rewrap_secrets.py b/tests/server_base/test_management_command_rewrap_secrets.py new file mode 100644 index 0000000000..fa56d536fe --- /dev/null +++ b/tests/server_base/test_management_command_rewrap_secrets.py @@ -0,0 +1,80 @@ +from contextlib import redirect_stderr, redirect_stdout +from io import StringIO +from unittest.mock import patch +from django.core.management import call_command +from django.db import models +from django.test import TestCase +from django.utils.crypto import get_random_string +from zentral.contrib.jamf.models import JamfInstance +from zentral.core.secret_engines import secret_engines + + +class RewrapSecretsManagementCommandTestCase(TestCase): + def setUp(self): + super().setUp() + secret_engines.load_config({}) + self.addCleanup(secret_engines.load_config, {}) + + def _force_jamf_instance(self): + jamf_instance = JamfInstance.objects.create( + host="{}.example.com".format(get_random_string(12)), + port=443, + path="/JSSResource", + user=get_random_string(12) + ) + jamf_instance.set_password(get_random_string(12)) + super(JamfInstance, jamf_instance).save() + jamf_instance.refresh_from_db() + return jamf_instance + + def _load_fernet_engine(self): + secret_engines.load_config({ + "fernet": {"backend": "zentral.core.secret_engines.backends.fernet", + "passwords": ["undeuxtrois"]} + }) + + def _call_command(self, *args): + stdout = StringIO() + original_save = models.Model.save + with (patch.object(models.Model, "save", autospec=True, side_effect=original_save) as save, + redirect_stdout(stdout), + redirect_stderr(StringIO())): + call_command("rewrap_secrets", *args) + return stdout.getvalue(), save + + def _jamf_instance_save_calls(self, save, jamf_instance): + return [c for c in save.call_args_list + if isinstance(c.args[0], JamfInstance) and c.args[0].pk == jamf_instance.pk] + + def test_rewrap_secrets_saves_only_the_rewrapped_fields(self): + jamf_instance = self._force_jamf_instance() + password = jamf_instance.get_password() + self._load_fernet_engine() + stdout, save = self._call_command() + self.assertIn("JamfInstance: 1 objects to update", stdout) + save_calls = self._jamf_instance_save_calls(save, jamf_instance) + self.assertEqual(len(save_calls), 1) + self.assertEqual(save_calls[0].kwargs, {"update_fields": ["password"]}) + updated_jamf_instance = JamfInstance.objects.get(pk=jamf_instance.pk) + self.assertEqual(updated_jamf_instance.password.split("$")[0], "fernet") + self.assertEqual(updated_jamf_instance.get_password(), password) + self.assertEqual(updated_jamf_instance.updated_at, jamf_instance.updated_at) + self.assertEqual(updated_jamf_instance.version, jamf_instance.version) + + def test_rewrap_secrets_does_not_save_unchanged_objects(self): + jamf_instance = self._force_jamf_instance() + stdout, save = self._call_command() + self.assertIn("JamfInstance: 1 objects to update", stdout) + self.assertEqual(self._jamf_instance_save_calls(save, jamf_instance), []) + updated_jamf_instance = JamfInstance.objects.get(pk=jamf_instance.pk) + self.assertEqual(updated_jamf_instance.password, jamf_instance.password) + self.assertEqual(updated_jamf_instance.updated_at, jamf_instance.updated_at) + + def test_rewrap_secrets_dry_run(self): + jamf_instance = self._force_jamf_instance() + self._load_fernet_engine() + stdout, save = self._call_command("--dry-run") + self.assertIn("JamfInstance: 1 objects to update", stdout) + self.assertEqual(self._jamf_instance_save_calls(save, jamf_instance), []) + updated_jamf_instance = JamfInstance.objects.get(pk=jamf_instance.pk) + self.assertEqual(updated_jamf_instance.password, jamf_instance.password) From 5e0d0eb1dc0f353016ce7f69a50ae3caabe5b91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Fri, 20 Nov 2026 15:29:41 +0100 Subject: [PATCH 2/5] rewrap_secrets: let a model choose the objects to rewrap 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 --- .../base/management/commands/rewrap_secrets.py | 5 ++++- .../test_management_command_rewrap_secrets.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/server/base/management/commands/rewrap_secrets.py b/server/base/management/commands/rewrap_secrets.py index d5408b8130..ecbbd25f85 100644 --- a/server/base/management/commands/rewrap_secrets.py +++ b/server/base/management/commands/rewrap_secrets.py @@ -24,7 +24,10 @@ def handle(self, **options): for content_type in ContentType.objects.all(): model_class = content_type.model_class() if hasattr(model_class, "rewrap_secrets"): - qs = content_type.get_all_objects_for_this_type() + if hasattr(model_class, "rewrap_secrets_queryset"): + qs = model_class.rewrap_secrets_queryset() + else: + qs = content_type.get_all_objects_for_this_type() print(f"{model_class.__name__}:", qs.count(), "objects to update") if not dry_run: with transaction.atomic(): diff --git a/tests/server_base/test_management_command_rewrap_secrets.py b/tests/server_base/test_management_command_rewrap_secrets.py index fa56d536fe..ec3d32997d 100644 --- a/tests/server_base/test_management_command_rewrap_secrets.py +++ b/tests/server_base/test_management_command_rewrap_secrets.py @@ -70,6 +70,22 @@ def test_rewrap_secrets_does_not_save_unchanged_objects(self): self.assertEqual(updated_jamf_instance.password, jamf_instance.password) self.assertEqual(updated_jamf_instance.updated_at, jamf_instance.updated_at) + def test_rewrap_secrets_queryset(self): + jamf_instance = self._force_jamf_instance() + skipped_jamf_instance = self._force_jamf_instance() + self._load_fernet_engine() + with patch.object(JamfInstance, "rewrap_secrets_queryset", create=True, + return_value=JamfInstance.objects.filter(pk=jamf_instance.pk)): + stdout, save = self._call_command() + self.assertIn("JamfInstance: 1 objects to update", stdout) + self.assertEqual(len(self._jamf_instance_save_calls(save, jamf_instance)), 1) + self.assertEqual(self._jamf_instance_save_calls(save, skipped_jamf_instance), []) + self.assertEqual(JamfInstance.objects.get(pk=jamf_instance.pk).password.split("$")[0], "fernet") + self.assertEqual( + JamfInstance.objects.get(pk=skipped_jamf_instance.pk).password, + skipped_jamf_instance.password + ) + def test_rewrap_secrets_dry_run(self): jamf_instance = self._force_jamf_instance() self._load_fernet_engine() From dd41168c59820a6125348039da4bcf9ee93f3340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Fri, 20 Nov 2026 15:30:27 +0100 Subject: [PATCH 3/5] mdm: one secret engine context for the command kwargs 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 --- tests/mdm/test_command_audit.py | 6 +++--- .../contrib/mdm/commands/account_configuration.py | 10 +++------- zentral/contrib/mdm/commands/base.py | 4 ++++ zentral/contrib/mdm/commands/device_lock.py | 10 +++------- zentral/contrib/mdm/commands/erase_device.py | 6 +++--- .../contrib/mdm/commands/rotate_filevault_key.py | 10 +++------- .../mdm/commands/set_auto_admin_password.py | 10 +++------- .../contrib/mdm/commands/set_firmware_password.py | 12 ++++++------ zentral/contrib/mdm/commands/set_recovery_lock.py | 14 +++++--------- 9 files changed, 33 insertions(+), 49 deletions(-) diff --git a/tests/mdm/test_command_audit.py b/tests/mdm/test_command_audit.py index 9afa89f717..c19e8ab985 100644 --- a/tests/mdm/test_command_audit.py +++ b/tests/mdm/test_command_audit.py @@ -32,13 +32,13 @@ def setUpTestData(cls): # the policy itself def test_no_encrypted_kwarg_is_missing_from_this_list(self): - # get_secret_engine_kwargs() declares the encrypted field name in each command + # get_secret_engine_kwargs() gets the encrypted field name in each command # module. Anything new there has to be classified, or it would be serialized # verbatim the moment somebody adds it to audit_public_kwargs. found = set() for path in glob.glob("zentral/contrib/mdm/commands/*.py"): - found.update(re.findall(r"""field=["'](\w+)["']""", open(path).read())) - self.assertEqual(found - ENCRYPTED_KWARGS, set()) + found.update(re.findall(r"""get_secret_engine_kwargs\([\w.]+, ["'](\w+)["']\)""", open(path).read())) + self.assertEqual(found, ENCRYPTED_KWARGS) def test_encrypted_kwargs_are_never_public(self): for db_name, command_class in registered_commands.items(): diff --git a/zentral/contrib/mdm/commands/account_configuration.py b/zentral/contrib/mdm/commands/account_configuration.py index a547a9cbbc..0df19f715a 100644 --- a/zentral/contrib/mdm/commands/account_configuration.py +++ b/zentral/contrib/mdm/commands/account_configuration.py @@ -6,17 +6,13 @@ from zentral.contrib.mdm.payloads import substitute_variables from zentral.core.secret_engines import decrypt_str, encrypt_str from zentral.utils.passwords import build_password_hash_dict, serialize_password_hash_dict -from .base import register_command, Command +from .base import register_command, Command, get_secret_engine_kwargs from .set_auto_admin_password import generate_password logger = logging.getLogger("zentral.contrib.mdm.commands.account_configuration") -def get_secret_engine_kwargs(uuid): - return {"model": "mdm.devicecommand", "field": "admin_password", "uuid": str(uuid)} - - class AccountConfiguration(Command): request_type = "AccountConfiguration" audit_secret_kwargs = ("admin_password",) @@ -37,13 +33,13 @@ def create_for_target_and_dep_enrollment(cls, target, dep_enrollment): uuid = uuid4() if dep_enrollment.has_auto_admin(): password = generate_password(complexity=dep_enrollment.admin_password_complexity) - kwargs = {"admin_password": encrypt_str(password, **get_secret_engine_kwargs(uuid))} + kwargs = {"admin_password": encrypt_str(password, **get_secret_engine_kwargs(uuid, "admin_password"))} return super().create_for_target(target, kwargs=kwargs, uuid=uuid) def load_admin_password(self): encrypted_admin_password = self.db_command.kwargs.get("admin_password") if encrypted_admin_password: - return decrypt_str(encrypted_admin_password, **get_secret_engine_kwargs(self.uuid)) + return decrypt_str(encrypted_admin_password, **get_secret_engine_kwargs(self.uuid, "admin_password")) def build_command(self): if not isinstance(self.enrollment_session, DEPEnrollmentSession): diff --git a/zentral/contrib/mdm/commands/base.py b/zentral/contrib/mdm/commands/base.py index a6bf611413..7d2857e95d 100644 --- a/zentral/contrib/mdm/commands/base.py +++ b/zentral/contrib/mdm/commands/base.py @@ -17,6 +17,10 @@ AUDIT_OMITTED = "" +def get_secret_engine_kwargs(uuid, field): + return {"model": "mdm.devicecommand", "field": field, "uuid": str(uuid)} + + class Command: request_type = None db_name = None diff --git a/zentral/contrib/mdm/commands/device_lock.py b/zentral/contrib/mdm/commands/device_lock.py index 92193ea479..980b8ea80d 100644 --- a/zentral/contrib/mdm/commands/device_lock.py +++ b/zentral/contrib/mdm/commands/device_lock.py @@ -8,15 +8,11 @@ from zentral.contrib.mdm.models import Channel, Platform from zentral.core.secret_engines import decrypt_str, encrypt_str -from .base import Command, CommandBaseForm, CommandBaseSerializer, register_command +from .base import Command, CommandBaseForm, CommandBaseSerializer, register_command, get_secret_engine_kwargs logger = logging.getLogger("zentral.contrib.mdm.commands.device_lock") -def get_secret_engine_kwargs(uuid): - return {"model": "mdm.devicecommand", "field": "PIN", "uuid": str(uuid)} - - class DeviceLockHelperMixin: pin_regex = r"[0-9]{6}" @@ -34,7 +30,7 @@ def get_command_kwargs_with_data(self, uuid, data): kwargs["PhoneNumber"] = phone_number pin = data.get("pin") if pin: - kwargs["PIN"] = encrypt_str(pin, **get_secret_engine_kwargs(uuid)) + kwargs["PIN"] = encrypt_str(pin, **get_secret_engine_kwargs(uuid, "PIN")) return kwargs @@ -100,7 +96,7 @@ def verify_channel_and_device(channel, enrolled_device): def load_pin(self): encrypted_pin = self.db_command.kwargs.get("PIN") if encrypted_pin: - return decrypt_str(encrypted_pin, **get_secret_engine_kwargs(self.uuid)) + return decrypt_str(encrypted_pin, **get_secret_engine_kwargs(self.uuid, "PIN")) def build_command(self): payload = {} diff --git a/zentral/contrib/mdm/commands/erase_device.py b/zentral/contrib/mdm/commands/erase_device.py index e0214d8065..ddbc20ca73 100644 --- a/zentral/contrib/mdm/commands/erase_device.py +++ b/zentral/contrib/mdm/commands/erase_device.py @@ -3,7 +3,7 @@ from rest_framework import serializers from zentral.contrib.mdm.models import Channel, Platform from zentral.core.secret_engines import decrypt_str, encrypt_str -from .base import register_command, Command, CommandBaseForm, CommandBaseSerializer +from .base import register_command, Command, CommandBaseForm, CommandBaseSerializer, get_secret_engine_kwargs logger = logging.getLogger("zentral.contrib.mdm.commands.erase_device") @@ -40,7 +40,7 @@ def get_command_kwargs_with_data(self, uuid, data): if self.pin_required: kwargs["PIN"] = encrypt_str( data.get("pin"), - model="mdm.devicecommand", field="PIN", uuid=str(uuid) + **get_secret_engine_kwargs(uuid, "PIN") ) return kwargs @@ -95,7 +95,7 @@ def verify_channel_and_device(channel, enrolled_device): def load_pin(self): encrypted_pin = self.db_command.kwargs.get("PIN") if encrypted_pin: - return decrypt_str(encrypted_pin, model="mdm.devicecommand", field="PIN", uuid=str(self.uuid)) + return decrypt_str(encrypted_pin, **get_secret_engine_kwargs(self.uuid, "PIN")) def build_command(self): payload = {} diff --git a/zentral/contrib/mdm/commands/rotate_filevault_key.py b/zentral/contrib/mdm/commands/rotate_filevault_key.py index 1a30536ca7..c5bcaf5bd3 100644 --- a/zentral/contrib/mdm/commands/rotate_filevault_key.py +++ b/zentral/contrib/mdm/commands/rotate_filevault_key.py @@ -15,7 +15,7 @@ from zentral.core.secret_engines import decrypt, encrypt from zentral.utils.time import naive_utcnow -from .base import Command, CommandBaseForm, register_command +from .base import Command, CommandBaseForm, get_secret_engine_kwargs, register_command from .security_info import SecurityInfo logger = logging.getLogger("zentral.contrib.mdm.commands.rotate_filevault_key") @@ -79,9 +79,7 @@ def create_for_target( return super().create_for_target( target, kwargs={"encryption_key": encrypt(get_encryption_key_der_bytes(), - model="mdm.devicecommand", - field="encryption_key", - uuid=str(uuid))}, + **get_secret_engine_kwargs(uuid, "encryption_key"))}, queue=queue, delay=delay, uuid=uuid, ) @@ -96,9 +94,7 @@ def create_for_auto_rotation(cls, target, delay_min): def load_encryption_key(self): encrypted_encryption_key = self.db_command.kwargs["encryption_key"] encryption_key = decrypt(encrypted_encryption_key, - model="mdm.devicecommand", - field="encryption_key", - uuid=str(self.uuid)) + **get_secret_engine_kwargs(self.uuid, "encryption_key")) return load_der_private_key(encryption_key, password=None) def build_command(self): diff --git a/zentral/contrib/mdm/commands/set_auto_admin_password.py b/zentral/contrib/mdm/commands/set_auto_admin_password.py index b81d7cca76..5945d9a3ba 100644 --- a/zentral/contrib/mdm/commands/set_auto_admin_password.py +++ b/zentral/contrib/mdm/commands/set_auto_admin_password.py @@ -7,7 +7,7 @@ from zentral.contrib.mdm.models import Channel, DEPEnrollment, Platform from zentral.core.secret_engines import decrypt_str, encrypt_str from zentral.utils.passwords import build_password_hash_dict, serialize_password_hash_dict -from .base import register_command, Command, CommandBaseForm +from .base import register_command, Command, CommandBaseForm, get_secret_engine_kwargs logger = logging.getLogger("zentral.contrib.mdm.commands.set_auto_admin_password") @@ -34,14 +34,10 @@ def get_password_complexity(enrolled_device): return complexity -def get_secret_engine_kwargs(uuid): - return {"model": "mdm.devicecommand", "field": "new_password", "uuid": str(uuid)} - - def get_command_kwargs(uuid, new_password=None, complexity=3): if not new_password: new_password = generate_password(complexity) - return {"new_password": encrypt_str(new_password, **get_secret_engine_kwargs(uuid))} + return {"new_password": encrypt_str(new_password, **get_secret_engine_kwargs(uuid, "new_password"))} class SetAutoAdminPasswordForm(CommandBaseForm): @@ -88,7 +84,7 @@ def create_for_auto_rotation(cls, target, delay_min): def load_new_password(self): new_password = self.db_command.kwargs.get("new_password") - return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid)) + return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid, "new_password")) def build_command(self): return { diff --git a/zentral/contrib/mdm/commands/set_firmware_password.py b/zentral/contrib/mdm/commands/set_firmware_password.py index f5cba372b5..2192c9a178 100644 --- a/zentral/contrib/mdm/commands/set_firmware_password.py +++ b/zentral/contrib/mdm/commands/set_firmware_password.py @@ -3,9 +3,9 @@ from django import forms from zentral.contrib.mdm.models import Channel, Platform from zentral.core.secret_engines import decrypt_str, encrypt_str -from .base import register_command, Command, CommandBaseForm +from .base import register_command, Command, CommandBaseForm, get_secret_engine_kwargs from .restart_device import RestartDevice -from .set_recovery_lock import generate_password, get_secret_engine_kwargs, validate_recovery_password +from .set_recovery_lock import generate_password, validate_recovery_password logger = logging.getLogger("zentral.contrib.mdm.commands.set_recovery_lock") @@ -27,7 +27,7 @@ def get_command_kwargs(self, uuid): kwargs = {} new_password = self.cleaned_data.get("new_password") if new_password: - kwargs["new_password"] = encrypt_str(self.cleaned_data["new_password"], **get_secret_engine_kwargs(uuid)) + kwargs["new_password"] = encrypt_str(new_password, **get_secret_engine_kwargs(uuid, "new_password")) return kwargs @@ -46,7 +46,7 @@ def create_for_automatic_scheduling(cls, target, password=None): uuid = uuid4() return super().create_for_target( target, - kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid))}, + kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid, "new_password"))}, uuid=uuid, ) @@ -60,7 +60,7 @@ def create_for_auto_rotation(cls, target, delay_min): password = generate_password() return super().create_for_target( target, - kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid))}, + kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid, "new_password"))}, queue=True, delay=delay_min * 60, uuid=uuid, ) @@ -78,7 +78,7 @@ def verify_channel_and_device(channel, enrolled_device): def load_new_password(self): new_password = self.db_command.kwargs.get("new_password") if new_password: - return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid)) + return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid, "new_password")) else: return "" diff --git a/zentral/contrib/mdm/commands/set_recovery_lock.py b/zentral/contrib/mdm/commands/set_recovery_lock.py index fab045665f..ddbb04696b 100644 --- a/zentral/contrib/mdm/commands/set_recovery_lock.py +++ b/zentral/contrib/mdm/commands/set_recovery_lock.py @@ -6,7 +6,7 @@ from zentral.contrib.mdm.events import post_recovery_password_event from zentral.contrib.mdm.models import Channel, Platform from zentral.core.secret_engines import decrypt_str, encrypt_str -from .base import register_command, Command, CommandBaseForm +from .base import register_command, Command, CommandBaseForm, get_secret_engine_kwargs logger = logging.getLogger("zentral.contrib.mdm.commands.set_recovery_lock") @@ -29,10 +29,6 @@ def generate_password(length=12): return get_random_string(length) -def get_secret_engine_kwargs(uuid): - return {"model": "mdm.devicecommand", "field": "new_password", "uuid": str(uuid)} - - class SetRecoveryLockForm(CommandBaseForm): new_password = forms.CharField( label="New password", required=False, strip=True, @@ -49,7 +45,7 @@ def get_command_kwargs(self, uuid): kwargs = {} new_password = self.cleaned_data.get("new_password") if new_password: - kwargs["new_password"] = encrypt_str(self.cleaned_data["new_password"], **get_secret_engine_kwargs(uuid)) + kwargs["new_password"] = encrypt_str(new_password, **get_secret_engine_kwargs(uuid, "new_password")) return kwargs @@ -68,7 +64,7 @@ def create_for_automatic_scheduling(cls, target, password=None): uuid = uuid4() return super().create_for_target( target, - kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid))}, + kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid, "new_password"))}, uuid=uuid, ) @@ -81,7 +77,7 @@ def create_for_auto_rotation(cls, target, delay_min): password = generate_password() return super().create_for_target( target, - kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid))}, + kwargs={"new_password": encrypt_str(password, **get_secret_engine_kwargs(uuid, "new_password"))}, queue=True, delay=delay_min * 60, uuid=uuid, ) @@ -97,7 +93,7 @@ def verify_channel_and_device(channel, enrolled_device): def load_new_password(self): new_password = self.db_command.kwargs.get("new_password") if new_password: - return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid)) + return decrypt_str(new_password, **get_secret_engine_kwargs(self.uuid, "new_password")) else: return "" From a4bc2aeeb1b501e193ed31f11c034930733440f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Fri, 20 Nov 2026 15:30:44 +0100 Subject: [PATCH 4/5] mdm: rename audit_secret_kwargs to secret_kwargs 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 --- tests/mdm/test_command_audit.py | 2 +- zentral/contrib/mdm/commands/account_configuration.py | 2 +- zentral/contrib/mdm/commands/base.py | 11 ++++++----- zentral/contrib/mdm/commands/device_lock.py | 2 +- zentral/contrib/mdm/commands/erase_device.py | 2 +- zentral/contrib/mdm/commands/rotate_filevault_key.py | 2 +- .../contrib/mdm/commands/set_auto_admin_password.py | 2 +- zentral/contrib/mdm/commands/set_firmware_password.py | 2 +- zentral/contrib/mdm/commands/set_recovery_lock.py | 2 +- 9 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/mdm/test_command_audit.py b/tests/mdm/test_command_audit.py index c19e8ab985..f902662e55 100644 --- a/tests/mdm/test_command_audit.py +++ b/tests/mdm/test_command_audit.py @@ -49,7 +49,7 @@ def test_no_command_class_declares_a_kwarg_twice(self): for db_name, command_class in registered_commands.items(): with self.subTest(db_name): self.assertEqual( - set(command_class.audit_public_kwargs) & set(command_class.audit_secret_kwargs), + set(command_class.audit_public_kwargs) & set(command_class.secret_kwargs), set() ) diff --git a/zentral/contrib/mdm/commands/account_configuration.py b/zentral/contrib/mdm/commands/account_configuration.py index 0df19f715a..4f1c6a523b 100644 --- a/zentral/contrib/mdm/commands/account_configuration.py +++ b/zentral/contrib/mdm/commands/account_configuration.py @@ -15,7 +15,7 @@ class AccountConfiguration(Command): request_type = "AccountConfiguration" - audit_secret_kwargs = ("admin_password",) + secret_kwargs = ("admin_password",) @staticmethod def verify_channel_and_device(channel, enrolled_device): diff --git a/zentral/contrib/mdm/commands/base.py b/zentral/contrib/mdm/commands/base.py index 7d2857e95d..f5974e8b13 100644 --- a/zentral/contrib/mdm/commands/base.py +++ b/zentral/contrib/mdm/commands/base.py @@ -37,11 +37,12 @@ class Command: # Audit trail. A kwarg is only serialized if it is declared in audit_public_kwargs, # so a command class that forgets to declare anything is silent instead of leaky — - # adding a new secret kwarg cannot leak it by omission. Keys in audit_secret_kwargs - # are reported as redacted, which tells an auditor the value was there without - # disclosing it; anything undeclared is reported as omitted. + # adding a new secret kwarg cannot leak it by omission. secret_kwargs are the kwargs + # encrypted with get_secret_engine_kwargs(). They are reported as redacted, which tells + # an auditor the value was there without disclosing it; anything undeclared is reported + # as omitted. audit_public_kwargs = () - audit_secret_kwargs = () + secret_kwargs = () @classmethod def build_audit_kwargs(cls, kwargs): @@ -49,7 +50,7 @@ def build_audit_kwargs(cls, kwargs): for key, value in kwargs.items(): if key in cls.audit_public_kwargs: audit_kwargs[key] = value - elif key in cls.audit_secret_kwargs: + elif key in cls.secret_kwargs: audit_kwargs[key] = AUDIT_REDACTED else: audit_kwargs[key] = AUDIT_OMITTED diff --git a/zentral/contrib/mdm/commands/device_lock.py b/zentral/contrib/mdm/commands/device_lock.py index 980b8ea80d..8d26b9f0eb 100644 --- a/zentral/contrib/mdm/commands/device_lock.py +++ b/zentral/contrib/mdm/commands/device_lock.py @@ -84,7 +84,7 @@ class DeviceLock(Command): form_class = DeviceLockForm serializer_class = DeviceLockSerializer audit_public_kwargs = ("Message", "PhoneNumber") - audit_secret_kwargs = ("PIN",) + secret_kwargs = ("PIN",) @staticmethod def verify_channel_and_device(channel, enrolled_device): diff --git a/zentral/contrib/mdm/commands/erase_device.py b/zentral/contrib/mdm/commands/erase_device.py index ddbc20ca73..2e239ec1a9 100644 --- a/zentral/contrib/mdm/commands/erase_device.py +++ b/zentral/contrib/mdm/commands/erase_device.py @@ -86,7 +86,7 @@ class EraseDevice(Command): form_class = EraseDeviceForm serializer_class = EraseDeviceSerializer audit_public_kwargs = ("DisallowProximitySetup", "PreserveDataPlan") - audit_secret_kwargs = ("PIN",) + secret_kwargs = ("PIN",) @staticmethod def verify_channel_and_device(channel, enrolled_device): diff --git a/zentral/contrib/mdm/commands/rotate_filevault_key.py b/zentral/contrib/mdm/commands/rotate_filevault_key.py index c5bcaf5bd3..a2cadd72b1 100644 --- a/zentral/contrib/mdm/commands/rotate_filevault_key.py +++ b/zentral/contrib/mdm/commands/rotate_filevault_key.py @@ -54,7 +54,7 @@ class RotateFileVaultKey(Command): display_name = "Rotate FileVault key" verify_target_before_delivery = True form_class = RotateFileVaultKeyForm - audit_secret_kwargs = ("encryption_key",) + secret_kwargs = ("encryption_key",) @staticmethod def verify_channel_and_device(channel, enrolled_device): diff --git a/zentral/contrib/mdm/commands/set_auto_admin_password.py b/zentral/contrib/mdm/commands/set_auto_admin_password.py index 5945d9a3ba..18d422c93d 100644 --- a/zentral/contrib/mdm/commands/set_auto_admin_password.py +++ b/zentral/contrib/mdm/commands/set_auto_admin_password.py @@ -58,7 +58,7 @@ class SetAutoAdminPassword(Command): reschedule_notnow = True verify_target_before_delivery = True form_class = SetAutoAdminPasswordForm - audit_secret_kwargs = ("new_password",) + secret_kwargs = ("new_password",) @staticmethod def verify_channel_and_device(channel, enrolled_device): diff --git a/zentral/contrib/mdm/commands/set_firmware_password.py b/zentral/contrib/mdm/commands/set_firmware_password.py index 2192c9a178..32e9507257 100644 --- a/zentral/contrib/mdm/commands/set_firmware_password.py +++ b/zentral/contrib/mdm/commands/set_firmware_password.py @@ -37,7 +37,7 @@ class SetFirmwarePassword(Command): reschedule_notnow = True verify_target_before_delivery = True form_class = SetFirmwarePasswordForm - audit_secret_kwargs = ("new_password",) + secret_kwargs = ("new_password",) @classmethod def create_for_automatic_scheduling(cls, target, password=None): diff --git a/zentral/contrib/mdm/commands/set_recovery_lock.py b/zentral/contrib/mdm/commands/set_recovery_lock.py index ddbb04696b..b56d3f02d9 100644 --- a/zentral/contrib/mdm/commands/set_recovery_lock.py +++ b/zentral/contrib/mdm/commands/set_recovery_lock.py @@ -55,7 +55,7 @@ class SetRecoveryLock(Command): reschedule_notnow = True verify_target_before_delivery = True form_class = SetRecoveryLockForm - audit_secret_kwargs = ("new_password",) + secret_kwargs = ("new_password",) @classmethod def create_for_automatic_scheduling(cls, target, password=None): From e76efc50515cb7214c0cb8ac2d511f5c45873ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Falconnier?= Date: Fri, 20 Nov 2026 15:32:50 +0100 Subject: [PATCH 5/5] mdm: rewrap the secrets in the command kwargs 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 --- CHANGELOG.md | 2 + tests/mdm/test_command_rewrap_secrets.py | 161 +++++++++++++++++++++++ zentral/contrib/mdm/commands/base.py | 8 ++ zentral/contrib/mdm/models.py | 15 +++ 4 files changed, 186 insertions(+) create mode 100644 tests/mdm/test_command_rewrap_secrets.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ab6e528ef3..5c0d274619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -641,6 +641,8 @@ The MDM check-in, the OTA enrollment and the DEP web enrollment endpoints refuse `rewrap_secrets` saves only the fields that it rewraps now, and does not save an object that has no change. Before, it saved all the fields of all the objects, and set the `updated_at` field of each object to the time of the rewrap. +`rewrap_secrets` rewraps the secrets in the kwargs of the MDM commands now: the PIN of the `DeviceLock` and `EraseDevice` commands, the password of the `AccountConfiguration`, `SetAutoAdminPassword`, `SetRecoveryLock` and `SetFirmwarePassword` commands, and the encryption key of the `RotateFileVaultKey` command. Before, these secrets stayed encrypted with the old secret engine. After the removal of this engine, Zentral could not send or acknowledge a command that was still in the queue, and could not decrypt the FileVault PRK that a device returned for a `RotateFileVaultKey` command. + ## 2026.5 diff --git a/tests/mdm/test_command_rewrap_secrets.py b/tests/mdm/test_command_rewrap_secrets.py new file mode 100644 index 0000000000..cd132f9e90 --- /dev/null +++ b/tests/mdm/test_command_rewrap_secrets.py @@ -0,0 +1,161 @@ +from contextlib import redirect_stderr, redirect_stdout +from io import StringIO +import uuid +from django.core.management import call_command +from django.test import TestCase +from django.utils.crypto import get_random_string +from zentral.contrib.inventory.models import MetaBusinessUnit +from zentral.contrib.mdm.commands import (AccountConfiguration, DeviceInformation, DeviceLock, EraseDevice, + RotateFileVaultKey, SetAutoAdminPassword, SetFirmwarePassword, + SetRecoveryLock) +from zentral.contrib.mdm.commands.base import get_secret_engine_kwargs, load_command, registered_commands +from zentral.contrib.mdm.models import DeviceCommand, UserCommand +from zentral.core.secret_engines import decrypt, encrypt, secret_engines + +from .utils import force_dep_enrollment_session, force_enrolled_user + + +SECRET_COMMANDS = { + AccountConfiguration: "admin_password", + DeviceLock: "PIN", + EraseDevice: "PIN", + RotateFileVaultKey: "encryption_key", + SetAutoAdminPassword: "new_password", + SetFirmwarePassword: "new_password", + SetRecoveryLock: "new_password", +} + + +class CommandRewrapSecretsTestCase(TestCase): + @classmethod + def setUpTestData(cls): + cls.mbu = MetaBusinessUnit.objects.create(name=get_random_string(12)) + cls.mbu.create_enrollment_business_unit() + cls.session, _, _ = force_dep_enrollment_session(cls.mbu, completed=True) + cls.enrolled_device = cls.session.enrolled_device + + def setUp(self): + super().setUp() + secret_engines.load_config({}) + self.addCleanup(secret_engines.load_config, {}) + + def _load_fernet_engine(self): + secret_engines.load_config({ + "fernet": {"backend": "zentral.core.secret_engines.backends.fernet", + "passwords": ["undeuxtrois"]} + }) + + def _force_command(self, command_class, kwargs=None, secret=None, model=DeviceCommand, target=None): + command_uuid = uuid.uuid4() + kwargs = dict(kwargs or {}) + if secret is not None: + key = SECRET_COMMANDS[command_class] + kwargs[key] = encrypt(secret, **get_secret_engine_kwargs(command_uuid, key)) + if target is None: + target = {"enrolled_device": self.enrolled_device} + return model.objects.create(uuid=command_uuid, name=command_class.get_db_name(), kwargs=kwargs, **target) + + def _decrypt(self, db_command, key): + return decrypt(db_command.kwargs[key], **get_secret_engine_kwargs(db_command.uuid, key)) + + # the declarations + + def test_secret_commands(self): + self.assertEqual( + {command_class: command_class.secret_kwargs + for command_class in registered_commands.values() + if command_class.secret_kwargs}, + {command_class: (key,) for command_class, key in SECRET_COMMANDS.items()} + ) + + # rewrap_kwargs + + def test_rewrap_kwargs(self): + self._load_fernet_engine() + command_uuid = uuid.uuid4() + encrypted_pin = encrypt(b"123456", **get_secret_engine_kwargs(command_uuid, "PIN")) + kwargs = {"PIN": encrypted_pin, "Message": "lost"} + rewrapped_kwargs = DeviceLock.rewrap_kwargs(command_uuid, kwargs) + self.assertEqual(kwargs, {"PIN": encrypted_pin, "Message": "lost"}) + self.assertEqual(set(rewrapped_kwargs), {"PIN", "Message"}) + self.assertNotEqual(rewrapped_kwargs["PIN"], encrypted_pin) + self.assertEqual(decrypt(rewrapped_kwargs["PIN"], **get_secret_engine_kwargs(command_uuid, "PIN")), + b"123456") + self.assertEqual(rewrapped_kwargs["Message"], "lost") + + def test_rewrap_kwargs_empty_value(self): + self.assertEqual(SetRecoveryLock.rewrap_kwargs(uuid.uuid4(), {"new_password": ""}), {"new_password": ""}) + + # rewrap_secrets + + def test_rewrap_secrets(self): + for command_class, key in SECRET_COMMANDS.items(): + with self.subTest(command_class.request_type): + secret_engines.load_config({}) + secret = get_random_string(12).encode("utf-8") + db_command = self._force_command(command_class, kwargs={"public": "yes"}, secret=secret) + self.assertEqual(db_command.kwargs[key].split("$")[0], "noop") + self._load_fernet_engine() + db_command.rewrap_secrets() + self.assertEqual(db_command.kwargs[key].split("$")[0], "fernet") + self.assertEqual(self._decrypt(db_command, key), secret) + self.assertEqual(db_command.kwargs["public"], "yes") + + def test_rewrap_secrets_loaded_by_the_command(self): + db_command = self._force_command(DeviceLock, secret=b"123456") + self._load_fernet_engine() + db_command.rewrap_secrets() + db_command.save() + db_command.refresh_from_db() + self.assertEqual(db_command.kwargs["PIN"].split("$")[0], "fernet") + self.assertEqual(load_command(db_command).load_pin(), "123456") + + def test_rewrap_secrets_unknown_command(self): + db_command = DeviceCommand.objects.create( + uuid=uuid.uuid4(), + name=get_random_string(12), + kwargs={"PIN": "noop$MTIzNDU2"}, + enrolled_device=self.enrolled_device, + ) + self._load_fernet_engine() + db_command.rewrap_secrets() + self.assertEqual(db_command.kwargs, {"PIN": "noop$MTIzNDU2"}) + + # rewrap_secrets_queryset + + def test_device_command_rewrap_secrets_queryset(self): + expected_commands = [self._force_command(command_class, secret=b"yolo") for command_class in SECRET_COMMANDS] + self._force_command(EraseDevice, kwargs={"PreserveDataPlan": True}) + self._force_command(DeviceInformation) + DeviceCommand.objects.create(uuid=uuid.uuid4(), name=get_random_string(12), kwargs={"PIN": "noop$MTIzNDU2"}, + enrolled_device=self.enrolled_device) + self.assertEqual( + sorted(DeviceCommand.rewrap_secrets_queryset().values_list("pk", flat=True)), + sorted(db_command.pk for db_command in expected_commands) + ) + + def test_user_command_rewrap_secrets_queryset(self): + enrolled_user = force_enrolled_user(self.enrolled_device) + self._force_command(DeviceInformation, model=UserCommand, target={"enrolled_user": enrolled_user}) + self.assertEqual(UserCommand.rewrap_secrets_queryset().count(), 0) + db_command = self._force_command(DeviceLock, secret=b"123456", + model=UserCommand, target={"enrolled_user": enrolled_user}) + self.assertEqual(list(UserCommand.rewrap_secrets_queryset()), [db_command]) + + # management command + + def test_rewrap_secrets_management_command(self): + db_command = self._force_command(DeviceLock, kwargs={"Message": "lost"}, secret=b"123456") + other_db_command = self._force_command(DeviceInformation, kwargs={"yolo": "fomo"}) + self._load_fernet_engine() + stdout = StringIO() + with redirect_stdout(stdout), redirect_stderr(StringIO()): + call_command("rewrap_secrets") + self.assertIn("DeviceCommand: 1 objects to update", stdout.getvalue()) + self.assertIn("UserCommand: 0 objects to update", stdout.getvalue()) + updated_db_command = DeviceCommand.objects.get(pk=db_command.pk) + self.assertEqual(updated_db_command.kwargs["PIN"].split("$")[0], "fernet") + self.assertEqual(self._decrypt(updated_db_command, "PIN"), b"123456") + self.assertEqual(updated_db_command.kwargs["Message"], "lost") + self.assertEqual(updated_db_command.updated_at, db_command.updated_at) + self.assertEqual(DeviceCommand.objects.get(pk=other_db_command.pk).updated_at, other_db_command.updated_at) diff --git a/zentral/contrib/mdm/commands/base.py b/zentral/contrib/mdm/commands/base.py index f5974e8b13..3619efff31 100644 --- a/zentral/contrib/mdm/commands/base.py +++ b/zentral/contrib/mdm/commands/base.py @@ -8,6 +8,7 @@ from rest_framework import serializers from zentral.contrib.mdm.artifacts import Target from zentral.contrib.mdm.models import Channel, Command as DBCommand, DeviceCommand, UserCommand +from zentral.core.secret_engines import rewrap logger = logging.getLogger("zentral.contrib.mdm.commands.base") @@ -56,6 +57,13 @@ def build_audit_kwargs(cls, kwargs): audit_kwargs[key] = AUDIT_OMITTED return audit_kwargs + @classmethod + def rewrap_kwargs(cls, uuid, kwargs): + return { + key: rewrap(value, **get_secret_engine_kwargs(uuid, key)) if key in cls.secret_kwargs and value else value + for key, value in kwargs.items() + } + @classmethod def get_db_name(cls): return cls.db_name or cls.request_type diff --git a/zentral/contrib/mdm/models.py b/zentral/contrib/mdm/models.py index 86d9c78f77..db6e1b3677 100644 --- a/zentral/contrib/mdm/models.py +++ b/zentral/contrib/mdm/models.py @@ -3668,6 +3668,21 @@ def build_audit_kwargs(self): return {key: AUDIT_OMITTED for key in self.kwargs} return command_class.build_audit_kwargs(self.kwargs) + @classmethod + def rewrap_secrets_queryset(cls): + from zentral.contrib.mdm.commands.base import registered_commands + lookup = Q(pk__in=[]) + for name, command_class in registered_commands.items(): + if command_class.secret_kwargs: + lookup |= Q(name=name, kwargs__has_any_keys=list(command_class.secret_kwargs)) + return cls.objects.filter(lookup) + + def rewrap_secrets(self): + from zentral.contrib.mdm.commands.base import registered_commands + command_class = registered_commands.get(self.name) + if command_class is not None: + self.kwargs = command_class.rewrap_kwargs(self.uuid, self.kwargs) + def serialize_for_event(self, keys_only=False): d = {"pk": self.pk, "uuid": str(self.uuid), "name": self.name} if keys_only: