copy and pull images through argv helpers

Add retry_cmd, which runs an argv array instead of an eval'd command
string, and use it for the buildx copy and the local pull. Pull the
digest gpg verified and tag it, so a re-pushed tag cannot be installed.
This commit is contained in:
reyesj2 committed 2026-11-29 17:51:03 -06:00
1 parent f78382fee2
commit eb3ff80154
1 file changed
+41 -4
+41 -4
View File
@@ -252,6 +252,28 @@ verify_trusted_image() {
return 0
}
# retry()/run_check_net_err() eval their command string, so the argv helper is
# used for anything that must not be re-parsed by the shell.
retry_cmd() {
local max_attempts=$1
local delay=$2
shift 2
local attempt=1 rc=0
while :; do
if "$@"; then
return 0
else
rc=$?
fi
echo "Command failed with error $rc (attempt $attempt of $max_attempts): $*"
if [ "$attempt" -ge "$max_attempts" ]; then
return $rc
fi
attempt=$((attempt + 1))
sleep "$delay"
done
}
copy_verified_image() {
local i=$1
local IMAGE_LOG=$SIGNPATH/$i.copy.log
@@ -267,7 +289,9 @@ copy_verified_image() {
mount_shared_blobs "$host" "$i" "$manifest_file" "$IMAGE_LOG" pushed_blob_repo
# imagetools create, not docker push: Docker 29's containerd store cannot push a
# freshly pulled image. GODEBUG prefix keeps http2client=0 local (#16342).
retry 3 10 "GODEBUG=\"${GODEBUG:+${GODEBUG},}http2client=0\" docker buildx imagetools create --tag ${host}:5000/${IMAGEREPO}/${image} ${VERIFIED_REF}" >>"$IMAGE_LOG" 2>&1 || {
retry_cmd 3 10 env GODEBUG="${GODEBUG:+${GODEBUG},}http2client=0" \
docker buildx imagetools create --tag "${host}:5000/${IMAGEREPO}/${image}" "$VERIFIED_REF" \
>>"$IMAGE_LOG" 2>&1 || {
echo "Unable to copy $image to embedded registry" >>"$IMAGE_LOG" 2>&1
return 1
}
@@ -277,15 +301,28 @@ copy_verified_image() {
pull_verified_image() {
local i=$1
local IMAGE_LOG=$SIGNPATH/$i.pull.log
local image
read -r image <"$SIGNPATH/$i.ref"
local image VERIFIED_REF
{
read -r image
read -r VERIFIED_REF
} <"$SIGNPATH/$i.ref"
local host
host=$(registry_host)
retry 3 10 "docker pull ${host}:5000/${IMAGEREPO}/${image}" >>"$IMAGE_LOG" 2>&1 || {
# Pull by the digest that gpg verified, not by the mutable tag a compromised
# grid node could have re-pushed. Keep the tag name visible to `docker images`
# and to the salt checks that look for it.
local digest=${VERIFIED_REF#*@}
local digest_ref="${host}:5000/${IMAGEREPO}/${image%:*}@${digest}"
local tag_ref="${host}:5000/${IMAGEREPO}/${image}"
retry_cmd 3 10 docker pull "$digest_ref" >>"$IMAGE_LOG" 2>&1 || {
echo "Unable to pull $image from embedded registry" >>"$IMAGE_LOG" 2>&1
return 1
}
if ! docker tag "$digest_ref" "$tag_ref" >>"$IMAGE_LOG" 2>&1; then
echo "Unable to tag $image" >>"$IMAGE_LOG" 2>&1
return 1
fi
return 0
}