#!/usr/bin/env bash # # check-deps.sh fails when a module pinned in go.mod is older than the newest # release we can authoritatively observe. # # Two sources are accepted, in order: # # 1. The module proxy. When GOPROXY is not "off", `go list -m -u` is the # authority: it asks the proxy for the newest release of every pinned # module. This is what CI uses. # # 2. The dependency facts prepared by the execution profile and named by # MAINTAINER_DEPENDENCY_FACTS. The file records, for every module, the # newest version that profile observed on proxy.golang.org and when. It is # evidence rather than a live query, so the script prints its source and # observation time and refuses to use reports it cannot validate as complete. # # The local module cache is never consulted: it only contains versions that # happened to be downloaded, so it cannot establish what the newest release is. # When neither source is available the script exits with status 2 instead of # reporting success. # # Exit status: 0 = every pin is current, 1 = a pin is stale (or unverifiable # for an individual module), 2 = freshness could not be established at all. set -euo pipefail root="${CHECK_DEPS_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" cd "$root" facts_file="${MAINTAINER_DEPENDENCY_FACTS:-}" # requirements prints " " for every module required by go.mod. requirements() { go mod edit -json | awk -F'"' ' /^\t"Require": \[/ { in_require = 1 } in_require && /^\t\],?$/ { in_require = 0 } in_require && /"Path":/ { path = $4 } in_require && /"Version":/ { print path, $4 } ' } # behind reports whether version $1 is older than version $2. behind() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" = "$2" ] } pins="$(requirements)" if [ -z "$pins" ]; then echo 'go.mod requires no modules' >&2 exit 1 fi count="$(printf '%s\n' "$pins" | wc -l | tr -d ' ')" if [ "$(go env GOPROXY)" != "off" ]; then mods=() while read -r path version; do mods+=("$path@$version") done <<<"$pins" if ! listing="$(go list -m -u -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' "${mods[@]}")"; then echo 'could not query the module proxy for newer versions' >&2 exit 2 fi updates="$(printf '%s\n' "$listing" | awk 'NF == 3')" if [ -n "$updates" ]; then echo 'These modules have newer versions available:' >&2 printf '%s\n' "$updates" >&2 exit 1 fi echo "ok: $count go.mod requirements are up to date (module proxy)" exit 0 fi # Offline: fall back to the dependency facts prepared by the execution profile. # The facts file is refreshed by the execution profile, so a reader can race # with an atomic rewrite; give it a moment before declaring the facts missing. if [ -n "$facts_file" ]; then for _ in 1 2 3 4 5 6 7 8 9 10; do [ -r "$facts_file" ] && break sleep 0.2 done fi if [ -z "$facts_file" ] || [ ! -r "$facts_file" ]; then echo 'cannot verify dependency freshness: the module proxy is disabled (GOPROXY=off)' \ 'and MAINTAINER_DEPENDENCY_FACTS does not name a readable dependency-facts file' >&2 exit 2 fi field() { grep -o "\"$1\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" "$facts_file" | head -n 1 | sed 's/.*:[[:space:]]*"\(.*\)"/\1/' } schema="$(grep -o '"schema_version"[[:space:]]*:[[:space:]]*[0-9]*' "$facts_file" | head -n 1 | grep -o '[0-9]*$' || true)" source="$(field source)" freshness="$(field freshness_status)" scope="$(field query_scope)" observed_at="$(field observed_at)" digest="$(field manifest_digest)" if [ "$schema" != 1 ]; then echo "cannot verify dependency freshness: unsupported dependency-facts schema (${schema:-missing})" >&2 exit 2 fi if [ "$source" != 'https://proxy.golang.org' ]; then echo "cannot verify dependency freshness: unexpected dependency-facts source '${source}'" >&2 exit 2 fi if [ "$freshness" != 'available' ]; then echo "cannot verify dependency freshness: dependency facts are not marked available ('${freshness}')" >&2 exit 2 fi if [ "$scope" != 'module_path_latest' ]; then echo "cannot verify dependency freshness: dependency facts use query scope '${scope}', want module_path_latest" >&2 exit 2 fi if [ -z "$observed_at" ]; then echo 'cannot verify dependency freshness: dependency facts have no observation time' >&2 exit 2 fi # One record per observed module: name, latest version and item status. records="$(grep -o '{[^{}]*"latest"[^{}]*' "$facts_file" || true)" if [ -z "$records" ]; then echo 'cannot verify dependency freshness: dependency facts contain no module observations' >&2 exit 2 fi latest_for() { local want="$1" record name latest status while IFS= read -r record; do name="$(printf '%s\n' "$record" | sed -n 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" [ "$name" = "$want" ] || continue latest="$(printf '%s\n' "$record" | sed -n 's/.*"latest"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" status="$(printf '%s\n' "$record" | sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')" [ "$status" = 'observed' ] || return 1 printf '%s\n' "$latest" return 0 done <<<"$records" return 1 } stale='' unverified='' while read -r path version; do if ! latest="$(latest_for "$path")"; then unverified+=" $path $version (not covered by the dependency facts)"$'\n' continue fi if behind "$version" "$latest"; then stale+=" $path $version (latest observed: $latest)"$'\n' elif [ "$version" != "$latest" ]; then unverified+=" $path $version (pin is newer than the latest observation $latest)"$'\n' fi done <<<"$pins" if [ -n "$stale" ]; then echo 'These go.mod pins are behind the latest observed releases:' >&2 printf '%s' "$stale" >&2 exit 1 fi if [ -n "$unverified" ]; then echo 'These go.mod pins could not be checked against the dependency facts:' >&2 printf '%s' "$unverified" >&2 exit 1 fi echo "ok: $count go.mod requirements match the latest releases observed by the execution profile" echo "source: $source, observed at $observed_at (module_path_latest)" if [ -n "$digest" ]; then echo "note: dependency-facts manifest digest: $digest" fi