interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / tests / twin.sh

36 lines · 1892 bytes Raw Blame History
1#!/bin/sh
2# twin.sh: run an isolation probe inside an installed service's exact sandbox. Root only:
3# sudo -n sh tests/twin.sh SERVICE PROBE.py [ARGS...] [-- EXTRA_FILE...]
4# Copies /etc/aien-supervise/services/SERVICE.svc to SERVICE-twin.svc with only these changes:
5# the exec line runs `python3.12 -I /opt/aien-sup-twin/PROBE.py ARGS`, watch/restart/type lines
6# become `type oneshot` + `restart no`, and /opt/aien-sup-twin is bound read-only. User, groups,
7# binds, network rules, cgroup limits and the syscall filter are the service's own. The probe and
8# any EXTRA_FILE are copied into /opt/aien-sup-twin (root-owned). The twin's nftables table (same
9# uid rules as the service) and the twin file are removed afterwards. Exit status: the probe's.
10set -u
11[ "$(id -u)" = 0 ] || { echo "twin.sh: run as root" >&2; exit 2; }
12[ $# -ge 2 ] || { echo "usage: twin.sh SERVICE PROBE.py [ARGS...] [-- EXTRA_FILE...]" >&2; exit 2; }
13svc=$1; probe=$2; shift 2
14S=/etc/aien-supervise/services
15D=/opt/aien-sup-twin
16H=/usr/local/libexec/aien-supervise/aien-supervise-root
17[ -f "$S/$svc.svc" ] || { echo "twin.sh: no $S/$svc.svc" >&2; exit 2; }
18args=""; extra=""; inextra=0
19for a in "$@"; do
20 if [ "$a" = -- ]; then inextra=1; continue; fi
21 if [ $inextra = 1 ]; then extra="$extra $a"; else args="$args $a"; fi
22done
23install -d -o root -g root -m 0755 "$D"
24install -o root -g root -m 0644 "$probe" "$D/$(basename "$probe")"
25for f in $extra; do install -o root -g root -m 0644 "$f" "$D/$(basename "$f")"; done
26twin=$svc-twin
27trap 'rm -f "$S/$twin.svc"; nft delete table inet "aiens_$(echo "$twin" | tr - _)" 2>/dev/null; rm -rf "$D"' EXIT
28{
29 grep -vE '^(exec|watch|restart|type|every|delay|timeout) ' "$S/$svc.svc"
30 echo "exec /usr/bin/python3.12 -I $D/$(basename "$probe")$args"
31 echo "type oneshot"
32 echo "restart no"
33 echo "ro $D"
34} > "$S/$twin.svc"
35chmod 0644 "$S/$twin.svc"
36"$H" run "$twin"