Files
mc/vezha/gates.sh
T

427 lines
16 KiB
Bash

#!/bin/sh
set -eu
REGISTRY=git.vezha.io
OWNER=para
STOPGAP_SERVER_TAG=release.2025-09-07t16-13-09z
STOPGAP_SERVER_DIGEST=sha256:a1a8bd4ac40ad7881a245bab97323e18f971e4d4cba2c2007ec1bedd21cbaba2
STOPGAP_MC_TAG=release.2025-08-13t08-35-41z
STOPGAP_MC_DIGEST=sha256:eb4ea9884b77704230e2423e9004d2fa738dc272876b9cc41a297d29443b8780
TAG_RE='^release\.[0-9]{4}-[0-9]{2}-[0-9]{2}t[0-9]{2}-[0-9]{2}-[0-9]{2}z-[0-9a-f]{12}-r[0-9a-f]{8}$'
ACCEPT='application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.docker.distribution.manifest.list.v2+json'
GUARD="if: github.ref == 'refs/heads/vezha'"
SELF=$0
refuse() {
printf 'REFUSED %s: %s\n' "$1" "$2" >&2
exit 1
}
pin_value() {
sed -n "s/^$1=//p" "$2"
}
anon_head() {
token=$(curl -fsS "https://$REGISTRY/v2/token?service=container_registry&scope=repository:$OWNER/$1:pull" | jq -r .token) || return 1
curl -sS -o /dev/null -D - -H "Authorization: Bearer $token" -H "Accept: $ACCEPT" "https://$REGISTRY/v2/$OWNER/$1/manifests/$2" \
| tr -d '\r' | awk '/^HTTP/ { s = $2 } tolower($1) == "docker-content-digest:" { d = $2 } END { print s, d }'
}
fetch_ok() {
i=0
code=none
while [ "$i" -lt 3 ]; do
code=$(curl -sS -o /dev/null -L -m 900 -w '%{http_code}' "$1" || true)
if [ "$code" = 200 ]; then
return 0
fi
i=$((i + 1))
sleep 5
done
echo "$1 answered $code" >&2
return 1
}
cmd_pins() {
kind=$1
file=${2:-vezha/PINS}
case $kind in
mc) last=BASE_IMAGE ;;
minio) last=MC_IMAGE ;;
*) refuse pins "unknown kind $kind" ;;
esac
keys="UPSTREAM_RELEASE UPSTREAM_COMMIT MODULE MODULE_VERSION MODULE_SUM GO_BUILDER $last"
[ -f "$file" ] || refuse pins "$file is missing"
n=0
while IFS= read -r line || [ -n "$line" ]; do
n=$((n + 1))
if printf '%s' "$line" | grep -q '[[:space:]"'"'"']'; then
refuse pins "line $n contains a space or a quote"
fi
printf '%s' "$line" | grep -Eq '^[A-Z_]+=' || refuse pins "line $n is not KEY=VALUE"
k=${line%%=*}
known=0
for x in $keys; do
if [ "$x" = "$k" ]; then
known=1
fi
done
[ "$known" = 1 ] || refuse pins "unknown key $k"
done < "$file"
for k in $keys; do
c=$(grep -c "^$k=" "$file" || true)
[ "$c" -ge 1 ] || refuse pins "missing key $k"
[ "$c" -le 1 ] || refuse pins "duplicate key $k"
[ -n "$(pin_value "$k" "$file")" ] || refuse pins "empty value for key $k"
done
shape() {
printf '%s' "$(pin_value "$1" "$file")" | grep -Eq "$2" || refuse pins "key $1 has an unexpected form"
}
shape UPSTREAM_RELEASE '^RELEASE\.[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}Z$'
shape UPSTREAM_COMMIT '^[0-9a-f]{40}$'
shape MODULE "^github\\.com/minio/$kind\$"
shape MODULE_VERSION '^v0\.0\.0-[0-9]{14}-[0-9a-f]{12}$'
shape MODULE_SUM '^h1:[A-Za-z0-9+/]{43}=$'
for k in $keys; do
printf '%s=%s\n' "$k" "$(pin_value "$k" "$file")"
done
}
wf_lint() {
f=$1
if grep -q 'pull_request' "$f"; then
refuse refs "the workflow has a pull_request trigger"
fi
if grep -n 'latest' "$f" | grep -v 'ubuntu-latest'; then
refuse refs "the workflow mentions latest (lines above)"
fi
grep -q '^ contents: read$' "$f" || refuse refs "permissions contents: read is missing"
awk -v g="$GUARD" '
function flush() {
if (name == "") return
if (secret) secretsteps++
if ((risky || secret) && !guarded) {
printf "REFUSED refs: step \"%s\" can push or read a secret and has no vezha-ref guard\n", name > "/dev/stderr"
bad = 1
}
}
/^ - name: / { flush(); name = $0; sub(/^ - name: /, "", name); risky = 0; secret = 0; guarded = 0 }
/docker login|docker push|oras push/ { risky = 1 }
/secrets\./ { secret = 1 }
index($0, g) { guarded = 1 }
END {
flush()
if (secretsteps != 1) {
printf "REFUSED refs: secrets. appears in %d steps, expected exactly 1\n", secretsteps > "/dev/stderr"
bad = 1
}
exit bad
}
' "$f" || exit 1
}
cmd_wf_lint() {
wf_lint "${1:-.gitea/workflows/release.yml}"
}
cmd_refs() {
kind=$1
df=vezha/Dockerfile
pins=vezha/PINS
case $kind in
mc) last=BASE_IMAGE ;;
minio) last=MC_IMAGE ;;
*) refuse refs "unknown kind $kind" ;;
esac
if grep -nE '^ARG[[:space:]]+[A-Za-z_][A-Za-z0-9_]*=' "$df"; then
refuse refs "a Dockerfile ARG has a default (lines above)"
fi
for k in GO_BUILDER $last; do
printf '%s' "$(pin_value "$k" "$pins")" | grep -Eq '^[^@[:space:]]+@sha256:[0-9a-f]{64}$' || refuse refs "$k is not pinned by @sha256 digest"
done
if [ "$kind" = minio ]; then
printf '%s' "$(pin_value MC_IMAGE "$pins")" | grep -Eq "^$REGISTRY/$OWNER/minio-mc@sha256:[0-9a-f]{64}\$" || refuse refs "MC_IMAGE is not $REGISTRY/$OWNER/minio-mc@sha256:<digest>"
fi
stages=""
while IFS= read -r line; do
case $line in
FROM\ *) ;;
*) continue ;;
esac
# shellcheck disable=SC2086
set -- $line
shift
case $1 in --*) refuse refs "FROM option $1 is not allowed" ;; esac
img=$1
case $img in
scratch) ;;
'${GO_BUILDER}' | '${BASE_IMAGE}' | '${MC_IMAGE}')
var=${img#\$\{}
var=${var%\}}
[ "$var" = GO_BUILDER ] || [ "$var" = "$last" ] || refuse refs "FROM uses $var which is not a pinned image key of this repo"
;;
*@sha256:*)
printf '%s' "$img" | grep -Eq '@sha256:[0-9a-f]{64}$' || refuse refs "FROM $img has a malformed digest"
;;
*)
found=0
for s in $stages; do
if [ "$s" = "$img" ]; then
found=1
fi
done
[ "$found" = 1 ] || refuse refs "FROM $img is not pinned by digest"
;;
esac
if [ "${2:-}" = AS ]; then
stages="$stages $3"
fi
done < "$df"
for f in $(grep -oE '^COPY[[:space:]]+--from=[^[:space:]]+' "$df" | sed 's/.*--from=//'); do
case $f in
*'$'*) refuse refs "COPY --from=$f takes a variable" ;;
esac
found=0
for s in $stages; do
if [ "$s" = "$f" ]; then
found=1
fi
done
[ "$found" = 1 ] || refuse refs "COPY --from=$f is not a stage of this Dockerfile"
done
wf_lint .gitea/workflows/release.yml
}
cmd_provenance_remote() {
: "${GITHUB_REPOSITORY:?}" "${UPSTREAM_RELEASE:?}" "${UPSTREAM_COMMIT:?}"
body=$(curl -fsS "https://$REGISTRY/api/v1/repos/$GITHUB_REPOSITORY") || refuse provenance "anonymous GET of the repository failed"
[ "$(printf '%s' "$body" | jq -r .private)" = false ] || refuse provenance "the repository is not public"
[ "$(printf '%s' "$body" | jq -r .has_actions)" = true ] || refuse provenance "the repository has no actions"
[ "$(printf '%s' "$body" | jq -r .default_branch)" = vezha ] || refuse provenance "the default branch is not vezha"
got=$(GIT_TERMINAL_PROMPT=0 git ls-remote "https://$REGISTRY/$GITHUB_REPOSITORY" "refs/tags/${UPSTREAM_RELEASE}^{}" | cut -f1)
[ "$got" = "$UPSTREAM_COMMIT" ] || refuse provenance "tag $UPSTREAM_RELEASE resolves to '$got' but PINS says UPSTREAM_COMMIT=$UPSTREAM_COMMIT"
echo "provenance: tag $UPSTREAM_RELEASE resolves to $got"
}
cmd_provenance_src() {
: "${UPSTREAM_COMMIT:?}"
head=$(git -C src rev-parse HEAD)
[ "$head" = "$UPSTREAM_COMMIT" ] || refuse provenance "src HEAD is $head but PINS says UPSTREAM_COMMIT=$UPSTREAM_COMMIT"
echo "provenance: src HEAD is $head"
}
cmd_source_urls() {
: "${GITHUB_REPOSITORY:?}" "${UPSTREAM_RELEASE:?}"
for u in "https://$REGISTRY/$GITHUB_REPOSITORY/src/tag/$UPSTREAM_RELEASE" "https://$REGISTRY/$GITHUB_REPOSITORY/archive/$UPSTREAM_RELEASE.tar.gz"; do
fetch_ok "$u" || refuse source-urls "anonymous fetch of $u failed"
echo "source-urls: $u -> 200"
done
}
cmd_make_tag() {
: "${UPSTREAM_RELEASE:?}" "${UPSTREAM_COMMIT:?}" "${GITHUB_SHA:?}"
lower=$(printf '%s' "$UPSTREAM_RELEASE" | tr 'A-Z' 'a-z')
printf '%s-%s-r%s\n' "$lower" "$(printf '%s' "$UPSTREAM_COMMIT" | cut -c1-12)" "$(printf '%s' "$GITHUB_SHA" | cut -c1-8)"
}
cmd_tag() {
pkg=$1
tag=$2
case $tag in
"$STOPGAP_SERVER_TAG" | "$STOPGAP_MC_TAG") refuse tag "$tag is a stopgap tag and is never written" ;;
esac
printf '%s' "$tag" | grep -Eq "$TAG_RE" || refuse tag "$tag does not have the immutable tag form"
r=$(anon_head "$pkg" "$tag") || refuse tag "the anonymous registry token request failed"
case ${r%% *} in
404) echo "tag: $pkg:$tag does not exist yet" ;;
200) refuse tag "$pkg:$tag already exists, tags are write-once" ;;
*) refuse tag "the registry answered ${r%% *} for $pkg:$tag" ;;
esac
}
cmd_digest() {
r=$(anon_head "$1" "$2") || refuse digest "the anonymous registry token request failed"
[ "${r%% *}" = 200 ] || refuse digest "the registry answered ${r%% *} for $1:$2"
printf '%s\n' "${r#* }"
}
cmd_stopgap() {
s=$(anon_head minio "$STOPGAP_SERVER_TAG") || refuse stopgap "the anonymous registry token request failed"
m=$(anon_head minio-mc "$STOPGAP_MC_TAG") || refuse stopgap "the anonymous registry token request failed"
[ "${s#* }" = "$STOPGAP_SERVER_DIGEST" ] || refuse stopgap "minio:$STOPGAP_SERVER_TAG resolves to '${s#* }'"
[ "${m#* }" = "$STOPGAP_MC_DIGEST" ] || refuse stopgap "minio-mc:$STOPGAP_MC_TAG resolves to '${m#* }'"
echo "minio:$STOPGAP_SERVER_TAG ${s#* }"
echo "minio-mc:$STOPGAP_MC_TAG ${m#* }"
}
cmd_preflight() {
kind=$1
ref=$2
mc_image=${3:-}
[ "$kind" = minio ] || return 0
[ "$ref" = refs/heads/vezha ] || return 0
printf '%s' "$mc_image" | grep -Eq "^$REGISTRY/$OWNER/minio-mc@sha256:[0-9a-f]{64}\$" || refuse preflight "MC_IMAGE '$mc_image' is not $REGISTRY/$OWNER/minio-mc@sha256:<digest>"
[ "$mc_image" != "$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST" ] || refuse preflight "MC_IMAGE is the stopgap digest, which is never published from vezha"
}
cmd_login_guard() {
[ -n "${REGISTRY_TOKEN:-}" ] || {
echo "::error::REGISTRY_TOKEN secret is not set. Create a Gitea PAT with write:package scope and add it under repo Settings, Actions, Secrets."
refuse login-guard "REGISTRY_TOKEN is not set"
}
}
cmd_put_denied() {
[ "$1" -ne 0 ] || refuse put-denied "the read-only user's PUT succeeded"
grep -Eqi 'access denied|insufficient permissions' "$2" || refuse put-denied "the PUT failed but not with Access Denied"
}
cmd_prove() {
ref=$1
bin=$2
want=$3
docker pull "$ref" > /dev/null || refuse prove "pull of $ref failed"
cid=$(docker create "$ref") || refuse prove "docker create of $ref failed"
tmp=$(mktemp -d)
if ! docker cp "$cid:/usr/bin/$bin" "$tmp/$bin" > /dev/null; then
docker rm -f "$cid" > /dev/null
refuse prove "no /usr/bin/$bin in $ref"
fi
label=$(docker inspect -f '{{index .Config.Labels "io.vezha.binary.sha256"}}' "$cid")
docker rm -f "$cid" > /dev/null
got=$(sha256sum "$tmp/$bin" | cut -d' ' -f1)
rm -rf "$tmp"
[ "$got" = "$want" ] || refuse prove "/usr/bin/$bin in $ref has sha256 $got, expected $want"
[ "$label" = "$want" ] || refuse prove "label io.vezha.binary.sha256 of $ref is '$label', expected $want"
echo "prove: $ref carries /usr/bin/$bin with sha256 $got"
}
cmd_render_source() {
tmpl=$1
out=$2
sed \
-e "s|@UPSTREAM_RELEASE@|$UPSTREAM_RELEASE|g" \
-e "s|@UPSTREAM_COMMIT@|$UPSTREAM_COMMIT|g" \
-e "s|@RECIPE_COMMIT@|$GITHUB_SHA|g" \
-e "s|@GO_BUILDER@|$GO_BUILDER|g" \
-e "s|@MODULE@|$MODULE|g" \
-e "s|@MODULE_VERSION@|$MODULE_VERSION|g" \
-e "s|@MODULE_SUM@|$MODULE_SUM|g" \
-e "s|@BINARY_SHA256@|$BINARY_SHA256|g" \
-e "s|@BASE_IMAGE@|${BASE_IMAGE:-none}|g" \
-e "s|@MC_IMAGE@|${MC_IMAGE:-none}|g" \
"$tmpl" > "$out"
if grep -n '@[A-Z_0-9]*@' "$out"; then
refuse render-source "an unrendered placeholder remains (lines above)"
fi
}
cmd_source_offer() {
image=$1
repo=$2
release=$3
cid=$(docker create "$image") || refuse source-offer "docker create of $image failed"
tmp=$(mktemp -d)
if ! docker cp "$cid:/licenses" "$tmp/licenses" > /dev/null; then
docker rm -f "$cid" > /dev/null
refuse source-offer "$image has no /licenses"
fi
docker rm -f "$cid" > /dev/null
tagurl="https://$REGISTRY/$OWNER/$repo/src/tag/$release"
grep -qF "$tagurl" "$tmp/licenses/SOURCE" || refuse source-offer "/licenses/SOURCE does not name $tagurl"
urls=$(grep -rohE "https://$REGISTRY/[^[:space:])>\"']+" "$tmp/licenses" | sort -u)
[ -n "$urls" ] || refuse source-offer "no $REGISTRY URL in /licenses"
for u in $urls; do
fetch_ok "$u" || refuse source-offer "anonymous fetch of $u failed"
echo "source-offer: $u -> 200"
done
rm -rf "$tmp"
}
control() {
name=$1
gate=$2
shift 2
if out=$("$@" 2>&1); then
echo "CONTROL FAILED: $name was accepted" >&2
exit 1
fi
printf '%s' "$out" | grep -q "REFUSED $gate:" || {
echo "CONTROL FAILED: $name was refused without the REFUSED $gate message: $out" >&2
exit 1
}
echo "control ok: $name refused"
}
accept() {
name=$1
shift
"$@" > /dev/null 2>&1 || {
echo "CONTROL FAILED: $name was refused" >&2
exit 1
}
echo "control ok: $name accepted"
}
cmd_self_test() {
tmp=$(mktemp -d)
mc_stop="$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST"
control "stopgap server tag" tag sh "$SELF" tag minio "$STOPGAP_SERVER_TAG"
control "stopgap mc tag" tag sh "$SELF" tag minio-mc "$STOPGAP_MC_TAG"
control "moving tag" tag sh "$SELF" tag minio-mc latest
control "stopgap digest as MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha "$mc_stop"
control "tag form MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha "$REGISTRY/$OWNER/minio-mc:$STOPGAP_MC_TAG"
control "empty MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha ""
accept "stopgap MC_IMAGE off vezha" sh "$SELF" preflight minio refs/heads/dev/x "$mc_stop"
control "pull-by-digest of a different manifest" prove sh "$SELF" prove "$mc_stop" mc 0000000000000000000000000000000000000000000000000000000000000000
control "empty REGISTRY_TOKEN" login-guard env REGISTRY_TOKEN= sh "$SELF" login-guard
accept "set REGISTRY_TOKEN" env REGISTRY_TOKEN=present sh "$SELF" login-guard
printf 'mc: <ERROR> Unable to put. Access Denied.\n' > "$tmp/denied.txt"
printf 'done\n' > "$tmp/ok.txt"
control "allowed PUT" put-denied sh "$SELF" put-denied 0 "$tmp/ok.txt"
control "PUT that failed for another reason" put-denied sh "$SELF" put-denied 1 "$tmp/ok.txt"
accept "denied PUT" sh "$SELF" put-denied 1 "$tmp/denied.txt"
printf 'mc: <ERROR> Unable to write to one or more targets. Insufficient permissions to access this path.\n' > "$tmp/insufficient.txt"
accept "PUT refused for insufficient permissions" sh "$SELF" put-denied 1 "$tmp/insufficient.txt"
if [ -n "$(pin_value BASE_IMAGE vezha/PINS)" ]; then
kind=mc
last=BASE_IMAGE
else
kind=minio
last=MC_IMAGE
fi
for k in UPSTREAM_RELEASE UPSTREAM_COMMIT MODULE MODULE_VERSION MODULE_SUM GO_BUILDER $last; do
grep -v "^$k=" vezha/PINS > "$tmp/pins.missing" || true
sed "s|^$k=.*|$k=|" vezha/PINS > "$tmp/pins.empty"
control "PINS without $k" pins sh "$SELF" pins "$kind" "$tmp/pins.missing"
control "PINS with empty $k" pins sh "$SELF" pins "$kind" "$tmp/pins.empty"
done
printf 'on:\n pull_request:\njobs:\n' > "$tmp/wf.pr"
control "workflow with a pull_request trigger" refs sh "$SELF" wf-lint "$tmp/wf.pr"
rm -rf "$tmp"
echo "self-test: every control behaved"
}
cmd=${1:?usage: gates.sh <command> [args]}
shift
case $cmd in
pins) cmd_pins "$@" ;;
refs) cmd_refs "$@" ;;
wf-lint) cmd_wf_lint "$@" ;;
provenance-remote) cmd_provenance_remote ;;
provenance-src) cmd_provenance_src ;;
source-urls) cmd_source_urls ;;
make-tag) cmd_make_tag ;;
tag) cmd_tag "$@" ;;
digest) cmd_digest "$@" ;;
stopgap) cmd_stopgap ;;
preflight) cmd_preflight "$@" ;;
login-guard) cmd_login_guard ;;
put-denied) cmd_put_denied "$@" ;;
prove) cmd_prove "$@" ;;
render-source) cmd_render_source "$@" ;;
source-offer) cmd_source_offer "$@" ;;
self-test) cmd_self_test ;;
*) refuse usage "unknown command $cmd" ;;
esac