Debian: passwordless updates, offline updates, and install ISO builder

Ports the update behaviour of the Fedora clients to Debian, and adds the
Debian equivalent of putting ks.cfg on the installation medium.

Updates
-------
Debian's shipped polkit policy only lets a sudo-group user trigger
upgrade-system and trigger-offline-update without a password. The actions
pkcon refresh and pkcon update actually use (system-sources-refresh and
system-update) are auth_admin, so a desktop user was prompted - a
behavioural difference from Fedora. update_local_repo.py now installs
/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules granting exactly
those two plus package-install to group sudo. package-remove,
package-reinstall and package-install-untrusted stay auth_admin.

The offline update itself ports unchanged: Debian 13 PackageKit 1.3.1
supports offline-get-prepared and offline-trigger, and
packagekit-offline-update.service is wired into system-update.target.
Scheduling moves from an @reboot cron line to vdi-offline-update.timer
(6h, Persistent), with /root/auto_update.sh delegating to the same script
so there is one implementation instead of two.

Both are installed by update_local_repo.py, not only by the preseed, so
already-provisioned machines pick them up on their next 10-minute cron run.

install ISO
-----------
debian/make-iso.sh bakes preseed.cfg into a Debian netinst ISO and adds
auto=true priority=critical preseed/file=/cdrom/preseed.cfg to every boot
menu entry, then verifies its own output.

Measured, not assumed
---------------------
* subject.local == true never matches in this environment: pkcheck returns
  2 with the clause and 0 without it, same sudo-group user. It was in the
  first version of the rule and is removed; the reasoning is in the source.
* pkcon exits 5 for "no packages to update", which is the normal answer on
  a patched box and was being logged as a download failure. Fixed.
* offline-get-prepared exits 4 when nothing is prepared, so the Fedora
  "if ... offline-trigger" guard is load-bearing - triggering anyway can
  leave a dangling /system-update symlink that breaks the next boot.
* Extracting a netinst and rebuilding with "xorriso -as mkisofs" grows it
  792MB -> 988MB because Debian shares identical file extents; replaying
  the original session with -map keeps dedup and the boot record.
* A hardcoded menu list misses isolinux/gtk.cfg, where the BIOS *default*
  entry lives, so the machine still prompted. Menus are discovered now.

Tests: 90 passed, 0 failed, 2 skipped. T17 asserts the polkit decision in
both directions (authorised with the rule, prompted without it), because a
check that cannot fail proves nothing.
This commit is contained in:
Mistercoolertyper committed 2026-11-04 10:16:23 +01:00
1 parent 4f1a4530aa
commit 15d65183dc
6 files changed
+1335 -2

No files matched your search

+648
View File
@@ -123,6 +123,61 @@ purge_all_harness_lists() {
return 0
}
# Paths installed by update_local_repo.install_debian_update_policy(). These
# are REAL system paths, so the tests that touch them back up whatever is
# already there and restore it at teardown rather than deleting blindly.
POLKIT_RULE_PATH="/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules"
OFFLINE_SCRIPT="/usr/local/sbin/vdi-offline-update"
OFFLINE_SERVICE="/etc/systemd/system/vdi-offline-update.service"
OFFLINE_TIMER="/etc/systemd/system/vdi-offline-update.timer"
OFFLINE_STAMP="/var/lib/vdi-offline-update.last"
SYSTEM_UPDATE_LINK="/system-update"
BACKUP_DIR="$TMP_DIR/backup"
mkdir -p "$BACKUP_DIR"
# Save the current state of a path so teardown can put it back exactly.
# Records "present" or "absent" in a marker file, so "it did not exist before"
# is distinguishable from "the backup failed".
backup_path() {
local path="$1" name
name=$(printf '%s' "$path" | tr '/' '_')
if [ -L "$path" ]; then
printf 'symlink\n' > "$BACKUP_DIR/$name.state"
readlink "$path" > "$BACKUP_DIR/$name.target"
elif [ -e "$path" ]; then
printf 'present\n' > "$BACKUP_DIR/$name.state"
cp -a "$path" "$BACKUP_DIR/$name" 2>/dev/null
else
printf 'absent\n' > "$BACKUP_DIR/$name.state"
fi
}
restore_path() {
local path="$1" name state
name=$(printf '%s' "$path" | tr '/' '_')
state=$(cat "$BACKUP_DIR/$name.state" 2>/dev/null || echo absent)
case "$state" in
symlink)
# Never rm -rf a symlink target; only unlink the link itself.
[ -L "$path" ] && rm -f "$path"
ln -s "$(cat "$BACKUP_DIR/$name.target")" "$path" 2>/dev/null
;;
present)
cp -a "$BACKUP_DIR/$name" "$path" 2>/dev/null
;;
*)
# It did not exist before this run: remove it, but only if it is a
# regular file or symlink - never recurse into a directory.
if [ -L "$path" ]; then
rm -f "$path"
elif [ -f "$path" ]; then
rm -f "$path"
fi
;;
esac
}
cleanup() {
local status=$?
@@ -134,6 +189,15 @@ cleanup() {
printf '\n== cleanup\n'
# Undo the machine mutations from T16-T19 first: a polkit rule or a systemd
# unit left behind is more consequential than a temp directory.
for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK" \
"$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do
restore_path "$mutating_path" 2>/dev/null
done
command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload >/dev/null 2>&1
pkill -f 'vdi-test-sleeper' >/dev/null 2>&1
rm -f "$LIST_FILE" 2>/dev/null
# Remove any keyring this run installed, by its unique name.
rm -f "$KEYRING_DIR"/zz-vdi-test-* 2>/dev/null
@@ -945,6 +1009,543 @@ else
fail "T14 bash -n failed for run-tests.sh: $(cat "$TMP_DIR/basherr" | tr '\n' ' ')"
fi
# ---------------------------------------------------------------------------
# T16: polkit rule installed and correct
# ---------------------------------------------------------------------------
# These tests MUTATE the machine: they install a polkit rule and systemd units.
# Everything is backed up first and restored at teardown, and T15 verifies it.
head2 "T16 polkit rule for passwordless updates"
POLKIT_OK=0
if ! command -v pkcheck >/dev/null 2>&1; then
skip "T16 pkcheck not installed (no polkit) - cannot test authorisation"
elif [ ! -d /usr/share/polkit-1 ] && [ ! -d /etc/polkit-1 ]; then
skip "T16 polkit is not installed on this machine"
else
POLKIT_OK=1
backup_path "$POLKIT_RULE_PATH"
# Start from a known-clean slate: if the rule is already installed we still
# want to prove install_debian_update_policy() produces it.
rm -f "$POLKIT_RULE_PATH"
# Drive the real function, not a copy of its text.
if out=$(cd "$REPO_ROOT" && python3 - "$REPO_ROOT" <<'PYEOF' 2>&1
import sys, os
sys.path.insert(0, sys.argv[1])
os.chdir(sys.argv[1])
import update_local_repo as u
u.install_debian_update_policy()
print("CALLED")
PYEOF
); then
if printf '%s' "$out" | grep -q 'CALLED'; then
pass "T16 install_debian_update_policy() ran"
else
fail "T16 install_debian_update_policy() did not complete: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')"
fi
else
fail "T16 install_debian_update_policy() raised: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')"
fi
if [ -f "$POLKIT_RULE_PATH" ]; then
pass "T16 rule file created at $POLKIT_RULE_PATH"
else
fail "T16 rule file was not created"
fi
rule_mode=$(stat -c '%a' "$POLKIT_RULE_PATH" 2>/dev/null || echo missing)
if [ "$rule_mode" = "644" ]; then
pass "T16 rule mode is 644"
else
fail "T16 rule mode is '$rule_mode', expected 644"
fi
for action in system-sources-refresh system-update; do
if grep -q "org.freedesktop.packagekit.$action" "$POLKIT_RULE_PATH" 2>/dev/null; then
pass "T16 rule covers org.freedesktop.packagekit.$action"
else
fail "T16 rule does not mention $action"
fi
done
# The rule must not over-grant. package-remove is the one that matters:
# a user who can remove packages without a password can break the machine.
for action in package-remove package-reinstall package-install-untrusted; do
if grep -q "org.freedesktop.packagekit.$action" "$POLKIT_RULE_PATH" 2>/dev/null; then
fail "T16 rule over-grants: mentions $action"
else
pass "T16 rule does not grant $action"
fi
done
# Structural check: the JS must be balanced. `node` is not installed, so
# this is a counting check, not a parse - it catches truncation and the
# common copy/paste damage, not every syntax error.
if python3 - "$POLKIT_RULE_PATH" <<'PYEOF' 2>&1
import sys
src = open(sys.argv[1], encoding="utf-8").read()
# Strip // comments and string literals so braces inside them do not count.
out, i, n = [], 0, len(src)
while i < n:
if src.startswith("//", i):
while i < n and src[i] != "\n":
i += 1
elif src[i] in "\"'":
q = src[i]; i += 1
while i < n and src[i] != q:
i += 2 if src[i] == "\\" else 1
i += 1
else:
out.append(src[i]); i += 1
code = "".join(out)
pairs = {"}": "{", "]": "[", ")": "("}
stack = []
for ch in code:
if ch in "{[(":
stack.append(ch)
elif ch in pairs:
if not stack or stack.pop() != pairs[ch]:
print("UNBALANCED at %r" % ch); raise SystemExit(1)
print("BALANCED" if not stack else "UNCLOSED: %r" % stack)
raise SystemExit(0 if not stack else 1)
PYEOF
then
pass "T16 rule has balanced braces and parens"
else
fail "T16 rule is structurally unbalanced"
fi
if grep -q 'polkit.addRule' "$POLKIT_RULE_PATH" 2>/dev/null; then
pass "T16 rule registers via polkit.addRule"
else
fail "T16 rule does not call polkit.addRule - it would have no effect"
fi
# REGRESSION GUARD. An earlier version of the rule included
# `subject.local == true`, intending to restrict it to a seat physically in
# front of the machine. It never matches: pkcheck returns 2 with the clause
# and 0 without it, for the same sudo-group user. If it comes back, the
# passwordless update silently stops working while every other assertion in
# this test still passes - which is exactly why it is asserted here.
if grep -q 'subject\.local' "$POLKIT_RULE_PATH" 2>/dev/null; then
fail "T16 rule contains subject.local, which never matches (regression)"
else
pass "T16 rule does not contain the non-matching subject.local clause"
fi
# Idempotency: a second call must not rewrite the file.
first_hash=$(sha256sum "$POLKIT_RULE_PATH" 2>/dev/null | cut -d' ' -f1)
first_mtime=$(stat -c '%Y' "$POLKIT_RULE_PATH" 2>/dev/null)
sleep 1
( cd "$REPO_ROOT" && python3 -c "
import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u
u.install_debian_update_policy()" ) >/dev/null 2>&1
second_hash=$(sha256sum "$POLKIT_RULE_PATH" 2>/dev/null | cut -d' ' -f1)
if [ "$first_hash" = "$second_hash" ] && [ -n "$first_hash" ]; then
pass "T16 rule is byte-identical after a second call (idempotent)"
else
fail "T16 rule changed on a second call"
fi
fi
# ---------------------------------------------------------------------------
# T17: a sudo-group user is authorised WITHOUT a password
# ---------------------------------------------------------------------------
# This is the actual requirement. Testing the file's contents proves nothing
# about whether polkit acts on it, so this asks polkit itself, about a real
# process owned by a real sudo-group user. The control (rule removed -> rc=2)
# is what stops the test from passing on a machine where pkcheck is simply
# permissive.
head2 "T17 sudo-group user authorised without a password"
POLKIT_ACTION="org.freedesktop.packagekit.system-sources-refresh"
restart_polkit() {
if command -v systemctl >/dev/null 2>&1; then
systemctl restart polkit >/dev/null 2>&1 || systemctl restart polkitd >/dev/null 2>&1
# polkitd caches rules; give it a moment to re-read them.
sleep 1
fi
}
pkcheck_for_user() {
# Run pkcheck as root against a process owned by $1. Echoes the rc.
local user="$1" pid rc
pkill -u "$user" -f 'vdi-test-sleeper' >/dev/null 2>&1
setsid runuser -u "$user" -- bash -c 'exec -a vdi-test-sleeper sleep 120' >/dev/null 2>&1 &
sleep 1
pid=$(pgrep -u "$user" -f 'vdi-test-sleeper' | head -1)
if [ -z "$pid" ]; then
echo "NOPROC"
return
fi
pkcheck --action-id "$POLKIT_ACTION" --process "$pid" >/dev/null 2>&1
rc=$?
pkill -u "$user" -f 'vdi-test-sleeper' >/dev/null 2>&1
echo "$rc"
}
TEST_USER=""
if [ "$POLKIT_OK" != "1" ]; then
skip "T17 skipped with T16: no polkit on this machine"
elif ! systemctl is-system-running >/dev/null 2>&1 && ! systemctl is-active polkit >/dev/null 2>&1; then
skip "T17 polkit is not running and cannot be started (no systemd?) - authorisation untestable"
else
# Find a real, non-root user in the sudo group. A machine with none cannot
# answer the question the test is asking.
# NOTE: `getent group sudo` is "name:passwd:gid:members" - FOUR fields.
# Reading five would put the member list in the wrong variable and find
# nobody, which looks exactly like "no sudo user exists".
while IFS=: read -r _grp _pw _gid members; do
for member in $(printf '%s' "${members:-}" | tr ',' ' '); do
[ "$member" = "root" ] && continue
if id "$member" >/dev/null 2>&1; then
TEST_USER="$member"
break 2
fi
done
done < <(getent group sudo 2>/dev/null)
if [ -z "$TEST_USER" ]; then
skip "T17 no non-root user in group 'sudo' - cannot test the authorisation"
info "would have checked: pkcheck --process <pid of sudo-group user> -> rc=0"
info "with the rule installed, and rc=2 with the rule removed (control)."
else
info "test user: $TEST_USER (in group sudo)"
# Make sure the rule is installed before the positive case.
if [ ! -f "$POLKIT_RULE_PATH" ]; then
( cd "$REPO_ROOT" && python3 -c "
import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u
u.install_debian_update_policy()" ) >/dev/null 2>&1
fi
restart_polkit
with_rule=$(pkcheck_for_user "$TEST_USER")
case "$with_rule" in
0)
pass "T17 $TEST_USER is authorised without a password (pkcheck rc=0)"
;;
NOPROC)
fail "T17 could not start a process as $TEST_USER"
;;
*)
fail "T17 $TEST_USER still requires authentication (pkcheck rc=$with_rule, expected 0)"
;;
esac
# THE CONTROL. Without this the positive case proves nothing: a polkit
# that answers YES to everything would also produce rc=0.
mv "$POLKIT_RULE_PATH" "$TMP_DIR/rule.parked" 2>/dev/null
restart_polkit
without_rule=$(pkcheck_for_user "$TEST_USER")
mv "$TMP_DIR/rule.parked" "$POLKIT_RULE_PATH" 2>/dev/null
restart_polkit
case "$without_rule" in
2)
pass "T17 control: without the rule, authentication IS required (rc=2)"
;;
0)
fail "T17 control failed: authorised even without the rule - the test proves nothing"
;;
NOPROC)
fail "T17 control could not start a process as $TEST_USER"
;;
*)
fail "T17 control returned unexpected rc=$without_rule (expected 2)"
;;
esac
# The action the rule must NOT grant.
denied=$(pkcheck --action-id org.freedesktop.packagekit.package-remove \
--process "$(pgrep -u "$TEST_USER" -f 'vdi-test-sleeper' | head -1 || echo 0)" >/dev/null 2>&1; echo $?)
if [ "$denied" = "2" ] || [ "$denied" = "3" ]; then
pass "T17 package-remove still requires authentication (not over-granted)"
elif [ "$denied" = "0" ]; then
fail "T17 package-remove is authorised without a password - the rule over-grants"
else
info "T17 package-remove pkcheck rc=$denied (no process to check against)"
fi
fi
fi
# ---------------------------------------------------------------------------
# T18: offline-update script and systemd units
# ---------------------------------------------------------------------------
head2 "T18 offline-update script and units"
if [ ! -f "$OFFLINE_SCRIPT" ]; then
skip "T18 $OFFLINE_SCRIPT is not installed (update_local_repo.py has not run here)"
info "would have checked: mode 755, bash -n, systemd-analyze verify on both"
info "units, timer enabled, stamp written, throttle on a second run, and that"
info "any /system-update symlink points at something that exists."
else
mode=$(stat -c '%a' "$OFFLINE_SCRIPT" 2>/dev/null)
if [ "$mode" = "755" ]; then
pass "T18 $OFFLINE_SCRIPT is mode 755"
else
fail "T18 $OFFLINE_SCRIPT is mode '$mode', expected 755"
fi
if bash -n "$OFFLINE_SCRIPT" 2>"$TMP_DIR/offerr"; then
pass "T18 offline-update script passes bash -n"
else
fail "T18 offline-update script fails bash -n: $(cat "$TMP_DIR/offerr" | tr '\n' ' ')"
fi
if command -v systemd-analyze >/dev/null 2>&1; then
for unit in "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do
if [ ! -f "$unit" ]; then
fail "T18 $unit is missing"
continue
fi
if verr=$(systemd-analyze verify "$unit" 2>&1) && [ -z "$verr" ]; then
pass "T18 systemd-analyze verify is clean for $(basename "$unit")"
else
fail "T18 systemd-analyze verify complained about $(basename "$unit"): $(printf '%s' "$verr" | head -3 | tr '\n' ' ')"
fi
done
# The Documentation=man:pkcon(1) trap: verify runs the man binary.
if grep -q 'Documentation=man:' "$OFFLINE_SERVICE" "$OFFLINE_TIMER" 2>/dev/null; then
fail "T18 a unit has Documentation=man: which systemd-analyze verify tries to execute"
else
pass "T18 no Documentation=man: lines (they break verify)"
fi
# Enabling is done by `systemctl enable vdi-offline-update.timer` inside
# install_debian_update_policy(). That only persists when systemd is the
# running init - in a d-i chroot or a container it silently does nothing.
# So: assert it is enabled AFTER driving the installer function, and skip
# the assertion outright when systemd is not PID 1 rather than failing on
# an environment that cannot express the answer.
if [ "$(cat /proc/1/comm 2>/dev/null)" != "systemd" ]; then
skip "T18 timer-enabled assertion skipped: systemd is not PID 1 here"
info "would have checked: systemctl is-enabled vdi-offline-update.timer"
info "after install_debian_update_policy() ran."
else
( cd "$REPO_ROOT" && python3 -c "
import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u
u.install_debian_update_policy()" ) >/dev/null 2>&1
if systemctl is-enabled vdi-offline-update.timer >/dev/null 2>&1; then
pass "T18 vdi-offline-update.timer is enabled after install_debian_update_policy()"
else
fail "T18 vdi-offline-update.timer is not enabled after install_debian_update_policy()"
fi
fi
else
skip "T18 systemd-analyze not available; unit files not verified"
fi
# Behavioural: the stamp, the throttle, and the /system-update safety rule.
backup_path "$OFFLINE_STAMP"
backup_path "$SYSTEM_UPDATE_LINK"
rm -f "$OFFLINE_STAMP"
run1=$(timeout 300 "$OFFLINE_SCRIPT" 2>&1); rc1=$?
if [ -s "$OFFLINE_STAMP" ]; then
pass "T18 running the script writes the throttle stamp"
else
fail "T18 running the script did not write $OFFLINE_STAMP"
fi
# Exit 5 from pkcon means "no packages to update" and must not be reported
# as a download failure. This box is up to date, so that is the case here.
if printf '%s' "$run1" | grep -qi 'download failed'; then
fail "T18 script reported a download failure on an up-to-date box (pkcon exit 5 misclassified)"
printf '%s' "$run1" | grep -i 'download failed' | head -2 | sed 's/^/ /'
else
pass "T18 no spurious 'download failed' on an up-to-date box"
fi
if printf '%s' "$run1" | grep -qi 'no updates available'; then
pass "T18 pkcon exit 5 is reported as 'no updates available'"
else
info "T18 (this box may have had real updates; exit-5 path not exercised)"
fi
# THE DANGEROUS ONE: a dangling /system-update fails the next boot.
if [ -L "$SYSTEM_UPDATE_LINK" ]; then
target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null)
if [ -n "$target" ] && [ -e "$target" ]; then
pass "T18 /system-update symlink target exists ($target)"
else
fail "T18 /system-update is DANGLING (-> '$target') - the next boot would fail"
fi
elif [ -e "$SYSTEM_UPDATE_LINK" ]; then
info "T18 /system-update exists but is not a symlink - left alone"
else
pass "T18 no /system-update armed (nothing was prepared)"
fi
# Throttle: a second run immediately after must not re-run pkcon.
run2=$(timeout 60 "$OFFLINE_SCRIPT" 2>&1)
if printf '%s' "$run2" | grep -qi 'skipping'; then
pass "T18 a second run immediately is throttled ('skipping')"
else
fail "T18 a second run was not throttled: $(printf '%s' "$run2" | head -2 | tr '\n' ' ')"
fi
# Stamp of 0 must defeat the throttle, proving the throttle is time-based
# and not a permanent latch.
printf '0\n' > "$OFFLINE_STAMP"
run3=$(timeout 300 "$OFFLINE_SCRIPT" 2>&1)
if printf '%s' "$run3" | grep -qi 'skipping'; then
fail "T18 a stamp of 0 did not defeat the throttle"
else
pass "T18 a stamp of 0 makes the script run again"
fi
# Re-check the symlink after the forced run, since this is the run that may
# have armed it.
if [ -L "$SYSTEM_UPDATE_LINK" ]; then
target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null)
if [ -n "$target" ] && [ -e "$target" ]; then
pass "T18 after the forced run, /system-update target still exists"
else
fail "T18 after the forced run, /system-update is DANGLING"
fi
fi
fi
# ---------------------------------------------------------------------------
# T19: the preseed still generates a working post-install script
# ---------------------------------------------------------------------------
# Runs the preseed's own printf in a sandbox to regenerate /root/vdi-post.sh,
# then checks the result. It never executes the script: it writes /etc/hostname,
# /etc/crontab and systemd units by absolute path and would reconfigure the
# machine running the test.
head2 "T19 preseed generates a working post-install script"
PRESEED="$REPO_ROOT/debian/preseed.cfg"
if [ ! -f "$PRESEED" ]; then
skip "T19 no debian/preseed.cfg in this repo (owned by another change)"
else
if out=$(python3 - "$PRESEED" "$TMP_DIR" <<'PYEOF' 2>&1
import os, re, subprocess, sys
preseed, work = sys.argv[1], sys.argv[2]
lines = open(preseed, encoding="utf-8").read().splitlines()
# The late_command is one logical line continued with trailing backslashes.
start = None
for i, ln in enumerate(lines):
if ln.startswith("d-i preseed/late_command"):
start = i
break
if start is None:
print("NO-LATE-COMMAND")
raise SystemExit(1)
block, i = [], start
while i < len(lines):
block.append(lines[i])
if not lines[i].rstrip().endswith("\\"):
break
i += 1
cmd = "\n".join(block)
# Anchor on the vdi-post.sh redirect. A non-greedy match that is allowed to
# cross a ';' would also capture the firefox printf before it - which then
# tries to write /target/etc/firefox and fails. Anchor from the previous
# statement boundary instead.
# The late_command is a chain of statements, and the payload it writes itself
# contains ';' and '>' inside quoted arguments, so neither splitting on ';' nor
# a bare backwards search is safe. Anchor on the two unambiguous markers: the
# payload's own first argument ('#!/bin/bash') and the redirect target. The
# earlier firefox printf is excluded by construction, because it does not start
# with '#!/bin/bash'.
marker = "> /target/root/vdi-post.sh"
pos = cmd.find(marker)
if pos < 0:
print("NO-REDIRECT")
raise SystemExit(1)
anchor = 'printf "%s\\n" \'#!/bin/bash\''
start = cmd.find(anchor)
if start < 0:
print("NO-PRINTF-ANCHOR")
raise SystemExit(1)
if start > pos:
print("ANCHOR-AFTER-REDIRECT")
raise SystemExit(1)
args = cmd[start + len('printf "%s\\n" '):pos].strip()
if not args:
print("EMPTY-ARGS")
raise SystemExit(1)
# Sanity: the captured argument list must contain the script's first line and
# must not contain the firefox policy payload.
if "#!/bin/bash" not in args:
print("CAPTURED-WRONG-ARGUMENT")
raise SystemExit(1)
if "ManagedBookmarks" in args:
print("CAPTURED-FIREFOX-BLOCK")
raise SystemExit(1)
# Run the printf exactly as the preseed does, but write into our sandbox.
dest = os.path.join(work, "vdi-post.sh")
script = 'printf "%s\\n" ' + args + ' > ' + dest
proc = subprocess.run(["bash", "-c", script], capture_output=True, text=True)
if proc.returncode != 0:
print("PRINTF-FAILED: " + proc.stderr.strip()[:200])
raise SystemExit(1)
if not os.path.exists(dest) or os.path.getsize(dest) == 0:
print("EMPTY-OUTPUT")
raise SystemExit(1)
print("GENERATED " + dest)
PYEOF
); then
printf '%s\n' "$out" | sed 's/^/ /'
GEN="$TMP_DIR/vdi-post.sh"
pass "T19 preseed regenerates /root/vdi-post.sh"
if bash -n "$GEN" 2>"$TMP_DIR/err"; then
pass "T19 generated script passes bash -n"
else
fail "T19 generated script fails bash -n: $(cat "$TMP_DIR/err" | tr '\n' ' ')"
fi
# It must delegate, not duplicate, the pkcon sequence. Comments are
# stripped first: the generated script explains the delegation in a
# comment, and matching that would be a false positive.
if grep -v '^[[:space:]]*#' "$GEN" | grep -qE 'pkcon[[:space:]]+(refresh|offline-trigger|update)'; then
fail "T19 generated script still runs pkcon inline instead of delegating"
grep -v '^[[:space:]]*#' "$GEN" | grep -nE 'pkcon' | head -3 | sed 's/^/ /'
else
pass "T19 generated script delegates to vdi-offline-update (no inline pkcon)"
fi
if grep -q '/usr/local/sbin/vdi-offline-update' "$GEN"; then
pass "T19 generated script references /usr/local/sbin/vdi-offline-update"
else
fail "T19 generated script does not reference the offline-update script"
fi
if grep -qE 'systemctl enable cron' "$GEN"; then
pass "T19 generated script enables cron"
else
fail "T19 generated script does not enable cron"
fi
if grep -qE 'systemctl enable vdi-offline-update\.timer' "$GEN"; then
pass "T19 generated script enables vdi-offline-update.timer"
else
fail "T19 generated script does not enable vdi-offline-update.timer"
fi
else
fail "T19 could not extract the post-install script from preseed.cfg: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')"
fi
fi
# ---------------------------------------------------------------------------
# T15: the harness left nothing behind
# ---------------------------------------------------------------------------
@@ -972,6 +1573,53 @@ else
pass "T15 the repo working tree was not dirtied by Python"
fi
# T16-T19 install a polkit rule and systemd units and arm /system-update.
# Assert that none of that survives: restore each path to its pre-run state and
# then check it took. This is what makes "self-cleaning" a claim rather than
# an intention.
for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK"; do
restore_path "$mutating_path"
done
# The units and script are only removed if this run created them.
for created_path in "$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do
restore_path "$created_path"
done
# systemd cached the units; make it re-read whatever is there now.
if command -v systemctl >/dev/null 2>&1; then
systemctl daemon-reload >/dev/null 2>&1
fi
for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK" \
"$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do
name=$(printf '%s' "$mutating_path" | tr '/' '_')
was=$(cat "$BACKUP_DIR/$name.state" 2>/dev/null || echo absent)
if [ "$was" = "absent" ]; then
if [ -e "$mutating_path" ] || [ -L "$mutating_path" ]; then
fail "T15 $mutating_path was created by this run and not removed"
else
pass "T15 $mutating_path restored to its pre-run state (absent)"
fi
else
if [ -e "$mutating_path" ] || [ -L "$mutating_path" ]; then
pass "T15 $mutating_path restored (existed before the run)"
else
fail "T15 $mutating_path existed before the run and was not restored"
fi
fi
done
# A dangling /system-update is the dangerous residue: it fails the next boot.
if [ -L "$SYSTEM_UPDATE_LINK" ]; then
target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null)
if [ -n "$target" ] && [ -e "$target" ]; then
pass "T15 any /system-update left behind points at an existing target"
else
fail "T15 /system-update is left DANGLING - the next boot would fail"
fi
fi
# Did anything get installed? Every apt-get install this harness issues is
# --simulate, so the set of installed packages must be unchanged.
if command -v dpkg-query >/dev/null 2>&1; then