Files
mc/vezha/check-image.sh
T

139 lines
4.5 KiB
Bash

#!/bin/sh
set -eu
kind=${1:?usage: check-image.sh <mc|minio> <image>}
image=${2:?usage: check-image.sh <mc|minio> <image>}
REGISTRY=git.vezha.io
OWNER=para
STOPGAP_MC_DIGEST=sha256:eb4ea9884b77704230e2423e9004d2fa738dc272876b9cc41a297d29443b8780
fail() {
printf 'REFUSED image: %s\n' "$1" >&2
exit 1
}
for v in UPSTREAM_RELEASE UPSTREAM_COMMIT RECIPE_COMMIT BINARY_SHA256 GO_BUILDER MODULE_VERSION MODULE_SUM; do
eval "val=\${$v:-}"
[ -n "$val" ] || fail "environment variable $v is empty"
done
tmp=$(mktemp -d)
cid=""
cleanup() {
if [ -n "$cid" ]; then
docker rm -f "$cid" > /dev/null 2>&1 || true
fi
rm -rf "$tmp"
}
trap cleanup EXIT
cid=$(docker create "$image")
docker export "$cid" > "$tmp/fs.tar"
tar -tvf "$tmp/fs.tar" | sed 's| link to .*||; s| -> .*||' > "$tmp/list.txt"
docker inspect "$cid" > "$tmp/inspect.json"
mode_of() {
awk -v p="$1" '{ n = $NF; sub(/^\.\//, "", n); sub(/\/$/, "", n); if (n == p) { print $1; exit } }' "$tmp/list.txt"
}
need_file() {
m=$(mode_of "$1")
[ -n "$m" ] || fail "$image has no /$1"
echo "image: /$1 present ($m)"
}
member() {
tar -xOf "$tmp/fs.tar" "$1" 2> /dev/null || tar -xOf "$tmp/fs.tar" "./$1"
}
cfg() {
jq -r "$1" "$tmp/inspect.json"
}
bin=$kind
need_file "usr/bin/$bin"
need_file etc/ssl/certs/ca-certificates.crt
need_file licenses/LICENSE
need_file licenses/CREDITS
need_file licenses/SOURCE
case $kind in
mc)
need_file bin/sh
;;
minio)
need_file usr/bin/mc
need_file etc/passwd
need_file etc/group
need_file licenses/mc/LICENSE
need_file licenses/mc/CREDITS
if [ "${MC_IMAGE:-}" != "$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST" ]; then
need_file licenses/mc/SOURCE
fi
[ "$(mode_of tmp)" = drwxrwxrwt ] || fail "/tmp of $image is not mode 1777"
;;
esac
want_label() {
got=$(cfg ".[0].Config.Labels[\"$1\"] // \"\"")
[ "$got" = "$2" ] || fail "label $1 is '$got', expected '$2'"
echo "image: label $1 = $got"
}
want_label org.opencontainers.image.source "https://$REGISTRY/$OWNER/$kind"
want_label org.opencontainers.image.revision "$UPSTREAM_COMMIT"
want_label org.opencontainers.image.version "$UPSTREAM_RELEASE"
want_label org.opencontainers.image.licenses AGPL-3.0
want_label io.vezha.recipe "$RECIPE_COMMIT"
want_label io.vezha.binary.sha256 "$BINARY_SHA256"
want_cfg() {
got=$(cfg "$1")
[ "$got" = "$2" ] || fail "$3 is '$got', expected '$2'"
echo "image: $3 = $got"
}
user=$(cfg '.[0].Config.User // ""')
case $user in
"" | root | 0) echo "image: runs as root" ;;
*) fail "the image runs as user '$user', expected root" ;;
esac
case $kind in
mc)
want_cfg '.[0].Config.Entrypoint | tostring' '["mc"]' Entrypoint
want_cfg '.[0].Config.Cmd | tostring' 'null' Cmd
want_cfg '.[0].Config.Healthcheck | tostring' 'null' Healthcheck
;;
minio)
want_cfg '.[0].Config.Entrypoint | tostring' '["/usr/bin/minio"]' Entrypoint
want_cfg '.[0].Config.Cmd | tostring' '["server","/data"]' Cmd
want_cfg '.[0].Config.Volumes | keys | tostring' '["/data"]' Volumes
want_cfg '.[0].Config.ExposedPorts | keys | tostring' '["9000/tcp"]' ExposedPorts
want_cfg '.[0].Config.Healthcheck.Test | tostring' '["CMD","/usr/bin/mc","ready","local"]' Healthcheck
want_cfg '[.[0].Config.Env[] | select(. == "MC_CONFIG_DIR=/tmp/.mc")] | length' 1 MC_CONFIG_DIR
want_cfg '[.[0].Config.Env[] | select(. == "PATH=/usr/bin")] | length' 1 PATH
;;
esac
got=$(member "usr/bin/$bin" | sha256sum | cut -d' ' -f1)
[ "$got" = "$BINARY_SHA256" ] || fail "/usr/bin/$bin has sha256 $got, expected $BINARY_SHA256"
echo "image: /usr/bin/$bin sha256 $got"
member licenses/LICENSE | cmp -s - src/LICENSE || fail "/licenses/LICENSE differs from src/LICENSE"
member licenses/CREDITS | cmp -s - src/CREDITS || fail "/licenses/CREDITS differs from src/CREDITS"
member licenses/SOURCE > "$tmp/SOURCE"
for needle in \
"https://github.com/minio/$kind" \
"$UPSTREAM_RELEASE" \
"$UPSTREAM_COMMIT" \
"https://$REGISTRY/$OWNER/$kind/src/tag/$UPSTREAM_RELEASE" \
"https://$REGISTRY/$OWNER/$kind/archive/$UPSTREAM_RELEASE.tar.gz" \
"https://$REGISTRY/$OWNER/$kind/src/commit/$RECIPE_COMMIT" \
"$GO_BUILDER" \
"$MODULE_VERSION" \
"$MODULE_SUM" \
"$BINARY_SHA256"; do
grep -qF "$needle" "$tmp/SOURCE" || fail "/licenses/SOURCE does not name $needle"
done
echo "image: /licenses/SOURCE names the upstream, the tag, the recipe, the builder, the module and the binary"
[ -f .dockerignore ] || fail "no .dockerignore at the repository root"
echo "image: all checks passed for $image"