diff --git a/README.md b/README.md index 8e30bce..b3509ed 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,13 @@ How the agent works it out: - **Agent version**: each report includes it. Hosts running an older agent than the server shipped with get a note to run `agent/deploy.sh`; their status isn't changed. +- **Hostname check**: each report includes the host's own name. If its first + DNS label differs from the name the host is registered under (ignoring case), + the row gets a note: "registered as web01 but reports itself as db01". This + catches a token copied to the wrong machine. It never changes the status. A + host you registered under a custom label (` ` in + `agent/hosts`) will always show the note; register it under its `hostname -s` + to clear it. - **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel packages pulled in by the meta-package show up. Lines whose origin contains diff --git a/server/kernelwatch/status.py b/server/kernelwatch/status.py index e54b29e..cd9e761 100644 --- a/server/kernelwatch/status.py +++ b/server/kernelwatch/status.py @@ -106,8 +106,19 @@ def agent_outdated(version: str | None) -> bool: return bool(version) and _version_tuple(version) < _version_tuple(AGENT_VERSION) +def hostname_mismatch(registered: str, report: dict[str, Any]) -> str | None: + """The hostname the agent reports, if it can't be the machine `registered` names. + + Registered names are usually `hostname -s` while the agent reports `hostname -f`, + so only the first DNS label is compared, ignoring case.""" + reported = report.get("reported_hostname") + if not reported or reported.split(".")[0].lower() == registered.split(".")[0].lower(): + return None + return reported + + def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float, - cfg: Config) -> Status: + cfg: Config, host: str | None = None) -> Status: status = Status() if received_at is None or report is None: status.raise_to("stale", "never reported") @@ -142,6 +153,10 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float, elif apt_age / 3600 > cfg.apt_stale_after_hours: status.raise_to("update", f"package lists {apt_age / 86400:.0f}d old; apt update not running?") + if host and (reported := hostname_mismatch(host, report)): + status.note(f"registered as {host} but reports itself as {reported}; " + "token on the wrong machine?") + if agent_outdated(report.get("agent_version")): status.note(f"agent {report['agent_version']} is older than {AGENT_VERSION}; " "run agent/deploy.sh") diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py index 9a48b52..6f193c0 100644 --- a/server/kernelwatch/web.py +++ b/server/kernelwatch/web.py @@ -408,7 +408,7 @@ class Handler(BaseHTTPRequestHandler): rows = [] for h in hosts: - h["status"] = evaluate(h["received_at"], h["report"], now, self.cfg) + h["status"] = evaluate(h["received_at"], h["report"], now, self.cfg, h["host"]) hosts.sort(key=lambda h: (h["status"].rank, h["host"])) counts = {level: 0 for level in LEVELS} @@ -470,7 +470,7 @@ class Handler(BaseHTTPRequestHandler): return self.send(HTTPStatus.NOT_FOUND, page("Not found", "

Not found.

", user)) latest = history[0] if history else None - status = evaluate(latest and latest["received_at"], latest and latest["report"], now, self.cfg) + status = evaluate(latest and latest["received_at"], latest and latest["report"], now, self.cfg, name) r = latest["report"] if latest else {} def field(label: str, value: Any, code: bool = False) -> str: diff --git a/tests/test_status.py b/tests/test_status.py index 02f903d..89ef182 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -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") diff --git a/tests/test_web.py b/tests/test_web.py index 6bba422..71ef748 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -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 "}) + _, 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 <b>", body) + def test_login_dashboard_logout(self): self.add_user("admin", "a-long-password") token = self.add_host("web01")