| 1 | # shellcheck shell=bash |
| 2 | # The image comes from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | fail() { |
| 5 | echo "web-image: $*" >&2 |
| 6 | exit 1 |
| 7 | } |
| 8 | mkdir image root |
| 9 | tar -xzf "$image" -C image |
| 10 | config=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" |
| 13 | jq -e '.config.Volumes | keys == ["/var/lib/valley-web"]' "$config" > /dev/null || |
| 14 | fail "the image's one volume is not /var/lib/valley-web" |
| 15 | jq -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. |
| 20 | for 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 |
| 24 | done |
| 25 | # Resolve a root-level symlink inside the unpacked image so its target |
| 26 | # stays under root/. |
| 27 | inside() { |
| 28 | local link |
| 29 | link=$(readlink "root$1") || link=$1 |
| 30 | echo "root$link" |
| 31 | } |
| 32 | grep -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)" |
| 34 | grep -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" |
| 36 | grep -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" |
| 38 | grep -qx 'valley-web:x:990:' "$(inside /etc/group)" || fail "/etc/group does not name the valley-web group" |
| 39 | # Each program runs git at the store path it was built with, not at |
| 40 | # /bin/git, so the image must hold that store path. |
| 41 | for program in valley-web valley-web-receive; do |
| 42 | link=$(readlink "root/bin/$program") || fail "/bin/$program is missing" |
| 43 | [ -x "root$link" ] || fail "/bin/$program points at $link, which the image does not hold" |
| 44 | git=$(grep -ao '/nix/store/[a-z0-9]\{32\}-git-[^/]*/bin/git' "root$link" | head -n 1) |
| 45 | [ -n "$git" ] || fail "$program names no git" |
| 46 | [ -x "root$git" ] || fail "$program runs $git, which the image does not hold" |
| 47 | done |
| 48 | echo "web-image: the image holds both programs, the git they run, their users and the state directory" |
| 49 | touch "$out" |