Give the backup unit network access so its heartbeat ping can leave
CI / tests (Python 3.11) (pull_request) Successful in 28s
CI / tests (Python 3.10) (pull_request) Successful in 29s
CI / tests (Python 3.12) (pull_request) Successful in 25s
CI / tests (Python 3.13) (pull_request) Successful in 26s
CI / lint (pull_request) Successful in 2s
CI / tests (Python 3.11) (pull_request) Successful in 28s
CI / tests (Python 3.10) (pull_request) Successful in 29s
CI / tests (Python 3.12) (pull_request) Successful in 25s
CI / tests (Python 3.13) (pull_request) Successful in 26s
CI / lint (pull_request) Successful in 2s
kernelwatch-backup.service still had PrivateNetwork=yes and
RestrictAddressFamilies=AF_UNIX from before scheduled-backup pinged
backup_heartbeat_url. Every nightly ping failed DNS inside the sandbox
("heartbeat request failed (URLError)"); the backup itself succeeded, so
the only symptom was the heartbeat monitor going down after a day.
Allow AF_INET/AF_INET6 like the alerts unit, and add a test that every
unit running a command that sends requests (check-alerts,
scheduled-backup, notify-backup-failure) keeps network access.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
669f1dc545
commit
f9d9660f71
3 files changed
+31
-2
No files matched your search
@@ -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
|
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
|
directories, devices or kernel settings. apt drops to its `_apt` user for the
|
||||||
downloads, and nothing gets installed.
|
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
|
- **The agent** runs as a throwaway unprivileged user (`DynamicUser=yes`) with
|
||||||
no capabilities, a read-only filesystem view and a syscall filter. The token
|
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
|
reaches it through `LoadCredential=` and goes to curl on stdin, so it never
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ RestrictRealtime=yes
|
|||||||
RestrictSUIDSGID=yes
|
RestrictSUIDSGID=yes
|
||||||
LockPersonality=yes
|
LockPersonality=yes
|
||||||
MemoryDenyWriteExecute=yes
|
MemoryDenyWriteExecute=yes
|
||||||
RestrictAddressFamilies=AF_UNIX
|
# Network only for the backup_heartbeat_url ping after a successful backup.
|
||||||
PrivateNetwork=yes
|
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
||||||
SystemCallArchitectures=native
|
SystemCallArchitectures=native
|
||||||
SystemCallFilter=@system-service
|
SystemCallFilter=@system-service
|
||||||
UMask=0077
|
UMask=0077
|
||||||
@@ -329,6 +329,30 @@ class ServerInstallTest(InstallCase):
|
|||||||
for command in commands:
|
for command in commands:
|
||||||
self.assertRegex(help_text, rf"\b{re.escape(command)}\b")
|
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):
|
def test_on_failure_targets_are_shipped_units(self):
|
||||||
"""A typo'd OnFailure= is silently ignored by systemd, so check the names."""
|
"""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"))}
|
units = {n for n in os.listdir(SERVER_DIR) if n.endswith((".service", ".timer"))}
|
||||||
|
|||||||
Reference in new issue
Block a user