gunk-dev/the-valley

web-image.sh

source8e8db4f9b2eamain's published tip history
2.3 KiB
1# shellcheck shell=bash
2# The image comes from the derivation environment.
3# shellcheck disable=SC2154
4fail() {
5 echo "web-image: $*" >&2
6 exit 1
7}
8mkdir image root
9tar -xzf "$image" -C image
10config=image/$(jq -r '.[0].Config' image/manifest.json)
11[ "$(jq -r '.config.User' "$config")" = valley-web ] ||
12 fail "the image does not run as valley-web unless it is told otherwise"
13jq -e '.config.Volumes | keys == ["/var/lib/valley-web"]' "$config" > /dev/null ||
14 fail "the image's one volume is not /var/lib/valley-web"
15jq -e '.config.Entrypoint == null and .config.Cmd == null' "$config" > /dev/null ||
16 fail "the image starts a program of its own"
17
18# Every layer, unpacked in order, and the owner and mode of each entry as
19# the archive gives them, which an unprivileged unpacking cannot keep.
20for layer in $(jq -r '.[0].Layers[]' image/manifest.json); do
21 tar --numeric-owner --absolute-names -tvf "image/$layer" >> listing
22 tar -xf "image/$layer" -C root 2> /dev/null
23 chmod -R u+w root
24done
25# A file at the image's root is a link to a store path the image holds,
26# which only a path under root reaches here.
27inside() {
28 local link
29 link=$(readlink "root$1") || link=$1
30 echo "root$link"
31}
32grep -Eq '^drwxr-x--- 990/990 .* (\./)?var/lib/valley-web/?$' listing ||
33 fail "the state directory is not the receiver's, mode 0750: $(grep 'var/lib/valley-web' listing)"
34grep -qx 'valley-web-receive:x:990:990:valley-web-receive:/var/empty:/bin/false' "$(inside /etc/passwd)" ||
35 fail "/etc/passwd does not name the receiver's user"
36grep -qx 'valley-web:x:991:990:valley-web:/var/empty:/bin/false' "$(inside /etc/passwd)" ||
37 fail "/etc/passwd does not name valley-web's user"
38grep -qx 'valley-web:x:990:' "$(inside /etc/group)" || fail "/etc/group does not name the valley-web group"
39for program in valley-web valley-web-receive; do
40 link=$(readlink "root/bin/$program") || fail "/bin/$program is missing"
41 [ -x "root$link" ] || fail "/bin/$program points at $link, which the image does not hold"
42 git=$(grep -ao '/nix/store/[a-z0-9]\{32\}-git-[^/]*/bin/git' "root$link" | head -n 1)
43 [ -n "$git" ] || fail "$program names no git"
44 [ -x "root$git" ] || fail "$program runs $git, which the image does not hold"
45done
46echo "web-image: the image holds both programs, the git they run, their users and the state directory"
47touch "$out"