#!/bin/sh # The runner bootstrap. Nothing runs until the whole script has arrived: # the functions are only defined until main, on the last line. # own_home is this user's home from the user directory, never an # inherited HOME (su keeps the caller's). own_home() { user=$(id -un) h= if command -v dscl >/dev/null 2>&1; then h=$(dscl . -read "/Users/$user" NFSHomeDirectory 2>/dev/null | sed -n 's/^NFSHomeDirectory: //p') elif command -v getent >/dev/null 2>&1; then h=$(getent passwd "$user" | cut -d: -f6) fi [ -n "$h" ] && [ -d "$h" ] || return 1 printf '%s\n' "$h" } # make_scratch makes one private scratch directory without needing # TMPDIR: TMPDIR when it is a directory this user may write; the # platform's own temporary directory ($1, a Mac's per-user one); on a Mac # mktemp's default (not on Linux, where /tmp may be a shared RAM disk); # then a private folder in this user's own cache. make_scratch() { for root in "${TMPDIR:-}" "$1"; do if [ -n "$root" ] && [ -d "$root" ] && [ -w "$root" ]; then mktemp -d "$root/equipo-bootstrap-XXXXXXXX" && return 0 fi done if [ "$2" = darwin ]; then mktemp -d 2>/dev/null && return 0 fi home=$(own_home) || return 1 mkdir -p "$home/.cache/equipo-bootstrap" && chmod 700 "$home/.cache/equipo-bootstrap" && mktemp -d "$home/.cache/equipo-bootstrap/run-XXXXXXXX" } linux() { scratch=$(make_scratch "" linux) trap 'rm -rf "$scratch"' EXIT trap 'exit 1' HUP INT TERM curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/install-manifest.json" -o "$scratch/manifest.json" # Python parses data, not shell source. TLS authenticates the configured origin. digest=$(python3 - "$scratch/manifest.json" "$version" <<'PY' import json, re, sys m = json.load(open(sys.argv[1])) r = m['release'] assert m['version'] == sys.argv[2] and m['target'] == 'linux/amd64' # One build: the runner and tool proxy digests the release recorded. t = m['targets']['linux/amd64'] assert r['schema'] == 3 and r['runner_target'] == 'linux/amd64' assert re.fullmatch('[0-9a-f]{64}', m['runner_sha256']) and t['runner_sha256'] == m['runner_sha256'] assert re.fullmatch('[0-9a-f]{64}', t['tool_proxy_sha256']) assert [b['sha256'] for b in r['binaries'] if b['name'] == 'equipo-tool-proxy'] == [t['tool_proxy_sha256']] print(m['runner_sha256']) PY ) curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/equipo-runner" -o "$scratch/equipo-runner" printf '%s %s\n' "$digest" "$scratch/equipo-runner" | sha256sum --check --status chmod 700 "$scratch/equipo-runner" "$scratch/equipo-runner" verify-install-manifest "$scratch/manifest.json" >/dev/null # Prompts read the terminal because the bootstrap script may arrive on stdin. "$scratch/equipo-runner" install --release "$version" --origin "$origin" --instance "$instance" &2; exit 1; } export HOME scratch=$(make_scratch "$(getconf DARWIN_USER_TEMP_DIR 2>/dev/null || true)" darwin) trap 'rm -rf "$scratch"' EXIT trap 'exit 1' HUP INT TERM curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/install-manifest.json" -o "$scratch/manifest.json" [ "$(plutil -extract version raw -o - "$scratch/manifest.json")" = "$version" ] || exit 1 digest=$(plutil -extract 'targets.darwin/arm64.runner_sha256' raw -o - "$scratch/manifest.json") case "$digest" in *[!0-9a-f]*|'') exit 1 ;; esac [ "${#digest}" -eq 64 ] || exit 1 curl --fail --silent --show-error --proto '=https' --tlsv1.2 "$origin/releases/$version/darwin-arm64/equipo-runner" -o "$scratch/equipo-runner" printf '%s %s\n' "$digest" "$scratch/equipo-runner" | shasum -a 256 --check --status chmod 700 "$scratch/equipo-runner" "$scratch/equipo-runner" install --release "$version" --origin "$origin" --instance "$instance" } main() { set -eu umask 077 # su may leave a narrow PATH: the system's tools are always reachable. PATH="${PATH:-}${PATH:+:}/usr/bin:/bin:/usr/sbin:/sbin" export PATH case "$(uname -s)/$(uname -m)" in Linux/x86_64) platform=linux ;; Darwin/arm64) platform=darwin ;; *) exit 1 ;; esac origin=$1 version=$2 instance=$3 case "$origin" in https://*) ;; *) exit 1 ;; esac case "$version" in ''|*[!a-zA-Z0-9._-]*|latest|dev) exit 1 ;; esac "$platform" } main "$@"