diff --git a/build/scripts/build_digests_bundle.sh b/build/scripts/build_digests_bundle.sh index e99d02892..fddc096db 100755 --- a/build/scripts/build_digests_bundle.sh +++ b/build/scripts/build_digests_bundle.sh @@ -46,6 +46,70 @@ debug() { fi } +# strip_tag REF prints REF without its tag, leaving the registry port and any digest alone. +strip_tag() { + local ref="${1%@*}" + local last="${ref##*/}" + if [[ "$last" == *:* ]]; then + echo "${ref%:*}" + else + echo "$ref" + fi +} + +# retry_inspect CMD... runs CMD, retrying a transient registry failure up to three times, five +# seconds apart. On success INSPECT_OUT holds stdout. On failure INSPECT_ERR_MSG holds a bounded +# excerpt of the last stderr, and the function returns 1. +retry_inspect() { + local attempt err + local errfile="${TMPDIR}/inspect.err" + + for attempt in 1 2 3; do + if INSPECT_OUT=$("$@" 2>"${errfile}"); then + return 0 + fi + if (( attempt < 3 )); then + sleep 5 + fi + done + + err=$(cat "${errfile}") + err="${err//$'\n'/ }" + (( ${#err} > 300 )) && err="${err:0:90} ... ${err: -180}" + INSPECT_ERR_MSG="${err:-no output}" + INSPECT_ATTEMPTS="${attempt}" + return 1 +} + +# check_pinnable REF fails when REF cannot be pinned with an OpenShift PinnedImageSet: the +# reference must be a digest accepted by the API, and the machine config daemon runs +# `podman manifest inspect` on it, which rejects a single OCI image manifest with rc=125. +# +# The NAME_RE and the length limit mirror the CEL rules on spec.pinnedImages[*].name. They are +# deliberately duplicated from IMAGE_RE in +# che-operator/build/scripts/release/checkPinnedImages.sh: this release must not fetch the +# checker, so keep the two in sync by hand. +check_pinnable() { + local ref="$1" + local name_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}$' + local max_reference_length=447 + + if (( ${#ref} > max_reference_length )); then + error "$ref is longer than the ${max_reference_length} characters the PinnedImageSet API accepts" + fi + + if [[ ! "$ref" =~ $name_re ]]; then + error "$ref is not a digest reference accepted by the PinnedImageSet API" + fi + + # An empty store, because an image already present locally would be inspected offline. Errors + # go to stderr, which retry_inspect keeps: a dead digest, missing credentials, a TLS problem + # and a single OCI manifest all fail here, and they call for different actions. + retry_inspect podman --log-level error --root "$PINNED_IMAGE_STORE/root" --runroot "$PINNED_IMAGE_STORE/run" \ + manifest inspect "$ref" || + error "$ref cannot be pre-pulled: podman manifest inspect failed after ${INSPECT_ATTEMPTS} attempts: ${INSPECT_ERR_MSG}" +} + parse_args() { if [ "$#" -eq 0 ]; then usage @@ -83,6 +147,8 @@ preflight() { if [ ! -f "${SCRIPT_DIR}/../bundle.Dockerfile" ]; then error "Could not find bundle.Dockerfile from repository" fi + command -v podman >/dev/null 2>&1 || + error "podman is required for the pinned image check" } parse_args "$@" @@ -103,7 +169,13 @@ fi # Work in a temporary directory TMPDIR="$(mktemp -d)" +trap 'rm -rf "$TMPDIR"' EXIT info "Working in $TMPDIR" + +# An empty container store for the pinnability check, because an image already present locally +# would be inspected offline and hide a bad reference. +PINNED_IMAGE_STORE="$TMPDIR/pinned-image-store" +mkdir -p "$PINNED_IMAGE_STORE/root" "$PINNED_IMAGE_STORE/run" BUNDLE_DIR="${TMPDIR}/bundle" mkdir -p "$BUNDLE_DIR" PROCESSED_DIR="${TMPDIR}/bundle-processed" @@ -121,15 +193,44 @@ tar -xf "$TMPDIR/bundle.tar" --directory "$BUNDLE_DIR" # Make a copy of the input bundle for modifying, so that we can show a diff later cp -r "$BUNDLE_DIR"/* "$PROCESSED_DIR" +RELATED_IMAGE_ENVS='.spec.install.spec.deployments[]?.spec.template.spec + | (.containers[]?, .initContainers[]?) + | .env[]? + | select(.name | test("^RELATED_IMAGE_"))' + for FILE in "$PROCESSED_DIR"/manifests/*.clusterserviceversion.yaml; do info "Processing file $FILE" IMAGES=$(yq -r '.spec.relatedImages[].image' "$FILE") for IMAGE in $IMAGES; do - DIGEST=$(skopeo inspect --tls-verify=false "docker://${IMAGE}" 2>/dev/null | jq -r '.Digest') - IMAGE_WITH_DIGEST="${IMAGE%%:*}@${DIGEST}" + retry_inspect skopeo inspect --tls-verify=false "docker://${IMAGE}" || + error "Failed to inspect $IMAGE: ${INSPECT_ERR_MSG}" + DIGEST=$(jq -r '.Digest // empty' <<< "${INSPECT_OUT}") + if [[ -z "$DIGEST" || "$DIGEST" == "null" ]]; then + error "Failed to get the digest of $IMAGE" + fi + IMAGE_WITH_DIGEST="$(strip_tag "$IMAGE")@${DIGEST}" info "Replacing $IMAGE with $IMAGE_WITH_DIGEST" sed -i "s|$IMAGE|$IMAGE_WITH_DIGEST|g" "$FILE" done + + # The operator passes these to the workspace, so they reach administrators' PinnedImageSets. + info "Checking that every RELATED_IMAGE_* value of $FILE can be pinned" + INVALID=$(yq -r "[${RELATED_IMAGE_ENVS} | select((.value // \"\") == \"\" or (.value | test(\"\\n\")))] | length" "$FILE") || + error "Failed to read the RELATED_IMAGE_* values of $FILE" + if [[ "$INVALID" != "0" ]]; then + error "Empty or multi-line RELATED_IMAGE_* value in $FILE" + fi + REFS=$(yq -r "${RELATED_IMAGE_ENVS} | .value" "$FILE") || + error "Failed to read the RELATED_IMAGE_* values of $FILE" + if [[ -z "$REFS" ]]; then + error "No RELATED_IMAGE_* values found in $FILE" + fi + while IFS= read -r REF; do + if [[ -z "$REF" ]]; then + error "Empty RELATED_IMAGE_* value in $FILE" + fi + check_pinnable "$REF" + done <<< "$REFS" done if [ "$DEBUG" == true ]; then @@ -157,8 +258,13 @@ else $PODMAN push "$PUSH_IMAGE" 2>&1 | sed 's|^| |g' fi -NEW_BUNDLE_SHA=$(skopeo inspect "docker://${PUSH_IMAGE}" | jq -r '.Digest') -NEW_BUNDLE_DIGEST="${PUSH_IMAGE%%:*}@${NEW_BUNDLE_SHA}" +retry_inspect skopeo inspect "docker://${PUSH_IMAGE}" || + error "Failed to inspect $PUSH_IMAGE: ${INSPECT_ERR_MSG}" +NEW_BUNDLE_SHA=$(jq -r '.Digest // empty' <<< "${INSPECT_OUT}") +if [[ -z "$NEW_BUNDLE_SHA" ]]; then + error "Failed to get the digest of $PUSH_IMAGE" +fi +NEW_BUNDLE_DIGEST="$(strip_tag "$PUSH_IMAGE")@${NEW_BUNDLE_SHA}" info "Resolved image $NEW_BUNDLE_DIGEST from $PUSH_IMAGE" info "Rendering $NEW_BUNDLE_DIGEST to $RENDER"