#!/bin/sh # The public start script behind https://terminal.jufa.app: asks for the terminal's Logto app, fetches the protected # install.sh of a channel from the registry and runs it with the credentials. No secrets and no terminal logic: what # may change with a release is in install.sh. Built by scripts/build-start.sh. # # wget -qO- https://terminal.jufa.app | sudo sh # wget -qO- https://terminal.jufa.app | sudo sh -s -- --staging --channel beta # # Only wget, sed and what Raspberry Pi OS Lite has (base64, sha256sum, stty). Its own input is the pipe, so it asks on # the terminal (/dev/tty). The secret and the tokens go to wget in a config file of a private directory, never on a # command line; install.sh gets the credentials in its environment. set -eu # Filled in by scripts/build-start.sh. DELIVERY_REGISTRY=registry.jufa.app/jufa-terminal LOGTO_PRODUCTION=https://auth.jufa.app LOGTO_STAGING=https://auth.staging.jufa.app # Logto grants only the scopes asked for; the role `terminal` in the tenant's organization carries this one. SCOPE=read:releases TTY=${JUFA_TTY:-/dev/tty} REGISTRY_HOST=${DELIVERY_REGISTRY%%/*} say() { printf '%s\n' "$*" } die() { printf 'start: %s\n' "$*" >&2 exit 1 } # check_value NAME VALUE: only characters Logto's ids and secrets have; anything else would end up in a header or a # form field as it is. check_value() { case $2 in '') die "$1 is empty" ;; *[!A-Za-z0-9._~-]*) die "$1 has characters a Logto $1 never has; mistyped?" ;; esac } # get OUT URL [HEADER...]: fetches URL into OUT, with $post as form body if set. The headers go into a wgetrc in $dir, # so a secret in one is on no command line. Sets $status to the HTTP status, 000 without an answer. get() { out=$1 url=$2 shift 2 (umask 077 && : >"$dir/wgetrc") for header; do printf 'header = %s\n' "$header" >>"$dir/wgetrc" done [ -z "$post" ] || printf 'post_file = %s\n' "$post" >>"$dir/wgetrc" wget --config="$dir/wgetrc" -q -S --content-on-error --timeout=20 --tries=2 -O "$out" "$url" \ "$dir/headers" || true : >"$dir/wgetrc" post= status=$(sed -n 's/^ *HTTP\/[0-9.]* \([0-9][0-9][0-9]\).*/\1/p' "$dir/headers" | tail -n 1) status=${status:-000} } # header NAME: the last value of that response header. header() { sed -n "s/^ *$1: *//Ip" "$dir/headers" | tail -n 1 | tr -d '\r' } # json KEY FILE: a string value of a flat JSON object. json() { tr -d '\n' <"$2" | sed -n "s/.*\"$1\" *: *\"\([^\"]*\)\".*/\1/p" } basic() { printf '%s:%s' "$1" "$2" | base64 | tr -d '\n' } # claims JWT: the token's payload, decoded. claims() { payload=$(printf '%s' "$1" | cut -d . -f 2 | tr '_-' '/+') case $((${#payload} % 4)) in 2) payload="$payload==" ;; 3) payload="$payload=" ;; esac printf '%s' "$payload" | base64 -d 2>/dev/null || true } # ask PROMPT [secret]: an answer from the terminal into $answer; a secret is not echoed. ask() { printf '%s: ' "$1" >&2 if [ "${2:-}" = secret ] && [ -t 3 ]; then stty -echo <&3 IFS= read -r answer <&3 || answer= stty echo <&3 printf '\n' >&2 else IFS= read -r answer <&3 || answer= fi } # Runs only once sh has read the whole file: until then the pipe is still this script. main() { endpoint=$LOGTO_PRODUCTION channel=stable while [ $# -gt 0 ]; do case $1 in --staging) endpoint=$LOGTO_STAGING ;; --channel) [ $# -gt 1 ] || die '--channel needs beta or stable' channel=$2 shift ;; *) die "unknown option: $1 (--staging, --channel beta|stable)" ;; esac shift done case $channel in beta | stable) ;; *) die "unknown channel: $channel (beta or stable)" ;; esac [ "$(id -u)" -eq 0 ] || die 'run with sudo: wget -qO- https://terminal.jufa.app | sudo sh' exec 3<"$TTY" || die "cannot read answers from $TTY; run the command in a terminal" dir=$(mktemp -d) trap 'stty echo <&3 2>/dev/null || true; rm -rf "$dir"' EXIT trap 'exit 130' HUP INT TERM post= say "Terminal setup, channel $channel, Logto $endpoint" say 'The terminal'"'"'s Logto app (docs/provisioning.md in jufa-app-terminal, step 1):' ask 'Tenant id (Logto organization id)' tenant=$answer check_value 'tenant id' "$tenant" ask 'Client id (App ID)' client_id=$answer check_value 'client id' "$client_id" ask 'Client secret (App secret, not shown)' secret client_secret=$answer check_value 'client secret' "$client_secret" say 'Getting a token from Logto ...' (umask 077 && printf 'grant_type=client_credentials&resource=https%%3A%%2F%%2F%s&organization_id=%s&scope=%s' \ "$REGISTRY_HOST" "$tenant" "$(printf '%s' "$SCOPE" | sed 's/:/%3A/g')" >"$dir/form") post=$dir/form get "$dir/token.json" "$endpoint/oidc/token" "Authorization: Basic $(basic "$client_id" "$client_secret")" case $status in 200) ;; 000) die "Logto is not reachable at $endpoint; check the network" ;; *) error=$(json error "$dir/token.json") die "Logto refused the credentials (HTTP $status${error:+ $error}): check tenant id, client id and secret," \ "and that the app exists in this Logto ($endpoint)" ;; esac token=$(json access_token "$dir/token.json") case " $(claims "$token" | tr -d '\n' | sed -n 's/.*"scope" *: *"\([^"]*\)".*/\1/p') " in *" $SCOPE "*) ;; *) die "the app has no role terminal in organization $tenant: its token lacks the scope $SCOPE" \ '(docs/provisioning.md, step 1)' ;; esac say "Fetching install.sh of channel $channel ..." repository=${DELIVERY_REGISTRY#*/}/channels/install get "$dir/v2" "https://$REGISTRY_HOST/v2/" [ "$status" != 000 ] || die "the registry $REGISTRY_HOST is not reachable; check the network" challenge=$(header WWW-Authenticate) realm=$(printf '%s' "$challenge" | sed -n 's/.*realm="\([^"]*\)".*/\1/p') service=$(printf '%s' "$challenge" | sed -n 's/.*service="\([^"]*\)".*/\1/p') [ -n "$realm" ] || die "the registry $REGISTRY_HOST asks for no token (HTTP $status); is it the registry?" get "$dir/registry.json" "$realm?service=$service&scope=repository:$repository:pull" \ "Authorization: Basic $(basic terminal "$token")" case $status in 200) ;; 000) die "the registry $REGISTRY_HOST is not reachable; check the network" ;; 401 | 403) die "the registry $REGISTRY_HOST denied access (HTTP $status)" ;; *) die "the registry $REGISTRY_HOST gave no token (HTTP $status)" ;; esac bearer=$(json token "$dir/registry.json") [ -n "$bearer" ] || bearer=$(json access_token "$dir/registry.json") [ -n "$bearer" ] || die "the registry $REGISTRY_HOST gave no token" get "$dir/manifest.json" "https://$REGISTRY_HOST/v2/$repository/manifests/$channel" "Authorization: Bearer $bearer" \ 'Accept: application/vnd.oci.image.manifest.v1+json' case $status in 200) ;; 000) die "the registry $REGISTRY_HOST is not reachable; check the network" ;; 401 | 403) die "the registry $REGISTRY_HOST denied access to $repository:$channel (HTTP $status)" ;; 404) die "no install.sh for channel $channel in the registry ($repository:$channel)" ;; *) die "cannot fetch $repository:$channel (HTTP $status)" ;; esac # The artifact has one layer, the plain script. layers=$(tr -d '\n\t ' <"$dir/manifest.json" | sed -n 's/.*"layers":\[\([^]]*\)\].*/\1/p' | grep -o '"digest":"sha256:[0-9a-f]\{64\}"' | sed 's/.*"\(sha256:[0-9a-f]*\)"/\1/') [ "$(printf '%s\n' "$layers" | grep -c .)" -eq 1 ] || die "$repository:$channel is not the artifact of an install.sh (expected one layer)" digest=$layers get "$dir/install.sh" "https://$REGISTRY_HOST/v2/$repository/blobs/$digest" "Authorization: Bearer $bearer" [ "$status" = 200 ] || die "cannot fetch install.sh of channel $channel (HTTP $status)" [ "sha256:$(sha256sum "$dir/install.sh" | cut -d ' ' -f 1)" = "$digest" ] || die "install.sh of channel $channel does not match its digest $digest; not run" say "Running install.sh of channel $channel ($digest)" say '' JUFA_CHANNEL=$channel JUFA_TENANT_ID=$tenant JUFA_LOGTO_ENDPOINT=$endpoint JUFA_LOGTO_CLIENT_ID=$client_id JUFA_LOGTO_CLIENT_SECRET=$client_secret export JUFA_CHANNEL JUFA_TENANT_ID JUFA_LOGTO_ENDPOINT JUFA_LOGTO_CLIENT_ID JUFA_LOGTO_CLIENT_SECRET sh "$dir/install.sh" 3<&- } main "$@"