From f35cc007f5557ff3cca00b92b6b0df8d1b933609 Mon Sep 17 00:00:00 2001 From: patrick Date: Sat, 17 Oct 2026 17:13:55 -0400 Subject: [PATCH] Optionally refresh apt package lists before each report The agent's dist-upgrade simulation already finds new-ABI kernels and security updates, but only once apt's lists know about them. Debian doesn't refresh lists by default, so a kernel security release could stay invisible until someone ran apt update. install.sh --with-apt-update adds a marker that enables kernelwatch-apt-update.service, a sandboxed root oneshot the agent pulls in before each report. Failures (no network, apt lock held) are ignored so the report still goes out. deploy.sh now ships the unit; a test keeps deploy.sh's file list in sync with what install.sh uses. Agent 0.3.2, server 0.3.3. Co-Authored-By: Claude Opus 5.5 --- README.md | 21 +++++++++++++++- agent/deploy.sh | 2 +- agent/install.sh | 19 +++++++++++--- agent/kernelwatch-agent | 2 +- agent/kernelwatch-agent.service | 4 +-- agent/kernelwatch-apt-update.service | 37 ++++++++++++++++++++++++++++ server/kernelwatch/__init__.py | 4 +-- tests/test_deploy.py | 26 ++++++++++++++++++- 8 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 agent/kernelwatch-apt-update.service diff --git a/README.md b/README.md index 648bf47..8502d8d 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ How the agent works it out: two capabilities needrestart needs, and list-only mode, so it never restarts anything. It writes its results to `/run/kernelwatch/needrestart`, and the unprivileged agent unit runs it just before each report and reads the file. - Neither unit has both root and network access. Fix it on the host: + The agent never runs as root, and needrestart never gets network access. Fix it on the host: `sudo needrestart -r a` (or restart the listed services), and the next report clears it. - **Agent version**: each report includes it. Hosts running an older agent @@ -63,6 +63,14 @@ How the agent works it out: 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 `-security` count as security updates. +- **Fresh package lists**: the simulation only knows what apt's package lists + say, and the agent can't refresh them itself. Ubuntu refreshes them about once + a day. Debian doesn't by default unless `unattended-upgrades` is set up, so a + new kernel or security update can stay invisible until someone runs + `apt update`. Install with `--with-apt-update` and + `kernelwatch-apt-update.service` runs `apt-get update` just before each + report, so updates show up within the hour. Either way, lists older than + `apt_stale_after_hours` put the host in `update`. - **Phased updates** (Ubuntu only): Ubuntu rolls some updates out gradually and holds them back from a share of machines at first. apt doesn't offer these to the host yet, so they aren't counted as available updates. They appear as @@ -305,6 +313,12 @@ needs this; Ubuntu already has needrestart). It installs needrestart if missing and sets it to *list* services after apt upgrades instead of asking interactively. Delete `/etc/needrestart/conf.d/kernelwatch.conf` to undo that. +Add `--with-apt-update` to refresh apt's package lists before each report +(recommended on Debian, see above). It's the same as running `apt update` +hourly: lists only, nothing is installed. If apt is busy, the refresh is +skipped and the report goes ahead. `--without-apt-update` turns it off; +re-running the installer with neither keeps the current setting. + Useful on a host: ```sh @@ -333,6 +347,7 @@ agent/deploy.sh web01 # upgrade one agent/deploy.sh --rotate web01 # new token, reinstall agent/deploy.sh --dry-run # show the plan agent/deploy.sh -- --with-needrestart # pass options through to install.sh +agent/deploy.sh -- --with-apt-update # refresh package lists before each report ``` For each host it copies the agent into a private temp directory and runs @@ -374,6 +389,10 @@ sudo kernelwatch list-users capability set of just `CAP_SYS_PTRACE` + `CAP_DAC_READ_SEARCH` (to read other processes' memory maps), a read-only filesystem except `/run/kernelwatch`, and `-r l` (list, never restart). +- **apt-get update** (with `--with-apt-update`) runs as root with network + access, like apt's own `apt-daily.service`, but sandboxed: no access to home + directories, devices or kernel settings. apt drops to its `_apt` user for the + downloads, and nothing gets installed. - **The agent** runs as a throwaway unprivileged user (`DynamicUser=yes`) with no capabilities, a read-only filesystem view and a syscall filter. The token reaches it through `LoadCredential=` and goes to curl on stdin, so it never diff --git a/agent/deploy.sh b/agent/deploy.sh index 050fb0f..cd93864 100755 --- a/agent/deploy.sh +++ b/agent/deploy.sh @@ -19,7 +19,7 @@ here=$(cd "$(dirname "$0")" && pwd) CONF=${KERNELWATCH_DEPLOY_CONF:-$here/deploy.conf} HOSTS_FILE=${KERNELWATCH_HOSTS:-$here/hosts} FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer - kernelwatch-needrestart.service install.sh) + kernelwatch-needrestart.service kernelwatch-apt-update.service install.sh) NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$' SERVER_URL= diff --git a/agent/install.sh b/agent/install.sh index 19dbeb0..67c46c6 100755 --- a/agent/install.sh +++ b/agent/install.sh @@ -2,12 +2,16 @@ # Install or upgrade kernelwatch-agent on a Debian 12/13 or Ubuntu 22.04/24.04 host. # # sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem] -# [--with-needrestart] +# [--with-needrestart] [--with-apt-update | --without-apt-update] # # --with-needrestart installs needrestart (Ubuntu has it already) so the # dashboard can show services needing a restart, and sets it to only list # services after apt upgrades instead of asking interactively. # +# --with-apt-update refreshes apt's package lists (apt-get update) before each +# hourly report, so new kernel and security updates show up within the hour. +# --without-apt-update turns that off again. Without either, it stays as it was. +# # The host token (from `kernelwatch add-host ` on the server) is read # from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt, # so it never lands in shell history. Re-running without --server or a token @@ -19,6 +23,7 @@ server= ca= token_file= with_needrestart=0 +apt_update= while (($#)); do case $1 in @@ -26,7 +31,9 @@ while (($#)); do --ca) ca=${2:?--ca needs a file}; shift 2 ;; --token-file) token_file=${2:?--token-file needs a file}; shift 2 ;; --with-needrestart) with_needrestart=1; shift ;; - -h | --help) sed -n '2,14s/^# \{0,1\}//p' "$0"; exit 0 ;; + --with-apt-update) apt_update=on; shift ;; + --without-apt-update) apt_update=off; shift ;; + -h | --help) sed -n '2,19s/^# \{0,1\}//p' "$0"; exit 0 ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac done @@ -58,7 +65,13 @@ fi install -d -m 0755 /etc/kernelwatch install -m 0755 "$here/kernelwatch-agent" /usr/local/sbin/kernelwatch-agent install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" \ - "$here/kernelwatch-needrestart.service" /etc/systemd/system/ + "$here/kernelwatch-needrestart.service" "$here/kernelwatch-apt-update.service" \ + /etc/systemd/system/ + +case $apt_update in +on) touch /etc/kernelwatch/apt-update ;; +off) rm -f /etc/kernelwatch/apt-update ;; +esac if [[ -n $ca ]]; then install -m 0644 "$ca" /etc/kernelwatch/ca.pem diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index 7fa0307..f715a7b 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -6,7 +6,7 @@ # rules can change without redeploying agents. set -euo pipefail -AGENT_VERSION=0.3.1 +AGENT_VERSION=0.3.2 # Prefix for every file the agent reads. Only the test suite sets this. ROOT=${KERNELWATCH_ROOT:-} diff --git a/agent/kernelwatch-agent.service b/agent/kernelwatch-agent.service index c279415..2f7a8a8 100644 --- a/agent/kernelwatch-agent.service +++ b/agent/kernelwatch-agent.service @@ -1,7 +1,7 @@ [Unit] Description=Report kernel and update status to kernelwatch -Wants=network-online.target kernelwatch-needrestart.service -After=network-online.target apt-daily.service kernelwatch-needrestart.service +Wants=network-online.target kernelwatch-needrestart.service kernelwatch-apt-update.service +After=network-online.target apt-daily.service kernelwatch-needrestart.service kernelwatch-apt-update.service [Service] Type=oneshot diff --git a/agent/kernelwatch-apt-update.service b/agent/kernelwatch-apt-update.service new file mode 100644 index 0000000..f85be08 --- /dev/null +++ b/agent/kernelwatch-apt-update.service @@ -0,0 +1,37 @@ +[Unit] +Description=Refresh apt package lists (for kernelwatch) +# Pulled in by kernelwatch-agent.service before each report, so updates show up +# within the hour instead of whenever apt-daily next refreshes the lists (on +# Debian, by default, never). Enabled by `install.sh --with-apt-update`. +ConditionPathExists=/etc/kernelwatch/apt-update +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +# Same as `apt-get update`: downloads lists, installs nothing. A failure (no +# network, lock held by an admin's apt run) mustn't stop the report; the +# dashboard flags lists that stay old. +ExecStart=-/usr/bin/apt-get -q update +TimeoutStartSec=5min +Nice=10 +IOSchedulingClass=idle + +# apt needs root to write /var/lib/apt and drops to _apt for downloads, so this +# keeps root but loses everything apt update doesn't touch. +User=root +NoNewPrivileges=yes +ProtectHome=yes +PrivateTmp=yes +PrivateDevices=yes +ProtectKernelTunables=yes +ProtectKernelModules=yes +ProtectKernelLogs=yes +ProtectControlGroups=yes +ProtectClock=yes +ProtectHostname=yes +RestrictNamespaces=yes +RestrictRealtime=yes +LockPersonality=yes +RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK +SystemCallArchitectures=native diff --git a/server/kernelwatch/__init__.py b/server/kernelwatch/__init__.py index 3e7fc25..282e81e 100644 --- a/server/kernelwatch/__init__.py +++ b/server/kernelwatch/__init__.py @@ -1,7 +1,7 @@ """kernelwatch: fleet kernel and reboot status dashboard.""" -__version__ = "0.3.2" +__version__ = "0.3.3" # Version of agent/kernelwatch-agent shipped alongside this server; older agents # are noted on the dashboard. tests/test_status.py checks the two stay in sync. -AGENT_VERSION = "0.3.1" +AGENT_VERSION = "0.3.2" diff --git a/tests/test_deploy.py b/tests/test_deploy.py index d3d5cdc..1640d7b 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -5,6 +5,7 @@ replaced by a recorder so we can check what each host received. """ import os +import re import shutil import stat import subprocess @@ -57,7 +58,8 @@ class DeployTest(unittest.TestCase): os.makedirs(d) for name in ("deploy.sh", "kernelwatch-agent", "kernelwatch-agent.service", - "kernelwatch-agent.timer", "kernelwatch-needrestart.service"): + "kernelwatch-agent.timer", "kernelwatch-needrestart.service", + "kernelwatch-apt-update.service"): shutil.copy2(os.path.join(AGENT_DIR, name), self.agent) write_exec(os.path.join(self.agent, "install.sh"), FAKE_INSTALL) @@ -187,5 +189,27 @@ class DeployTest(unittest.TestCase): self.assertIn("deploy.conf.example", proc.stderr) + +class PackagingTest(unittest.TestCase): + def read(self, name): + with open(os.path.join(AGENT_DIR, name)) as f: + return f.read() + + def test_deploy_ships_every_file_install_uses(self): + files = re.search(r"^FILES=\(([^)]*)\)", self.read("deploy.sh"), re.M).group(1).split() + used = set(re.findall(r'"\$here/([^"]+)"', self.read("install.sh"))) + self.assertTrue(used) + self.assertEqual(used - set(files), set()) + + def test_apt_update_is_opt_in_and_runs_before_reports(self): + unit = self.read("kernelwatch-apt-update.service") + self.assertIn("ConditionPathExists=/etc/kernelwatch/apt-update", unit) + self.assertIn("ExecStart=-/usr/bin/apt-get -q update", unit, "failure mustn't block the report") + agent = self.read("kernelwatch-agent.service") + for key in ("Wants=", "After="): + line = next(l for l in agent.splitlines() if l.startswith(key)) + self.assertIn("kernelwatch-apt-update.service", line) + + if __name__ == "__main__": unittest.main()