From 4b3d0136e93e9b6d1de2ddaf3aae2cdade0dbb85 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Wed, 18 Nov 2026 18:09:52 +0100 Subject: [PATCH] Wait for the device certificate before probing Linux enrollment --- crates/server/src/bootstrap/lifecycle.py | 5 ++++ tests/linux_release_bootstrap.py | 35 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/crates/server/src/bootstrap/lifecycle.py b/crates/server/src/bootstrap/lifecycle.py index 298148f..c2f52a9 100644 --- a/crates/server/src/bootstrap/lifecycle.py +++ b/crates/server/src/bootstrap/lifecycle.py @@ -194,7 +194,12 @@ def installed(): def healthy(version, baseline, duration=90, pin=None): until = time.monotonic() + duration started = time.time() + certificate_pointer = STATE / 'certificates/current.json' while time.monotonic() < until: + # The agent writes this pointer after obtaining its first device certificate. + if not certificate_pointer.is_file(): + time.sleep(2) + continue try: record = installed() contact = record.get('last_contact_at') diff --git a/tests/linux_release_bootstrap.py b/tests/linux_release_bootstrap.py index dd4fa3c..977aaad 100644 --- a/tests/linux_release_bootstrap.py +++ b/tests/linux_release_bootstrap.py @@ -1,5 +1,6 @@ """Retry coverage for the HTTPS Linux bootstrap without installing on the test host.""" from contextlib import ExitStack, redirect_stdout +from datetime import datetime, timezone from importlib.util import module_from_spec, spec_from_file_location import io import json @@ -17,6 +18,40 @@ lifecycle = module_from_spec(spec) spec.loader.exec_module(lifecycle) +class EnrollmentHeartbeat(unittest.TestCase): + def test_health_probe_waits_for_the_first_certificate_before_fetching(self): + with tempfile.TemporaryDirectory() as folder: + state = Path(folder) + certificate = state / 'certificates/current.json' + certificate.parent.mkdir() + now = datetime(2026, 11, 18, 17, tzinfo=timezone.utc) + record = {'last_contact_at': now.isoformat(), + 'agent_version': '0.1.0-arm64.1', 'connection_generation': 1} + + def finish_enrollment(_seconds): + probe.assert_not_called() + certificate.touch() + + with patch.object(lifecycle, 'STATE', state), \ + patch.object(lifecycle.time, 'time', return_value=now.timestamp()), \ + patch.object(lifecycle.time, 'monotonic', side_effect=[0, 0, 2]), \ + patch.object(lifecycle.time, 'sleep', side_effect=finish_enrollment) as wait, \ + patch.object(lifecycle, 'installed', return_value=record) as probe, \ + patch.object(lifecycle.subprocess, 'run', return_value=SimpleNamespace(returncode=0)): + self.assertTrue(lifecycle.healthy('0.1.0-arm64.1', 0)) + wait.assert_called_once_with(2) + probe.assert_called_once_with() + + def test_missing_certificate_still_times_out(self): + with tempfile.TemporaryDirectory() as folder, \ + patch.object(lifecycle, 'STATE', Path(folder)), \ + patch.object(lifecycle.time, 'monotonic', side_effect=[0, 0, 2]), \ + patch.object(lifecycle.time, 'sleep'), \ + patch.object(lifecycle, 'installed', return_value={}) as probe: + self.assertFalse(lifecycle.healthy('0.1.0-arm64.1', 0, duration=1)) + probe.assert_not_called() + + class EnrollmentRetry(unittest.TestCase): def setUp(self): self.stack = ExitStack()