Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(nginx): verify digests under their full name, with an optional mirror
Alfredo Lopezdde9bd027943
diff --git a/scripts/tests/test-verify-nginx-image-digests.sh b/scripts/tests/test-verify-nginx-image-digests.shnew file mode 100755index 000000000..99ae6416a--- /dev/null+++ b/scripts/tests/test-verify-nginx-image-digests.sh@@ -0,0 +1,147 @@+#!/usr/bin/env bash+# Contract for scripts/verify-nginx-image-digests.sh, offline: a fake docker+# first on PATH records every call and answers with an inline image config.+# Each case checks the exit code, the message and the recorded docker calls,+# so a rejection without a registry call is told apart from one that never+# ran (the error message is the witness for an empty call log).+set -euo pipefail++REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"+VERIFIER="$REPO_ROOT/scripts/verify-nginx-image-digests.sh"+WORK="$(mktemp -d)"+trap 'rm -rf "$WORK"' EXIT+OUT="$WORK/out"+export FAKE_DOCKER_LOG="$WORK/docker.log"+failures=0+cases=0++DIGEST="sha256:$(printf 'a%.0s' {1..64})"+REF="nginx:1.30.6-alpine@$DIGEST"++mkdir -p "$WORK/bin"+cat >"$WORK/bin/docker" <<'EOF'+#!/usr/bin/env bash+printf '%s\n' "$*" >>"$FAKE_DOCKER_LOG"+if [ -n "${FAKE_DOCKER_FAIL:-}" ]; then+ echo "fake docker: inspect failed" >&2+ exit 1+fi+printf '%s\n' "$FAKE_IMAGE_JSON"+EOF+chmod +x "$WORK/bin/docker"++fail() {+ echo "FAIL: $*" >&2+ failures=$((failures + 1))+}++# image_index AMD64_VERSION ARM64_VERSION: imagetools JSON for a two-platform index.+image_index() {+ printf '{"linux/amd64":{"config":{"Env":["PATH=/usr/bin","NGINX_VERSION=%s"]}},"linux/arm64":{"config":{"Env":["PATH=/usr/bin","NGINX_VERSION=%s"]}}}' "$1" "$2"+}++# run_case NAME WANT_EXIT WANT_CALLS IMAGE_JSON ARGS...: runs the verifier with+# the fake docker and checks the exit code and the number of docker calls.+run_case() {+ local name="$1" want_exit="$2" want_calls="$3" status=0 calls+ export FAKE_IMAGE_JSON="$4"+ shift 4+ cases=$((cases + 1))+ : >"$FAKE_DOCKER_LOG"+ PATH="$WORK/bin:$PATH" bash "$VERIFIER" "$@" >"$OUT" 2>&1 || status=$?+ calls="$(grep -c . "$FAKE_DOCKER_LOG" || true)"+ if [ "$status" -ne "$want_exit" ]; then+ fail "$name: exit $status, expected $want_exit: $(cat "$OUT")"+ fi+ if [ "$calls" -ne "$want_calls" ]; then+ fail "$name: $calls docker calls, expected $want_calls: $(cat "$FAKE_DOCKER_LOG")"+ fi+}++expect_out() {+ if ! grep -qF -- "$2" "$OUT"; then+ fail "$1 should print '$2': $(cat "$OUT")"+ fi+}++expect_call() {+ if ! grep -qxF -- "$2" "$FAKE_DOCKER_LOG"; then+ fail "$1 should call docker with '$2': $(cat "$FAKE_DOCKER_LOG")"+ fi+}++run_case "matching index" 0 1 "$(image_index 1.30.6 1.30.6)" "$REF"+expect_out "matching index" "ok: nginx@$DIGEST linux/amd64 NGINX_VERSION=1.30.6"+expect_out "matching index" "ok: nginx@$DIGEST linux/arm64 NGINX_VERSION=1.30.6"+expect_call "matching index" "buildx imagetools inspect nginx@$DIGEST --format {{json .Image}}"+ok_lines="$(grep -c '^ok: ' "$OUT" || true)"+if [ "$ok_lines" -ne 2 ]; then+ fail "matching index printed $ok_lines ok: lines, expected 2: $(cat "$OUT")"+fi++run_case "mismatch on both platforms" 1 1 "$(image_index 1.30.1 1.30.1)" "$REF"+expect_out "mismatch on both platforms" "linux/amd64 declares NGINX_VERSION=1.30.1, tag says 1.30.6"+expect_out "mismatch on both platforms" "linux/arm64 declares NGINX_VERSION=1.30.1, tag says 1.30.6"++run_case "arm64 mismatch" 1 1 "$(image_index 1.30.6 1.30.1)" "$REF"+expect_out "arm64 mismatch" "linux/arm64 declares NGINX_VERSION=1.30.1, tag says 1.30.6"++run_case "amd64 mismatch" 1 1 "$(image_index 1.30.1 1.30.6)" "$REF"+expect_out "amd64 mismatch" "linux/amd64 declares NGINX_VERSION=1.30.1, tag says 1.30.6"++run_case "index without arm64" 1 1 '{"linux/amd64":{"config":{"Env":["NGINX_VERSION=1.30.6"]}}}' "$REF"+expect_out "index without arm64" "has no linux/arm64 manifest"++run_case "single-platform manifest" 1 1 '{"config":{"Env":["NGINX_VERSION=1.30.6"]}}' "$REF"+expect_out "single-platform manifest" "has no linux/amd64 manifest"++FAKE_DOCKER_FAIL=1 run_case "inspect failure" 1 1 "" "$REF"+expect_out "inspect failure" "could not inspect nginx@$DIGEST"++run_case "malformed reference" 1 0 "$(image_index 1.30.6 1.30.6)" "nginx@$DIGEST:1.30.6"+expect_out "malformed reference" "ERROR: nginx@$DIGEST:1.30.6: "++run_case "tag below the floor" 1 0 "$(image_index 1.30.1 1.30.1)" "nginx:1.30.1-alpine@$DIGEST"+expect_out "tag below the floor" "release 1.30.1 is below"++run_case "reference without a digest" 1 0 "$(image_index 1.30.6 1.30.6)" "nginx:1.30.6-alpine"+expect_out "reference without a digest" "nginx:1.30.6-alpine has no digest to verify"++# No arguments: the references come from the validator. A copy of the two+# scripts and the policy, with a validator that lists nothing, must fail.+mkdir -p "$WORK/empty/scripts"+cp "$VERIFIER" "$REPO_ROOT/scripts/nginx-image-policy.sh" "$WORK/empty/scripts/"+printf '#!/usr/bin/env bash\nexit 0\n' >"$WORK/empty/scripts/validate-nginx-images.sh"+cases=$((cases + 1))+: >"$FAKE_DOCKER_LOG"+status=0+PATH="$WORK/bin:$PATH" bash "$WORK/empty/scripts/verify-nginx-image-digests.sh" >"$OUT" 2>&1 || status=$?+if [ "$status" -ne 1 ] || ! grep -qF "no digest-pinned NGINX references to verify" "$OUT"; then+ fail "no references: exit $status, expected 1 and the no-references error: $(cat "$OUT")"+fi++NGINX_REGISTRY_MIRROR=mirror.gcr.io run_case "mirror" 0 1 "$(image_index 1.30.6 1.30.6)" "$REF"+expect_call "mirror" "buildx imagetools inspect mirror.gcr.io/library/nginx@$DIGEST --format {{json .Image}}"+expect_out "mirror" "ok: mirror.gcr.io/library/nginx@$DIGEST linux/amd64 NGINX_VERSION=1.30.6 (for $REF)"++NGINX_REGISTRY_MIRROR=mirror.gcr.io run_case "mirror, unprivileged image" 0 1 "$(image_index 1.30.6 1.30.6)" \+ "docker.io/nginxinc/nginx-unprivileged:1.30.6-alpine@$DIGEST"+expect_call "mirror, unprivileged image" "buildx imagetools inspect mirror.gcr.io/nginxinc/nginx-unprivileged@$DIGEST --format {{json .Image}}"++NGINX_REGISTRY_MIRROR=mirror.gcr.io FAKE_DOCKER_FAIL=1 run_case "mirror inspect failure" 1 1 "" "$REF"+expect_call "mirror inspect failure" "buildx imagetools inspect mirror.gcr.io/library/nginx@$DIGEST --format {{json .Image}}"+expect_out "mirror inspect failure" "could not inspect mirror.gcr.io/library/nginx@$DIGEST"++NGINX_REGISTRY_MIRROR=mirror.gcr.io run_case "registry-qualified reference" 0 1 "$(image_index 1.30.6 1.30.6)" \+ "registry.internal:5000/mirror/nginx:1.30.6-alpine@$DIGEST"+expect_call "registry-qualified reference" "buildx imagetools inspect registry.internal:5000/mirror/nginx@$DIGEST --format {{json .Image}}"++echo "cases=$cases failures=$failures"+if [ "$cases" -eq 0 ]; then+ echo "FAIL: no cases ran" >&2+ exit 1+fi+if [ "$failures" -ne 0 ]; then+ exit 1+fi+echo "OK -- verify-nginx-image-digests contract ($cases cases)"diff --git a/scripts/verify-nginx-image-digests.sh b/scripts/verify-nginx-image-digests.shindex 48e084ef3..d15c1d6bc 100755--- a/scripts/verify-nginx-image-digests.sh+++ b/scripts/verify-nginx-image-digests.sh@@ -9,10 +9,20 @@# Usage: verify-nginx-image-digests.sh [name:tag@sha256:digest ...]# With no arguments, the references come from# validate-nginx-images.sh --list-digest-pins. Needs docker buildx and jq.+#+# Each reference is inspected once, under the name it is written with. When+# NGINX_REGISTRY_MIRROR is set (CI uses mirror.gcr.io), Docker Hub names+# (nginx, docker.io/library/nginx, nginxinc/nginx-unprivileged,+# docker.io/nginxinc/nginx-unprivileged) are inspected on that mirror instead:+# same digest, same bytes. A failed inspect is an error; there is no retry and+# no second registry. A variable registry prefix is not resolved.set -euo pipefailSCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"+# shellcheck source=scripts/nginx-image-policy.sh+source "$SCRIPT_DIR/nginx-image-policy.sh"REQUIRED_PLATFORMS="linux/amd64 linux/arm64"+MIRROR="${NGINX_REGISTRY_MIRROR:-}"if [ "$#" -gt 0 ]; thenrefs="$(printf '%s\n' "$@")"@@ -27,17 +37,32 @@ fifailed=0checked=0while IFS= read -r ref; do- name_tag="${ref%@*}"- digest="${ref#*@}"- tag="${name_tag##*:}"- if [ "$name_tag" = "$ref" ] || ! [[ "$tag" =~ ^([0-9]+\.[0-9]+\.[0-9]+)(-[[:alnum:]_.-]+)?$ ]]; then- echo "ERROR: $ref is not name:<x.y.z>[-suffix]@sha256:<digest>" >&2+ problem="$(nginx_ref_problem "$ref")"+ split_ref "$ref"+ if [ -n "$problem" ]; then+ echo "ERROR: $ref: $problem" >&2failed=1continuefi- want="${BASH_REMATCH[1]}"- if ! image_json="$(docker buildx imagetools inspect "${name_tag%:*}@$digest" --format '{{json .Image}}')"; then- echo "ERROR: could not inspect $ref" >&2+ if [ -z "$ref_digest" ]; then+ echo "ERROR: $ref has no digest to verify" >&2+ failed=1+ continue+ fi+ want="${ref_tag%%-*}"+ name="${ref%@*}"+ name="${name%:*}"+ case "$name" in+ nginx | docker.io/library/nginx) hub_repo="library/nginx" ;;+ nginxinc/nginx-unprivileged | docker.io/nginxinc/nginx-unprivileged) hub_repo="nginxinc/nginx-unprivileged" ;;+ *) hub_repo="" ;;+ esac+ target="$name@$ref_digest"+ if [ -n "$MIRROR" ] && [ -n "$hub_repo" ]; then+ target="$MIRROR/$hub_repo@$ref_digest"+ fi+ if ! image_json="$(docker buildx imagetools inspect "$target" --format '{{json .Image}}')"; then+ echo "ERROR: could not inspect $target (for $ref)" >&2failed=1continuefi@@ -51,13 +76,13 @@ while IFS= read -r ref; dofor platform in $REQUIRED_PLATFORMS; dogot="$(awk -v p="$platform" '$1 == p { print $2 }' <<<"$versions")"if [ -z "$got" ]; then- echo "ERROR: $ref has no $platform manifest" >&2+ echo "ERROR: $target has no $platform manifest (for $ref)" >&2failed=1elif [ "$got" != "$want" ]; then- echo "ERROR: $ref $platform declares NGINX_VERSION=$got, tag says $want" >&2+ echo "ERROR: $target $platform declares NGINX_VERSION=$got, tag says $want (for $ref)" >&2failed=1else- echo "ok: $ref $platform NGINX_VERSION=$got"+ echo "ok: $target $platform NGINX_VERSION=$got (for $ref)"fidonechecked=$((checked + 1))