From 46f5ecec3965477270cef2687a7b69a85fbe3005 Mon Sep 17 00:00:00 2001 From: sebastian Date: Thu, 24 Sep 2026 15:45:03 +0200 Subject: [PATCH] mdm: A DEP device refresh keeps the MDM migration deadline Apple leaves the MDM migration deadline out of the device details, even when the device has one. Zentral read an absent key as no deadline, so the refresh of a device, and the profile assignments that read the device back from the device details endpoint, cleared the deadline that the synchronization had just stored. The device lists, the fetch and the sync, report the deadline, and keep reporting it after it is removed in Apple Business Manager. Only a change of MDM server clears it. The deadline is updated from the keys of the payload now: a deadline in the payload is stored, a deleted operation clears it, because the device left the MDM server, and an absent key leaves it alone. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + tests/mdm/test_dep.py | 120 +++++++++++++++++++++++++++++++++++++ zentral/contrib/mdm/dep.py | 10 +++- 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d71c40f4a..3296f9d81d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,8 @@ The `file_uri` and `file_sha256` attributes of the data asset endpoints are not Zentral uses version 10 of the Apple device assignment protocol now, and not version 3. Apple sends more device attributes with it. The web console and the DEP device endpoints show them: the Wi-Fi, Bluetooth and Ethernet MAC addresses, the EID, the IMEI and MEID numbers, the MDM migration deadline, and if the device replaces another one. The list endpoint has a new `is_replacement_device` filter. +The MDM migration deadline is the one Apple reports. Apple keeps reporting it when it is removed in Apple Business Manager, and clears it only when the device changes MDM server, so Zentral shows it until then too. A refresh of the device keeps it: Apple leaves it out of the device details. + Only the devices that a synchronization reports get the new attributes. A delta synchronization reports the devices that changed, so a full synchronization (`?full_sync=true`) is necessary to get the attributes of all the devices. It posts one `dep_device_change` event per device, one time only: the new attributes get their value, and the `os` of an iPad changes from `iOS` to `iPadOS`. Apple can throttle the assignment of the default enrollment. Zentral counts the throttled devices apart from the failed ones now, and assigns them again after the delay Apple gives. diff --git a/tests/mdm/test_dep.py b/tests/mdm/test_dep.py index 97571f5058..dadafd0b65 100644 --- a/tests/mdm/test_dep.py +++ b/tests/mdm/test_dep.py @@ -11,6 +11,7 @@ assign_dep_virtual_server_default_enrollment, define_dep_profile, iter_unassigned_dep_device_serial_numbers, + refresh_dep_device, sync_dep_virtual_server_devices, ) from zentral.contrib.mdm.dep_client import ( @@ -599,6 +600,125 @@ def device_iterator(): self.assertEqual(dep_device.last_op_date, datetime(2026, 8, 24, 12, 0, 0)) self.assertFalse(dep_device.is_deleted()) + # MDM migration deadline + + def force_dep_device_with_migration_deadline(self, server): + dep_device = force_dep_device(server=server, profile_status=DEPDevice.PROFILE_STATUS_EMPTY) + DEPDevice.objects.filter(pk=dep_device.pk).update(mdm_migration_deadline=datetime(2026, 9, 24, 22, 0, 0), + last_op_date=None) + dep_device.refresh_from_db() + return dep_device + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_token") + def test_sync_dep_virtual_server_devices_mdm_server_change_clears_the_migration_deadline(self, from_dep_token): + server = self.force_synced_server() + dep_device = self.force_dep_device_with_migration_deadline(server) + + def device_iterator(): + # the device moved to another MDM server + yield from [{'op_date': '2026-09-25T08:00:00Z', + 'op_type': 'deleted', + 'serial_number': dep_device.serial_number}] + return get_random_string(12) + + client = Mock() + client.sync_devices.return_value = CursorIterator(device_iterator()) + from_dep_token.return_value = client + list(sync_dep_virtual_server_devices(server)) + dep_device.refresh_from_db() + self.assertEqual(dep_device.last_op_type, DEPDevice.OP_TYPE_DELETED) + self.assertIsNone(dep_device.mdm_migration_deadline) + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_token") + def test_sync_dep_virtual_server_devices_keeps_the_migration_deadline(self, from_dep_token): + server = self.force_synced_server() + dep_device = self.force_dep_device_with_migration_deadline(server) + + def device_iterator(): + yield from [{'op_date': '2026-09-25T08:00:00Z', + 'op_type': 'modified', + 'profile_status': 'empty', + 'serial_number': dep_device.serial_number}] + return get_random_string(12) + + client = Mock() + client.sync_devices.return_value = CursorIterator(device_iterator()) + from_dep_token.return_value = client + list(sync_dep_virtual_server_devices(server)) + dep_device.refresh_from_db() + self.assertEqual(dep_device.last_op_type, DEPDevice.OP_TYPE_MODIFIED) + self.assertEqual(dep_device.mdm_migration_deadline, datetime(2026, 9, 24, 22, 0, 0)) + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_virtual_server") + def test_assign_dep_virtual_server_default_enrollment_keeps_the_migration_deadline(self, from_dep_virtual_server): + server, enrollment = self.force_server_with_default_enrollment() + dep_device = self.force_dep_device_with_migration_deadline(server) + client = Mock() + client.get_device_batch_size.return_value = DEVICE_BATCH_SIZE + client.assign_profile.return_value = {"devices": {dep_device.serial_number: "SUCCESS"}} + client.get_devices.return_value = { + dep_device.serial_number: { + "profile_uuid": str(enrollment.uuid).upper().replace("-", ""), + "profile_status": "assigned", + "profile_assign_time": "2026-09-24T10:45:02Z", + } + } + from_dep_virtual_server.return_value = client + + self.assertEqual(assign_dep_virtual_server_default_enrollment(server)["assigned"], 1) + dep_device.refresh_from_db() + self.assertEqual(dep_device.enrollment, enrollment) + self.assertEqual(dep_device.mdm_migration_deadline, datetime(2026, 9, 24, 22, 0, 0)) + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_virtual_server") + def test_assign_dep_device_profile_keeps_the_migration_deadline(self, from_dep_virtual_server): + server, enrollment = self.force_server_with_default_enrollment() + dep_device = self.force_dep_device_with_migration_deadline(server) + client = Mock() + client.assign_profile.return_value = {"devices": {dep_device.serial_number: "SUCCESS"}} + client.get_devices.return_value = { + dep_device.serial_number: { + "profile_uuid": str(enrollment.uuid).upper().replace("-", ""), + "profile_status": "assigned", + "profile_assign_time": "2026-09-24T10:45:02Z", + } + } + from_dep_virtual_server.return_value = client + + assign_dep_device_profile(dep_device, enrollment) + dep_device.refresh_from_db() + self.assertEqual(dep_device.enrollment, enrollment) + self.assertEqual(dep_device.mdm_migration_deadline, datetime(2026, 9, 24, 22, 0, 0)) + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_virtual_server") + def test_refresh_dep_device_keeps_the_migration_deadline(self, from_dep_virtual_server): + dep_device = self.force_dep_device_with_migration_deadline(force_dep_virtual_server()) + client = Mock() + client.get_devices.return_value = { + dep_device.serial_number: {"profile_status": "empty", + "serial_number": dep_device.serial_number} + } + from_dep_virtual_server.return_value = client + + refresh_dep_device(dep_device) + dep_device.refresh_from_db() + self.assertEqual(dep_device.mdm_migration_deadline, datetime(2026, 9, 24, 22, 0, 0)) + + @patch("zentral.contrib.mdm.dep.DEPClient.from_dep_virtual_server") + def test_refresh_dep_device_updates_the_migration_deadline(self, from_dep_virtual_server): + dep_device = self.force_dep_device_with_migration_deadline(force_dep_virtual_server()) + client = Mock() + client.get_devices.return_value = { + dep_device.serial_number: {"mdm_migration_deadline": "2026-09-24T23:00:00Z", + "profile_status": "empty", + "serial_number": dep_device.serial_number} + } + from_dep_virtual_server.return_value = client + + refresh_dep_device(dep_device) + dep_device.refresh_from_db() + self.assertEqual(dep_device.mdm_migration_deadline, datetime(2026, 9, 24, 23, 0, 0)) + # events def force_synced_server(self): diff --git a/zentral/contrib/mdm/dep.py b/zentral/contrib/mdm/dep.py index 3cf0959741..3fc00f8958 100644 --- a/zentral/contrib/mdm/dep.py +++ b/zentral/contrib/mdm/dep.py @@ -149,7 +149,6 @@ def dep_device_update_dict(device, known_enrollments=None): if known_enrollments is None: known_enrollments = {} update_d = {"enrollment": None, - "mdm_migration_deadline": None, "profile_uuid": None, "profile_assign_time": None, "profile_push_time": None, @@ -175,6 +174,14 @@ def dep_device_update_dict(device, known_enrollments=None): update_d["is_replacement_device"] = bool(device.get("is_replacement_device")) + # Apple leaves the migration deadline out of the device details even when there is one, and keeps + # reporting it in the device lists after it is removed in Apple Business Manager. Only a change of + # MDM server clears it, so an absent key leaves it alone, and a deleted operation clears it. + if "mdm_migration_deadline" in device: + update_d["mdm_migration_deadline"] = parser.parse(device["mdm_migration_deadline"]) + elif device.get("op_type") == DEPDevice.OP_TYPE_DELETED: + update_d["mdm_migration_deadline"] = None + # Apple reports this one only with a deleted operation, so an absent key must leave it alone # rather than clear it. Left out of the update dict, it is left out of the update statement. if "released_by_replacement" in device: @@ -205,7 +212,6 @@ def dep_device_update_dict(device, known_enrollments=None): # datetime nullable attributes for attr in ("device_assigned_date", - "mdm_migration_deadline", "profile_assign_time", "profile_push_time"): try: