|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2019-2026 Red Hat, Inc. |
| 4 | +# This program and the accompanying materials are made |
| 5 | +# available under the terms of the Eclipse Public License 2.0 |
| 6 | +# which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | +# |
| 8 | +# SPDX-License-Identifier: EPL-2.0 |
| 9 | +# |
| 10 | +# Contributors: |
| 11 | +# Red Hat, Inc. - initial API and implementation |
| 12 | +# |
| 13 | +# Checks that every image referenced by the CSV's RELATED_IMAGE_* variables can be pinned with |
| 14 | +# an OpenShift PinnedImageSet (machineconfiguration.openshift.io/v1). Each value must be a |
| 15 | +# digest reference accepted by the API, and must pass `podman manifest inspect` - the command |
| 16 | +# the machine config daemon runs to prefetch the set. |
| 17 | +# |
| 18 | +# The case this check exists for is a single OCI image manifest. It is well formed and |
| 19 | +# pullable, but `podman manifest inspect` fails on it with rc=125 ("Treating single images as |
| 20 | +# manifest lists is not implemented"). Because the prefetch is all-or-nothing per node, one |
| 21 | +# such image silently disables the whole set. |
| 22 | +# |
| 23 | +# make-release.sh calls this after addDigests.sh, on the digest-pinned stable CSV, with |
| 24 | +# --forbid-tags next: addDigests.sh records the tag each digest was pinned from as a commented |
| 25 | +# `# tag:` line, and an image that came from a moving tag is rebuilt and removed behind the |
| 26 | +# pinned digest, which breaks workspaces. The argument is a comma-separated list of tag names or |
| 27 | +# glob patterns; stable releases pass `next` today and can add `latest` once those references |
| 28 | +# are version-tagged. The next catalog keeps moving tags by design and does not pass the flag. |
| 29 | +# |
| 30 | +# Usage: checkPinnedImages.sh --csv <CSV> [--authfile <file>] [--forbid-tags <tags>] |
| 31 | +# |
| 32 | +# Each inspection is retried up to three times, five seconds apart, because a registry can fail |
| 33 | +# transiently; the inspection is read-only, so retrying it is safe. |
| 34 | +# |
| 35 | +# Requires podman and the python-yq (kislyuk) implementation. Set CONTAINERS_REGISTRIES_CONF to |
| 36 | +# check pre-release images through a mirror. |
| 37 | + |
| 38 | +set -Eeuo pipefail |
| 39 | + |
| 40 | +# IMAGE_RE is the CEL rule on spec.pinnedImages[*].name (openshift/api, ImageDigestFormat), |
| 41 | +# with the digest expression inlined. In words, a pinnable reference looks like: |
| 42 | +# |
| 43 | +# host[:port]/[namespace/]name@sha256:<64 lowercase hex characters> |
| 44 | +# |
| 45 | +# - host: dot-separated labels, so at least one dot. A dotless host, such as localhost:5000, |
| 46 | +# is not accepted here nor by the API. |
| 47 | +# - port: optional, 2 to 5 digits. |
| 48 | +# - namespace: optional, at most one segment of up to 61 characters. |
| 49 | +# - name: letters, digits, '_', '.' and '-'; it may be empty. |
| 50 | +# - digest: no tag before the '@', and exactly 64 lowercase hex characters after 'sha256:'. |
| 51 | +# |
| 52 | +# Do not tighten any part of it: the API accepts an empty namespace segment and an empty name, |
| 53 | +# so rejecting those here would reject references the cluster accepts. |
| 54 | +IMAGE_RE='^([a-zA-Z0-9-]+\.)+[a-zA-Z0-9-]+(:[0-9]{2,5})?/([a-zA-Z0-9_-]{0,61}/)?[a-zA-Z0-9_.-]*@sha256:[a-f0-9]{64}$' |
| 55 | +MAX_REFERENCE_LENGTH=447 |
| 56 | + |
| 57 | +die() { |
| 58 | + echo "[ERROR] $*" >&2 |
| 59 | + exit 1 |
| 60 | +} |
| 61 | + |
| 62 | +CSV="" |
| 63 | +AUTHFILE="" |
| 64 | +FORBID_TAGS="" |
| 65 | +FORBID_TAGS_GIVEN=false |
| 66 | + |
| 67 | +while [[ $# -gt 0 ]]; do |
| 68 | + case "$1" in |
| 69 | + --csv) |
| 70 | + [[ $# -ge 2 ]] || die "--csv requires a value" |
| 71 | + CSV="$2" |
| 72 | + shift 2 |
| 73 | + ;; |
| 74 | + --authfile) |
| 75 | + [[ $# -ge 2 ]] || die "--authfile requires a value" |
| 76 | + AUTHFILE="$2" |
| 77 | + shift 2 |
| 78 | + ;; |
| 79 | + --forbid-tags) |
| 80 | + [[ $# -ge 2 ]] || die "--forbid-tags requires a value" |
| 81 | + FORBID_TAGS="$2" |
| 82 | + FORBID_TAGS_GIVEN=true |
| 83 | + shift 2 |
| 84 | + ;; |
| 85 | + -h | --help) |
| 86 | + sed -n '/^# Usage:/,/^$/s/^# \{0,1\}//p' "$0" |
| 87 | + exit 0 |
| 88 | + ;; |
| 89 | + *) die "unknown argument: $1 (see --help)" ;; |
| 90 | + esac |
| 91 | +done |
| 92 | + |
| 93 | +[[ -n "${CSV}" ]] || die "usage: $(basename "$0") --csv <CSV> [--authfile <file>] [--forbid-tags <tags>]" |
| 94 | +[[ -r "${CSV}" ]] || die "${CSV} cannot be read" |
| 95 | +[[ -z "${AUTHFILE}" || -r "${AUTHFILE}" ]] || die "${AUTHFILE} cannot be read" |
| 96 | +command -v podman >/dev/null 2>&1 || die "podman is not installed" |
| 97 | +command -v yq >/dev/null 2>&1 || die "yq is not installed; the python-yq (kislyuk) implementation is required" |
| 98 | +yq --version 2>/dev/null | grep -q mikefarah && die "yq is the mikefarah implementation; the python-yq (kislyuk) implementation is required" |
| 99 | + |
| 100 | +# A throwaway store, because an image already present locally makes `podman manifest inspect` |
| 101 | +# resolve offline and hide a bad reference. |
| 102 | +STORE=$(mktemp -d "${TMPDIR:-/tmp}/checkPinnedImages.XXXXXX") |
| 103 | +trap 'rm -rf "${STORE}"' EXIT |
| 104 | +mkdir -p "${STORE}/root" "${STORE}/run" |
| 105 | + |
| 106 | +# The env list is read with python-yq, so quoting, comments, folded scalars and key order are the |
| 107 | +# YAML parser's problem rather than this script's. Fail on an entry that cannot be pinned by value: |
| 108 | +# valueFrom, a missing, empty or multi-line value. |
| 109 | +RELATED_ENVS='.spec.install.spec.deployments[]? |
| 110 | + | .spec.template.spec |
| 111 | + | (.containers[]?, .initContainers[]?) |
| 112 | + | .env[]? |
| 113 | + | select((.name // "") | startswith("RELATED_IMAGE_"))' |
| 114 | + |
| 115 | +related_count=$(yq -r "[${RELATED_ENVS}] | length" "${CSV}") || |
| 116 | + die "could not parse the RELATED_IMAGE_* variables of ${CSV}" |
| 117 | + |
| 118 | +if [[ "${related_count}" == "0" ]]; then |
| 119 | + die "no RELATED_IMAGE_* variables found in ${CSV}" |
| 120 | +fi |
| 121 | + |
| 122 | +value_from=$(yq -r "[${RELATED_ENVS} | select(.valueFrom != null)] | length" "${CSV}") || |
| 123 | + die "could not parse the RELATED_IMAGE_* variables of ${CSV}" |
| 124 | +if [[ "${value_from}" != "0" ]]; then |
| 125 | + die "a RELATED_IMAGE_* value uses valueFrom in ${CSV}" |
| 126 | +fi |
| 127 | + |
| 128 | +invalid=$(yq -r "[${RELATED_ENVS} | select((.value | type) != \"string\" or .value == \"\" or (.value | test(\"\\n\")))] | length" "${CSV}") || |
| 129 | + die "could not parse the RELATED_IMAGE_* variables of ${CSV}" |
| 130 | +if [[ "${invalid}" != "0" ]]; then |
| 131 | + die "a RELATED_IMAGE_* value is empty or multi-line in ${CSV}" |
| 132 | +fi |
| 133 | + |
| 134 | +refs=$(yq -r "${RELATED_ENVS} | .value" "${CSV}") || |
| 135 | + die "could not read the RELATED_IMAGE_* values of ${CSV}" |
| 136 | + |
| 137 | +forbidden_origins="" |
| 138 | +if [[ "${FORBID_TAGS_GIVEN}" == true ]]; then |
| 139 | + # addDigests.sh keeps the source tag as a commented line, so a digest alone does not say |
| 140 | + # whether the image is rebuilt behind it. Without those lines the rule cannot work, so fail |
| 141 | + # instead of passing every image. |
| 142 | + grep -qE '^[[:space:]]*#[[:space:]]*tag:' "${CSV}" || |
| 143 | + die "--forbid-tags needs the '# tag:' origin lines that addDigests.sh writes; none found in ${CSV}" |
| 144 | + |
| 145 | + IFS=',' read -r -a forbidden_tags <<< "${FORBID_TAGS}" |
| 146 | + valid_patterns=() |
| 147 | + for pattern in "${forbidden_tags[@]}"; do |
| 148 | + pattern="${pattern#"${pattern%%[![:space:]]*}"}" |
| 149 | + pattern="${pattern%"${pattern##*[![:space:]]}"}" |
| 150 | + if [[ -n "${pattern}" ]]; then |
| 151 | + valid_patterns+=("${pattern}") |
| 152 | + fi |
| 153 | + done |
| 154 | + if (( ${#valid_patterns[@]} == 0 )); then |
| 155 | + die "--forbid-tags needs at least one tag name or pattern" |
| 156 | + fi |
| 157 | + |
| 158 | + origins=$(grep -E '^[[:space:]]*#[[:space:]]*tag:' "${CSV}" | |
| 159 | + sed -e 's|^[[:space:]]*#[[:space:]]*tag:[[:space:]]*||' | sort -u || true) |
| 160 | + |
| 161 | + while IFS= read -r origin; do |
| 162 | + origin="${origin#"${origin%%[![:space:]]*}"}" |
| 163 | + origin="${origin%"${origin##*[![:space:]]}"}" |
| 164 | + # Remove a matching pair of surrounding quotes, single or double. |
| 165 | + origin="${origin#[\"\']}" |
| 166 | + origin="${origin%[\"\']}" |
| 167 | + if [[ -z "${origin}" ]]; then |
| 168 | + continue |
| 169 | + fi |
| 170 | + # An origin that is already a digest reference has no tag to forbid. |
| 171 | + if [[ "${origin}" == *"@sha256:"* ]]; then |
| 172 | + continue |
| 173 | + fi |
| 174 | + # An untagged reference resolves to the implicit latest tag. |
| 175 | + last_segment="${origin##*/}" |
| 176 | + if [[ "${last_segment}" == *:* ]]; then |
| 177 | + tag="${last_segment##*:}" |
| 178 | + else |
| 179 | + tag="latest" |
| 180 | + fi |
| 181 | + for pattern in "${valid_patterns[@]}"; do |
| 182 | + # shellcheck disable=SC2053 # the pattern intentionally supports globs |
| 183 | + if [[ "${tag}" == ${pattern} ]]; then |
| 184 | + forbidden_origins+="${origin}"$'\n' |
| 185 | + break |
| 186 | + fi |
| 187 | + done |
| 188 | + done <<< "${origins}" |
| 189 | +fi |
| 190 | + |
| 191 | +AUTHARGS=() |
| 192 | +[[ -n "${AUTHFILE}" ]] && AUTHARGS=(--authfile "${AUTHFILE}") |
| 193 | + |
| 194 | +failures=0 |
| 195 | +forbidden=0 |
| 196 | +while IFS= read -r ref; do |
| 197 | + [[ -n "${ref}" ]] || continue |
| 198 | + |
| 199 | + if (( ${#ref} > MAX_REFERENCE_LENGTH )) || [[ ! "${ref}" =~ ${IMAGE_RE} ]]; then |
| 200 | + echo "[FAIL] ${ref}" |
| 201 | + echo " not a digest reference accepted by the PinnedImageSet API (expected host/path@sha256:<64 hex>)" |
| 202 | + failures=$((failures + 1)) |
| 203 | + continue |
| 204 | + fi |
| 205 | + |
| 206 | + inspected=false |
| 207 | + for attempt in 1 2 3; do |
| 208 | + if output=$(podman --log-level error --root "${STORE}/root" --runroot "${STORE}/run" \ |
| 209 | + manifest inspect "${AUTHARGS[@]}" "${ref}" 2>&1); then |
| 210 | + inspected=true |
| 211 | + break |
| 212 | + fi |
| 213 | + # A registry can fail transiently, for example a blip while the release pushes the images. |
| 214 | + # The inspection is read-only, so retrying it is safe. |
| 215 | + (( attempt < 3 )) && sleep 5 |
| 216 | + done |
| 217 | + if [[ "${inspected}" != true ]]; then |
| 218 | + # The single-manifest error embeds the whole manifest JSON; keep the head and the tail, |
| 219 | + # where the cause is. |
| 220 | + output="${output//$'\n'/ }" |
| 221 | + (( ${#output} > 300 )) && output="${output:0:90} ... ${output: -180}" |
| 222 | + echo "[FAIL] ${ref}" |
| 223 | + echo " podman manifest inspect failed after 3 attempts: ${output}" |
| 224 | + failures=$((failures + 1)) |
| 225 | + fi |
| 226 | +done < <(sort -u <<<"${refs}") |
| 227 | + |
| 228 | +if [[ -n "${forbidden_origins}" ]]; then |
| 229 | + while IFS= read -r origin; do |
| 230 | + [[ -n "${origin}" ]] || continue |
| 231 | + echo "[FAIL] ${origin}" |
| 232 | + echo " pinned from a tag listed in --forbid-tags, which can move and leave the digest behind" |
| 233 | + forbidden=$((forbidden + 1)) |
| 234 | + done <<< "${forbidden_origins}" |
| 235 | +fi |
| 236 | + |
| 237 | +if (( failures > 0 || forbidden > 0 )); then |
| 238 | + summary="" |
| 239 | + (( failures > 0 )) && summary="${failures} image(s) failed the pinned-image check" |
| 240 | + (( forbidden > 0 )) && summary="${summary:+${summary}; }${forbidden} image(s) were pinned from a forbidden tag" |
| 241 | + die "${summary}" |
| 242 | +fi |
| 243 | + |
| 244 | +echo "[INFO] every image can be pinned with a PinnedImageSet" |
0 commit comments