diff --git a/packages/cli/install b/packages/cli/install index de5c8b08d..41450729c 100755 --- a/packages/cli/install +++ b/packages/cli/install @@ -31,7 +31,9 @@ report_error() { [[ "${SENTRY_CLI_NO_TELEMETRY:-}" == "1" ]] && return 0 ( - set +e # Telemetry must never fail the script + # Telemetry must never fail the script or report its own failures. + set +e + trap - ERR local msg="${1:-unknown error}" local step="${2:-unknown}" local event_id @@ -70,8 +72,26 @@ die() { exit 1 } -# Catch unexpected failures from set -e / pipefail (e.g., gunzip failing) -trap 'die "Unexpected failure at line $LINENO" "trap"' ERR +# Catch unexpected failures from set -e / pipefail. errtrace (-E) makes +# functions and command substitutions inherit the trap; without it, a failure +# inside a function exits without a message or a report. $BASH_COMMAND is the +# failing command's unexpanded source text, so the report names the command +# without the paths or values it ran with. +set -E +on_unexpected_failure() { + local status=$1 line=$2 failed_command=$3 function_name=$4 + # A failing command substitution also fails the command that contains it, + # which reports from the parent shell. + if (( BASH_SUBSHELL > 0 )); then + exit "$status" + fi + local where="line ${line}" + if [[ -n "$function_name" ]]; then + where="${where} in ${function_name}()" + fi + die "Unexpected failure at ${where} (exit ${status}): ${failed_command:0:200}" "trap" +} +trap 'on_unexpected_failure "$?" "$LINENO" "$BASH_COMMAND" "${FUNCNAME[0]:-}"' ERR usage() { cat <. Compares PATH entries like samePath: one trailing +# slash is ignored, and so is case on macOS and Windows. The subshell keeps +# nocasematch local. +in_path() ( + local entry entries + [[ -n "${PATH:-}" ]] || return 1 + if [[ "$os" == "darwin" || "$os" == "windows" ]]; then + shopt -s nocasematch + fi + IFS=: read -ra entries <<< "$PATH" + for entry in "${entries[@]}"; do + if [[ "${#entry}" -gt 1 ]]; then + entry="${entry%/}" + fi + [[ "$entry" == "$1" ]] && return 0 + done + return 1 +) +if [[ -z "$setup_dir" && -n "${HOME:-}" ]]; then + setup_dir="${HOME}/.local/bin" + for dir in "${HOME}/.local/bin" "${HOME}/bin"; do + if [[ -d "$dir" ]] && in_path "$dir"; then + setup_dir="$dir" + break + fi + done +fi +setup_binary="" +setup_blob="" +if [[ -n "$setup_dir" ]]; then + setup_binary="${setup_dir}/sentry${suffix}.download" + setup_blob="${setup_dir}/sentry-install-blob-$$" +fi + +# Retry transient network failures (timeouts, refused connections, HTTP 408, +# 429, and 5xx) with curl's exponential backoff. The binary needs glibc 2.28 +# or macOS 11, whose curl (7.61, 7.64) has --retry-connrefused (7.52) but not +# --retry-all-errors (7.71). +curl_retry=(--retry 3 --retry-connrefused --retry-max-time 60) + +# Usage: download_to . Exits with curl's status: 22 for an HTTP +# error, 23 when the file cannot be written. +download_to() { + curl -fsSL "${curl_retry[@]}" -o "$2" "$1" +} + +# Usage: unpack_to . gzip_intact has already checked the +# archive, so a failure here means the file could not be written (23). +unpack_to() { + gunzip -c "$1" > "$2" || return 23 +} + +# Usage: gzip_intact . +gzip_intact() { + gunzip -t "$1" 2>/dev/null +} + +# Usage: write_staged [args...]. Runs +# ` [args...] ` for the path in . When the writer cannot +# write there (exit 23), points at and runs it again. +write_staged() { + local var=$1 fallback=$2 status + shift 2 + "$@" "${!var}" && return 0 + status=$? + rm -f "${!var}" + if [[ "$status" != 23 || -z "$fallback" || "${!var}" == "$fallback" ]] \ + || ! mkdir -p "${fallback%/*}" 2>/dev/null; then + return "$status" + fi + echo -e "${MUTED}Not enough room in the temporary directory; using ${fallback%/*} instead.${NC}" >&2 + printf -v "$var" '%s' "$fallback" + "$@" "$fallback" && return 0 + status=$? + rm -f "$fallback" + return "$status" +} + +# Usage: checksum_matches . A missing checksum or a +# missing hash tool counts as a match; a mismatch records the actual hash in +# bad_checksum. +bad_checksum="" +checksum_matches() { + [[ -z "$2" ]] && return 0 + local actual + if command -v sha256sum >/dev/null 2>&1; then + actual=$(sha256sum "$1" | awk '{print $1}') || actual="" + elif command -v shasum >/dev/null 2>&1; then + actual=$(shasum -a 256 "$1" | awk '{print $1}') || actual="" + else + echo -e "${MUTED}Warning: sha256sum and shasum are unavailable; skipping checksum verification.${NC}" >&2 + return 0 + fi + # Callers run this as a condition, where set -e and the ERR trap are off, so + # a failing hash tool is checked here. + [[ -n "$actual" ]] || die "Failed to compute the SHA-256 checksum of the download" "checksum" + [[ "$actual" == "$2" ]] && return 0 + bad_checksum="$actual" + return 1 +} + +# Usage: download_verified [check]. +# Downloads to the path in (see write_staged), and downloads it +# once more when the transfer breaks, the checksum does not match, or +# rejects the file. Returns curl's status for an HTTP error (22) or nowhere to +# write (23), since another try cannot help, and 1 when both copies are bad. +download_verified() { + local var=$1 fallback=$2 url=$3 sha=$4 check=${5:-} attempt status=0 name + # Drop the query string: GHCR redirects to a signed URL. + name="${url%%\?*}" + name="${name##*/}" + for attempt in 1 2; do + bad_checksum="" + if write_staged "$var" "$fallback" download_to "$url"; then + if checksum_matches "${!var}" "$sha" && { [[ -z "$check" ]] || "$check" "${!var}"; }; then + return 0 + fi + status=1 + else + status=$? + if [[ "$status" == 22 || "$status" == 23 ]]; then + return "$status" + fi + fi + if [[ "$attempt" == 1 ]]; then + echo -e "${MUTED}The download of ${name} was incomplete or corrupt. Downloading it again...${NC}" >&2 + fi + done + return "$status" +} + +no_room_message="There is not enough room to stage sentry (about 140 MB): the temporary directory and the install directory are both full or not writable. Free up space or set TMPDIR to another directory, then retry." + +# Usage: unpack_binary . +unpack_binary() { + write_staged tmp_binary "$setup_binary" unpack_to "$1" \ + || die "$no_room_message" "unpack" +} if [[ "$requested_version" == "nightly" ]]; then # Nightly build: download from GHCR via OCI blob protocol. @@ -207,7 +372,7 @@ if [[ "$requested_version" == "nightly" ]]; then echo -e "${MUTED}Fetching nightly build from GHCR...${NC}" # Step 1: Get anonymous pull token - if ! GHCR_TOKEN=$(curl -sf \ + if ! GHCR_TOKEN=$(curl -sf "${curl_retry[@]}" \ "https://ghcr.io/token?scope=repository:getsentry/toolkit:pull" \ | awk -F'"' '{for(i=1;i<=NF;i++) if($i=="token"){print $(i+2);exit}}'); then die "Failed to get GHCR token" "ghcr-token" @@ -217,7 +382,7 @@ if [[ "$requested_version" == "nightly" ]]; then fi # Step 2: Fetch the OCI manifest for the :nightly tag - if ! MANIFEST=$(curl -sf \ + if ! MANIFEST=$(curl -sf "${curl_retry[@]}" \ -H "Authorization: Bearer $GHCR_TOKEN" \ -H "Accept: application/vnd.oci.image.manifest.v1+json" \ "https://ghcr.io/v2/getsentry/toolkit/manifests/nightly"); then @@ -254,23 +419,42 @@ if [[ "$requested_version" == "nightly" ]]; then # Step 5: Fetch the blob. GHCR normally redirects to Azure, but it can also # return the blob directly with HTTP 200. Do not forward the auth header to - # the redirect target. - if ! blob_status=$(curl -sS -D "$blob_headers" -o "$blob_file" -w '%{http_code}' \ - -H "Authorization: Bearer $GHCR_TOKEN" \ - "https://ghcr.io/v2/getsentry/toolkit/blobs/${digest}"); then + # the redirect target. The headers and then the status code go to stdout, + # so only the body needs room on disk (see write_staged). + # Usage: fetch_ghcr_blob . Sets blob_response. + fetch_ghcr_blob() { + blob_response=$(curl -sS "${curl_retry[@]}" -D - -o "$1" -w '%{http_code}' \ + -H "Authorization: Bearer $GHCR_TOKEN" \ + "https://ghcr.io/v2/getsentry/toolkit/blobs/${digest}") + } + blob_response="" + fetch_status=0 + write_staged blob_file "$setup_blob" fetch_ghcr_blob || fetch_status=$? + if [[ "$fetch_status" == 23 ]]; then + die "$no_room_message" "ghcr-blob" + elif [[ "$fetch_status" != 0 ]]; then die "Failed to fetch nightly blob from GHCR" "ghcr-blob" fi + blob_status="${blob_response##*$'\n'}" case "$blob_status" in 200) - gunzip -c "$blob_file" > "$tmp_binary" + gzip_intact "$blob_file" || die "The nightly blob from GHCR is corrupt" "ghcr-blob" + unpack_binary "$blob_file" ;; 3??) - redir_url=$(awk '/^[Ll][Oo][Cc][Aa][Tt][Ii][Oo][Nn]:[[:space:]]*/ { sub(/^[^:]*:[[:space:]]*/, ""); sub(/\r$/, ""); print; exit }' "$blob_headers") + redir_url=$(awk '/^[Ll][Oo][Cc][Aa][Tt][Ii][Oo][Nn]:[[:space:]]*/ { sub(/^[^:]*:[[:space:]]*/, ""); sub(/\r$/, ""); print; exit }' <<< "$blob_response") if [[ -z "$redir_url" ]]; then die "Failed to get blob redirect URL from GHCR" "ghcr-redirect" fi - curl -fsSL "$redir_url" | gunzip > "$tmp_binary" + fetch_status=0 + download_verified blob_file "$setup_blob" "$redir_url" "" gzip_intact || fetch_status=$? + if [[ "$fetch_status" == 23 ]]; then + die "$no_room_message" "ghcr-blob" + elif [[ "$fetch_status" != 0 ]]; then + die "Failed to download nightly blob" "ghcr-blob" + fi + unpack_binary "$blob_file" ;; *) die "Failed to fetch nightly blob from GHCR (HTTP ${blob_status})" "ghcr-blob" @@ -298,13 +482,17 @@ else raw_url="" raw_sha="" registry_version="" - registry_status=$(curl -sS --max-time 10 -o "$metadata_file" -w '%{http_code}' \ + # Read the body from curl's stdout, not a temp file: on some Windows shells, + # bash could not read the file that curl had just written. The status code + # follows the body on its own line. + registry_response=$(curl -sS --max-time 10 "${curl_retry[@]}" -w '\n%{http_code}' \ "https://release-registry.services.sentry.io/apps/sentry/${registry_path}" 2>/dev/null) \ - || registry_status="000" + || registry_response="000" + registry_status="${registry_response##*$'\n'}" if [[ "$registry_status" == "200" ]]; then # Asset names, URLs, and checksums contain no whitespace, so stripping it # lets the parser below treat compact and pretty-printed JSON the same. - metadata=$(tr -d ' \t\r\n' < "$metadata_file") + metadata=$(tr -d ' \t\r\n' <<< "${registry_response%$'\n'*}") registry_version=$(printf '%s\n' "$metadata" \ | sed -n 's/.*"version":"\([^"]*\)".*/\1/p') if [[ "$registry_path" == "latest" && ! "$registry_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then @@ -348,10 +536,43 @@ else die "Unknown Sentry CLI version: ${version}" "resolve-version" else echo -e "${MUTED}Could not resolve ${registry_path} from Sentry's release registry. Trying GitHub...${NC}" >&2 + # Usage: github_api . Prints the response body. Runners that share an + # IP exhaust GitHub's anonymous rate limit, so send GITHUB_TOKEN or + # GH_TOKEN when one is set. The header goes through stdin, so the token + # stays out of the process list and of $BASH_COMMAND in error reports. + # Rate-limited responses (403, 429) are retried with a growing delay. + github_api() { + local token="${GITHUB_TOKEN:-${GH_TOKEN:-}}" response status attempt + for attempt in 1 2 3; do + if [[ -n "$token" ]]; then + response=$(curl -sSL "${curl_retry[@]}" -w '\n%{http_code}' -H @- "$1" \ + <<< "Authorization: Bearer ${token}") || return + else + response=$(curl -sSL "${curl_retry[@]}" -w '\n%{http_code}' "$1") || return + fi + status="${response##*$'\n'}" + case "$status" in + 200) + printf '%s\n' "${response%$'\n'*}" + return 0 + ;; + 403|429) ;; + *) return 22 ;; + esac + if [[ "$attempt" != 3 ]]; then + echo -e "${MUTED}GitHub rate-limited the request; retrying in $((attempt * 2))s...${NC}" >&2 + sleep $((attempt * 2)) + fi + done + if [[ -z "$token" ]]; then + echo -e "${MUTED}GitHub rate-limited this machine. Setting GITHUB_TOKEN raises the limit.${NC}" >&2 + fi + return 22 + } if [[ "$registry_path" == "latest" ]]; then release_tag="" for page in {1..20}; do - if ! releases=$(curl -fsSL "https://api.github.com/repos/getsentry/toolkit/releases?per_page=100&page=${page}"); then + if ! releases=$(github_api "https://api.github.com/repos/getsentry/toolkit/releases?per_page=100&page=${page}"); then die "Failed to list Toolkit releases" "gh-version" fi # Read the full page: exiting awk early can SIGPIPE printf under pipefail. @@ -410,40 +631,39 @@ else echo -e "${MUTED}Warning: GitHub fallback downloads are not checksum-verified.${NC}" >&2 fi - # Usage: verify_sha256 ; a missing checksum is a no-op. - verify_sha256() { - [[ -z "$2" ]] && return 0 - local actual - if command -v sha256sum >/dev/null 2>&1; then - actual=$(sha256sum "$1" | awk '{print $1}') - elif command -v shasum >/dev/null 2>&1; then - actual=$(shasum -a 256 "$1" | awk '{print $1}') - else - echo -e "${MUTED}Warning: sha256sum and shasum are unavailable; skipping checksum verification.${NC}" >&2 - return 0 - fi - if [[ "$actual" != "$2" ]]; then - die "Checksum mismatch for ${asset} ${version}: expected $2, got ${actual}" "checksum" - fi - } - echo -e "${MUTED}Downloading sentry v${version}...${NC}" # Prefer the gzip asset (~60% smaller, ~37 MB vs ~99 MB); fall back to the - # raw binary when it is missing. - if [[ -n "$gz_url" ]] && curl -fsSL -o "$blob_file" "$gz_url" 2>/dev/null; then - verify_sha256 "$blob_file" "$gz_sha" - gunzip -c "$blob_file" > "$tmp_binary" + # raw binary when it is missing or unusable. A checksum mismatch on both + # tries is fatal, never a reason to fall back. + gz_status=1 + if [[ -n "$gz_url" ]]; then + download_verified blob_file "$setup_blob" "$gz_url" "$gz_sha" gzip_intact \ + && gz_status=0 || gz_status=$? + fi + if [[ -n "$bad_checksum" ]]; then + die "Checksum mismatch for ${asset} ${version}: expected ${gz_sha}, got ${bad_checksum}" "checksum" + fi + if [[ "$gz_status" == 0 ]]; then + unpack_binary "$blob_file" elif [[ -n "$raw_url" ]]; then - curl -fsSL --progress-bar -o "$tmp_binary" "$raw_url" \ - || die "Failed to download ${raw_url}" "download" - verify_sha256 "$tmp_binary" "$raw_sha" + rm -f "$blob_file" + raw_status=0 + download_verified tmp_binary "$setup_binary" "$raw_url" "$raw_sha" || raw_status=$? + if [[ -n "$bad_checksum" ]]; then + die "Checksum mismatch for ${asset} ${version}: expected ${raw_sha}, got ${bad_checksum}" "checksum" + elif [[ "$raw_status" == 23 ]]; then + die "$no_room_message" "download" + elif [[ "$raw_status" != 0 ]]; then + die "Failed to download ${raw_url}" "download" + fi else die "Failed to download ${gz_url}" "download" fi fi chmod +x "$tmp_binary" +init_binary="${tmp_binary%/*}/sentry-install-init-$$${suffix}" # `sentry init` runs from a hard link to the downloaded binary: setup removes # the temp binary after installing it, and the link means the installer never diff --git a/packages/cli/install.ps1 b/packages/cli/install.ps1 index 2dc4c8ac1..f97f460a9 100644 --- a/packages/cli/install.ps1 +++ b/packages/cli/install.ps1 @@ -123,12 +123,30 @@ function Install-SentryCli { return @{ Url = "$($entry.Value.url)"; Sha256 = "$($entry.Value.checksums.'sha256-hex')" } } + # Usage: Test-Sha256 ; a missing checksum matches. + function Test-Sha256([string]$Path, [string]$Expected) { + if (-not $Expected) { return $true } + $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant() + return $actual -eq $Expected.ToLowerInvariant() + } + # Usage: Assert-Sha256 ; a missing checksum is a no-op. function Assert-Sha256([string]$Path, [string]$Expected) { - if (-not $Expected) { return } + if (Test-Sha256 $Path $Expected) { return } $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant() - if ($actual -ne $Expected.ToLowerInvariant()) { - Stop-Install "Checksum mismatch for $asset ${version}: expected $Expected, got $actual" + Stop-Install "Checksum mismatch for $asset ${version}: expected $Expected, got $actual" + } + + # Usage: Repair-Download . Downloads the file once + # more when it does not match its checksum, like the bash installer, since + # a broken transfer or proxy can corrupt the first copy. + function Repair-Download([string]$Path, [string]$Url, [string]$Expected) { + if (Test-Sha256 $Path $Expected) { return } + Write-Host 'The download was incomplete or corrupt. Downloading it again...' -ForegroundColor DarkGray + try { + Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Path + } catch { + Stop-Install "Failed to download $Url" } } @@ -218,7 +236,8 @@ function Install-SentryCli { Write-Host "Downloading sentry v$version..." -ForegroundColor DarkGray try { # Prefer the gzip asset (~60% smaller); fall back to the raw binary when it - # is missing. A checksum mismatch is fatal, never a reason to fall back. + # is missing. A checksum mismatch that a second download does not fix is + # fatal, never a reason to fall back. $downloaded = $false if ($null -ne $gz) { try { @@ -229,6 +248,7 @@ function Install-SentryCli { } } if ($downloaded) { + Repair-Download $tmpArchive $gz.Url $gz.Sha256 Assert-Sha256 $tmpArchive $gz.Sha256 $source = [System.IO.File]::OpenRead($tmpArchive) try { @@ -244,6 +264,7 @@ function Install-SentryCli { } catch { Stop-Install "Failed to download $($raw.Url)" } + Repair-Download $tmpBinary $raw.Url $raw.Sha256 Assert-Sha256 $tmpBinary $raw.Sha256 } else { Stop-Install "Failed to download $($gz.Url)" diff --git a/packages/cli/test/lib/install-scenarios.ts b/packages/cli/test/lib/install-scenarios.ts index 81316951a..402332a61 100644 --- a/packages/cli/test/lib/install-scenarios.ts +++ b/packages/cli/test/lib/install-scenarios.ts @@ -174,10 +174,12 @@ export const INSTALL_SCENARIOS: InstallScenario[] = [ name: "rejects a download whose checksum does not match the registry", registry: { status: 200, tamper: true }, ok: false, - output: ["Checksum mismatch"], + output: ["Downloading it again", "Checksum mismatch"], + // A mismatch can come from a broken transfer, so it downloads once more. requests: (asset) => [ `${REGISTRY}/latest`, toolkitDownload(LATEST_VERSION, `${asset}.gz`), + toolkitDownload(LATEST_VERSION, `${asset}.gz`), ], }, { diff --git a/packages/cli/test/lib/install-script.test.ts b/packages/cli/test/lib/install-script.test.ts index c924f335b..ffa19026c 100644 --- a/packages/cli/test/lib/install-script.test.ts +++ b/packages/cli/test/lib/install-script.test.ts @@ -3,7 +3,7 @@ * verify terminal reconnection for setup and the existing init handoff. */ -import { spawnSync } from "node:child_process"; +import { spawn, spawnSync } from "node:child_process"; import { chmodSync, existsSync, @@ -14,6 +14,8 @@ import { rmSync, writeFileSync, } from "node:fs"; +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { gzipSync } from "node:zlib"; @@ -48,8 +50,9 @@ if [[ "$1" == "cli" && "$2" == "setup" ]]; then printf '%s\\n' "$@" > "$SENTRY_TEST_DIR/setup-args" printf '%s\\n' "$0" > "$SENTRY_TEST_DIR/setup-binary" record_tty 3> "$SENTRY_TEST_DIR/setup-tty" - mkdir -p "$SENTRY_INSTALL_DIR" - cp "$0" "$SENTRY_INSTALL_DIR/sentry" + install_dir="\${SENTRY_INSTALL_DIR:-\${0%/*}}" + mkdir -p "$install_dir" + cp "$0" "$install_dir/sentry" if [[ "\${SENTRY_TEST_KEEP_TEMP_BINARY:-}" != "1" ]]; then rm "$0" fi @@ -101,12 +104,27 @@ describe("install script", () => { set -euo pipefail url="\${!#}" out="" +write_out="" +data="" args=("$@") for ((i = 0; i < \${#args[@]}; i++)); do - if [[ "\${args[i]}" == "-o" ]]; then out="\${args[i + 1]}"; fi + case "\${args[i]}" in + -o) out="\${args[i + 1]}" ;; + -w) write_out="\${args[i + 1]}" ;; + -d) data="\${args[i + 1]}" ;; + esac done -emit() { if [[ -n "$out" ]]; then cat > "$out"; else cat; fi; } +# Like curl, exit 23 when the output file cannot be written. +emit() { if [[ -n "$out" ]]; then cat > "$out" || exit 23; else cat; fi; } +# Usage: nth . Counts the requests of one kind. +nth() { + local file="$SENTRY_TEST_DIR/count-$1" n + n=$(( $(cat "$file" 2>/dev/null || echo 0) + 1 )) + echo "$n" > "$file" + echo "$n" +} printf '%s\\n' "$url" >> "$SENTRY_TEST_DIR/curl-urls" +printf '%s\\n' "$*" >> "$SENTRY_TEST_DIR/curl-args" case "$url" in https://release-registry.services.sentry.io/apps/sentry/*) version="\${url##*/}" @@ -116,14 +134,28 @@ case "$url" in else : | emit fi - printf '%s' "$SENTRY_TEST_REGISTRY_STATUS" + printf "\${write_out//'%{http_code}'/$SENTRY_TEST_REGISTRY_STATUS}" + ;; + https://o1.ingest.us.sentry.io/api/*/envelope/) + printf '%s\\n' "$data" > "$SENTRY_TEST_DIR/error-report" ;; *"/repos/getsentry/toolkit/releases?per_page=100&page="*) + if [[ " $* " == *" -H @- "* ]]; then cat >> "$SENTRY_TEST_DIR/github-headers"; fi + if (( $(nth github) <= \${SENTRY_TEST_GITHUB_RATE_LIMITED:-0} )); then + printf '{"message":"API rate limit exceeded"}' + printf "\${write_out//'%{http_code}'/403}" + exit 0 + fi page_file="SENTRY_TEST_RELEASES_PAGE_\${url##*=}" if [[ -n "\${!page_file:-}" ]]; then cat "\${!page_file}"; else printf '[]'; fi + printf "\${write_out//'%{http_code}'/200}" ;; https://github.com/*/releases/download/*.gz) if [[ "\${SENTRY_TEST_GZ_MISSING:-}" == "1" ]]; then exit 22; fi + if (( $(nth gz) <= \${SENTRY_TEST_GZ_CORRUPT:-0} )); then + head -c 100 "$SENTRY_TEST_GZ_FILE" | emit + exit 0 + fi emit < "$SENTRY_TEST_GZ_FILE" ;; https://github.com/*/releases/download/*) @@ -252,12 +284,15 @@ ${manifest} JSON ;; "https://objects.example/nightly.gz") - for arg in "$@"; do - if [[ "$arg" == Authorization:* ]]; then - exit 90 - fi + output="" + while [[ $# -gt 0 ]]; do + case "$1" in + Authorization:*) exit 90 ;; + -o) output="$2"; shift 2 ;; + *) shift ;; + esac done - cat "$SENTRY_TEST_GZIP" + cp "$SENTRY_TEST_GZIP" "$output" 2>/dev/null || exit 23 ;; *"/blobs/"*) headers="" @@ -269,13 +304,15 @@ JSON *) shift ;; esac done + # The installer reads the headers from stdout. + if [[ "$headers" != "-" ]]; then exit 92; fi if [[ "$SENTRY_TEST_NIGHTLY_REDIRECT" == "1" ]]; then - printf 'HTTP/1.1 307 Temporary Redirect\\r\\nLocation: https://objects.example/nightly.gz\\r\\n\\r\\n' > "$headers" - : > "$output" + : 2>/dev/null > "$output" || exit 23 + printf 'HTTP/1.1 307 Temporary Redirect\\r\\nLocation: https://objects.example/nightly.gz\\r\\n\\r\\n' printf '307' else - printf 'HTTP/1.1 200 OK\\r\\n\\r\\n' > "$headers" - cp "$SENTRY_TEST_GZIP" "$output" + cp "$SENTRY_TEST_GZIP" "$output" 2>/dev/null || exit 23 + printf 'HTTP/1.1 200 OK\\r\\n\\r\\n' printf '200' fi ;; @@ -290,7 +327,7 @@ esac join(binDir, "gunzip"), `#!/usr/bin/env bash set -euo pipefail -if [[ $# -gt 0 && "$1" != "-c" ]]; then +if [[ $# -gt 0 && "$1" != "-c" && "$1" != "-t" ]]; then exit 64 fi exec /usr/bin/gunzip "$@" @@ -410,6 +447,28 @@ process.exitCode = result.status ?? 1; ]); }); + test.each([ + { response: "direct HTTP 200 response", redirect: false }, + { response: "HTTP redirect", redirect: true }, + ])( + "stages nightly from a $response in the install directory without a temp directory", + ({ redirect }) => { + configureNightlyDownload(redirect); + env.TMPDIR = join(testDir, "deleted"); + const result = spawnSync( + "bash", + [installScript, "--version", "nightly", "--no-modify-path"], + { env, encoding: "utf8", timeout: 10_000 }, + ); + + expect(result.status, result.stdout + result.stderr).toBe(0); + expect(recorded("setup-binary")).toEqual([ + join(installDir, "sentry.download"), + ]); + expect(readdirSync(installDir)).toEqual(["sentry"]); + }, + ); + test.each([ { failure: "token", flag: "SENTRY_TEST_NIGHTLY_TOKEN_FAIL" }, { failure: "manifest", flag: "SENTRY_TEST_NIGHTLY_MANIFEST_FAIL" }, @@ -586,6 +645,225 @@ process.exitCode = result.status ?? 1; expect(recorded("curl-urls")).toEqual([]); }); + function install(): ReturnType & { output: string } { + const result = spawnSync("bash", [installScript, "--no-modify-path"], { + env, + encoding: "utf8", + timeout: 20_000, + }); + return { ...result, output: `${result.stdout}${result.stderr}` }; + } + + // CLI-BJ: transient network errors used to end the install. + test("retries transient registry errors with the real curl", async () => { + const realCurl = spawnSync("sh", ["-c", "command -v curl"], { + encoding: "utf8", + }).stdout.trim(); + let registryRequests = 0; + const server = createServer((request, response) => { + const path = request.url ?? ""; + if (path.startsWith("/release-registry.services.sentry.io/")) { + registryRequests++; + if (registryRequests <= 2) { + response.writeHead(503).end(); + return; + } + response.end( + registryTemplate(served, {}).replaceAll( + "__VERSION__", + LATEST_VERSION, + ), + ); + } else if (path.endsWith(`/${hostAsset}.gz`)) { + response.end(served.gz); + } else { + response.writeHead(404).end(); + } + }); + await new Promise((resolve) => + server.listen(0, "127.0.0.1", resolve), + ); + const { port } = server.address() as AddressInfo; + // Send every https:// URL to the local server. + writeFileSync( + join(binDir, "curl"), + `#!/usr/bin/env bash +args=() +for arg in "$@"; do + case "$arg" in https://*) arg="http://127.0.0.1:${port}/\${arg#https://}" ;; esac + args+=("$arg") +done +exec ${realCurl} "\${args[@]}" +`, + { mode: 0o755 }, + ); + try { + const child = spawn("bash", [installScript, "--no-modify-path"], { env }); + let output = ""; + child.stdout.on("data", (chunk) => (output += chunk)); + child.stderr.on("data", (chunk) => (output += chunk)); + const status = await new Promise((resolve) => child.on("close", resolve)); + expect(status, output).toBe(0); + } finally { + server.close(); + } + expect(registryRequests).toBe(3); + expect(existsSync(join(installDir, "sentry"))).toBe(true); + }, 30_000); + + test("downloads a corrupt archive again", () => { + env.SENTRY_TEST_GZ_CORRUPT = "1"; + const result = install(); + + expect(result.status, result.output).toBe(0); + expect(result.output).toContain("Downloading it again"); + expect( + recorded("curl-urls").filter((url) => url.endsWith(".gz")), + ).toHaveLength(2); + expect(existsSync(join(installDir, "sentry"))).toBe(true); + }); + + // CLI-BJ: a full temporary directory made gunzip hit the generic ERR trap. + test("unpacks into the install directory when the temp directory is full", () => { + // The first unpack fails like a full disk; later ones work. + writeFileSync( + join(binDir, "gunzip"), + `#!/usr/bin/env bash +if [[ "$1" == "-c" && ! -e "$SENTRY_TEST_DIR/unpacked-once" ]]; then + touch "$SENTRY_TEST_DIR/unpacked-once" + echo 'gzip: stdout: No space left on device' >&2 + exit 1 +fi +exec /usr/bin/gunzip "$@" +`, + { mode: 0o755 }, + ); + const result = install(); + + expect(result.status, result.output).toBe(0); + expect(result.output).not.toContain("Unexpected failure"); + // Staged under setup's own temp name, which setup renames into place. + expect(recorded("setup-binary")).toEqual([ + join(installDir, "sentry.download"), + ]); + expect(existsSync(join(installDir, "sentry"))).toBe(true); + expect(installerTempFiles()).toEqual([]); + }); + + test("downloads into the install directory when the temp directory is missing", () => { + env.TMPDIR = join(testDir, "deleted"); + const result = install(); + + expect(result.status, result.output).toBe(0); + expect(recorded("setup-binary")).toEqual([ + join(installDir, "sentry.download"), + ]); + expect(readdirSync(installDir)).toEqual(["sentry"]); + }); + + test("stages in a home bin directory listed in PATH with a trailing slash", () => { + const homeBin = join(testDir, "home", "bin"); + mkdirSync(homeBin); + delete env.SENTRY_INSTALL_DIR; + env.PATH = `${env.PATH}:${homeBin}/`; + env.TMPDIR = join(testDir, "deleted"); + const result = install(); + + expect(result.status, result.output).toBe(0); + // Setup's determineInstallDir picks the same directory, so it can rename + // the staged binary instead of copying it. + expect(recorded("setup-binary")).toEqual([ + join(homeBin, "sentry.download"), + ]); + }); + + test("names the full disk only when no directory has room", () => { + writeFileSync( + join(binDir, "gunzip"), + `#!/usr/bin/env bash +if [[ "$1" == "-c" ]]; then + echo 'gzip: stdout: No space left on device' >&2 + exit 1 +fi +exec /usr/bin/gunzip "$@" +`, + { mode: 0o755 }, + ); + const result = install(); + + expect(result.status).not.toBe(0); + expect(result.output).toContain("There is not enough room to stage sentry"); + expect(result.output).not.toContain("Unexpected failure"); + expect(recorded("setup-args")).toEqual([]); + expect(installerTempFiles()).toEqual([]); + expect(readdirSync(installDir)).toEqual([]); + }); + + describe("when the registry is down", () => { + beforeEach(() => { + env.SENTRY_TEST_REGISTRY_STATUS = "503"; + const page = join(testDir, "releases-1.json"); + writeFileSync( + page, + JSON.stringify([{ tag_name: "cli@0.46.0", prerelease: false }]), + ); + env.SENTRY_TEST_RELEASES_PAGE_1 = page; + }); + + test("authenticates to GitHub without exposing the token", () => { + env.GITHUB_TOKEN = "test-token-value"; + delete env.SENTRY_CLI_NO_TELEMETRY; + const result = install(); + + expect(result.status, result.output).toBe(0); + expect(recorded("github-headers")).toEqual([ + "Authorization: Bearer test-token-value", + ]); + expect(recorded("curl-args").join("\n")).not.toContain( + "test-token-value", + ); + expect(result.output).not.toContain("test-token-value"); + }); + + test("waits out a GitHub rate limit", () => { + env.SENTRY_TEST_GITHUB_RATE_LIMITED = "1"; + const result = install(); + + expect(result.status, result.output).toBe(0); + const firstPage = + "https://api.github.com/repos/getsentry/toolkit/releases?per_page=100&page=1"; + expect( + recorded("curl-urls").filter((url) => url === firstPage), + ).toHaveLength(2); + expect(existsSync(join(installDir, "sentry"))).toBe(true); + }); + }); + + // CLI-BJ: the trap reported only a line number, which bash 3.2 sets to the + // closing `fi` of the enclosing block. + test("reports the failing command of an unexpected failure without local paths", () => { + writeFileSync( + join(binDir, "chmod"), + "#!/bin/sh\necho 'chmod: Operation not permitted' >&2\nexit 1\n", + { mode: 0o755 }, + ); + delete env.SENTRY_CLI_NO_TELEMETRY; + const result = install(); + + const failure = '(exit 1): chmod +x "$tmp_binary"'; + expect(result.status).not.toBe(0); + expect(result.output).toMatch(/Unexpected failure at line \d+ \(exit 1\)/); + expect(result.output).toContain(failure); + // spawnSync returns only after the backgrounded report_error subshell + // exits, because it keeps the installer's stdout and stderr open. + const [, , event] = recorded("error-report"); + const report = JSON.parse(event ?? "{}"); + expect(report.message.formatted).toContain(failure); + expect(report.tags.step).toBe("trap"); + expect(event).not.toContain(testDir); + expect(recorded("setup-args")).toEqual([]); + }); + test("connects setup to the controlling terminal and cleans up after setup", () => { env.SENTRY_TEST_KEEP_TEMP_BINARY = "1"; const result = runInTerminal();