gunk-dev/the-valley

web-image.sh

source6303fac04580main's published tip history
2.6 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# Unpack every layer in order and record each entry's owner and mode from
19# the tar headers, since unprivileged extraction drops ownership.
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# Resolve a root-level symlink inside the unpacked image so its target
26# stays under root/.
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 -Eq '^drwx------ 991/990 .* (\./)?var/lib/valley-web/browser/?$' listing ||
35 fail "the browser state's directory is not valley-web's alone, mode 0700: $(grep 'var/lib/valley-web/browser' listing)"
36grep -qx 'valley-web-receive:x:990:990:valley-web-receive:/var/empty:/bin/false' "$(inside /etc/passwd)" ||
37 fail "/etc/passwd does not name the receiver's user"
38grep -qx 'valley-web:x:991:990:valley-web:/var/empty:/bin/false' "$(inside /etc/passwd)" ||
39 fail "/etc/passwd does not name valley-web's user"
40grep -qx 'valley-web:x:990:' "$(inside /etc/group)" || fail "/etc/group does not name the valley-web group"
41# Each program runs git at the store path it was built with, not at
42# /bin/git, so the image must hold that store path.
43for program in valley-web valley-web-receive; do
44 link=$(readlink "root/bin/$program") || fail "/bin/$program is missing"
45 [ -x "root$link" ] || fail "/bin/$program points at $link, which the image does not hold"
46 git=$(grep -ao '/nix/store/[a-z0-9]\{32\}-git-[^/]*/bin/git' "root$link" | head -n 1)
47 [ -n "$git" ] || fail "$program names no git"
48 [ -x "root$git" ] || fail "$program runs $git, which the image does not hold"
49done
50echo "web-image: the image holds both programs, the git they run, their users, the state directory and the browser state's directory"
51touch "$out"