Flag hosts whose reported hostname differs from their registered name

A note on the dashboard row and host page, not a status change, so a token
copied to the wrong machine is visible. Only the first DNS label is compared,
ignoring case, since registered names are usually hostname -s and the agent
reports hostname -f.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-29 21:17:40 -04:00
1 parent 313961d70b
commit 63ca790779
4 files changed
+49 -3

No files matched your search

+20
View File
@@ -34,6 +34,26 @@ class EvaluateTest(unittest.TestCase):
self.assertEqual(s.level, "ok", s.reasons)
self.assertEqual(s.reasons, [])
def test_hostname_mismatch_is_a_note_not_a_level(self):
rep = report(reported_hostname="db01.example.internal")
s = evaluate(NOW - 60, rep, NOW, CFG, "web01")
self.assertEqual(s.level, "ok")
self.assertEqual(len(s.reasons), 1)
self.assertIn("registered as web01 but reports itself as db01.example.internal", s.reasons[0])
# Same worst level as without the check.
rep = report(reported_hostname="db01", reboot_required=True)
self.assertEqual(evaluate(NOW - 60, rep, NOW, CFG, "web01").level, "reboot")
def test_hostname_match_is_lenient_and_optional(self):
for registered, reported in (("web01", "web01.example.internal"), ("WEB01", "web01"),
("web01.example.internal", "web01"), ("web01", "web01"),
("web01", None)):
with self.subTest(registered=registered, reported=reported):
rep = report() if reported is None else report(reported_hostname=reported)
self.assertEqual(evaluate(NOW - 60, rep, NOW, CFG, registered).reasons, [])
# Without a registered name (alerts, history rows) nothing is compared.
self.assertEqual(evaluate(NOW - 60, report(reported_hostname="db01"), NOW, CFG).reasons, [])
def test_never_reported(self):
self.assertEqual(evaluate(None, None, NOW, CFG).level, "stale")
+11
View File
@@ -163,6 +163,17 @@ class DashboardTest(ServerCase):
status, _, _ = self.login("admin", "a-long-password", {"Origin": "https://evil.example"})
self.assertEqual(status, 403)
def test_hostname_mismatch_shown_on_dashboard_and_host_page(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")
self.post_report(token, {"kernel_running": "6.8.0-85-generic", "apt_lists_age_seconds": 60,
"reported_hostname": "db01.example.internal <b>"})
_, headers, _ = self.login("admin", "a-long-password")
cookie = {"Cookie": headers["Set-Cookie"].split(";")[0]}
for path in ("/", "/host/web01"):
body = self.request("GET", path, headers=cookie)[2]
self.assertIn("registered as web01 but reports itself as db01.example.internal &lt;b&gt;", body)
def test_login_dashboard_logout(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")