Files
glacier-ng/tests/installed_alpine.sh
2026-10-29 09:15:34 -04:00

156 lines
6.8 KiB
Bash

#!/bin/sh
# The installed layout (ROADMAP 1.4): syspkg, gpkg and gstore run from the
# store, as packaged by ~/Projects/everest/scripts/container.sh:
# usr/lib/glacier-ng/{syspkg,gpkg,gstore,lib/*.lua}
# usr/lib/glacier-ng/lib/glacier -> /usr/lib/glacier (absolute)
# usr/bin/X -> ../lib/glacier-ng/X
# etc/glacier/glacier.conf
# plus libglacier-ng's usr/lib/glacier/*.so. Both are installed into the
# container's own / by syspkg, then /app is moved away, so every later
# run can only work by loading from its realpath in the store:
# - each tool runs from any directory, as root and as a user;
# - syspkg installs and removes, gstore -n creates a user, a wheel user
# runs `doas gstore -n` with no password, and gpkg installs;
# - syspkg's and gstore's trust checks still hold in the store: a
# group-writable .lua there, or a non-root-owned store dir, is
# refused.
# Runs INSIDE the Dockerfile.alpine_root image:
#
# docker run --rm -v "$PWD/tests/installed_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
FAILS=0
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
# R CMD...: run a shell command line, answering y. Sets $OUT and $RC.
R() {
OUT=$( (echo y | sh -c "$1"; echo "rc=$?") 2>&1 | nocolor)
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
echo "$OUT" | sed 's/^/ /'
}
# U CMD...: the same, as user glacier (uid 1000, in wheel).
U() { R "su glacier -c $(printf "'%s'" "$1")"; }
has() { echo "$OUT" | grep -qF -- "$1"; }
lacks() { ! has "$1"; }
rc() { [ "$RC" = "$1" ]; }
hr() { echo; echo "== $*"; }
P=/srv/pkgs
mkdir -p $P
cp /app/tests/mkpkg /srv/mkpkg
chmod 755 /srv /srv/pkgs
# seal DIR NAME VER: DIR/NAME-VER (files/ + manifest) -> $P/NAME-VER.gpkg
seal() {
printf 'PKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_DESC = "t";\nPKG_MAINT = "t@localhost";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "base";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
"$2" "$3" > "$1/$2-$3/manifest.gpm.cfg"
tar czf "$P/$2-$3.gpkg" -C "$1" "$2-$3"
printf 'sha256:%s\n' "$(sha256sum "$P/$2-$3.gpkg" | cut -d' ' -f1)" > "$P/$2-$3.gpkg.sha256"
gsign -S -s /root/glacier-test.sec -m "$P/$2-$3.gpkg.sha256" >/dev/null
chmod 644 "$P/$2-$3.gpkg" "$P/$2-$3.gpkg.sha256" "$P/$2-$3.gpkg.sha256.sig"
}
w=$(mktemp -d)
f=$w/libglacier-ng-4.0.0/files
mkdir -p $f/usr/lib/glacier
install -m 755 /app/lib/glacier/libglacier_*.so /app/lib/glacier/gsandbox $f/usr/lib/glacier/
seal $w libglacier-ng 4.0.0
f=$w/glacier-ng-4.0.0/files
d=$f/usr/lib/glacier-ng
mkdir -p $d/lib $f/usr/bin $f/etc/glacier
(cd /app && install -m 755 syspkg gpkg gstore $d/ &&
install -m 644 lib/cargs.lua lib/color.lua lib/glacier_cdef.lua lib/gpkg_common.lua lib/gpkg_build.lua lib/build-driver.sh $d/lib/ &&
install -m 644 etc/glacier/glacier.conf $f/etc/glacier/)
ln -s /usr/lib/glacier $d/lib/glacier
for t in syspkg gpkg gstore; do ln -s ../lib/glacier-ng/$t $f/usr/bin/$t; done
seal $w glacier-ng 4.0.0
rm -rf $w
/srv/mkpkg -o $P demo 1.0.0 extra >/dev/null
/srv/mkpkg -o $P udemo 1.0.0 extra >/dev/null
# The image's glacier.conf came from the Dockerfile: let the package own it.
rm /etc/glacier/glacier.conf
printf '%s\n' 'permit nopass :wheel as root cmd gstore args -n' \
'permit nopass :wheel as root cmd /usr/bin/gstore args -n' > /etc/doas.conf
chmod 0400 /etc/doas.conf
hr "install glacier with /app's syspkg, then remove /app"
R "/app/syspkg -n && /app/syspkg -l $P/libglacier-ng-4.0.0.gpkg $P/glacier-ng-4.0.0.gpkg"
check "the install succeeds" rc 0
check "it ends with no errors" has "Completed with no errors."
mv /app /app.gone
check "/usr/bin/syspkg resolves into the store" \
sh -c 'case $(readlink -f /usr/bin/syspkg) in /glacier/sys/store/base/glacier-ng/glacier-ng-4.0.0/*) ;; *) exit 1 ;; esac'
check "glacier.conf is installed" grep -q KEEP_OLD /etc/glacier/glacier.conf
hr "syspkg from the store"
R "cd / && /usr/bin/syspkg -h"
check "syspkg -h works from /" rc 0
R "cd /tmp && syspkg -l $P/demo-1.0.0.gpkg"
check "syspkg -l (bare name, from /tmp) installs" rc 0
check "...with no errors" has "Completed with no errors."
check "the installed program runs" sh -c '/glacier/sys/links/bin/demo | grep -q "demo 1.0.0 says hi"'
R "cd /srv && syspkg -x demo"
check "syspkg -x removes" rc 0
check "...and its link is gone" sh -c '! [ -e /glacier/sys/links/bin/demo ]'
hr "a user, through the store's gstore and gpkg"
U "cd / && doas -n gstore -n"
check "a wheel user's doas gstore -n needs no password" rc 0
check "...and creates their directory" test -d /glacier/usr/1000
U "cd /tmp && gpkg -l $P/udemo-1.0.0.gpkg"
check "gpkg -l (from /tmp) installs" rc 0
check "...with no errors" has "Completed with no errors."
U "/glacier/usr/1000/links/bin/udemo"
check "the user's program runs" has "udemo 1.0.0 says hi"
U "cd / && gpkg -x udemo"
check "gpkg -x removes" rc 0
R "cd /tmp && gstore -n 1001"
check "root's gstore -n UID works from the store" rc 0
hr "the trust checks hold in the store"
L=$(readlink -f /usr/lib/glacier-ng/lib/gpkg_common.lua)
chmod g+w "$L"
R "cd / && syspkg -l $P/demo-1.0.0.gpkg"
check "syspkg refuses a group-writable .lua in the store" rc 1
check "...saying which" has "Refusing to run: $L is group- or world-writable."
chmod g-w "$L"
L=$(readlink -f /usr/lib/glacier-ng/lib/glacier_cdef.lua)
chmod g+w "$L"
R "cd / && gstore -n 1002"
check "gstore refuses a group-writable .lua it loads" has "Refusing to run: $L is group- or world-writable."
check "...and creates nothing" test ! -e /glacier/usr/1002
chmod g-w "$L"
V=$(readlink -f /usr/bin/syspkg); V=${V%/usr/lib/glacier-ng/syspkg}
chown 1000 $V
R "cd / && syspkg -l $P/demo-1.0.0.gpkg"
check "syspkg refuses a store dir not owned by root" has "Refusing to run: $V is not owned by root."
check "...and installs nothing" test ! -e /glacier/sys/links/bin/demo
chown 0 $V
S=$(readlink -f /usr/lib/glacier/libglacier_log.so)
chmod o+w "$S"
R "cd / && syspkg -l $P/demo-1.0.0.gpkg"
check "syspkg refuses a world-writable .so behind lib/glacier" rc 1
check "...saying so" has "is group- or world-writable."
chmod o-w "$S"
# The .so's are reached through lib/glacier -> /usr/lib/glacier -> links
# into libglacier-ng's store dir: that dir's ancestors must be checked
# too, not just those of the script's own realpath.
V=${S%/usr/lib/glacier/libglacier_log.so}
chown 1000 $V
R "cd / && syspkg -l $P/demo-1.0.0.gpkg"
check "syspkg refuses libglacier-ng's store dir not owned by root" has "Refusing to run: $V is not owned by root."
check "...and installs nothing" test ! -e /glacier/sys/links/bin/demo
R "cd / && gstore -n 1003"
check "gstore refuses it too" has "Refusing to run: $V is not owned by root."
check "...and creates nothing" test ! -e /glacier/usr/1003
chown 0 $V
R "cd / && syspkg -l $P/demo-1.0.0.gpkg"
check "with everything restored, syspkg works again" rc 0
echo
echo "FAILURES: $FAILS"
[ $FAILS -eq 0 ] && echo "ALL PASSED"
exit $FAILS