Check that the release's security repository is configured and fresh
CI / tests (Python 3.10) (pull_request) Successful in 36s
CI / tests (Python 3.11) (pull_request) Successful in 36s
CI / tests (Python 3.12) (pull_request) Successful in 37s
CI / tests (Python 3.13) (pull_request) Successful in 37s
CI / lint (pull_request) Successful in 4s

security_count is only meaningful if apt has the release's security
suite. A host missing it from its sources shows 0 security updates
forever, and one whose apt can't fetch it (expired key, wrong URL after
a release upgrade, a mirror that fell behind) keeps the last copy while
the update stamps and other lists move on, so neither the simulation nor
the package list age notices.

The agent (0.9.0) now finds apt's list for <codename>-security by its
Suite: (Ubuntu) or Codename: (Debian) field, since file names depend on
the mirror (mirror+file: sources, several mirrors), and reports
security_repo_suite and security_repo_age_seconds from its Date:, the
newest copy winning. The server puts the host in update when there's no
such list, or when it's older than security_repo_stale_after_days
(default 7) while the package lists are otherwise fresh; if all of
them are old, the existing "package lists N days old" reason covers
it. Releases past the end of support are skipped: their suite has
stopped publishing, and support.check already says so. ESM suites
publish too rarely to judge by date and aren't checked.

The host page gets a "Security repository" row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-01 14:58:10 -05:00
1 parent fa6cca4f64
commit c9912d5fbc
12 files changed
+261 -4

No files matched your search

+64
View File
@@ -463,6 +463,70 @@ X
os.utime(partial, (t, t))
self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 3600, delta=60)
def write_list(self, name, fields, age=3600, mtime_age=None):
"""An apt list (Release or InRelease) in the fake root, its Date: `age` seconds ago."""
body = "".join(f"{k}: {v}\n" for k, v in fields.items())
if age is not None:
body += "Date: " + time.strftime("%a, %d %b %Y %H:%M:%S UTC", time.gmtime(time.time() - age)) + "\n"
body += "MD5Sum:\n 0123 456 main/binary-amd64/Packages\nSHA256:\n abcd 456 Suite: trixie-security\n"
if name.endswith("_InRelease"):
body = "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA512\n\n" + body + "-----BEGIN PGP SIGNATURE-----\n"
self.write_root(f"var/lib/apt/lists/{name}", body, age=mtime_age)
def set_os(self, text):
self.write_root("etc/os-release", text)
def test_security_repo_debian(self):
self.write_list("deb.debian.org_debian_dists_trixie_InRelease",
{"Suite": "stable", "Codename": "trixie"}, age=60)
self.write_list("security.debian.org_debian-security_dists_trixie-security_InRelease",
{"Suite": "stable-security", "Codename": "trixie-security"}, age=5 * 3600)
self.set_os('ID=debian\nVERSION_ID="13"\nVERSION_CODENAME=trixie\n')
r = self.report()
self.assertEqual(r["security_repo_suite"], "trixie-security")
self.assertAlmostEqual(r["security_repo_age_seconds"], 5 * 3600, delta=60)
def test_security_repo_ubuntu_newest_mirror_and_odd_file_names(self):
# Ubuntu: Suite is the pocket, Codename the release. Mirrors and mirror+file:
# sources give other file names; the newest copy counts.
self.write_list("archive.ubuntu.com_ubuntu_dists_noble-security_InRelease",
{"Suite": "noble-security", "Codename": "noble"}, age=3 * 86400)
self.write_list("_etc_apt_mirrors_ubuntu.list_dists_noble-security_InRelease",
{"Suite": "noble-security", "Codename": "noble"}, age=7200)
self.write_list("esm.ubuntu.com_infra_ubuntu_dists_noble-infra-security_InRelease",
{"Suite": "noble-infra-security", "Codename": "noble-infra-security"}, age=60)
self.set_os('ID=ubuntu\nVERSION_ID="24.04"\nVERSION_CODENAME=noble\nUBUNTU_CODENAME=noble\n')
r = self.report()
self.assertEqual(r["security_repo_suite"], "noble-security")
self.assertAlmostEqual(r["security_repo_age_seconds"], 7200, delta=60)
def test_security_repo_missing(self):
self.write_list("deb.debian.org_debian_dists_trixie_InRelease",
{"Suite": "stable", "Codename": "trixie"})
self.set_os('ID=debian\nVERSION_ID="13"\nVERSION_CODENAME=trixie\n')
r = self.report()
self.assertEqual(r["security_repo_suite"], "trixie-security")
self.assertIsNone(r["security_repo_age_seconds"])
def test_security_repo_matches_fields_not_checksum_lines(self):
# write_list puts "Suite: trixie-security" in a checksum line of every list.
self.write_list("deb.debian.org_debian_dists_trixie_Release",
{"Suite": "stable", "Codename": "trixie"})
self.set_os('ID=debian\nVERSION_ID="13"\nVERSION_CODENAME=trixie\n')
self.assertIsNone(self.report()["security_repo_age_seconds"])
def test_security_repo_without_date_uses_mtime(self):
self.write_list("local_dists_trixie-security_Release",
{"Codename": "trixie-security"}, age=None, mtime_age=4 * 3600)
self.set_os('ID=debian\nVERSION_ID="13"\nVERSION_CODENAME=trixie\n')
self.assertAlmostEqual(self.report()["security_repo_age_seconds"], 4 * 3600, delta=60)
def test_security_repo_without_codename(self):
# The default fake os-release has no VERSION_CODENAME (like Debian sid).
r = self.report()
self.assertIsNone(r["security_repo_suite"])
self.assertIsNone(r["security_repo_age_seconds"])
def test_send_without_config_fails_cleanly(self):
proc = self.run_agent()
self.assertNotEqual(proc.returncode, 0)
+9
View File
@@ -117,6 +117,15 @@ class ConfigTest(unittest.TestCase):
with self.subTest(bad=bad), self.assertRaises(SystemExit):
config.load(self.write(tmp, f"unattended_upgrades_stale_after_days = {bad}\n"))
def test_security_repo_stale_days_validated(self):
with tempdir() as tmp:
self.assertEqual(config.Config().security_repo_stale_after_days, 7.0)
self.assertEqual(config.load(self.write(tmp, "security_repo_stale_after_days = 0\n"))
.security_repo_stale_after_days, 0)
for bad in ("-1", "inf", "nan", '"7"'):
with self.subTest(bad=bad), self.assertRaises(SystemExit):
config.load(self.write(tmp, f"security_repo_stale_after_days = {bad}\n"))
def test_fallback_parser_matches_tomllib(self):
"""Python 3.10 has no tomllib; the fallback must read the same configs."""
if config.tomllib is None:
+53
View File
@@ -352,6 +352,59 @@ class EvaluateTest(unittest.TestCase):
evaluate(NOW, rep, NOW, Config(os_eol_warn_days=200)).reasons[0])
self.assertEqual(self.level(rep).reasons, [], "outside the default 90 days")
def sec(self, age=3 * 3600, suite="trixie-security", cfg=CFG, **kw):
fields = dict(os_id="debian", os_version_id="13", security_repo_suite=suite,
security_repo_age_seconds=age)
fields.update(kw)
return evaluate(NOW - 60, report(**fields), NOW, cfg)
def test_security_repo_fresh(self):
s = self.sec()
self.assertEqual((s.level, s.reasons), ("ok", []))
def test_security_repo_missing(self):
s = self.sec(age=None)
self.assertEqual(s.level, "update")
self.assertEqual(s.reasons, ["apt has no trixie-security repository, so security updates "
"can't arrive; add it to the apt sources and run apt update"])
def test_security_repo_stale_while_apt_update_succeeds(self):
s = self.sec(age=10 * 86400)
self.assertEqual(s.level, "update")
self.assertEqual(s.reasons, ["apt's trixie-security list is 10d old while apt update otherwise "
"succeeds; run apt update and look for errors about trixie-security"])
self.assertEqual(self.sec(age=6 * 86400).level, "ok", "within the default 7 days")
def test_security_repo_stale_with_all_lists_stale_reports_once(self):
s = self.sec(age=10 * 86400, apt_lists_age_seconds=10 * 86400)
self.assertEqual(s.reasons, ["package lists 10d old; apt update not running?"])
def test_security_repo_check_can_be_disabled(self):
cfg = Config(security_repo_stale_after_days=0)
self.assertEqual(self.sec(age=None, cfg=cfg).level, "ok")
self.assertEqual(self.sec(age=30 * 86400, cfg=cfg).level, "ok")
def test_security_repo_not_reported(self):
# Agents before 0.9.0, and releases without a codename.
self.assertEqual(self.sec(suite=None, age=None).level, "ok")
rep = report(os_id="debian", os_version_id="13")
self.assertEqual(self.level(rep).level, "ok")
def test_security_repo_ignored_past_end_of_support(self):
# NOW is 2027-01-15. Debian 11's LTS ended 2026-08-31, so bullseye-security no
# longer publishes; the end-of-support reason is the one that matters.
s = self.sec(age=60 * 86400, suite="bullseye-security", os_version_id="11")
self.assertEqual(len(s.reasons), 1, s.reasons)
self.assertIn("Debian 11 security support ended", s.reasons[0])
# Ubuntu 20.04 past standard support with ESM: focal-security is frozen, but fine.
s = self.sec(age=400 * 86400, suite="focal-security", os_id="ubuntu", os_version_id="20.04",
ubuntu_esm_infra="enabled")
self.assertEqual((s.level, s.reasons), ("ok", []))
def test_security_repo_checked_within_standard_support(self):
s = self.sec(age=10 * 86400, suite="jammy-security", os_id="ubuntu", os_version_id="22.04")
self.assertEqual(s.level, "update")
def uu(self, success=6 * 3600, run=6 * 3600, state="enabled", **kw):
return self.level(report(unattended_upgrades=state, unattended_upgrades_success_age_seconds=success,
unattended_upgrades_run_age_seconds=run, **kw))
+18
View File
@@ -221,6 +221,24 @@ class DashboardTest(ServerCase):
body = self.request("GET", "/host/web01", headers={"Cookie": headers["Set-Cookie"].split(";")[0]})[2]
self.assertIn("standard until 2029-05-31, ESM until 2034-04-25 (esm-infra: enabled)", body)
def test_host_page_shows_security_repo(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")
self.post_report(token, {"kernel_running": "6.8.0-136-generic", "apt_lists_age_seconds": 60,
"security_repo_suite": "noble-security",
"security_repo_age_seconds": 14 * 3600 + 120})
_, headers, _ = self.login("admin", "a-long-password")
cookie = {"Cookie": headers["Set-Cookie"].split(";")[0]}
body = self.request("GET", "/host/web01", headers=cookie)[2]
self.assertIn("noble-security, published 14h 2m before the report", body)
self.post_report(self.add_host("web02"), {"kernel_running": "6.8.0-136-generic",
"apt_lists_age_seconds": 60,
"security_repo_suite": "noble-security"})
body = self.request("GET", "/host/web02", headers=cookie)[2]
self.assertIn("noble-security: not in apt&#x27;s sources", body)
self.assertIn("apt has no noble-security repository", body)
def test_login_dashboard_logout(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")