From e53eeb76a4612e7718dc6b6a08c0fa5b55eacd5e Mon Sep 17 00:00:00 2001 From: Marko Petzold Date: Wed, 11 Nov 2026 16:39:46 +0100 Subject: [PATCH] fix: install Ubuntu packages through public resolvers when the runner's resolver fails --- .github/workflows/selftest.yml | 6 +- .github/workflows/shell-gate.yml | 16 ++-- README.md | 8 ++ scripts/apt-install.sh | 81 ++++++++++++++++ scripts/test_apt_install.sh | 160 +++++++++++++++++++++++++++++++ 5 files changed, 260 insertions(+), 11 deletions(-) create mode 100755 scripts/apt-install.sh create mode 100755 scripts/test_apt_install.sh diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml index c433ddf..c0c8871 100644 --- a/.github/workflows/selftest.yml +++ b/.github/workflows/selftest.yml @@ -90,6 +90,8 @@ jobs: run: bash scripts/test_publish.sh - name: sign-provenance.sh check unit tests run: bash scripts/test_sign_provenance.sh + - name: apt-install.sh unit tests + run: bash scripts/test_apt_install.sh - name: tools.lock has every tool for x86_64 and arm64 run: bash scripts/test_tools_lock.sh # Public repository: no internal document names or plan IDs. @@ -338,9 +340,7 @@ jobs: with: persist-credentials: false - run: scripts/install-tool.sh just shellcheck shfmt - - run: | - sudo apt-get update -qq - sudo apt-get install -y -qq --no-install-recommends bats bats-support bats-assert + - run: scripts/apt-install.sh bats bats-support bats-assert - name: the planted findings must fail `just lint` and `just test` working-directory: tests/fixtures/shell-fail run: | diff --git a/.github/workflows/shell-gate.yml b/.github/workflows/shell-gate.yml index a64b867..15372cb 100644 --- a/.github/workflows/shell-gate.yml +++ b/.github/workflows/shell-gate.yml @@ -15,8 +15,11 @@ # shellcheck and shfmt from tools.lock in every job, yamllint (lint) and bats # with bats-support and bats-assert (unit) from the Ubuntu archive. A build # that needs more (mmdebstrap, genimage, qemu) names them in -# `build-apt-packages`. `build-artifact-path` hands the build output to a -# later job of the caller (an image test, a release), as an artifact. +# `build-apt-packages`. Ubuntu packages are installed by the harness's +# scripts/apt-install.sh, which asks public resolvers when the runner's +# resolver does not know the archive's names. `build-artifact-path` hands the +# build output to a later job of the caller (an image test, a release), as an +# artifact. # # Job names stay `lint` / `unit` / `build`, as in the other gates. name: shell-gate @@ -119,9 +122,8 @@ jobs: env: PKGS: ${{ inputs.apt-packages }} run: | - sudo apt-get update -qq # shellcheck disable=SC2086 # a space-separated list of package names - sudo apt-get install -y -qq --no-install-recommends yamllint $PKGS + "$HARNESS/scripts/apt-install.sh" yamllint $PKGS - name: just ${{ inputs.lint-recipe }} working-directory: ${{ inputs.working-directory }} env: @@ -160,9 +162,8 @@ jobs: env: PKGS: ${{ inputs.apt-packages }} run: | - sudo apt-get update -qq # shellcheck disable=SC2086 # a space-separated list of package names - sudo apt-get install -y -qq --no-install-recommends bats bats-support bats-assert $PKGS + "$HARNESS/scripts/apt-install.sh" bats bats-support bats-assert $PKGS - name: just ${{ inputs.test-recipe }} working-directory: ${{ inputs.working-directory }} env: @@ -202,9 +203,8 @@ jobs: env: PKGS: ${{ inputs.apt-packages }} ${{ inputs.build-apt-packages }} run: | - sudo apt-get update -qq # shellcheck disable=SC2086 # a space-separated list of package names - sudo apt-get install -y -qq --no-install-recommends $PKGS + "$HARNESS/scripts/apt-install.sh" $PKGS - name: just ${{ inputs.build-recipe }} ${{ inputs.build-args }} working-directory: ${{ inputs.working-directory }} env: diff --git a/README.md b/README.md index 46be758..fb9b141 100644 --- a/README.md +++ b/README.md @@ -382,3 +382,11 @@ The summary lists exceptions that expire within 30 days. categories, `fail-on`, VEX expiry and the code-scanning copy. - **Release script.** `bash scripts/test_release.sh` tests the release intent, the run modes of `release-image.yml` and the idempotent tag. +- **Ubuntu packages.** Every `apt-get` on a runner goes through + [scripts/apt-install.sh](scripts/apt-install.sh): when a name of the + runner's package sources does not resolve, it asks public resolvers + (1.1.1.1, 8.8.8.8) before the first try; each `apt-get` call gets 10 + minutes; a failed try gets a second one, through public resolvers when the + first did not use them. A self-hosted runner keeps its resolver. + `bash scripts/test_apt_install.sh` tests it with stand-ins for `apt-get` + and the resolver, and that no workflow calls `apt-get` itself. diff --git a/scripts/apt-install.sh b/scripts/apt-install.sh new file mode 100755 index 0000000..b38d84f --- /dev/null +++ b/scripts/apt-install.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# Installs Ubuntu packages on a runner with apt-get, and asks public resolvers +# for the archive's names when the runner's resolver does not know them. +# apt-install.sh ... +# +# On 2026-11-11 the hosted runners could not resolve the names of Ubuntu's +# archive for hours, while the archive was up and public resolvers answered. +# apt-get waited out every name: jobs hung for 14 to 50 minutes and then +# failed. So this script +# +# 1. asks for the names in the runner's package sources first, and switches +# to public resolvers (1.1.1.1, 8.8.8.8) at once when one of them does +# not resolve; +# 2. gives one apt-get call 10 minutes; +# 3. tries a second time when the first try fails, through public resolvers +# when the first try did not use them yet. +# +# What is installed is the same either way. A self-hosted runner +# (RUNNER_ENVIRONMENT=self-hosted) keeps its own resolver: it outlives the job +# and may need it for private names; it only gets the second try. +# +# Env (the tests set them): APT_INSTALL_RESOLV_CONF, APT_INSTALL_SOURCES (files +# and folders, space-separated), APT_INSTALL_SUDO (empty: none), +# APT_INSTALL_TIMEOUT (seconds per apt-get call). +set -euo pipefail + +[ "$#" -gt 0 ] || { echo "usage: apt-install.sh ..." >&2; exit 2; } + +resolv=${APT_INSTALL_RESOLV_CONF:-/etc/resolv.conf} +sources=${APT_INSTALL_SOURCES:-/etc/apt/sources.list /etc/apt/sources.list.d /etc/apt/apt-mirrors.txt} +sudo=${APT_INSTALL_SUDO-sudo} +limit=${APT_INSTALL_TIMEOUT:-600} + +die() { echo "apt-install: $*" >&2; exit 1; } + +# hosts: the host names in the runner's package sources, one per line. +hosts() { + # shellcheck disable=SC2086 # a space-separated list of files and folders + grep -rhoE 'https?://[A-Za-z0-9.-]+' $sources 2>/dev/null | sed 's|^[a-z]*://||' | sort -u || true +} + +# resolves: does every host of the package sources resolve? +resolves() { + local h bad=0 + for h in $(hosts); do + timeout 15 getent hosts "$h" >/dev/null || { + echo "apt-install: $h does not resolve" >&2 + bad=1 + } + done + return "$bad" +} + +public=0 +public_resolvers() { + if [ "${RUNNER_ENVIRONMENT:-}" = self-hosted ]; then + echo "::warning::apt-install: $1; a self-hosted runner keeps its resolver" + return 0 + fi + echo "::warning::apt-install: $1; asking public resolvers (1.1.1.1, 8.8.8.8)" + # resolv.conf is a link into systemd-resolved's folder on the runners. + $sudo rm -f "$resolv" + printf 'nameserver 1.1.1.1\nnameserver 8.8.8.8\n' | $sudo tee "$resolv" >/dev/null + public=1 +} + +fetch() { + $sudo timeout "$limit" apt-get update -qq && + $sudo timeout "$limit" apt-get install -y -qq --no-install-recommends "$@" +} + +resolves || public_resolvers "a name of the package sources does not resolve" +if ! fetch "$@"; then + if [ "$public" = 0 ] && [ "${RUNNER_ENVIRONMENT:-}" != self-hosted ]; then + public_resolvers "apt-get failed" + else + echo "::warning::apt-install: apt-get failed; second try" + fi + fetch "$@" || die "apt-get failed twice: $*" +fi +echo "installed $*" diff --git a/scripts/test_apt_install.sh b/scripts/test_apt_install.sh new file mode 100755 index 0000000..2a6a725 --- /dev/null +++ b/scripts/test_apt_install.sh @@ -0,0 +1,160 @@ +#!/usr/bin/env bash +# Tests for apt-install.sh against stand-ins for apt-get, getent and timeout +# (nothing is installed, no resolver is changed), and that the workflows +# install packages on a runner only through it: +# bash scripts/test_apt_install.sh +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +script="$here/apt-install.sh" +workflows="$here/../.github/workflows" +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +fail=0 + +n=0 +# setup: the runner's resolver; package sources as on a hosted runner (a +# deb822 file whose archive lives in the mirror list, a classic list with the +# security archive); stand-ins in $t/bin: +# timeout runs the command, without the limit +# getent every name resolves unless it is listed in $t/unknown +# apt-get logs its call and the resolver file of that moment; fails as +# often as $t/fail says +setup() { + n=$((n + 1)) + t="$work/$n" + mkdir -p "$t/bin" "$t/sources" + echo "nameserver 127.0.0.53" >"$t/resolv.conf" + printf 'Types: deb\nURIs: mirror+file:/etc/apt/apt-mirrors.txt\nSuites: noble\n' >"$t/sources/ubuntu.sources" + printf 'http://azure.archive.ubuntu.com/ubuntu/\tpriority:1\nhttp://archive.ubuntu.com/ubuntu/\tpriority:2\n' >"$t/apt-mirrors.txt" + printf 'deb https://security.ubuntu.com/ubuntu noble-security main\n' >"$t/sources/security.list" + printf '#!/usr/bin/env bash\nshift\nexec "$@"\n' >"$t/bin/timeout" + cat >"$t/bin/getent" </dev/null +EOF + cat >"$t/bin/apt-get" <>"$t/calls" +n=\$(cat "$t/fail" 2>/dev/null || echo 0) +if [ "\$n" -gt 0 ]; then echo \$((n - 1)) >"$t/fail"; exit 100; fi +EOF + chmod +x "$t/bin/"* +} + +kind=github-hosted # the runner's RUNNER_ENVIRONMENT +check() { # check + local name=$1 want=$2 got=0 + shift 2 + env PATH="$t/bin:$PATH" APT_INSTALL_SUDO="" APT_INSTALL_RESOLV_CONF="$t/resolv.conf" \ + APT_INSTALL_SOURCES="$t/sources $t/apt-mirrors.txt $t/missing" RUNNER_ENVIRONMENT="$kind" \ + bash "$script" "$@" >"$t/out" 2>&1 : the command succeeds + local name=$1 + shift + if "$@" >/dev/null 2>&1; then echo "ok $name"; else echo "FAIL $name"; fail=1; fi +} + +# shellcheck disable=SC2329 # called through expect +said() { grep -qF -- "$1" "$t/out"; } +# shellcheck disable=SC2329 # called through expect +call() { sed -n "${1}p" "$t/calls" 2>/dev/null; } +# shellcheck disable=SC2329 # called through expect +calls() { if [ -e "$t/calls" ]; then wc -l <"$t/calls" | tr -d ' '; else echo 0; fi; } + +runner="[nameserver 127.0.0.53 ]" +public="[nameserver 1.1.1.1 nameserver 8.8.8.8 ]" + +setup +check "names resolve and apt-get works" 0 yamllint bats +expect "works: the runner's resolver stays" test "$(cat "$t/resolv.conf")" = "nameserver 127.0.0.53" +expect "works: update, then install" test "$(calls)" = 2 +expect "works: update through the runner's resolver" test "$(call 1)" = "update -qq $runner" +expect "works: install with the gates' flags" test "$(call 2)" = "install -y -qq --no-install-recommends yamllint bats $runner" +expect "works: no warning" test -z "$(grep '::warning::' "$t/out")" + +setup +echo azure.archive.ubuntu.com >"$t/unknown" +check "a name of the mirror list does not resolve" 0 bats +expect "unresolved: names it" said "azure.archive.ubuntu.com does not resolve" +expect "unresolved: warns" said "::warning::apt-install: a name of the package sources does not resolve; asking public resolvers" +expect "unresolved: public resolvers before the first try" test "$(call 1)" = "update -qq $public" +expect "unresolved: one try" test "$(calls)" = 2 + +setup +echo security.ubuntu.com >"$t/unknown" +check "a name of a classic list does not resolve" 0 bats +expect "classic list: public resolvers before the first try" test "$(call 1)" = "update -qq $public" + +setup +echo 1 >"$t/fail" +check "apt-get fails once" 0 bats +expect "fails once: warns" said "::warning::apt-install: apt-get failed; asking public resolvers" +expect "fails once: the first try through the runner's resolver" test "$(call 1)" = "update -qq $runner" +expect "fails once: the second try through public resolvers" test "$(call 3)" = "install -y -qq --no-install-recommends bats $public" +expect "fails once: three calls" test "$(calls)" = 3 + +setup +# update succeeds, install fails, then update and install succeed +cat >"$t/bin/apt-get" <>"$t/calls" +[ "\$1" != install ] || [ -e "$t/second" ] || { touch "$t/second"; exit 100; } +EOF +check "the install fails once after a good update" 0 bats +expect "install fails once: both run again" test "$(tr '\n' ' ' <"$t/calls")" = "update install update install " + +setup +echo 5 >"$t/fail" +check "apt-get fails twice: the step fails" 1 bats +expect "fails twice: says so" said "apt-get failed twice: bats" +expect "fails twice: two tries" test "$(calls)" = 2 + +setup +echo security.ubuntu.com >"$t/unknown" +echo 1 >"$t/fail" +check "public resolvers already asked and apt-get fails once" 0 bats +expect "asked already: one more try" said "::warning::apt-install: apt-get failed; second try" +expect "asked already: no second switch" test "$(grep -c 'asking public resolvers' "$t/out")" = 1 + +setup +echo azure.archive.ubuntu.com >"$t/unknown" +echo 1 >"$t/fail" +kind=self-hosted +check "a self-hosted runner" 0 bats +kind=github-hosted +expect "self-hosted: keeps its resolver" test "$(cat "$t/resolv.conf")" = "nameserver 127.0.0.53" +expect "self-hosted: says so" said "a self-hosted runner keeps its resolver" +expect "self-hosted: still a second try" said "::warning::apt-install: apt-get failed; second try" +expect "self-hosted: both tries through its resolver" test "$(call 3)" = "install -y -qq --no-install-recommends bats $runner" + +setup +check "no package is a usage error" 1 +expect "no package: says how" said "usage: apt-install.sh" +expect "no package: nothing installed" test "$(calls)" = 0 + +# The workflows: no apt-get of their own; the shell gate's three jobs through +# their harness checkout, the self-test's planted-failure job through this +# repository's checkout. +if grep -rnE 'apt(-get)? +(update|install)' "$workflows" >"$work/apt" 2>&1; then + echo "FAIL the workflows install packages only through apt-install.sh:" + sed 's/^/ /' "$work/apt" + fail=1 +else + echo "ok the workflows install packages only through apt-install.sh" +fi +# shellcheck disable=SC2016 # $HARNESS of the workflow's text, not of this shell +expect "shell-gate.yml: lint, unit and build use it" test "$(grep -cF '"$HARNESS/scripts/apt-install.sh"' "$workflows/shell-gate.yml")" = 3 +expect "selftest.yml: shell-gate-fail uses it" grep -q 'scripts/apt-install.sh bats bats-support bats-assert' "$workflows/selftest.yml" + +[ "$fail" = 0 ] && echo "all apt-install.sh tests passed" +exit "$fail"