302 lines
15 KiB
Bash
302 lines
15 KiB
Bash
#!/bin/sh
|
|
# Repository indexes (REPO_DESIGN.txt sections 4 and 5, step 3):
|
|
# - grepo writes DIR/repo.idx from DIR's packages and signs it; SERIAL
|
|
# is the date form YYYYMMDDNN, above the previous index's; -n, -e;
|
|
# packages of another repo, misnamed or non-regular are refused;
|
|
# - syspkg -y / gpkg -y fetch every repos.conf url's index (file://
|
|
# here), check its signature with a key listed for THAT repo, parse
|
|
# it strictly, refuse a rollback, the same serial with other bytes,
|
|
# an expired index and another repo's index, and only then rename it
|
|
# into <base>/repos; any failure keeps the accepted copy;
|
|
# - <base>/repos gets the scope's owner and modes; it is refused if
|
|
# loosened or a symlink; -y holds the scope lock; -R stores under
|
|
# the install root, with the host's keyring.
|
|
# Runs INSIDE the Dockerfile.alpine_root image (its test key,
|
|
# glacier-test, is listed for base):
|
|
#
|
|
# docker run --rm -v "$PWD/tests/repo_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() {
|
|
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
|
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
|
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
|
}
|
|
has() { echo "$OUT" | grep -qF -- "$1"; }
|
|
lacks() { ! has "$1"; }
|
|
rc() { [ "$RC" = "$1" ]; }
|
|
hr() { echo; echo "== $*"; }
|
|
export PATH=/app:$PATH
|
|
|
|
K=/etc/glacier/keys
|
|
CONF=/etc/glacier/repos.conf
|
|
TEST_SEC=/root/glacier-test.sec
|
|
B=/srv/base
|
|
E=/srv/extra
|
|
S=/glacier/sys/repos
|
|
mkdir -p $B $E && chmod 755 /srv $B $E
|
|
|
|
gsign -G -p $K/extrakey.pub -s /root/extrakey.sec -c extrakey >/dev/null
|
|
gsign -G -p /root/stranger.pub -s /root/stranger.sec -c stranger >/dev/null
|
|
conf() { printf 'repos = (\n%s\n);\n' "$1" > $CONF; chmod 644 $CONF; }
|
|
BASE_E=' { name = "base"; url = "file:///srv/base"; keys = [ "glacier-test" ]; }'
|
|
EXTRA_E=' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; }'
|
|
pk() { /app/tests/mkpkg -o "$1" ${5:+-k "$5"} "$2" "$3" "$4" >/dev/null; }
|
|
field() { sed -n "s/^$1 = \\([0-9]*\\)L;\$/\\1/p" "$2"; }
|
|
|
|
syspkg -n >/dev/null
|
|
|
|
hr "grepo"
|
|
pk $B core 1.0.0 base $TEST_SEC
|
|
pk $B app 2.1.0 base $TEST_SEC
|
|
/app/tests/mkpkg -o $B -k $TEST_SEC -d core:1.0.0 -c rival:0.0.0 tool 1.2.3 base >/dev/null
|
|
R grepo -s $TEST_SEC -r base $B
|
|
check "grepo writes an index" rc 0
|
|
check "...saying so" has "[✓] Wrote $B/repo.idx: 3 packages, serial $(date -u +%Y%m%d)01."
|
|
check "...signed: gsign verifies it with the test key" gsign -V -p $K/glacier-test.pub -m $B/repo.idx
|
|
check "...the first SERIAL is today's date form" [ "$(field SERIAL $B/repo.idx)" = "$(date -u +%Y%m%d)01" ]
|
|
VU=$(field VALID_UNTIL $B/repo.idx); NOW=$(date +%s)
|
|
check "...valid for 30 days by default" [ $((VU - NOW)) -gt $((30*86400 - 60)) -a $((VU - NOW)) -le $((30*86400)) ]
|
|
check "...packages sorted, with their sha256 and size" sh -c "grep -A1 'NAME = \"app\"' $B/repo.idx | grep -q \"SHA256 = \\\"sha256:$(sha256sum $B/app-2.1.0.gpkg | cut -c1-64)\\\"; SIZE = $(wc -c < $B/app-2.1.0.gpkg)L;\""
|
|
check "...dependencies and conflicts from the manifest" grep -qF 'DEPS = ( { NAME = "core"; VER = "1.0.0"; } ); CONFS = ( { NAME = "rival"; VER = "0.0.0"; } );' $B/repo.idx
|
|
check "...no temporary files left" [ "$(ls -A $B | grep -c '^\.')" = 0 ]
|
|
check "...the index is world-readable" [ "$(stat -c %a $B/repo.idx)" = 644 ]
|
|
R grepo -s $TEST_SEC -r base $B
|
|
check "a second run the same day takes the next serial" has "serial $(date -u +%Y%m%d)02."
|
|
R grepo -s $TEST_SEC -r base -n 5 $B
|
|
check "-n below the previous serial is refused" has "[x] SERIAL must be above $(date -u +%Y%m%d)02, the previous index's."
|
|
check "...exit 1, index unchanged" sh -c "[ $RC = 1 ] && grep -q '^SERIAL = $(date -u +%Y%m%d)02L;' $B/repo.idx"
|
|
R grepo -s $TEST_SEC -r base -n 2090010101 -e 1 $B
|
|
check "-n above it is used" grep -q '^SERIAL = 2090010101L;' $B/repo.idx
|
|
VU=$(field VALID_UNTIL $B/repo.idx); NOW=$(date +%s)
|
|
check "-e 1 makes it valid for one day" [ $((VU - NOW)) -gt $((86400 - 60)) -a $((VU - NOW)) -le 86400 ]
|
|
R grepo -s $TEST_SEC -r base -e 0 $B
|
|
check "-e 0 is refused" has "[x] DAYS must be a whole number from 1 to 3650."
|
|
R grepo -s $TEST_SEC -r base
|
|
check "no DIR prints the usage" has "usage: grepo -s KEY.sec -r REPO [-n SERIAL] [-e DAYS] DIR"
|
|
R grepo -s /root/missing.sec -r base $B
|
|
check "an unreadable secret key is refused" has "[x] Secret key /root/missing.sec cannot be read."
|
|
R grepo -s $TEST_SEC -r 'ba/se' $B
|
|
check "an unsafe repo name is refused" has "[x] Repository name ba/se is not a safe name ([A-Za-z0-9._-])."
|
|
cp $B/repo.idx /srv/before.idx
|
|
pk $B stray 1.0.0 extra
|
|
R grepo -s $TEST_SEC -r base $B
|
|
check "a package of another repo is refused" has "[x] $B/stray-1.0.0.gpkg is a extra package, not base."
|
|
check "...and the index is left as it was" cmp -s $B/repo.idx /srv/before.idx
|
|
check "...with no temporary files left" [ "$(ls -A $B | grep -c '^\.')" = 0 ]
|
|
rm $B/stray-1.0.0.gpkg*
|
|
cp $B/core-1.0.0.gpkg $B/renamed-1.0.0.gpkg
|
|
R grepo -s $TEST_SEC -r base $B
|
|
check "a package named unlike its manifest is refused" has "[x] $B/renamed-1.0.0.gpkg must be named core-1.0.0.gpkg, as its manifest says."
|
|
rm $B/renamed-1.0.0.gpkg
|
|
ln -s /srv/base/core-1.0.0.gpkg $B/link-1.0.0.gpkg
|
|
R grepo -s $TEST_SEC -r base $B
|
|
check "a symlinked package is refused" has "[x] Refusing $B/link-1.0.0.gpkg: not a regular file."
|
|
rm $B/link-1.0.0.gpkg
|
|
R grepo -s $TEST_SEC -r extra $B
|
|
check "a DIR holding another repo's index is refused" has "[x] $B/repo.idx is the index of another repository."
|
|
mkdir -p /srv/empty
|
|
R grepo -s $TEST_SEC -r community /srv/empty
|
|
check "an empty DIR makes an empty index" has "[✓] Wrote /srv/empty/repo.idx: 0 packages"
|
|
R grepo -s $TEST_SEC -r base -n 2090010102 $B
|
|
check "grepo works again after those refusals" rc 0
|
|
|
|
hr "signify interop"
|
|
if apk add --no-cache signify >/dev/null 2>&1; then
|
|
check "signify verifies grepo's index" signify -V -q -p $K/glacier-test.pub -m $B/repo.idx -x $B/repo.idx.sig
|
|
else
|
|
echo "FAIL: apk add signify"; FAILS=$((FAILS+1))
|
|
fi
|
|
|
|
hr "syspkg -y: accepting"
|
|
rm -f $CONF
|
|
R syspkg -y
|
|
check "no repos.conf: nothing to refresh" has "[x] No remote repositories are configured."
|
|
check "...exit 1" rc 1
|
|
conf ' { name = "base"; keys = [ "glacier-test" ]; }'
|
|
R syspkg -y
|
|
check "repos.conf without a url: the same" has "[x] No remote repositories are configured."
|
|
conf "$BASE_E"
|
|
R syspkg -y
|
|
check "an index signed by a key listed for base is accepted" has "[✓] Index for base: serial 2090010102, 3 packages."
|
|
check "...exit 0" rc 0
|
|
check "...stored byte for byte" cmp -s $B/repo.idx $S/base.idx
|
|
check "...with its signature" cmp -s $B/repo.idx.sig $S/base.idx.sig
|
|
check "...in a root-owned 0755 repos dir" [ "$(stat -c '%u %a' $S)" = "0 755" ]
|
|
check "...as root-owned 0644 files" [ "$(stat -c '%u %a' $S/base.idx) $(stat -c '%u %a' $S/base.idx.sig)" = "0 644 0 644" ]
|
|
check "...no temporary files left" [ "$(ls -A $S | tr '\n' ' ')" = "base.idx base.idx.sig " ]
|
|
R syspkg -y
|
|
check "refreshing again: up to date" has "[i] Index for base is up to date: serial 2090010102."
|
|
check "...exit 0" rc 0
|
|
grepo -s $TEST_SEC -r base -n 2090010103 $B >/dev/null
|
|
R syspkg --refresh
|
|
check "a newer index is accepted (--refresh)" has "[✓] Index for base: serial 2090010103, 3 packages."
|
|
check "...replacing the stored one" cmp -s $B/repo.idx $S/base.idx
|
|
|
|
hr "syspkg -y: refusing"
|
|
cp $S/base.idx /srv/accepted.idx
|
|
# serve TEXT [KEY.sec]: publish TEXT as base's index, signed (test key
|
|
# by default; "-" for no signature).
|
|
serve() {
|
|
printf '%s\n' "$1" > $B/repo.idx
|
|
rm -f $B/repo.idx.sig
|
|
[ "${2:-}" = - ] || gsign -S -s "${2:-$TEST_SEC}" -m $B/repo.idx >/dev/null
|
|
}
|
|
kept() { cmp -s $S/base.idx /srv/accepted.idx; }
|
|
FUTURE=$(( $(date +%s) + 86400 ))
|
|
SHA="sha256:$(printf '%064d' 0)"
|
|
idx() { # idx SERIAL [VALID_UNTIL [PKGS [REPO]]]
|
|
printf 'REPO = "%s";\nSERIAL = %sL;\nVALID_UNTIL = %sL;\nPKGS = (%s);' \
|
|
"${4:-base}" "$1" "${2:-$FUTURE}" "${3:-}"
|
|
}
|
|
PKG1="{ NAME = \"a\"; VER = \"1.0.0\"; FILE = \"a-1.0.0.gpkg\"; SHA256 = \"$SHA\"; SIZE = 1L; DEPS = (); CONFS = (); }"
|
|
|
|
serve "$(idx 2090010102)"
|
|
R syspkg -y
|
|
check "an older serial is refused" has "[x] Index for base is older than the one already accepted."
|
|
check "...exit 1" rc 1
|
|
check "...keeping the accepted index" kept
|
|
check "...and no temporary files" [ "$(ls -A $S | tr '\n' ' ')" = "base.idx base.idx.sig " ]
|
|
serve "$(idx 2090010103 "" "$PKG1")"
|
|
R syspkg -y
|
|
check "the same serial with other contents is refused" has "[x] Index for base has the serial of the one already accepted, but other contents."
|
|
check "...keeping the accepted index" kept
|
|
serve "$(idx 2090010200 $(( $(date +%s) - 10 )))"
|
|
R syspkg -y
|
|
check "an expired index is refused" has "[x] Index for base has expired."
|
|
check "...keeping the accepted index" kept
|
|
serve "$(idx 2090010200)" -
|
|
R syspkg -y
|
|
check "no signature: refused as unfetchable" has "[x] Cannot fetch the index for base."
|
|
check "...keeping the accepted index" kept
|
|
serve "$(idx 2090010200)" /root/stranger.sec
|
|
R syspkg -y
|
|
check "a key in no keyring is refused" has "[x] Index for base is not signed by a key trusted for base."
|
|
check "...keeping the accepted index" kept
|
|
serve "$(idx 2090010200)" /root/extrakey.sec
|
|
conf "$BASE_E
|
|
,$EXTRA_E"
|
|
R syspkg -y
|
|
check "a key listed only for extra is refused for base" has "[x] Index for base is not signed by a key trusted for base."
|
|
check "...keeping the accepted index" kept
|
|
conf "$BASE_E"
|
|
serve "$(idx 2090010200)"
|
|
printf '# appended after signing\n' >> $B/repo.idx
|
|
R syspkg -y
|
|
check "bytes changed after signing are refused" has "[x] Index for base is not signed by a key trusted for base."
|
|
check "...keeping the accepted index" kept
|
|
serve "$(idx 2090010200 "" "" extra)"
|
|
R syspkg -y
|
|
check "another repo's index (validly signed) is refused" has "[x] Index fetched for base is the index of another repository."
|
|
check "...keeping the accepted index" kept
|
|
|
|
# Signed, but not a well-formed index: each one "Malformed index".
|
|
malformed() {
|
|
serve "$2"
|
|
R syspkg -y
|
|
check "malformed: $1" sh -c "echo \"\$0\" | grep -qF '[x] Malformed index for base.' && cmp -s $S/base.idx /srv/accepted.idx" "$OUT"
|
|
}
|
|
malformed "not libconfig" "this is not an index"
|
|
malformed "an unknown top-level setting" "$(idx 2090010200) EXTRA = 1;"
|
|
malformed "a missing setting" 'REPO = "base"; SERIAL = 2090010200L; PKGS = ();'
|
|
malformed "SERIAL 0" "$(idx 0)"
|
|
malformed "SERIAL a string" "$(idx '"2090010200"' | sed 's/"L;/";/')"
|
|
# An include that WOULD make a valid index: only the '@' rule stops it.
|
|
printf 'PKGS = ();\n' > /srv/inc.cfg
|
|
malformed "@include" "$(idx 2090010200 | grep -v '^PKGS')
|
|
@include \"/srv/inc.cfg\""
|
|
malformed "an unknown package field" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's/CONFS = ();/CONFS = (); X = 1;/')")"
|
|
malformed "a missing package field" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's/SIZE = 1L; //')")"
|
|
malformed "FILE not NAME-VER.gpkg" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#a-1.0.0.gpkg#../x-1.0.0.gpkg#')")"
|
|
malformed "an unsafe NAME" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#NAME = "a"#NAME = "a/b"#; s#a-1.0.0.gpkg#a/b-1.0.0.gpkg#')")"
|
|
malformed "a version with a leading zero" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#1\.0\.0#1.00.0#g')")"
|
|
malformed "a version with a tag" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#1\.0\.0#1.0.0-rc1#g')")"
|
|
malformed "uppercase hex" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#sha256:0#sha256:A#')")"
|
|
malformed "a short hash" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#sha256:0#sha256:#')")"
|
|
malformed "a negative SIZE" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#SIZE = 1L#SIZE = -1L#')")"
|
|
malformed "the same NAME and VER twice" "$(idx 2090010200 "" "$PKG1, $PKG1")"
|
|
malformed "a dependency without VER" "$(idx 2090010200 "" "$(echo "$PKG1" | sed 's#DEPS = ()#DEPS = ( { NAME = "b"; } )#')")"
|
|
serve "$(idx 2090010200 "" "$PKG1, $(echo "$PKG1" | sed 's#1\.0\.0#1.0.1#g')")"
|
|
R syspkg -y
|
|
check "two versions of one package are fine" has "[✓] Index for base: serial 2090010200, 2 packages."
|
|
cp $S/base.idx /srv/accepted.idx
|
|
|
|
hr "fetch failures and several repos"
|
|
grepo -s /root/extrakey.sec -r extra $E >/dev/null 2>&1 || true
|
|
pk $E ex 1.0.0 extra
|
|
grepo -s /root/extrakey.sec -r extra $E >/dev/null
|
|
conf ' { name = "base"; url = "file:///srv/nowhere"; keys = [ "glacier-test" ]; }
|
|
,'"$EXTRA_E"
|
|
R syspkg -y
|
|
check "an unreachable repo fails" has "[x] Cannot fetch the index for base."
|
|
check "...the next repo is still refreshed" has "[✓] Index for extra: serial"
|
|
check "...exit 1" rc 1
|
|
check "...base's accepted index kept" kept
|
|
check "...extra's stored" cmp -s $E/repo.idx $S/extra.idx
|
|
conf ' { name = "base"; url = "http://example.org/base"; keys = [ "glacier-test" ]; }'
|
|
R syspkg -y
|
|
check "an http:// url is refused" has "[x] Malformed /etc/glacier/repos.conf."
|
|
conf ' { name = "base"; url = "https://127.0.0.1:1/base"; keys = [ "glacier-test" ]; }'
|
|
R syspkg -y
|
|
check "an https url that can't be reached fails in one line" has "[x] Cannot fetch the index for base."
|
|
check "...exit 1" rc 1
|
|
conf "$BASE_E"
|
|
|
|
hr "the repos directory and the lock"
|
|
cp $S/base.idx /srv/accepted.idx
|
|
printf 'garbage\n' > $S/base.idx
|
|
R syspkg -y
|
|
check "a corrupt accepted index is refused, not replaced" has "[x] Malformed $S/base.idx."
|
|
check "...exit 1" rc 1
|
|
check "...left for the admin" grep -q garbage $S/base.idx
|
|
cp /srv/accepted.idx $S/base.idx
|
|
chmod 775 $S
|
|
R syspkg -y
|
|
check "a group-writable repos dir is refused" has "[x] Refusing $S: not a directory owned by UID 0 with mode 755."
|
|
chmod 755 $S
|
|
mv $S /srv/repos.real && ln -s /srv/repos.real $S
|
|
R syspkg -y
|
|
check "a symlinked repos dir is refused" has "[x] Refusing $S: not a directory owned by UID 0 with mode 755."
|
|
rm $S && mv /srv/repos.real $S
|
|
flock /glacier/sys/lock sleep 3 &
|
|
sleep 1
|
|
R syspkg -y
|
|
check "-y waits for no one: a held lock refuses" has "[x] Another glacier transaction is already in progress for the system scope."
|
|
wait
|
|
R syspkg -y -l x
|
|
check "-y with -l is refused" has "[x] -n, -l, -x and -y are mutually exclusive."
|
|
R syspkg -y extra
|
|
check "-y takes no arguments" has "[x] Unexpected argument: extra"
|
|
adduser -D tester >/dev/null 2>&1
|
|
R su tester -c 'syspkg -y'
|
|
check "syspkg -y as a user is refused" has "[x] Cannot open system index: Permission denied"
|
|
|
|
hr "syspkg -R DIR -y"
|
|
mkdir -p /srv/img && chmod 755 /srv/img
|
|
syspkg -R /srv/img -n >/dev/null
|
|
cp $S/base.idx /srv/host.idx
|
|
grepo -s $TEST_SEC -r base -n 2090010300 $B >/dev/null
|
|
R syspkg -R /srv/img -y
|
|
check "-R stores the index under the install root" cmp -s $B/repo.idx /srv/img/glacier/sys/repos/base.idx
|
|
check "...leaving the host's alone" cmp -s $S/base.idx /srv/host.idx
|
|
|
|
hr "gpkg -y"
|
|
U=$(id -u tester)
|
|
gstore -n "$U" >/dev/null
|
|
R su tester -c 'gpkg -y'
|
|
check "gpkg -y refreshes into the user's base" has "[✓] Index for base: serial 2090010300"
|
|
check "...owned by the user, 0700" [ "$(stat -c '%u %a' /glacier/usr/$U/repos)" = "$U 700" ]
|
|
check "...files 0600" [ "$(stat -c '%u %a' /glacier/usr/$U/repos/base.idx)" = "$U 600" ]
|
|
check "...byte for byte" cmp -s $B/repo.idx /glacier/usr/$U/repos/base.idx
|
|
serve "$(idx 2090010200)"
|
|
R su tester -c 'gpkg -y'
|
|
check "gpkg -y refuses a rollback too" has "[x] Index for base is older than the one already accepted."
|
|
check "...exit 1" rc 1
|
|
R su tester -c 'gpkg -y base'
|
|
check "gpkg -y takes no arguments" has "[x] -y takes no arguments."
|
|
|
|
echo
|
|
echo "FAILURES: $FAILS"
|
|
[ $FAILS -eq 0 ]
|