needrestart has to inspect every process, which an unprivileged agent can't (and unprivileged it gave false kernel results). Add kernelwatch-needrestart.service: root with only CAP_SYS_PTRACE and CAP_DAC_READ_SEARCH, PrivateNetwork=yes, list-only (-r l). It writes `needrestart -b` output to /run/kernelwatch/needrestart; the agent unit pulls it in before each report and reads the file (ignored if older than 2h). The agent no longer runs needrestart itself. Skipped cleanly when needrestart isn't installed: the dashboard shows "?" and status is unaffected. Server: services_need_restart / sessions_need_restart fields, an "update"-level reason, a Services column, and a note (not an escalation) for hosts running an agent older than the one shipped with the server. install.sh --with-needrestart installs needrestart and sets it to list services after apt upgrades instead of prompting; deploy.sh passes anything after `--` to install.sh. Agent 0.2.0, server 0.2.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.2 KiB
Python
70 lines
2.2 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",
|
|
"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}
|