Merge pull request 'Flag hosts whose reported hostname differs from their registered name' (#13) from feat/hostname-mismatch into main
Reviewed-on: #13
This commit was merged in pull request #13.
This commit is contained in:
commit
40bfdc3236
5 files changed
+56
-3
No files matched your search
@@ -59,6 +59,13 @@ How the agent works it out:
|
|||||||
- **Agent version**: each report includes it. Hosts running an older agent
|
- **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
|
than the server shipped with get a note to run `agent/deploy.sh`; their status
|
||||||
isn't changed.
|
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 (`<ssh target> <name>` 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
|
- **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
|
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
|
packages pulled in by the meta-package show up. Lines whose origin contains
|
||||||
|
|||||||
@@ -106,8 +106,19 @@ def agent_outdated(version: str | None) -> bool:
|
|||||||
return bool(version) and _version_tuple(version) < _version_tuple(AGENT_VERSION)
|
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,
|
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()
|
status = Status()
|
||||||
if received_at is None or report is None:
|
if received_at is None or report is None:
|
||||||
status.raise_to("stale", "never reported")
|
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:
|
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?")
|
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")):
|
if agent_outdated(report.get("agent_version")):
|
||||||
status.note(f"agent {report['agent_version']} is older than {AGENT_VERSION}; "
|
status.note(f"agent {report['agent_version']} is older than {AGENT_VERSION}; "
|
||||||
"run agent/deploy.sh")
|
"run agent/deploy.sh")
|
||||||
|
|||||||
@@ -408,7 +408,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
rows = []
|
rows = []
|
||||||
for h in hosts:
|
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"]))
|
hosts.sort(key=lambda h: (h["status"].rank, h["host"]))
|
||||||
|
|
||||||
counts = {level: 0 for level in LEVELS}
|
counts = {level: 0 for level in LEVELS}
|
||||||
@@ -470,7 +470,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
return self.send(HTTPStatus.NOT_FOUND, page("Not found", "<main><p>Not found.</p></main>", user))
|
return self.send(HTTPStatus.NOT_FOUND, page("Not found", "<main><p>Not found.</p></main>", user))
|
||||||
|
|
||||||
latest = history[0] if history else None
|
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 {}
|
r = latest["report"] if latest else {}
|
||||||
|
|
||||||
def field(label: str, value: Any, code: bool = False) -> str:
|
def field(label: str, value: Any, code: bool = False) -> str:
|
||||||
|
|||||||
@@ -34,6 +34,26 @@ class EvaluateTest(unittest.TestCase):
|
|||||||
self.assertEqual(s.level, "ok", s.reasons)
|
self.assertEqual(s.level, "ok", s.reasons)
|
||||||
self.assertEqual(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):
|
def test_never_reported(self):
|
||||||
self.assertEqual(evaluate(None, None, NOW, CFG).level, "stale")
|
self.assertEqual(evaluate(None, None, NOW, CFG).level, "stale")
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,17 @@ class DashboardTest(ServerCase):
|
|||||||
status, _, _ = self.login("admin", "a-long-password", {"Origin": "https://evil.example"})
|
status, _, _ = self.login("admin", "a-long-password", {"Origin": "https://evil.example"})
|
||||||
self.assertEqual(status, 403)
|
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 <b>", body)
|
||||||
|
|
||||||
def test_login_dashboard_logout(self):
|
def test_login_dashboard_logout(self):
|
||||||
self.add_user("admin", "a-long-password")
|
self.add_user("admin", "a-long-password")
|
||||||
token = self.add_host("web01")
|
token = self.add_host("web01")
|
||||||
|
|||||||
Reference in new issue
Block a user