#!/bin/sh # Installer of the Repsy CLI (RPS-1919): installs `rp` and `docker-credential-repsy` from cli.repsy.io. # # curl --proto '=https' --tlsv1.2 -fsSL https://cli.repsy.io/install.sh | sh # curl --proto '=https' --tlsv1.2 -fsSL https://cli.repsy.io/install.sh | sh -s -- --version 1.2.3 --dir ~/bin # # Options (arguments win over the environment): # --version X install this version (RP_VERSION); default is the one in https://cli.repsy.io/latest.json # --dir DIR install into DIR (RP_INSTALL_DIR); default ~/.local/bin, /usr/local/bin for root # --force overwrite an existing rp # --require-signature fail when the signature of the checksums cannot be verified (cosign missing) # --help # # Exit codes: 0 success, 1 general failure, 2 usage error, 3 existing installation, 4 verification failure. # # What is verified: the SHA-256 of the archive against checksums.txt always, and the cosign signature of # checksums.txt when cosign is installed. The script itself and the archive come from the same server over # HTTPS, so on this path the root of trust is TLS; the signature protects the checksums, not the script. # # The whole script is one function that runs on the last line: a download cut short by `curl | sh` is a # syntax error and runs nothing. POSIX sh only; no bashisms, no jq. set -eu BASE_URL_DEFAULT="https://cli.repsy.io" CERT_ISSUER="https://token.actions.githubusercontent.com" CERT_IDENTITY_PREFIX="https://github.com/repsyio/repsy-cli/.github/workflows/release.yml@refs/tags/v" WORK_DIR="" INSTALL_DIR="" die() { code="$1" shift printf 'rp install: %s\n' "$*" >&2 exit "$code" } warn() { printf 'rp install: warning: %s\n' "$*" >&2 } info() { printf '%s\n' "$*" } cleanup() { if [ -n "$WORK_DIR" ] && [ -d "$WORK_DIR" ]; then rm -rf "$WORK_DIR" fi if [ -n "$INSTALL_DIR" ]; then rm -f "$INSTALL_DIR/.rp.new.$$" "$INSTALL_DIR/.docker-credential-repsy.new.$$" fi } usage() { cat <<'EOF' Usage: install.sh [--version X.Y.Z] [--dir DIR] [--force] [--require-signature] --version X.Y.Z install this version instead of the latest (env RP_VERSION) --dir DIR install into DIR (env RP_INSTALL_DIR); default ~/.local/bin, /usr/local/bin for root --force overwrite an existing rp --require-signature fail unless the signature of the checksums is verified (needs cosign) --help show this text EOF } # A base URL other than the default is for tests: https, or http on the loopback only. check_base_url() { case "$1" in https://?*) ;; http://127.0.0.1 | http://127.0.0.1:* | http://127.0.0.1/* | http://localhost | http://localhost:* | http://localhost/* | "http://[::1]" | "http://[::1]:"* | "http://[::1]/"*) ;; *) die 2 "RP_BASE_URL must be https, or http on the loopback (got '$1')" ;; esac } # fetch URL FILE: HTTPS only, TLS 1.2 or newer, redirects stay on HTTPS. Loopback http is allowed in test mode. # It reports a failure on stderr and returns 1; the caller decides the exit code. fetch() { fetch_url="$1" fetch_out="$2" if command -v curl >/dev/null 2>&1; then fetch_proto="=https" if [ -n "${RP_BASE_URL:-}" ]; then fetch_proto="=http,https" fi if ! curl --proto "$fetch_proto" --proto-redir "=https" --tlsv1.2 --fail --silent --show-error --location \ --output "$fetch_out" "$fetch_url"; then rm -f "$fetch_out" warn "download failed: $fetch_url" return 1 fi elif command -v wget >/dev/null 2>&1; then fetch_flags="" if [ -z "${RP_BASE_URL:-}" ]; then if ! wget --help 2>&1 | grep -q -- '--https-only'; then warn "this wget cannot restrict downloads to HTTPS (BusyBox?); install curl" return 1 fi fetch_flags="--https-only" fi # shellcheck disable=SC2086 # fetch_flags is empty or one flag if ! wget --quiet $fetch_flags --output-document "$fetch_out" "$fetch_url"; then rm -f "$fetch_out" warn "download failed: $fetch_url" return 1 fi else warn "curl or wget is required" return 1 fi } detect_platform() { case "$(uname -s)" in Linux) PLATFORM_OS="linux" ;; Darwin) PLATFORM_OS="darwin" ;; MINGW* | MSYS* | CYGWIN*) die 1 "this is a Windows shell; use install.ps1 from PowerShell instead" ;; *) die 1 "unsupported operating system: $(uname -s)" ;; esac case "$(uname -m)" in x86_64 | amd64) PLATFORM_ARCH="amd64" ;; aarch64 | arm64) PLATFORM_ARCH="arm64" ;; *) die 1 "unsupported architecture: $(uname -m)" ;; esac # A shell started under Rosetta reports x86_64 on an Apple Silicon machine; the native binary is wanted. if [ "$PLATFORM_OS" = "darwin" ] && [ "$PLATFORM_ARCH" = "amd64" ]; then if [ "$(sysctl -n hw.optional.arm64 2>/dev/null || true)" = "1" ]; then PLATFORM_ARCH="arm64" fi fi } # valid_version V: X.Y.Z with an optional prerelease, no leading v. valid_version() { printf '%s' "$1" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$' } # json_first_field FILE NAME: the first string or number value of "NAME" in a JSON file, read with sed. # One member per line is made first, so a pretty printed or a minified file reads the same. json_first_field() { tr ',{' '\n ' <"$1" | sed -n 's/^[[:space:]]*"'"$2"'"[[:space:]]*:[[:space:]]*"\{0,1\}\([^"}[:space:]]*\)"\{0,1\}.*$/\1/p' | head -n 1 } resolve_version() { if [ -n "$VERSION" ]; then VERSION="${VERSION#v}" valid_version "$VERSION" || die 2 "'$VERSION' is not a version like 1.2.3" return fi fetch "$BASE_URL/latest.json" "$WORK_DIR/latest.json" || exit 1 # A file cut short must not be read as far as it goes: it has to end in the closing brace. if [ "$(tail -c 64 "$WORK_DIR/latest.json" | tr -d ' \n\r\t' | tail -c 1)" != "}" ]; then die 1 "latest.json is incomplete (it does not end with '}'); try again" fi schema="$(json_first_field "$WORK_DIR/latest.json" schemaVersion)" [ "$schema" = "1" ] || die 1 "latest.json has schemaVersion '${schema:-none}', this installer reads 1; download a newer install.sh" VERSION="$(json_first_field "$WORK_DIR/latest.json" version)" [ -n "$VERSION" ] || die 1 "latest.json has no version" valid_version "$VERSION" || die 1 "latest.json has an invalid version '$VERSION'" } # describe_owner PATH: a hint how the file at PATH was installed. describe_owner() { case "$1" in */Cellar/* | /opt/homebrew/* | /home/linuxbrew/*) printf 'Homebrew (brew upgrade repsy-cli)' ;; *) if command -v dpkg >/dev/null 2>&1 && dpkg -S "$1" >/dev/null 2>&1; then printf 'the dpkg package %s' "$(dpkg -S "$1" 2>/dev/null | head -n 1 | cut -d: -f1)" elif command -v rpm >/dev/null 2>&1 && rpm -qf "$1" >/dev/null 2>&1; then printf 'the rpm package %s' "$(rpm -qf "$1" 2>/dev/null | head -n 1)" else printf 'unknown' fi ;; esac } check_existing() { existing="$(command -v rp 2>/dev/null || true)" case "$existing" in /*) ;; *) existing="" ;; esac if [ -z "$existing" ]; then for candidate in "$INSTALL_DIR/rp" "$INSTALL_DIR/docker-credential-repsy"; do if [ -e "$candidate" ] || [ -L "$candidate" ]; then existing="$candidate" break fi done fi [ -n "$existing" ] || return 0 if [ "$FORCE" = "1" ]; then warn "overwriting $existing" return 0 fi { printf 'rp install: rp is already installed: %s\n' "$existing" printf ' installed by: %s\n' "$(describe_owner "$existing")" printf ' Nothing was changed. To replace it with %s, run again with --force.\n' "$VERSION" } >&2 exit 3 } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 "$1" | sed 's/^.*= *//' else die 1 "sha256sum, shasum or openssl is required to verify the download" fi } # verify_signature: cosign verifies the bundle of checksums.txt against the identity of the release workflow # of this very version. The identity is stated here, never read from the server. verify_signature() { SIGNATURE_STATE="not verified" if ! command -v cosign >/dev/null 2>&1; then if [ "$REQUIRE_SIGNATURE" = "1" ]; then die 4 "--require-signature was given but cosign is not installed (https://docs.sigstore.dev/cosign/system_config/installation/)" fi warn "cosign is not installed: the signature of checksums.txt was NOT verified." warn "Only the SHA-256 checksum is checked. That catches a damaged download, not a tampered server." warn "Install cosign and run again, or pass --require-signature to make this an error." return 0 fi fetch "$BASE_URL/releases/$VERSION/$SUMS_NAME.sigstore.json" "$WORK_DIR/$SUMS_NAME.sigstore.json" || die 4 "the signature of checksums.txt could not be downloaded; nothing was installed" expected_identity="$CERT_IDENTITY_PREFIX$VERSION" # RP_COSIGN_PUBLIC_KEY lets the tests sign with a throwaway key; it is ignored outside test mode. if [ -n "${RP_BASE_URL:-}" ] && [ -n "${RP_COSIGN_PUBLIC_KEY:-}" ]; then set -- --key "$RP_COSIGN_PUBLIC_KEY" --insecure-ignore-tlog else set -- --certificate-identity "$expected_identity" --certificate-oidc-issuer "$CERT_ISSUER" fi if ! cosign verify-blob --bundle "$WORK_DIR/$SUMS_NAME.sigstore.json" "$@" "$WORK_DIR/$SUMS_NAME" >"$WORK_DIR/cosign.out" 2>&1; then cat "$WORK_DIR/cosign.out" >&2 die 4 "the signature of checksums.txt is not valid for $expected_identity; nothing was installed" fi if [ -n "${RP_BASE_URL:-}" ] && [ -n "${RP_COSIGN_PUBLIC_KEY:-}" ]; then SIGNATURE_STATE="verified (cosign, test key $RP_COSIGN_PUBLIC_KEY)" else SIGNATURE_STATE="verified (cosign, $expected_identity)" fi } verify_checksum() { matches="$(awk -v n="$ARCHIVE" '($2 == n || $2 == "*" n) { print $1 }' "$WORK_DIR/$SUMS_NAME")" count="$(printf '%s\n' "$matches" | grep -c . || true)" [ "$count" = "1" ] || die 4 "checksums.txt has $count lines for $ARCHIVE, expected exactly 1; nothing was installed" printf '%s' "$matches" | grep -Eq '^[0-9a-fA-F]{64}$' || die 4 "checksums.txt has a malformed checksum for $ARCHIVE; nothing was installed" wanted="$(printf '%s' "$matches" | tr 'A-F' 'a-f')" actual="$(sha256_of "$WORK_DIR/$ARCHIVE" | tr 'A-F' 'a-f')" [ "$wanted" = "$actual" ] || die 4 "the SHA-256 of $ARCHIVE does not match checksums.txt (expected $wanted, got $actual); nothing was installed" } # extract_binaries: only rp and docker-credential-repsy, after the archive listing is checked. extract_binaries() { mkdir "$WORK_DIR/extract" tar -tzf "$WORK_DIR/$ARCHIVE" >"$WORK_DIR/listing" 2>"$WORK_DIR/tar.err" || die 1 "$ARCHIVE is not a readable archive" # BusyBox tar lists such a name already stripped and only says so on stderr. if grep -qi 'removing leading' "$WORK_DIR/tar.err"; then die 4 "$ARCHIVE has an unsafe path ($(head -n 1 "$WORK_DIR/tar.err")); nothing was installed" fi while IFS= read -r entry; do case "$entry" in /* | ../* | */../* | .. | */..) die 4 "$ARCHIVE has an unsafe path '$entry'; nothing was installed" ;; esac done <"$WORK_DIR/listing" tar -xzf "$WORK_DIR/$ARCHIVE" -C "$WORK_DIR/extract" rp docker-credential-repsy || die 1 "$ARCHIVE does not contain rp and docker-credential-repsy" for bin in rp docker-credential-repsy; do if [ -L "$WORK_DIR/extract/$bin" ] || [ ! -f "$WORK_DIR/extract/$bin" ]; then die 4 "$bin in $ARCHIVE is not a regular file; nothing was installed" fi done } # install_binaries: copy next to the target under a temporary name, then rename, so a reader never sees half a file. install_binaries() { for bin in rp docker-credential-repsy; do staged="$INSTALL_DIR/.$bin.new.$$" cp "$WORK_DIR/extract/$bin" "$staged" || die 1 "cannot write to $INSTALL_DIR" chmod 0755 "$staged" mv -f "$staged" "$INSTALL_DIR/$bin" || die 1 "cannot replace $INSTALL_DIR/$bin" done } check_installed() { reported="$("$INSTALL_DIR/rp" version --output json 2>/dev/null | json_first_field /dev/stdin version || true)" [ "$reported" = "$VERSION" ] || die 1 "$INSTALL_DIR/rp reports version '${reported:-nothing}', expected $VERSION; the files were left in place" found="$(command -v rp 2>/dev/null || true)" if [ -z "$found" ]; then PATH_NOTE="$INSTALL_DIR is not on your PATH. Add it for this shell with: export PATH=\"$INSTALL_DIR:\$PATH\" and put the same line in your shell profile (~/.profile, ~/.bashrc or ~/.zshrc) to keep it. This script does not edit them." elif [ "$found" != "$INSTALL_DIR/rp" ]; then PATH_NOTE="warning: 'rp' on your PATH is $found, not the one just installed; another directory comes first in PATH." else PATH_NOTE="" fi } main() { VERSION="${RP_VERSION:-}" INSTALL_DIR="${RP_INSTALL_DIR:-}" FORCE=0 REQUIRE_SIGNATURE=0 while [ $# -gt 0 ]; do case "$1" in --version) [ $# -ge 2 ] || die 2 "--version needs a value"; VERSION="$2"; shift 2 ;; --version=*) VERSION="${1#--version=}"; shift ;; --dir) [ $# -ge 2 ] || die 2 "--dir needs a value"; INSTALL_DIR="$2"; shift 2 ;; --dir=*) INSTALL_DIR="${1#--dir=}"; shift ;; --force) FORCE=1; shift ;; --require-signature) REQUIRE_SIGNATURE=1; shift ;; -h | --help) usage; exit 0 ;; *) usage >&2; die 2 "unknown argument '$1'" ;; esac done BASE_URL="${RP_BASE_URL:-$BASE_URL_DEFAULT}" BASE_URL="${BASE_URL%/}" check_base_url "$BASE_URL" trap cleanup EXIT trap 'exit 1' HUP INT TERM WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/rp-install.XXXXXX")" || die 1 "cannot create a temporary directory" detect_platform resolve_version if [ -z "$INSTALL_DIR" ]; then if [ "$(id -u)" = "0" ]; then INSTALL_DIR="/usr/local/bin" else [ -n "${HOME:-}" ] || die 2 "HOME is not set; pass --dir" INSTALL_DIR="$HOME/.local/bin" fi fi INSTALL_DIR="${INSTALL_DIR%/}" check_existing ARCHIVE="repsy-cli_${VERSION}_${PLATFORM_OS}_${PLATFORM_ARCH}.tar.gz" SUMS_NAME="repsy-cli_${VERSION}_checksums.txt" info "Installing rp $VERSION (${PLATFORM_OS}/${PLATFORM_ARCH}) into $INSTALL_DIR" fetch "$BASE_URL/releases/$VERSION/$ARCHIVE" "$WORK_DIR/$ARCHIVE" || exit 1 fetch "$BASE_URL/releases/$VERSION/$SUMS_NAME" "$WORK_DIR/$SUMS_NAME" || exit 1 verify_signature verify_checksum extract_binaries mkdir -p "$INSTALL_DIR" || die 1 "cannot create $INSTALL_DIR" [ -w "$INSTALL_DIR" ] || die 1 "$INSTALL_DIR is not writable; choose another directory with --dir" install_binaries check_installed info "" info "rp $VERSION is installed in $INSTALL_DIR (rp, docker-credential-repsy)." info " checksum: verified (sha256, $ARCHIVE)" info " signature: $SIGNATURE_STATE" if [ -n "$PATH_NOTE" ]; then info "" info "$PATH_NOTE" fi } main "$@"