#!/bin/sh # Trident HTTP Cache Proxy - Universal Installer # # Usage: # curl -sSL https://get.trident-cache.com/install.sh | sh # curl -sSL https://get.trident-cache.com/install.sh | sh -s -- --prefix /opt/trident # # Options: # --prefix DIR Installation prefix (default: /usr/local) # --version VER Install specific version (default: latest) # --with-service Install systemd service and default config # --skip-checksum Skip SHA-256 checksum verification (air-gapped installs only) # --help Show this help # # Supply-chain verification: # By default the installer downloads the release `checksums.txt` first, uses # each validated digest in its download URL, and verifies the downloaded bytes # before installing, aborting on any # mismatch. # # What that does and does not prove: `checksums.txt` comes from the same host # as the archive, so the check catches a corrupt, truncated or stale-cached # download, not a compromised host. The installer also verifies a keyless # cosign signature when `cosign` (2.4 or later) is installed AND the release # publishes `.sigstore.json`, the keyless bundle its release run # signs (from 1.8.2). Releases without one report "No # cosign signature published" and the SHA-256 check is the one that runs. # # To verify by hand instead of piping to sh, use release coordinate v1.8.0-r3 # for engine version 1.8.0; other GA versions use vX.Y.Z: # curl -fsSLO https://pkg.trident-cache.com/releases//trident-linux-x64.tar.gz # curl -fsSLO https://pkg.trident-cache.com/releases//checksums.txt # artifact=trident-linux-x64.tar.gz # awk -v file="$artifact" ' # NF == 2 { # name=$2; sub(/^\*/, "", name); sub(/^.*\//, "", name) # if (name == file) { digest=$1; count++ } # } # END { # if (count != 1 || length(digest) != 64 || digest ~ /[^0-9a-f]/) exit 1 # print digest " " file # } # ' checksums.txt > archive.sha256 && # if command -v sha256sum >/dev/null 2>&1; then # sha256sum -c archive.sha256 # else # shasum -a 256 -c archive.sha256 # fi set -e REPO="Trident-Cache/trident-cache" RELEASE_ROOT="https://pkg.trident-cache.com/releases" PREFIX="/usr/local" VERSION="" INSTALL_SERVICE=false SKIP_CHECKSUM=false # The only signer a cosign-verified artifact may come from: a workflow of this # repository, run from a release tag or from main. Anchored at both ends; # matched unanchored, the repository URL could appear anywhere in another # signer's identity. COSIGN_IDENTITY='^https://github\.com/Trident-Cache/trident-cache/\.github/workflows/[A-Za-z0-9._-]+\.ya?ml@refs/(tags/v[0-9][A-Za-z0-9.+-]*|heads/main)$' # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- info() { printf "\033[1;34m==>\033[0m %s\n" "$1" } error() { printf "\033[1;31mError:\033[0m %s\n" "$1" >&2 exit 1 } warn() { printf "\033[1;33mWarning:\033[0m %s\n" "$1" >&2 } need_cmd() { if ! command -v "$1" >/dev/null 2>&1; then error "Required command not found: $1" fi } # --------------------------------------------------------------------------- # Detect platform # --------------------------------------------------------------------------- detect_os() { case "$(uname -s)" in Linux*) echo "linux" ;; Darwin*) echo "macos" ;; FreeBSD*) echo "freebsd" ;; MINGW*|MSYS*|CYGWIN*) echo "windows" ;; *) error "Unsupported operating system: $(uname -s)" ;; esac } detect_arch() { case "$(uname -m)" in x86_64|amd64) echo "x64" ;; aarch64|arm64) echo "arm64" ;; *) error "Unsupported architecture: $(uname -m)" ;; esac } # --------------------------------------------------------------------------- # Fetch the last fully verified public GA version, without GitHub credentials. # --------------------------------------------------------------------------- get_latest_version() { if command -v curl >/dev/null 2>&1; then curl -fsSL "${RELEASE_ROOT}/latest-version" elif command -v wget >/dev/null 2>&1; then wget -qO- "${RELEASE_ROOT}/latest-version" else error "Neither curl nor wget found. Please install one of them." fi } # --------------------------------------------------------------------------- # Download file # --------------------------------------------------------------------------- # # Named so that it is not a prefix of any function defined after it: a copy of # this script cut off in the middle of a later `name() {` line would otherwise # call the shorter-named function instead of defining the longer one. fetch_file() { url="$1" dest="$2" if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$dest" "$url" elif command -v wget >/dev/null 2>&1; then wget -qO "$dest" "$url" else error "Neither curl nor wget found." fi } # Best-effort download that does NOT abort the script on a 404/network error. # Returns non-zero so the caller can decide whether the missing file is fatal. try_download() { url="$1" dest="$2" if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$dest" "$url" 2>/dev/null elif command -v wget >/dev/null 2>&1; then wget -qO "$dest" "$url" 2>/dev/null else return 1 fi } # Compute the SHA-256 of a file, portable across sha256sum (Linux) and # `shasum -a 256` (macOS). Prints the hex digest, or returns non-zero if no # tool is available. compute_sha256() { file="$1" if command -v sha256sum >/dev/null 2>&1; then sha256sum "$file" | awk '{print $1}' elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$file" | awk '{print $1}' else return 1 fi } # --------------------------------------------------------------------------- # Supply-chain verification # --------------------------------------------------------------------------- # # Read one exact, unique SHA-256 entry from the release manifest. Fetch it once # for the archive and all versioned templates. A digest query keeps a changed # same-version artifact separate from a cached canonical URL; byte verification # remains mandatory even if a cache ignores that query. artifact_checksum() ( artifact_file="$1" base_url="$2" checksums="${TMPDIR}/checksums.txt" if [ ! -f "$checksums" ] && ! try_download "${base_url}/checksums.txt" "$checksums"; then error "Could not download checksums.txt from ${base_url}. The release artifact \ cannot be integrity-verified. Re-run with --skip-checksum ONLY if you trust the source \ (e.g. an air-gapped mirror you control)." fi awk -v file="$artifact_file" ' NF == 2 { name=$2; sub(/^\*/, "", name); sub(/^\.\//, "", name) if (name == file) { digest=$1; count++ } } END { if (count != 1 || length(digest) != 64 || digest ~ /[^0-9a-f]/) exit 1 print digest } ' "$checksums" || error "Missing, duplicate or malformed checksum for ${artifact_file} — refusing to install." ) # Download only after validating the expected checksum, then check the actual # bytes before the caller can extract or install them. Subshells keep helper # variables isolated on POSIX shells, which have no portable local keyword. # Args: $1 = artifact filename, $2 = destination, $3 = release base URL. download_artifact() ( artifact_file="$1" archive="$2" base_url="$3" artifact_url="${base_url}/${artifact_file}" if [ "$SKIP_CHECKSUM" != true ]; then expected="$(artifact_checksum "$artifact_file" "$base_url")" || exit 1 artifact_url="${artifact_url}?sha256=${expected}" fi info "Downloading ${artifact_url}" fetch_file "$artifact_url" "$archive" verify_artifact "$archive" "$artifact_file" "$base_url" ) # Verify the downloaded artifact against the release `checksums.txt`. Aborts on # mismatch. When cosign is present and a signature/certificate pair is # published for the artifact, the keyless cosign signature is verified too — # but the SHA-256 check is mandatory regardless of cosign availability (unless # the operator explicitly passed --skip-checksum). # # Args: $1 = path to downloaded archive, $2 = artifact base filename, # $3 = release download base URL. verify_artifact() ( archive="$1" artifact_file="$2" base_url="$3" if [ "$SKIP_CHECKSUM" = true ]; then warn "Skipping checksum verification (--skip-checksum). The downloaded" warn "binary is NOT integrity-checked. Use this only for trusted/air-gapped sources." return 0 fi info "Verifying SHA-256 checksum..." expected="$(artifact_checksum "$artifact_file" "$base_url")" || exit 1 actual="$(compute_sha256 "$archive")" || error "No sha256sum/shasum tool available to \ verify the download. Install coreutils (sha256sum) or pass --skip-checksum at your own risk." if [ "$expected" != "$actual" ]; then error "checksum mismatch for ${artifact_file}: expected ${expected}, got ${actual}. \ The download may be corrupt or tampered with. Aborting." fi info " Checksum OK (${actual})" # Optional cosign verification (keyless / Rekor). Runs when cosign is # installed and the release publishes .sigstore.json: the bundle # its release run signs with GitHub's OIDC identity (review I11), which # must name a workflow of this repository on a release tag or main # (COSIGN_IDENTITY). A bundle that does not verify aborts the install. A # cosign before 2.4 cannot read the bundle format; it is told so and the # SHA-256 check stands. "Too old" is decided from `cosign version`, never # from an error message a hostile bundle could shape. Releases before the # bundles publish none. if command -v cosign >/dev/null 2>&1; then bundle="${TMPDIR}/${artifact_file}.sigstore.json" cosign_version="$(cosign version 2>/dev/null | sed -n 's/^GitVersion:[[:space:]]*v\{0,1\}\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -n 1)" cosign_major="${cosign_version%%.*}" cosign_minor="${cosign_version#*.}" if ! try_download "${base_url}/${artifact_file}.sigstore.json" "$bundle"; then warn "No cosign signature published for ${artifact_file}; relying on SHA-256 only." elif [ -z "$cosign_version" ] || [ "$cosign_major" -lt 2 ] \ || { [ "$cosign_major" -eq 2 ] && [ "$cosign_minor" -lt 4 ]; }; then warn "cosign ${cosign_version:-of unknown version} cannot read the release's signature bundle (2.4 or later can); relying on SHA-256 only." else info "Verifying cosign signature..." if cosign verify-blob --new-bundle-format --bundle "$bundle" \ --certificate-identity-regexp "$COSIGN_IDENTITY" \ --certificate-oidc-issuer 'https://token.actions.githubusercontent.com' \ "$archive" >/dev/null 2>&1; then info " cosign signature OK" else error "cosign signature verification FAILED for ${artifact_file}. Aborting." fi fi else warn "cosign not installed; skipping signature verification (SHA-256 was verified)." fi ) # --------------------------------------------------------------------------- # Everything the installer DOES lives in main(). Above this point the script # only sets defaults and defines functions, and main() is called once, on the # last line of the file. A download cut short while piping into `sh` therefore # cannot run half of a command: a shell runs a function only after it has read # the whole definition, so a truncated copy ends in a syntax error or in # definitions that are never called. # --------------------------------------------------------------------------- main() { # --------------------------------------------------------------------------- # Parse arguments # --------------------------------------------------------------------------- while [ $# -gt 0 ]; do case "$1" in --prefix) PREFIX="$2" shift 2 ;; --version) VERSION="$2" shift 2 ;; --with-service) INSTALL_SERVICE=true shift ;; --skip-checksum) SKIP_CHECKSUM=true shift ;; --help) sed -n '2,/^$/s/^# \{0,1\}//p' "$0" exit 0 ;; *) error "Unknown option: $1" ;; esac done # --------------------------------------------------------------------------- # Main # --------------------------------------------------------------------------- OS="$(detect_os)" ARCH="$(detect_arch)" # These are the archives actually built by the release matrix. Refuse unsupported # combinations before network or filesystem installation work. case "${OS}-${ARCH}" in linux-x64|linux-arm64|macos-arm64|freebsd-x64) ;; *) error "No release archive is built for ${OS}-${ARCH}." ;; esac info "Detected platform: ${OS}/${ARCH}" # Resolve version if [ -z "$VERSION" ]; then info "Fetching latest release version..." VERSION="$(get_latest_version)" if [ -z "$VERSION" ]; then error "Could not determine latest version. Use --version to specify." fi fi VERSION="v${VERSION#v}" if ! printf '%s\n' "$VERSION" | LC_ALL=C grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$'; then error "Invalid public release version: ${VERSION}. Expected X.Y.Z or vX.Y.Z." fi # The 1.8.0 reissue has its own immutable public bundle. Keep the engine version # and latest-version contract unchanged, and preserve every other GA path. RELEASE_COORDINATE="$VERSION" case "$VERSION" in v1.8.0) RELEASE_COORDINATE="v1.8.0-r3" ;; esac info "Installing Trident ${VERSION}" # Build artifact name ARTIFACT="trident-${OS}-${ARCH}" if [ "$OS" = "windows" ]; then EXT="zip" else EXT="tar.gz" fi # Download TMPDIR="$(mktemp -d)" trap 'rm -rf "$TMPDIR"' EXIT # Verify integrity (SHA-256 mandatory, cosign best-effort) BEFORE extracting # or installing anything from the archive. RELEASE_BASE="${RELEASE_ROOT}/${RELEASE_COORDINATE}" download_artifact "${ARTIFACT}.${EXT}" "${TMPDIR}/trident.${EXT}" "$RELEASE_BASE" # Extract info "Extracting..." if [ "$EXT" = "tar.gz" ]; then tar -xzf "${TMPDIR}/trident.${EXT}" -C "$TMPDIR" else need_cmd unzip unzip -q "${TMPDIR}/trident.${EXT}" -d "$TMPDIR" fi # Install binaries info "Installing to ${PREFIX}/bin/" mkdir -p "${PREFIX}/bin" for bin in trident trident-cli trident-snap; do if [ "$OS" = "windows" ]; then bin="${bin}.exe" fi if [ -f "${TMPDIR}/${bin}" ]; then install -m 0755 "${TMPDIR}/${bin}" "${PREFIX}/bin/${bin}" info " Installed ${bin}" fi done # Optionally install systemd service and config if [ "$INSTALL_SERVICE" = true ] && [ "$OS" = "linux" ]; then info "Installing systemd service..." # Templates are public, versioned and covered by the same mandatory manifest. BASE_RAW="${RELEASE_BASE}" mkdir -p /etc/trident # The configuration is handed to the service's group below, so the # group has to exist first. if ! getent group trident >/dev/null 2>&1; then groupadd --system trident fi if ! getent passwd trident >/dev/null 2>&1; then useradd --system --gid trident --home-dir /var/lib/trident \ --shell /usr/sbin/nologin --comment "Trident HTTP Cache Proxy" trident fi if [ ! -f /etc/trident/config.json ] && [ ! -f /etc/trident/config.toml ]; then info " Installing default config to /etc/trident/config.json" download_artifact "dist/config.json" "${TMPDIR}/config.json" "$BASE_RAW" # Admin tokens and `${VAR}` secrets live here: root and the service's # group only. install -m 0640 "${TMPDIR}/config.json" /etc/trident/config.json chown root:trident /etc/trident/config.json else info " Existing config found, skipping" fi if [ ! -f /etc/default/trident ]; then # Keep an existing JSON configuration usable without migrating it. # Prefer TOML when both formats already exist, as the package default does. if [ -f /etc/trident/config.toml ]; then SERVICE_CONFIG=/etc/trident/config.toml else SERVICE_CONFIG=/etc/trident/config.json fi download_artifact "dist/trident.default" "${TMPDIR}/trident.default" "$BASE_RAW" TRIDENT_INSTALL_CONFIG="$SERVICE_CONFIG" awk ' /^TRIDENT_CONFIG=/ { printf "TRIDENT_CONFIG=%s\n", ENVIRON["TRIDENT_INSTALL_CONFIG"] found++ next } { print } END { if (found != 1) exit 1 } ' "${TMPDIR}/trident.default" > "${TMPDIR}/trident.default.configured" install -m 0640 "${TMPDIR}/trident.default.configured" /etc/default/trident chown root:trident /etc/default/trident fi download_artifact "dist/trident.service" "${TMPDIR}/trident.service" "$BASE_RAW" SERVICE_BINARY="$(CDPATH= cd "${PREFIX}/bin" && pwd -P)/trident" # Package units use /usr/bin; binary installs use the selected prefix. # Quote systemd syntax and escape specifiers/environment expansion literally. TRIDENT_INSTALL_BINARY="$SERVICE_BINARY" awk ' function quoted(value, result, i, c) { for (i = 1; i <= length(value); i++) { c = substr(value, i, 1) if (c == "\\" || c == "\"") result = result "\\" c else if (c == "%") result = result "%%" else if (c == "$") result = result "$$" else if (c == "\n") result = result "\\n" else if (c == "\r") result = result "\\r" else if (c == "\t") result = result "\\t" else result = result c } return result } /^ExecStart=\/usr\/bin\/trident / { sub(/^ExecStart=\/usr\/bin\/trident/, "") printf "ExecStart=\"%s\"%s\n", quoted(ENVIRON["TRIDENT_INSTALL_BINARY"]), $0 found++ next } { print } END { if (found != 1) exit 1 } ' "${TMPDIR}/trident.service" > "${TMPDIR}/trident.service.configured" install -m 0644 "${TMPDIR}/trident.service.configured" /usr/lib/systemd/system/trident.service mkdir -p /var/lib/trident /var/log/trident chown trident:trident /var/lib/trident /var/log/trident systemctl daemon-reload info " Service installed. Enable with: systemctl enable --now trident" fi # Verify if command -v "${PREFIX}/bin/trident" >/dev/null 2>&1; then INSTALLED_VERSION="$("${PREFIX}/bin/trident" --version 2>/dev/null || echo "unknown")" info "Trident installed successfully: ${INSTALLED_VERSION}" else info "Trident installed successfully to ${PREFIX}/bin/" fi info "Run 'trident --help' to get started." } # Keep this the last line, and keep the braces: every truncation of it is a # syntax error, so main never runs without its arguments (a bare `main "$@"` # cut after `main` would install with the defaults instead of the options that # were passed). { main "$@"; }