Give the backup unit network access so its heartbeat ping can leave #24

Merged
patrick merged 1 commits from fix/backup-heartbeat-network into main 2026-10-31 19:04:21 +00:00
3 changed files with 31 additions and 2 deletions

No files matched your search

+5
View File
@@ -25,6 +25,11 @@ How kernelwatch is locked down, and what it deliberately can't do.
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 server's timer units** (alert check, nightly backup, backup-failure
notice) run as the `kernelwatch` user with no capabilities and a syscall
filter, limited to Unix and IP sockets. They need outbound network access
for ntfy and the heartbeat pings, so `PrivateNetwork=` isn't an option
there; `tests/test_install.py` enforces that.
- **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
+2 -2
View File
@@ -33,8 +33,8 @@ RestrictRealtime=yes
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
RestrictAddressFamilies=AF_UNIX
PrivateNetwork=yes
# Network only for the backup_heartbeat_url ping after a successful backup.
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallArchitectures=native
SystemCallFilter=@system-service
UMask=0077
+24
View File
@@ -329,6 +329,30 @@ class ServerInstallTest(InstallCase):
for command in commands:
self.assertRegex(help_text, rf"\b{re.escape(command)}\b")
def test_units_that_send_requests_have_network_access(self):
"""These commands ping a heartbeat or send to ntfy. A unit that runs one with its
network sandboxed away fails only in a log line (the request fails, the run
doesn't), so the heartbeat monitor just goes quiet."""
outbound = {"check-alerts", "scheduled-backup", "notify-backup-failure"}
checked = set()
for unit in os.listdir(SERVER_DIR):
if not unit.endswith(".service"):
continue
text = read(os.path.join(SERVER_DIR, unit))
if not outbound & set(re.findall(r"kernelwatch-server (\S+)$", text, re.M)):
continue
checked.add(unit)
with self.subTest(unit=unit):
self.assertNotRegex(text, r"(?m)^PrivateNetwork=(yes|true|1|on)$")
self.assertNotRegex(text, r"(?m)^IPAddressDeny=")
families = re.findall(r"(?m)^RestrictAddressFamilies=(.*)$", text)
self.assertTrue(families, "set RestrictAddressFamilies= explicitly")
for line in families:
self.assertFalse(line.startswith("~"))
self.assertTrue({"AF_INET", "AF_INET6"} <= set(line.split()), line)
self.assertEqual(checked, {"kernelwatch-alerts.service", "kernelwatch-backup.service",
"kernelwatch-backup-failed.service"})
def test_on_failure_targets_are_shipped_units(self):
"""A typo'd OnFailure= is silently ignored by systemd, so check the names."""
units = {n for n in os.listdir(SERVER_DIR) if n.endswith((".service", ".timer"))}