Files
kernel-automation/server/kernelwatch/report.py
T
patrickandClaude Opus 5.5 6d29197cfc
CI / tests (Python 3.10) (pull_request) Successful in 26s
CI / tests (Python 3.11) (pull_request) Successful in 25s
CI / tests (Python 3.12) (pull_request) Successful in 25s
CI / tests (Python 3.13) (pull_request) Successful in 25s
CI / lint (pull_request) Failing after 3s
Report a failed apt simulation instead of reading it as "no updates"
The agent ran `apt-get -s dist-upgrade 2>/dev/null || true`, so when apt
failed (unmet dependencies, a broken sources entry, a damaged package cache)
the empty output counted as zero updates and the host could show ok while
nothing could be installed. The agent now keeps apt's exit status and stderr,
reports apt_simulation_ok and the first "E:" line as apt_simulation_error,
and sends the update counts as null. The server puts such a host in update
with "apt can't work out pending updates: ...", and the host page shows the
result. Agents before 0.5.0 don't send the field and are judged as before.

Also show "–" rather than "None" for an unknown count in a host's history,
and stop hard-coding the agent version in the status CLI tests. Agent 0.5.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 23:22:24 -04:00

78 lines
2.5 KiB
Python

"""Validation of agent reports. Unknown fields are dropped; known ones are type-checked."""
from __future__ import annotations
from typing import Any
MAX_BODY_BYTES = 64 * 1024
_MAX_STR = 256
_MAX_LIST = 200
class ReportError(ValueError):
pass
# field -> kind. "str", "int", "bool", "strlist". All fields are optional.
FIELDS: dict[str, str] = {
"agent_version": "str",
"reported_hostname": "str",
"os_id": "str",
"os_version_id": "str",
"os_pretty_name": "str",
"kernel_running": "str",
"kernel_build": "str",
"kernel_running_pkg_version": "str",
"kernels_installed": "strlist",
"kernel_newest_installed": "str",
"needrestart_ksta": "int",
"needrestart_kexp": "str",
"services_need_restart": "strlist",
"sessions_need_restart": "int",
"reboot_required": "bool",
"reboot_required_pkgs": "strlist",
"upgradable_count": "int",
"security_count": "int",
"kernel_upgradable": "strlist",
"phased_packages": "strlist",
"livepatch_state": "str",
"livepatch_check_state": "str",
"livepatch_supported": "str",
"livepatch_upgrade_required": "str",
"livepatch_fix_count": "int",
"apt_simulation_ok": "bool",
"apt_simulation_error": "str",
"apt_lists_age_seconds": "int",
"uptime_seconds": "int",
}
def _check(name: str, kind: str, value: Any) -> Any:
if value is None:
return None
if kind == "str":
if not isinstance(value, str):
raise ReportError(f"{name} must be a string")
return value[:_MAX_STR]
if kind == "int":
if not isinstance(value, int) or isinstance(value, bool):
raise ReportError(f"{name} must be an integer")
return value
if kind == "bool":
if not isinstance(value, bool):
raise ReportError(f"{name} must be a boolean")
return value
if kind == "strlist":
if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
raise ReportError(f"{name} must be a list of strings")
return [v[:_MAX_STR] for v in value[:_MAX_LIST]]
raise AssertionError(kind)
def sanitize(raw: Any) -> dict[str, Any]:
if not isinstance(raw, dict):
raise ReportError("report must be a JSON object")
if not isinstance(raw.get("kernel_running"), str) or not raw["kernel_running"]:
raise ReportError("kernel_running is required")
return {name: _check(name, kind, raw[name]) for name, kind in FIELDS.items() if name in raw}