diff --git a/README.md b/README.md index 0202028..77fa6f8 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ fleet is running its newest kernel, has updates waiting, or needs a reboot. ingest API and a login-protected dashboard. You don't need pip, a venv or a build step. -Supported: agents on **Debian 12/13** and **Ubuntu 24.04 LTS**; server on -**Debian 12+** or **Ubuntu 24.04** (needs the system `python3` ≥ 3.11). +Supported: agents on **Debian 12/13** and **Ubuntu 22.04/24.04 LTS**; server on +**Debian 12+** or **Ubuntu 22.04+** (the system `python3`, 3.10 or newer). ``` host ──(systemd timer)── kernelwatch-agent ──HTTPS POST /api/v1/report──┐ diff --git a/agent/install.sh b/agent/install.sh index 32d3d8e..5a956f4 100755 --- a/agent/install.sh +++ b/agent/install.sh @@ -26,7 +26,7 @@ done # shellcheck source=/dev/null . /etc/os-release case "$ID:$VERSION_ID" in -debian:12 | debian:13 | ubuntu:24.04) ;; +debian:12 | debian:13 | ubuntu:22.04 | ubuntu:24.04) ;; *) echo "warning: untested OS $PRETTY_NAME; continuing" >&2 ;; esac diff --git a/server/install.sh b/server/install.sh index de95e1c..b6fa274 100755 --- a/server/install.sh +++ b/server/install.sh @@ -11,8 +11,8 @@ set -euo pipefail here=$(cd "$(dirname "$0")" && pwd) [[ $(id -u) == 0 ]] || { echo "run as root" >&2; exit 1; } -if ! /usr/bin/python3 -c 'import sys; sys.exit(sys.version_info < (3, 11))' 2>/dev/null; then - echo "kernelwatch needs /usr/bin/python3 >= 3.11 (Debian 12+, Ubuntu 24.04)" >&2 +if ! /usr/bin/python3 -c 'import sys; sys.exit(sys.version_info < (3, 10))' 2>/dev/null; then + echo "kernelwatch needs /usr/bin/python3 >= 3.10 (Debian 12+, Ubuntu 22.04+)" >&2 exit 1 fi diff --git a/server/kernelwatch-server b/server/kernelwatch-server index 336d994..bb5e0f7 100755 --- a/server/kernelwatch-server +++ b/server/kernelwatch-server @@ -1,5 +1,5 @@ #!/usr/bin/python3 -I -"""Entry point. Runs from a checkout or /opt/kernelwatch with the system python3 (3.11+).""" +"""Entry point. Runs from a checkout or /opt/kernelwatch with the system python3 (3.10+).""" import os import sys diff --git a/server/kernelwatch/config.py b/server/kernelwatch/config.py index 194630c..c67b580 100644 --- a/server/kernelwatch/config.py +++ b/server/kernelwatch/config.py @@ -4,8 +4,14 @@ from __future__ import annotations import dataclasses import os -import tomllib +import re from dataclasses import dataclass +from typing import Any + +try: + import tomllib +except ModuleNotFoundError: # Python 3.10 (Ubuntu 22.04): see _parse_flat_toml + tomllib = None DEFAULT_PATH = "/etc/kernelwatch/server.toml" @@ -38,7 +44,8 @@ def load(path: str | None = None) -> Config: raise SystemExit(f"config file not found: {path}") with open(path, "rb") as f: - raw = tomllib.load(f) + data = f.read() + raw = tomllib.loads(data.decode()) if tomllib else _parse_flat_toml(data.decode(), path) fields = {f.name: f for f in dataclasses.fields(Config)} unknown = sorted(set(raw) - set(fields)) @@ -54,3 +61,45 @@ def load(path: str | None = None) -> Config: raise SystemExit(f"{path}: {name} must be {expected.__name__}") values[name] = value return Config(**values) + + +_KEY_VALUE = re.compile(r"^([A-Za-z0-9_-]+)\s*=\s*(.*)$") + + +def _parse_flat_toml(text: str, path: str) -> dict[str, Any]: + """The subset of TOML the config uses (top-level `key = value` with strings, + booleans, integers and floats), for Pythons without tomllib.""" + out: dict[str, Any] = {} + for lineno, line in enumerate(text.splitlines(), 1): + line = line.strip() + if not line or line.startswith("#"): + continue + m = _KEY_VALUE.match(line) + if not m: + raise SystemExit(f"{path}:{lineno}: expected `key = value`") + key, value = m.groups() + if key in out: + raise SystemExit(f"{path}:{lineno}: duplicate key {key}") + out[key] = _parse_flat_value(value, f"{path}:{lineno}") + return out + + +def _parse_flat_value(value: str, where: str) -> Any: + if value[:1] in ('"', "'"): + end = value.find(value[0], 1) + rest = value[end + 1:].strip() if end > 0 else "" + if end < 0 or (rest and not rest.startswith("#")): + raise SystemExit(f"{where}: bad string") + s = value[1:end] + if value[0] == '"' and "\\" in s: + raise SystemExit(f"{where}: escapes aren't supported; use a 'literal string'") + return s + value = value.split("#", 1)[0].strip() + if value in ("true", "false"): + return value == "true" + for conv in (int, float): + try: + return conv(value.replace("_", "")) + except ValueError: + pass + raise SystemExit(f"{where}: unsupported value {value!r}") diff --git a/tests/test_report_auth_config.py b/tests/test_report_auth_config.py index 48daa3e..97b9e87 100644 --- a/tests/test_report_auth_config.py +++ b/tests/test_report_auth_config.py @@ -1,7 +1,8 @@ import os import unittest +from unittest import mock -from helpers import tempdir +from helpers import REPO, tempdir from kernelwatch import auth, config from kernelwatch.report import ReportError, sanitize @@ -66,6 +67,26 @@ class ConfigTest(unittest.TestCase): with self.assertRaises(SystemExit): config.load(self.write(tmp, "secure_cookies = 1\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: + self.skipTest("tomllib not available to compare against") + samples = [open(os.path.join(REPO, "server", "server.toml.example")).read(), + "db_path = '/srv/kw.db' # literal\nlisten_port = 8_080\n" + "stale_after_hours = 2.5\nsecure_cookies = false\nlisten_host = \"0.0.0.0\"\n"] + for text in samples: + with self.subTest(text=text[:40]): + self.assertEqual(config._parse_flat_toml(text, "x"), config.tomllib.loads(text)) + + def test_load_without_tomllib(self): + with tempdir() as tmp, mock.patch.object(config, "tomllib", None): + cfg = config.load(self.write(tmp, 'listen_port = 9000 # comment\ndb_path = "/x/kw.db"\n')) + self.assertEqual((cfg.listen_port, cfg.db_path), (9000, "/x/kw.db")) + for bad in ("[server]\n", "listen_port = [1]\n", 'db_path = "a\\b"\n', + "listen_port = 1\nlisten_port = 2\n", 'db_path = "unterminated\n'): + with self.subTest(bad=bad), self.assertRaises(SystemExit): + config.load(self.write(tmp, bad)) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_status.py b/tests/test_status.py index 9af0e3b..bd7e498 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -87,6 +87,19 @@ class EvaluateTest(unittest.TestCase): rep["kernel_running_pkg_version"] = "6.14.0-33.34~24.04.1" self.assertEqual(self.level(rep).level, "reboot") + def test_real_ubuntu_hwe_builds(self): + # Captured from real hosts (22.04.5 and 24.04.4, both HWE). + for running, build, pkg in ( + ("6.8.0-138-generic", "#138~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 7 13:43:15 UTC", + "6.8.0-138.138~22.04.1"), + ("7.0.0-38-generic", "#38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC Mon Sep 14 16:37:11 UTC 2", + "7.0.0-38.38~24.04.4"), + ): + with self.subTest(build=build): + rep = report(kernel_running=running, kernel_newest_installed=running, needrestart_ksta=None, + kernel_build=build, kernel_running_pkg_version=pkg) + self.assertEqual(self.level(rep).level, "ok", self.level(rep).reasons) + def test_unrecognised_build_falls_back(self): rep = report(needrestart_ksta=None, kernel_build="#1 SMP custom", kernel_running_pkg_version="1.0", kernel_newest_installed="6.12.60+deb13-amd64")