#!/usr/bin/env bash # Proxframe installer (ADR-0032, ADR-0069). Paste in a Proxmox VE node's Shell: # curl -fsSL https://get.proxframe.app | bash # No key is needed. An older personal line still # works (an environment variable, so the key never shows in the process list): # curl -fsSL https://get.proxframe.app | PROXFRAME_KEY=github_pat_... bash # To update a hub without the in-app updater, or to remove it: # curl -fsSL https://get.proxframe.app | bash -s -- --update (or --remove) # From 0.7, setup's --update refuses a package older than the hub runs unless # --allow-older is added. That stops an old update line or an old run of this # installer, not a download service that offers an old release (its own setup # has no such check); the Cloudflare account serving this file could ship code # anyway (ADR-0069). # # It downloads the newest Proxframe release, checks # that the release's manifest carries our signature and that the package # matches it exactly, then runs the package's setup, which shows its plan # and asks before changing anything. This file is public; it contains no # product code. set -euo pipefail # Releases only, never the source code (ADR-0034). REPO="baki2001/proxframe-releases" API="https://api.github.com/repos/${REPO}" # Without a key the download service reads the same releases for us (ADR-0069). SERVICE="https://dl.proxframe.app/repos/${REPO}" # Where setup expects the package, the same place a manual upload goes. PACKAGE="/var/lib/vz/template/cache/proxframe-hub-test.tar.gz" WORK="" # The only key whose signature is accepted (keys/release-signing.pub.pem; # crates/updater/src/signature.rs trusts the same one). VERIFY_KEY="-----BEGIN PUBLIC KEY----- MCowBQYDK2VwAyEAUoJoygH0kR/fF2/mgtcWFl5SO9UBPM1PXhDZLkw9sbo= -----END PUBLIC KEY-----" # Where a person reports a failure. SUPPORT="help@proxframe.app" # Output: one voice, six line types. Colour only in a terminal, only on the # label or step; the words carry the meaning without it. c() { [[ -t ${1} && -z "${NO_COLOR:-}" && "${TERM:-dumb}" != dumb ]]; } # c 1 = stdout, c 2 = stderr if c 1; then B=$'\e[1m' D=$'\e[2m' N=$'\e[0m'; else B= D= N=; fi if c 2; then ER=$'\e[31m' EN=$'\e[0m'; else ER= EN=; fi title() { printf '\n%s%s%s\n' "$B" "$*" "$N"; } step() { printf ' %s%s%s\n' "$B" "$*" "$N"; } detail() { printf ' %s%s%s\n' "$D" "$*" "$N"; } # die WHAT [NEXT]: nothing has been changed anywhere in this script. die() { printf '\n%sProblem:%s %s\n' "$ER" "$EN" "$1" >&2 [[ -z "${2:-}" ]] || printf ' %s\n' "$2" >&2 printf ' Nothing was changed.\n' >&2 exit 1 } # A release that fails a check is never used; the same way forward for all. die_unsafe() { die "$1" "It wasn't installed. If this keeps happening, report it to ${SUPPORT}."; } cleanup() { if [[ -n "$WORK" ]]; then rm -rf "$WORK"; fi; } # holds the key # curl with the key (if one was given) read from a file, so it is never on a # command line. Only https; on a redirect to GitHub's file host curl drops it. github() { local auth=() [[ -s "$WORK/auth" ]] && auth=(-H @"$WORK/auth") curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \ --max-time 600 "${auth[@]}" -H "X-GitHub-Api-Version: 2022-11-28" \ -H "User-Agent: proxframe-installer" "$@" } # No key is needed (ADR-0069). A key from an older personal line is still # used, and then talks to GitHub directly. save_key() { local key="${1//[[:space:]]/}" [[ -z "$key" ]] && return 0 [[ "$key" =~ ^[A-Za-z0-9_]{1,255}$ ]] \ || die "That doesn't look like a download key (those start with github_pat_)." "Run the line without it; no key is needed." (umask 077 printf '%s\n' "$key" >"$WORK/key" printf 'Authorization: Bearer %s\n' "$key" >"$WORK/auth") } # Reads one asset's id and size from the release JSON: " ". asset() { # asset < release.json perl -MJSON::PP -e ' my $r = decode_json(do { local $/; }); for my $a (@{ $r->{assets} // [] }) { if ($a->{name} eq $ARGV[0]) { print "$a->{id} $a->{size}\n"; exit 0 } } exit 1' "$1" } field() { # field < release.txt; a key given twice counts as missing awk -F= -v k="$1" '$1 == k { n++; sub(/^[^=]*=/, ""); v = $0 } END { if (n != 1) exit 1; print v }' } # " " for one asset, both plain numbers; nothing else reaches the shell. asset_of() { # asset_of ; sets id and size read -r id size < <(asset "$1" <"$WORK/release.json") \ && [[ "$id" =~ ^[0-9]{1,20}$ && "$size" =~ ^[0-9]{1,15}$ ]] } main() { local given="${PROXFRAME_KEY:-}" unset PROXFRAME_KEY PROXFRAME_KEY_FILE # everything after sees the key only as a file [[ "$(id -u)" == 0 ]] || die "This needs to run as root." "Use the node's Shell in the Proxmox web UI; it signs you in as root." command -v pveversion >/dev/null && command -v pct >/dev/null \ || die "This isn't a Proxmox VE node." "Run it in a node's Shell in the Proxmox web UI." [[ "$(dpkg --print-architecture)" == amd64 ]] \ || die "Proxframe runs only on Intel/AMD (amd64) nodes for now." # Checking our signature needs OpenSSL 3 (Proxmox VE 8 and newer). [[ "$(openssl version 2>/dev/null)" =~ ^OpenSSL\ ([3-9]|[1-9][0-9]) ]] \ || die "This needs Proxmox VE 8 or newer." "Upgrade the node, then run this again." WORK="$(mktemp -d)" chmod 700 "$WORK" trap cleanup EXIT save_key "$given" [[ -s "$WORK/auth" ]] || API="$SERVICE" title "Proxframe installer" step "Finding the newest release" local status status="$(github -o "$WORK/release.json" -w '%{http_code}' \ -H "Accept: application/vnd.github+json" "${API}/releases/latest" 2>/dev/null)" || true case "$status" in 200) ;; 401) die "The download key wasn't accepted; it may have expired or been mistyped." "Run the line again without it; no key is needed." ;; 429) die "The download server is busy." "Try again in a few minutes." ;; 403|404) die "The downloads aren't reachable from here." "If you used a key, run the line without it. Otherwise try again later." ;; *) die "Couldn't reach the download server (HTTP status: ${status:-none})." "Check this node's internet connection and try again." ;; esac local tag tag="$(perl -MJSON::PP -e 'my $r = decode_json(do { local $/; }); print $r->{tag_name} // ""' <"$WORK/release.json")" \ || die "Couldn't read the download server's answer." "Try again later." # Checked before it is shown or used anywhere. [[ "$tag" =~ ^v[0-9]{1,9}\.[0-9]{1,9}\.[0-9]{1,9}$ ]] \ || die_unsafe "The newest release has an unexpected name." local id size asset_of release.txt \ || die_unsafe "The newest release (${tag}) has no release.txt." [[ "$size" -le 16384 ]] || die_unsafe "The newest release's release.txt is too big." github --max-filesize 16384 -H "Accept: application/octet-stream" -o "$WORK/release.txt" "${API}/releases/assets/${id}" \ || die "The release's manifest couldn't be downloaded." "Try again." # Our signature over the exact bytes checked next (ADR-0034). asset_of release.txt.sig && [[ "$size" == 64 ]] \ || die_unsafe "The newest release (${tag}) isn't signed." github --max-filesize 64 -H "Accept: application/octet-stream" -o "$WORK/release.txt.sig" "${API}/releases/assets/${id}" \ || die "The release's signature couldn't be downloaded." "Try again." printf '%s\n' "$VERIFY_KEY" >"$WORK/signing.pem" openssl pkeyutl -verify -pubin -inkey "$WORK/signing.pem" -rawin \ -in "$WORK/release.txt" -sigfile "$WORK/release.txt.sig" >/dev/null 2>&1 \ || die_unsafe "The newest release's signature doesn't match, so it may not come from Proxframe." # The checks the hub's updater makes on what it installs (crates/updater, # manifest.rs), except min_updater_version: setup brings its own updater. local version name want_size want_sum ! LC_ALL=C grep -q '[[:cntrl:]]' "$WORK/release.txt" \ || die_unsafe "release.txt is malformed." [[ "$(field product <"$WORK/release.txt")" == stillfield-hub ]] || die_unsafe "The newest release isn't a Proxframe hub." [[ "$(field arch <"$WORK/release.txt")" == amd64 ]] || die_unsafe "The newest release isn't for Intel/AMD." version="$(field version <"$WORK/release.txt")" || die_unsafe "release.txt has no version." [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ && "v${version}" == "$tag" ]] \ || die_unsafe "release.txt says another version than the release (${tag})." name="$(field package_name <"$WORK/release.txt")" || die_unsafe "release.txt names no package." [[ "$name" =~ ^[A-Za-z0-9_-][A-Za-z0-9._-]{0,99}$ ]] || die_unsafe "release.txt names an odd package." want_size="$(field package_size <"$WORK/release.txt")" || die_unsafe "release.txt has no package size." want_sum="$(field package_sha256 <"$WORK/release.txt" | tr 'A-F' 'a-f')" || die_unsafe "release.txt has no checksum." [[ "$want_size" =~ ^[1-9][0-9]{0,14}$ && "$want_sum" =~ ^[0-9a-f]{64}$ ]] || die_unsafe "release.txt is malformed." asset_of "$name" \ || die_unsafe "The newest release has no ${name}." [[ "$size" == "$want_size" ]] || die_unsafe "${name} isn't the size release.txt says." step "Downloading Proxframe ${version}" github --max-filesize "$want_size" -H "Accept: application/octet-stream" -o "$WORK/package.tar.gz" "${API}/releases/assets/${id}" \ || die "The download didn't finish." "Try again." [[ "$(stat -c %s "$WORK/package.tar.gz")" == "$want_size" ]] \ || die "The download isn't the size it should be." "Try again." [[ "$(sha256sum "$WORK/package.tar.gz" | cut -d' ' -f1)" == "$want_sum" ]] \ || die_unsafe "The download's SHA-256 doesn't match release.txt, so it was not used." detail "Signature and SHA-256 match: ${want_sum}" tar -xzOf "$WORK/package.tar.gz" setup.sh >"$WORK/setup.sh" \ || die_unsafe "The package has no setup.sh." [[ -s "$WORK/key" ]] && export PROXFRAME_KEY_FILE="$WORK/key" # Kept where a manual upload goes, so setup's remove and update lines work # (also when setup is interrupted). install -D -m 644 "$WORK/package.tar.gz" "$PACKAGE" if grep -qF PROXFRAME_KEPT_AT "$WORK/setup.sh"; then # Setup unpacks the very bytes checked above, in this root-only folder, # not the copy in Proxmox's template folder that others may be allowed to # replace (supply-chain-02). It names the kept copy in what it prints. PROXFRAME_KEPT_AT="$PACKAGE" bash "$WORK/setup.sh" --package "$WORK/package.tar.gz" "$@"