From ddbf3e971aa364a7efb0b84c659ae939a84141b2 Mon Sep 17 00:00:00 2001 From: reyesj2 <94730068+reyesj2@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:51:03 -0600 Subject: [PATCH] verify images with buildx instead of a registry client Read the manifest with imagetools inspect --raw and the config by manifest digest, each under timeout and retry_cmd. Every failure in this path gets its own message, and the .sig download is bounded by timeouts and --fail. --- salt/common/tools/sbin/so-image-common | 161 +++++++++++++++---------- 1 file changed, 97 insertions(+), 64 deletions(-) diff --git a/salt/common/tools/sbin/so-image-common b/salt/common/tools/sbin/so-image-common index 698efd1f7..644031124 100755 --- a/salt/common/tools/sbin/so-image-common +++ b/salt/common/tools/sbin/so-image-common @@ -13,6 +13,8 @@ STATUS_CONF='/opt/so/conf/so-status/so-status.conf' # PIDs of the current launch_pool children, so the INT/TERM trap can kill them. POOL_PIDS=() _pool_tree=() +# Registry metadata is small, so this only bounds a stall. Overridable for tests. +SO_IMAGE_INSPECT_TIMEOUT=${SO_IMAGE_INSPECT_TIMEOUT:-120} # shellcheck disable=SC2120 container_list() { @@ -131,11 +133,28 @@ record_shared_blobs() { done < <(manifest_blob_digests "$manifest_file") } -verify_trusted_image() { - local i=$1 - local IMAGE_LOG=$SIGNPATH/$i.verify.log +image_ref() { # image_ref => the full registry reference + printf '%s/%s/%s' "$CONTAINER_REGISTRY" "$IMAGEREPO" "$1" +} - local image sig_url +# One small function per inspect: retry_cmd logs its attempts on stdout, so the +# command's own output is redirected to its file here. +fetch_manifest() { # fetch_manifest + timeout "$SO_IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect --raw "$1" >"$2" +} + +# buildx's --format output is the contract with the signed .txt, so it stays in +# one place that is easy to adjust. +fetch_config() { # fetch_config + timeout "$SO_IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect \ + --format '{{json .Image}}' "$1" >"$2" +} + +# The ES image follows the elasticsearch version, everything else the SO +# version. Prints ": ". +resolve_image() { # resolve_image + local i=$1 + local image_version if [[ " ${IMAGES_USING_ES_VERSION[*]} " =~ [[:space:]]${i}[[:space:]] ]]; then # this is an es container so use version defined in elasticsearch defaults.yaml local UPDATE_DIR='/tmp/sogh/securityonion' @@ -150,62 +169,47 @@ verify_trusted_image() { if [[ -f "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" ]]; then v2=$(egrep " +version: " "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]') fi - local highest_es_version=$(compare_es_versions "$v1" "$v2") - image=$i:$highest_es_version$IMAGE_TAG_SUFFIX - sig_url=https://sigs.securityonion.net/es-$highest_es_version/$image.sig + image_version=$(compare_es_versions "$v1" "$v2") + printf '%s:%s%s https://sigs.securityonion.net/es-%s/%s:%s%s.sig\n' \ + "$i" "$image_version" "$IMAGE_TAG_SUFFIX" "$image_version" "$i" "$image_version" "$IMAGE_TAG_SUFFIX" else # this is not an es container so use the so version for the version - image=$i:$VERSION$IMAGE_TAG_SUFFIX - sig_url=https://sigs.securityonion.net/$VERSION/$image.sig + image_version=$VERSION + printf '%s:%s%s https://sigs.securityonion.net/%s/%s:%s%s.sig\n' \ + "$i" "$image_version" "$IMAGE_TAG_SUFFIX" "$image_version" "$i" "$image_version" "$IMAGE_TAG_SUFFIX" fi +} + +verify_trusted_image() { + local i=$1 + local IMAGE_LOG=$SIGNPATH/$i.verify.log + + local image sig_url + read -r image sig_url < <(resolve_image "$i") local image_name=$i - local image_tag=${image#*:} - local token_file=$SIGNPATH/$image.token - local auth_file=$SIGNPATH/$image.auth local manifest_file=$SIGNPATH/$image.manifest local config_file=$SIGNPATH/$image.config + local REF + REF=$(image_ref "$image") + local VERIFIED_REF - local manifest_url=https://$CONTAINER_REGISTRY/v2/$IMAGEREPO/$image_name/manifests/$image_tag - local manifest_accept='application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json' + local rc=0 + local digest_re='^sha256:[a-f0-9]{64}$' - # Probe anonymously first; only fetch a token on a 401 challenge, since a - # pull-through mirror may serve /v2/ without auth and have no /token at all. - : >"$auth_file" - local challenge - challenge=$(curl -sS --retry 5 --retry-delay 60 --retry-all-errors -o /dev/null -D - \ - -H "Accept: $manifest_accept" "$manifest_url" 2>>"$IMAGE_LOG" | - tr -d '\r' | grep -i '^www-authenticate: *bearer ') - if [ -n "$challenge" ]; then - local realm service scope - realm=$(sed -n 's/.*realm="\([^"]*\)".*/\1/p' <<<"$challenge") - service=$(sed -n 's/.*service="\([^"]*\)".*/\1/p' <<<"$challenge") - scope=$(sed -n 's/.*scope="\([^"]*\)".*/\1/p' <<<"$challenge") - if [ -z "$realm" ]; then - echo "Unable to parse registry auth challenge for $image" >>"$IMAGE_LOG" 2>&1 - return 1 - fi - run_check_net_err \ - "curl --fail --retry 5 --retry-delay 60 --retry-all-errors -sSL -G --data-urlencode 'service=$service' --data-urlencode 'scope=$scope' '$realm' -o $token_file" \ - "Could not obtain registry token for $image, please ensure connectivity to $CONTAINER_REGISTRY" \ - noretry >>"$IMAGE_LOG" 2>&1 - local registry_token - registry_token=$(jq -r '.token // .access_token // empty' "$token_file") - rm -f "$token_file" - if [ -z "$registry_token" ] || [ "$registry_token" = "null" ]; then - echo "Unable to parse registry token for $image" >>"$IMAGE_LOG" 2>&1 - return 1 - fi - printf 'Authorization: Bearer %s\n' "$registry_token" >"$auth_file" - unset registry_token + retry_cmd 3 10 fetch_manifest "$REF" "$manifest_file" >>"$IMAGE_LOG" 2>&1 || rc=$? + if [ "$rc" -ne 0 ]; then + echo "Could not fetch manifest for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1 + echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1 + return 1 fi - - run_check_net_err \ - "curl --fail --retry 5 --retry-delay 60 --retry-all-errors -sSL -H '@$auth_file' -H 'Accept: $manifest_accept' -o $manifest_file $manifest_url" \ - "Could not fetch manifest for $image, please ensure connectivity to $CONTAINER_REGISTRY" \ - noretry >>"$IMAGE_LOG" 2>&1 - if [ ! -s "$manifest_file" ]; then - echo "Unable to inspect $image" >>"$IMAGE_LOG" 2>&1 + if [ ! -s "$manifest_file" ] || ! jq -e . "$manifest_file" >/dev/null 2>&1; then + echo "Unable to read the manifest for $image: empty or not JSON" >>"$IMAGE_LOG" 2>&1 + return 1 + fi + # The sig covers these exact bytes, so an index would sign the wrong document. + if jq -e '.manifests | type == "array"' "$manifest_file" >/dev/null 2>&1; then + echo "$image manifest is a multi-platform index, expected a single manifest" >>"$IMAGE_LOG" 2>&1 return 1 fi @@ -214,32 +218,61 @@ verify_trusted_image() { local config_digest config_digest=$(jq -r '.config.digest // empty' "$manifest_file") if [ -z "$manifest_digest" ] || [ -z "$config_digest" ] || [ "$config_digest" = "null" ]; then - echo "Unable to inspect $image" >>"$IMAGE_LOG" 2>&1 + echo "Unable to read the manifest for $image: no config digest" >>"$IMAGE_LOG" 2>&1 return 1 fi - run_check_net_err \ - "curl --fail --retry 5 --retry-delay 60 --retry-all-errors -sSL -H '@$auth_file' -o $config_file https://$CONTAINER_REGISTRY/v2/$IMAGEREPO/$image_name/blobs/$config_digest" \ - "Could not fetch image config for $image, please ensure connectivity to $CONTAINER_REGISTRY" \ - noretry >>"$IMAGE_LOG" 2>&1 - rm -f "$auth_file" + # Every digest ends up in a URL (blob fetches, blob mounts, the wave + # planner), so one malformed digest rejects the whole manifest. + if [[ ! $config_digest =~ $digest_re ]]; then + echo "Unable to inspect $image: invalid config digest '$config_digest'" >>"$IMAGE_LOG" 2>&1 + return 1 + fi + local layer_digest + while IFS= read -r layer_digest; do + if [ -z "$layer_digest" ]; then + continue + fi + if [[ ! $layer_digest =~ $digest_re ]]; then + echo "Unable to inspect $image: invalid layer digest '$layer_digest'" >>"$IMAGE_LOG" 2>&1 + return 1 + fi + done < <(jq -r '.layers[]? | .digest' "$manifest_file") + + rc=0 + # By digest, not by tag: a tag can be re-pushed between the two calls, and the + # .txt must describe the manifest bytes that sha256sum and the sig cover. + VERIFIED_REF="${CONTAINER_REGISTRY}/${IMAGEREPO}/${image_name}@sha256:${manifest_digest}" + retry_cmd 3 10 fetch_config "$VERIFIED_REF" "$config_file" >>"$IMAGE_LOG" 2>&1 || rc=$? + if [ "$rc" -ne 0 ]; then + echo "Could not fetch image config for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1 + echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1 + return 1 + fi if [ ! -s "$config_file" ]; then - echo "Unable to inspect $image" >>"$IMAGE_LOG" 2>&1 + echo "Unable to read the config for $image" >>"$IMAGE_LOG" 2>&1 return 1 fi # Get signature - run_check_net_err \ - "curl --retry 5 --retry-delay 60 -A '$CURLTYPE/$CURRENTVERSION/$OS/$(uname -r)' $sig_url --output $SIGNPATH/$image.sig" \ - "Could not pull signature file for $image, please ensure connectivity to https://sigs.securityonion.net " \ - noretry >>"$IMAGE_LOG" 2>&1 + rc=0 + curl --fail --connect-timeout 10 --max-time 120 --retry 5 --retry-delay 60 -sSL \ + -A "$CURLTYPE/$CURRENTVERSION/$OS/$(uname -r)" \ + --output "$SIGNPATH/$image.sig" "$sig_url" >>"$IMAGE_LOG" 2>&1 || rc=$? + if [ "$rc" -ne 0 ]; then + echo "Could not pull signature file for $image, please ensure connectivity to https://sigs.securityonion.net " >>"$IMAGE_LOG" 2>&1 + echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1 + return 1 + fi # .txt layout must stay byte-identical to the old `docker inspect | jq` output # because the gpg signature is verified against it. - local VERIFIED_REF="${CONTAINER_REGISTRY}/${IMAGEREPO}/${image_name}@sha256:${manifest_digest}" - printf '%s\n' "\"${VERIFIED_REF}\"" >"$SIGNPATH/$image.txt" + if ! printf '%s\n' "\"${VERIFIED_REF}\"" >"$SIGNPATH/$image.txt"; then + echo "Unable to write the signed ref line for $image" >>"$IMAGE_LOG" 2>&1 + return 1 + fi if ! jq ".created, .rootfs.diff_ids" "$config_file" >>"$SIGNPATH/$image.txt"; then - echo "Unable to inspect $image" >>"$IMAGE_LOG" 2>&1 + echo "Unable to extract created and diff_ids for $image" >>"$IMAGE_LOG" 2>&1 return 1 fi local GPGTEST