#!/bin/sh # # EzModo CLI Installation Script # # This file is POSIX sh, not bash: it is documented as `curl ... | sh`, and on # Debian/Ubuntu `sh` is dash. Check changes with `shellcheck -s sh` and run # cli/scripts/test-install.sh, which executes it under dash and bash (#3127). # # Usage: # # Latest version # curl -fsSL https://ezmodo.com/cli/install/scripts/latest/install.sh | sh # # # Specific version # curl -fsSL https://ezmodo.com/cli/install/scripts/v1.0.0/install.sh | sh # # or: curl -fsSL https://ezmodo.com/cli/install/scripts/latest/install.sh | EZMODO_VERSION=1.0.0 sh # # # Internal use (staging environment) # curl -fsSL https://ezmodo.com/cli/install/scripts/latest/install.sh | sh -s -- --env=staging # # Environment overrides: # EZMODO_VERSION version to install (default: the version this script ships with) # EZMODO_ENV production (default) or staging # EZMODO_INSTALL_DIR where to put the binary (default: /usr/local/bin) # EZMODO_BASE_URL download host (default: derived from EZMODO_ENV). Must be # https://, except http://localhost or http://127.0.0.1 # and file:/// for local testing (#3349). # EZMODO_REQUIRE_SIGNATURE 1 = refuse to install unless the signature of # checksums.txt verifies (needs cosign). Default: verify # when cosign is installed, warn when it is not (#3356). # # Transport: every download is curl --proto =https --tlsv1.2, redirects # included, so neither a plain-http base URL nor a redirect can downgrade it. # # Signature (#3356): checksums.txt proves the download is intact, not who built # it, because it comes from the same host as the binary. The release workflow # therefore signs it with Sigstore keyless signing (cosign), and this script # checks that signature against EzModo's release workflow identity when cosign # is installed. No key is involved: the identity below IS the trust anchor. # # Know the limit: this script is served from that same host, so a CDN that # lies about the binary can lie about this script too. The check here guards # against a swapped or stale binary and checksums.txt; for assurance that does # not rest on the CDN, verify yourself with the identity from a source you # trust (the help centre, or the release notes on GitHub): # # cosign verify-blob --bundle checksums.txt.sigstore.json \ # --certificate-identity https://github.com/EasyModeOnly/ezmodo/.github/workflows/cli-deploy.yml@refs/heads/main \ # --certificate-oidc-issuer https://token.actions.githubusercontent.com \ # checksums.txt # set -e # Colors, only when stderr is a terminal. if [ -t 2 ]; then RED=$(printf '\033[0;31m') GREEN=$(printf '\033[0;32m') YELLOW=$(printf '\033[1;33m') BLUE=$(printf '\033[0;34m') NC=$(printf '\033[0m') else RED='' GREEN='' YELLOW='' BLUE='' NC='' fi # Configuration INSTALL_DIR="${EZMODO_INSTALL_DIR:-/usr/local/bin}" BINARY_NAME="ezmodo" # IMPORTANT: This version is automatically injected by the deployment pipeline # When deploying, sed replaces this with the actual version from package.json SCRIPT_VERSION="1.6.0" # Who signs checksums.txt (#3356): EzModo's CLI release workflow, run from # main. A signature from any other workflow, branch or repo does not verify. SIGNER_IDENTITY="https://github.com/EasyModeOnly/ezmodo/.github/workflows/cli-deploy.yml@refs/heads/main" SIGNER_ISSUER="https://token.actions.githubusercontent.com" REQUIRE_SIGNATURE="${EZMODO_REQUIRE_SIGNATURE:-0}" # Parse arguments / environment variables (EZMODO_ENV / EZMODO_VERSION). EZMODO_ENV="${EZMODO_ENV:-production}" VERSION="${EZMODO_VERSION:-$SCRIPT_VERSION}" for arg in "$@"; do case $arg in --version=*) VERSION="${arg#*=}" ;; --env=*) EZMODO_ENV="${arg#*=}" ;; esac done # Determine base URL based on environment if [ "$EZMODO_ENV" = "staging" ]; then DEFAULT_BASE_URL="https://staging.ezmodo.com" TAG_SUFFIX="-staging" else DEFAULT_BASE_URL="https://ezmodo.com" TAG_SUFFIX="" fi BASE_URL="${EZMODO_BASE_URL:-$DEFAULT_BASE_URL}" BASE_URL="${BASE_URL%/}" # Functions # All print functions write to stderr so they never mix with captured output. print_step() { printf '%s==>%s %s\n' "$BLUE" "$NC" "$1" >&2 } print_success() { printf '%s✓%s %s\n' "$GREEN" "$NC" "$1" >&2 } print_error() { printf '%s✗%s %s\n' "$RED" "$NC" "$1" >&2 } print_warning() { printf '%s!%s %s\n' "$YELLOW" "$NC" "$1" >&2 } # Accept $1 as the download base URL, and set CURL_PROTO to the one protocol # curl may use for it (and for any redirect). Only https reaches the network; # plain http is allowed to this machine alone, for a local CDN, and file:/// # for the installer's own tests. A URL with userinfo is refused, since # http://localhost@evil.example goes to evil.example. check_base_url() { case $1 in *@*) print_error "EZMODO_BASE_URL must not contain '@': $1" return 1 ;; https://?*) CURL_PROTO='=https' ;; http://localhost | http://localhost[:/]* | http://127.0.0.1 | http://127.0.0.1[:/]*) CURL_PROTO='=http' ;; file:///*) CURL_PROTO='=file' ;; *) print_error "Refusing to download over anything but https: $1" print_error "EZMODO_BASE_URL must start with https:// (http:// only for localhost or 127.0.0.1)." return 1 ;; esac } # curl restricted to the base URL's protocol, redirects included, and to # TLS 1.2 or later. fetch() { curl --proto "$CURL_PROTO" --proto-redir "$CURL_PROTO" --tlsv1.2 -fsSL "$@" } # Detect OS and architecture detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case "$OS" in darwin) OS="darwin" ;; linux) OS="linux" ;; mingw*|msys*|cygwin*) OS="windows" ;; *) print_error "Unsupported operating system: $OS" exit 1 ;; esac case "$ARCH" in x86_64|amd64) ARCH="x64" ;; arm64|aarch64) ARCH="arm64" ;; *) print_error "Unsupported architecture: $ARCH" exit 1 ;; esac if [ "$OS" = "windows" ] && [ "$ARCH" = "arm64" ]; then print_error "Windows on ARM64 has no native build. Use install.ps1, which installs the x64 build." exit 1 fi PLATFORM="${OS}-${ARCH}" print_step "Detected platform: $PLATFORM" } # Print the SHA-256 of $1, using whichever tool this system has. # sha256sum is GNU coreutils (Linux); macOS ships shasum instead. sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}' else return 1 fi } fail_download() { rm -rf "$TMP_DIR" exit 1 } # Fail when a signature is required, otherwise warn and carry on. signature_unavailable() { if [ "$REQUIRE_SIGNATURE" = 1 ]; then print_error "$1" print_error "EZMODO_REQUIRE_SIGNATURE=1, so nothing is installed." fail_download fi print_warning "$1" print_warning "Not checked: that EzModo's release workflow signed checksums.txt." } # Check checksums.txt's Sigstore signature (#3356) before any checksum in it # is trusted. A signature that is present and fails is always fatal. verify_signature() { if ! command -v cosign >/dev/null 2>&1; then signature_unavailable "cosign is not installed, so the signature of checksums.txt cannot be checked (https://docs.sigstore.dev/cosign/system_config/installation/)." return 0 fi print_step "Verifying the signature of checksums.txt..." BUNDLE_FILE="$TMP_DIR/checksums.txt.sigstore.json" if ! fetch "$SIGNATURE_URL" -o "$BUNDLE_FILE" 2>/dev/null || [ ! -s "$BUNDLE_FILE" ]; then signature_unavailable "No signature for v${VERSION}${TAG_SUFFIX} (releases before #3356 are unsigned): $SIGNATURE_URL" return 0 fi if cosign verify-blob --bundle "$BUNDLE_FILE" \ --certificate-identity "$SIGNER_IDENTITY" \ --certificate-oidc-issuer "$SIGNER_ISSUER" \ "$CHECKSUMS_FILE" > "$TMP_DIR/cosign.log" 2>&1; then print_success "Signature verified: checksums.txt was signed by EzModo's release workflow" else print_error "Signature verification failed: checksums.txt was not signed by $SIGNER_IDENTITY" sed 's/^/ /' "$TMP_DIR/cosign.log" >&2 fail_download fi } # Verify the downloaded binary against checksums.txt. Every failure here is # fatal: a binary we cannot verify is not installed. verify_checksum() { print_step "Verifying checksum..." # To a file, so the signature is checked over the exact bytes served. CHECKSUMS_FILE="$TMP_DIR/checksums.txt" if ! fetch "$CHECKSUM_URL" -o "$CHECKSUMS_FILE" 2>/dev/null || [ ! -s "$CHECKSUMS_FILE" ]; then print_error "Could not fetch checksums from $CHECKSUM_URL" fail_download fi verify_signature CHECKSUMS=$(cat "$CHECKSUMS_FILE") # Exact match on the file name column, so one platform never matches another. EXPECTED_CHECKSUM=$(printf '%s\n' "$CHECKSUMS" | awk -v f="$BINARY_FILE" '$2 == f || $2 == "*" f {print $1; exit}') if [ -z "$EXPECTED_CHECKSUM" ]; then print_error "No checksum for $BINARY_FILE in checksums.txt" fail_download fi if ! ACTUAL_CHECKSUM=$(sha256_of "$TMP_FILE"); then print_error "Cannot verify the download: neither sha256sum nor shasum is installed." fail_download fi if [ "$ACTUAL_CHECKSUM" = "$EXPECTED_CHECKSUM" ]; then print_success "Checksum verified" else print_error "Checksum mismatch!" print_error "Expected: $EXPECTED_CHECKSUM" print_error "Got: $ACTUAL_CHECKSUM" fail_download fi } # Download the binary into $TMP_FILE and verify it. download_binary() { print_step "Installing version: $VERSION (environment: $EZMODO_ENV)" # Construct binary filename if [ "$OS" = "windows" ]; then BINARY_FILE="ezmodo-${PLATFORM}.exe" else BINARY_FILE="ezmodo-${PLATFORM}" fi # Path: /cli/install/bin/v{VERSION}{-staging}/ DOWNLOAD_URL="${BASE_URL}/cli/install/bin/v${VERSION}${TAG_SUFFIX}/${BINARY_FILE}" CHECKSUM_URL="${BASE_URL}/cli/install/bin/v${VERSION}${TAG_SUFFIX}/checksums.txt" SIGNATURE_URL="${CHECKSUM_URL}.sigstore.json" print_step "Downloading from CDN: $BINARY_FILE..." TMP_DIR=$(mktemp -d) TMP_FILE="$TMP_DIR/$BINARY_NAME" if ! fetch "$DOWNLOAD_URL" -o "$TMP_FILE" 2>/dev/null; then print_error "CDN download failed. Binary for $PLATFORM may not be available." print_error "Version: v${VERSION}${TAG_SUFFIX}" print_error "URL: $DOWNLOAD_URL" if [ "$PLATFORM" = "linux-arm64" ]; then print_error "Linux arm64 builds start after CLI v0.24.0; earlier versions have none." fi print_error "" print_error "Please verify the version exists or contact support." fail_download fi print_success "Downloaded successfully" verify_checksum } # Install binary install_binary() { # Check if already installed if [ -f "$INSTALL_DIR/$BINARY_NAME" ]; then CURRENT_VERSION=$("$INSTALL_DIR/$BINARY_NAME" --version 2>/dev/null || echo "unknown version") print_step "Current installation: $CURRENT_VERSION" IS_UPGRADE="true" # Detect environment switch case "$CURRENT_VERSION" in *staging*) CURRENT_IS_STAGING="true" ;; *) CURRENT_IS_STAGING="false" ;; esac if [ "$CURRENT_IS_STAGING" = "true" ] && [ "$EZMODO_ENV" != "staging" ]; then print_step "Switching from staging to $EZMODO_ENV environment" elif [ "$CURRENT_IS_STAGING" = "false" ] && [ "$EZMODO_ENV" = "staging" ]; then print_step "Switching from production to staging environment" else print_step "Upgrading to version $VERSION ($EZMODO_ENV)" fi else print_step "Installing fresh installation to $INSTALL_DIR..." IS_UPGRADE="false" fi # Install the binary if [ ! -w "$INSTALL_DIR" ]; then print_warning "Installation requires sudo access" sudo mv "$TMP_FILE" "$INSTALL_DIR/$BINARY_NAME" sudo chmod +x "$INSTALL_DIR/$BINARY_NAME" else mv "$TMP_FILE" "$INSTALL_DIR/$BINARY_NAME" chmod +x "$INSTALL_DIR/$BINARY_NAME" fi rm -rf "$TMP_DIR" print_success "Installed to $INSTALL_DIR/$BINARY_NAME" } # Verify installation verify_installation() { print_step "Verifying installation..." if ! VERSION_OUTPUT=$("$INSTALL_DIR/$BINARY_NAME" --version 2>/dev/null); then print_error "Installation verification failed: $INSTALL_DIR/$BINARY_NAME --version did not run" exit 1 fi print_success "Verification complete: $VERSION_OUTPUT" case ":$PATH:" in *":$INSTALL_DIR:"*) ;; *) print_warning "$INSTALL_DIR is not in your PATH" print_warning "Add this to your shell profile:" echo "" >&2 echo " export PATH=\"$INSTALL_DIR:\$PATH\"" >&2 echo "" >&2 ;; esac } # Main installation flow main() { echo "" >&2 echo "╔══════════════════════════════════════╗" >&2 echo "║ EzModo CLI Installation Script ║" >&2 echo "╚══════════════════════════════════════╝" >&2 echo "" >&2 check_base_url "$BASE_URL" || exit 1 detect_platform download_binary install_binary verify_installation echo "" >&2 print_success "Installation complete!" echo "" >&2 echo "Next steps:" >&2 # Show different instructions based on whether this was an upgrade if [ "$IS_UPGRADE" = "true" ]; then echo " 1. Get help: $BINARY_NAME --help" >&2 else echo " 1. Authenticate: $BINARY_NAME auth login" >&2 echo " 2. Get help: $BINARY_NAME --help" >&2 fi echo "" >&2 echo "Documentation: https://ezmodo.com/docs/emo/ezmodo" >&2 echo "" >&2 } # Run installation main "$@"