diff --git a/.github/actions/ci-install-pinned-ubuntu-packages.sh b/.github/actions/ci-install-pinned-ubuntu-packages.sh index ced9cd5a273..9c0d5edeca2 100755 --- a/.github/actions/ci-install-pinned-ubuntu-packages.sh +++ b/.github/actions/ci-install-pinned-ubuntu-packages.sh @@ -39,22 +39,44 @@ fi runner_temp_mode="$(stat -c '%a' "$RUNNER_TEMP")" apt_lists="$(mktemp -d "$RUNNER_TEMP/nemoclaw-apt-lists.XXXXXXXX")" +isolated_sources_dir="$(mktemp -d "$RUNNER_TEMP/nemoclaw-ubuntu-sources.XXXXXXXX")" +isolated_sources="$isolated_sources_dir/ubuntu.sources" cleanup() { local status=$? trap - EXIT sudo rm -rf -- "$apt_lists" || status=1 + rm -rf -- "$isolated_sources_dir" || status=1 sudo chmod "$runner_temp_mode" "$RUNNER_TEMP" || status=1 exit "$status" } trap cleanup EXIT +# APT cannot reliably fetch a mirror+file auxiliary list from a custom lists +# directory during install. Resolve only the runner's Ubuntu mirrorlist URI; +# keep each stanza's suites, components, and Signed-By key. +if ! sudo awk ' + $0 == "URIs: mirror+file:/etc/apt/apt-mirrors.txt" { + print "URIs: https://archive.ubuntu.com/ubuntu" + replaced++ + next + } + /mirror\+file:/ { unexpected = 1 } + { print } + END { if (replaced < 1 || unexpected) exit 1 } +' "$ubuntu_sources" | tee "$isolated_sources" >/dev/null; then + echo "::error title=Unsupported Ubuntu APT source::Expected the Ubuntu archive mirrorlist URI in $ubuntu_sources." >&2 + exit 1 +fi +chmod 0755 "$isolated_sources_dir" +chmod 0644 "$isolated_sources" + # APT's _apt user needs traversal into the isolated package-list directory. sudo chmod o+x "$RUNNER_TEMP" sudo chmod 0755 "$apt_lists" sudo install -d -o _apt -g root -m 0700 "$apt_lists/partial" apt_options=( - -o "Dir::Etc::sourcelist=$ubuntu_sources" + -o "Dir::Etc::sourcelist=$isolated_sources" -o "Dir::Etc::sourceparts=-" -o "Dir::State::lists=$apt_lists" -o "Acquire::http::Timeout=$acquire_timeout" diff --git a/test/automation/pull-requests/pr-workflow-contract.test.ts b/test/automation/pull-requests/pr-workflow-contract.test.ts index cd71c6e1ca8..60d79c2f874 100644 --- a/test/automation/pull-requests/pr-workflow-contract.test.ts +++ b/test/automation/pull-requests/pr-workflow-contract.test.ts @@ -103,10 +103,28 @@ function runPinnedAptFixture( const runnerTemp = join(temp, "runner-temp"); const aptCalls = join(temp, "apt-calls"); const timeoutCalls = join(temp, "timeout-calls"); + const sourceCapture = join(temp, "source-capture"); + const fakeUbuntuSources = join(temp, "ubuntu.sources"); mkdirSync(fakeBin); mkdirSync(runnerTemp, { mode: 0o700 }); writeFileSync(aptCalls, ""); writeFileSync(timeoutCalls, ""); + writeFileSync(sourceCapture, ""); + writeFileSync( + fakeUbuntuSources, + `Types: deb +URIs: mirror+file:/etc/apt/apt-mirrors.txt +Suites: noble noble-updates noble-backports +Components: main restricted universe multiverse +Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + +Types: deb +URIs: ${mode === "unexpected-source" ? "mirror+file:/etc/apt/unexpected.txt" : "mirror+file:/etc/apt/apt-mirrors.txt"} +Suites: noble-security +Components: main restricted universe multiverse +Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg +`, + ); writeFileSync( join(fakeBin, "sudo"), `#!/usr/bin/env bash @@ -115,6 +133,7 @@ if [[ "$1" == chmod && "$3" == "$RUNNER_TEMP" ]]; then command chmod "$2" "$3" || exit $? fi if [[ "$1" == test && "$FAKE_APT_MODE" == missing-source ]]; then exit 1; fi +if [[ "$1" == awk ]]; then command awk "$2" "$FAKE_UBUNTU_SOURCES"; exit $?; fi if [[ "$1" == timeout ]]; then shift; timeout "$@"; exit $?; fi if [[ "$1" == apt-get ]]; then echo 'apt-get must run under sudo timeout' >&2; exit 99; fi exit 0 @@ -125,6 +144,17 @@ exit 0 join(fakeBin, "apt-get"), `#!/usr/bin/env bash printf 'apt-get %s\\n' "$*" >> "$APT_CALLS" +source_path='' +for arg in "$@"; do + if [[ "$arg" == Dir::Etc::sourcelist=* ]]; then source_path="\${arg#*=}"; fi +done +if [[ -n "$source_path" && -f "$source_path" ]]; then cp "$source_path" "$SOURCE_CAPTURE"; fi +if [[ "$FAKE_APT_MODE" == mirror-file-failure && "$*" == *' install '* ]]; then + if [[ "$source_path" == /etc/apt/sources.list.d/ubuntu.sources ]] || grep -q 'mirror+file:' "$source_path"; then + echo 'Downloading mirror file failed' >&2 + exit 100 + fi +fi if [[ "$FAKE_APT_MODE" == update-error ]]; then for ((line=1; line<=100; line++)); do printf 'apt diagnostic %s\\n' "$line" >&2; done exit 86 @@ -165,12 +195,14 @@ if [[ "$FAKE_APT_MODE" == recover-update && "$*" == *' update' && $(wc -l < "$TI ...process.env, ADVISOR_DIR: process.cwd(), APT_CALLS: aptCalls, + FAKE_UBUNTU_SOURCES: fakeUbuntuSources, FAKE_APT_MODE: mode, FD_FIND_VERSION: "9.0.0-1", GITHUB_ACTION_PATH: join(process.cwd(), ".github/actions/ci-cli-coverage-shard"), PATH: `${fakeBin}:${process.env.PATH ?? ""}`, RIPGREP_VERSION: "14.1.0-1", RUNNER_TEMP: runnerTemp, + SOURCE_CAPTURE: sourceCapture, TIMEOUT_CALLS: timeoutCalls, }; try { @@ -189,6 +221,7 @@ if [[ "$FAKE_APT_MODE" == recover-update && "$*" == *' update' && $(wc -l < "$TI stderr: String(result.stderr), runnerTempMode: statSync(runnerTemp).mode & 0o777, calls: readFileSync(aptCalls, "utf8").trim().split("\n"), + sourceText: readFileSync(sourceCapture, "utf8"), timeoutCalls: readFileSync(timeoutCalls, "utf8").trim().split("\n"), }; } finally { @@ -642,7 +675,9 @@ printf '%s %s\\n' '6bf226944684f56c84dd014e8b979d27425c0148f61b3bd99bcc6f39e9dc const aptCalls = result.calls.filter((call) => call.startsWith("apt-get ")); expect(aptCalls).toHaveLength(1); expect(result.timeoutCalls).toHaveLength(1); - expect(aptCalls[0]).toContain("Dir::Etc::sourcelist=/etc/apt/sources.list.d/ubuntu.sources"); + expect(aptCalls[0]).toMatch( + /Dir::Etc::sourcelist=\S+\/nemoclaw-ubuntu-sources\.\S+\/ubuntu\.sources/u, + ); expect(aptCalls[0]).toContain("Dir::Etc::sourceparts=-"); expect(aptCalls[0]).toMatch(/Dir::State::lists=\S+\/nemoclaw-apt-lists\.\S+/u); expect(aptCalls[0]).toMatch(/ update$/u); @@ -657,13 +692,38 @@ printf '%s %s\\n' '6bf226944684f56c84dd014e8b979d27425c0148f61b3bd99bcc6f39e9dc const lists = aptCalls.map((call) => call.match(/Dir::State::lists=(\S+)/u)?.[1]); expect(lists[0]).toMatch(/\/nemoclaw-apt-lists\.\S+$/u); expect(lists[1]).toBe(lists[0]); - expect(aptCalls[1]).toContain("Dir::Etc::sourcelist=/etc/apt/sources.list.d/ubuntu.sources"); + const sources = aptCalls.map((call) => call.match(/Dir::Etc::sourcelist=(\S+)/u)?.[1]); + expect(sources[0]).toMatch(/\/nemoclaw-ubuntu-sources\.\S+\/ubuntu\.sources$/u); + expect(sources[1]).toBe(sources[0]); expect(aptCalls[1]).toContain("Dir::Etc::sourceparts=-"); expect(aptCalls[1]).toContain( "install -y --no-install-recommends fd-find=9.0.0-1 ripgrep=14.1.0-1", ); }); + it("resolves the runner mirrorlist before pinned install with isolated APT lists (#11320)", () => { + const result = runPinnedAptFixture("mirror-file-failure"); + expect(result.status, result.stderr).toBe(0); + expect(result.sourceText.match(/URIs: https:\/\/archive\.ubuntu\.com\/ubuntu/gu)).toHaveLength( + 2, + ); + expect(result.sourceText).not.toContain("mirror+file:"); + expect(result.sourceText).toContain("Suites: noble noble-updates noble-backports"); + expect(result.sourceText).toContain("Components: main restricted universe multiverse"); + expect( + result.sourceText.match(/Signed-By: \/usr\/share\/keyrings\/ubuntu-archive-keyring\.gpg/gu), + ).toHaveLength(2); + expect(result.sourceText).toContain("Suites: noble-security"); + expect(result.calls.filter((call) => call.startsWith("apt-get "))).toHaveLength(2); + }); + + it("rejects an unexpected runner mirrorlist before APT runs (#11320)", () => { + const result = runPinnedAptFixture("unexpected-source"); + expect(result.status).toBe(1); + expect(result.stderr).toContain("Unsupported Ubuntu APT source"); + expect(result.calls.filter((call) => call.startsWith("apt-get "))).toHaveLength(0); + }); + it.each([ ["CLI shards", requiredStep(sharedActions.cliCoverageShard, "Install pinned Pi search tools")], [