wrolpi/upgrade.sh
lrnselfreliance bde1209c63
An upgrade refuses to roll back to an older commit of the current branch (#910)
* An upgrade refuses to roll back to an older commit of the current branch

upgrade.sh verifies the fetched commit's signature, but a signature proves a commit is
WROLPi's, not that it is the newest; an intercepted fetch could serve an old signed
release and the forced checkout would roll the device back to it.  refuse_rollback stops
when the target is an older commit of the branch the device is on (a detached HEAD from a
release image counts as that branch).  Branch switches, the same commit, and history the
device does not contain (Debian 12 devices on debian12-final) are allowed, and -d allows
a deliberate downgrade; it reaches the upgrade service through the env file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An upgrade also refuses a signed commit with a lower version from other history

Ancestry alone only caught older commits in the device's own history; any other signed
commit, such as debian12-final or an old experimental branch, passed.  refuse_rollback
now also compares wrolpi/version.txt, which is inside the signed tree, and refuses a lower
or missing version.  A force-pushed branch at the same or a higher version still upgrades.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Tests pin the two parts of refuse_rollback that only one rule catches

An earlier commit of the same version is refused only by the ancestry check, and
0.30.10-beta after 0.30.3-beta only orders correctly with sort -V; the fixture's
single-digit versions passed with either part removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 09:33:25 -06:00

211 lines
8.2 KiB
Bash
Executable file

#! /usr/bin/env bash
# This script will upgrade the WROLPi API and App. It is required that you install WROLPi first.
Help() {
# Display Help
echo "Upgrade WROLPi API and App on this machine."
echo
echo "Syntax: upgrade.sh [-h] [-b BRANCH] [-d]"
echo "options:"
echo "h Print this help."
echo "b Upgrade from this git BRANCH (default: current branch, or 'release')."
echo "d Allow a downgrade to an older commit of the current branch."
echo
}
BRANCH=""
BRANCH_OVERRIDE=false
# The upgrade service reads this from the env file written below.
ALLOW_DOWNGRADE=${ALLOW_DOWNGRADE:-false}
while getopts ":hb:d" option; do
case $option in
h) # display Help
Help
exit
;;
b)
BRANCH="${OPTARG}"
BRANCH_OVERRIDE=true
;;
d)
ALLOW_DOWNGRADE=true
;;
*) # invalid argument(s)
echo "Error: Invalid option"
exit 1
;;
esac
done
if [ ! -d /opt/wrolpi ] || [ ! -d /opt/wrolpi/wrolpi ]; then
echo "You must install WROLPi first. Try: /opt/wrolpi/install.sh"
exit 2
fi
# Re-execute this script if it wasn't called with sudo.
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
# Determine which branch to use for upgrade.
if [ "$BRANCH_OVERRIDE" = false ]; then
CURRENT_BRANCH=$(git -C /opt/wrolpi branch --show-current 2>/dev/null)
if [ -n "$CURRENT_BRANCH" ]; then
BRANCH="$CURRENT_BRANCH"
else
BRANCH="release"
fi
fi
# If not running via the wrolpi-upgrade service, delegate to it so logs are captured.
if [ -z "$WROLPI_UPGRADE_SERVICE" ]; then
trap 'echo ""; echo "Note: The upgrade service is still running in the background."; echo "Monitor with: journalctl -fu wrolpi-upgrade.service"' INT
echo "Delegating upgrade to wrolpi-upgrade.service..."
# Self-heal: ensure the unit is installed and current before starting it.
# repair.sh installs it normally, but that runs late in the upgrade, so a
# missing or stale unit would otherwise break delegation entirely.
install -m644 /opt/wrolpi/etc/raspberrypios/wrolpi-upgrade.service \
/etc/systemd/system/wrolpi-upgrade.service
systemctl daemon-reload
# Write branch config for the service.
echo "BRANCH=${BRANCH}" >/tmp/wrolpi-upgrade.env
echo "ALLOW_DOWNGRADE=${ALLOW_DOWNGRADE}" >>/tmp/wrolpi-upgrade.env
chown wrolpi:wrolpi /tmp/wrolpi-upgrade.env
# Start following journal in background (new entries only).
journalctl -fn0 -u wrolpi-upgrade.service &
JOURNAL_PID=$!
# Start service (blocks until complete for oneshot).
systemctl start wrolpi-upgrade.service
EXIT_CODE=$?
# Clean up journal follower.
kill $JOURNAL_PID 2>/dev/null
wait $JOURNAL_PID 2>/dev/null
exit $EXIT_CODE
fi
# Clean up and report status on exit.
trap '
EXIT_STATUS=$?
rm -f /tmp/wrolpi-upgrade.env
# Clean up temporary GPG keyring if it exists.
if [ -n "$GNUPGHOME" ] && [ -d "$GNUPGHOME" ]; then
rm -rf "$GNUPGHOME"
fi
if [ $EXIT_STATUS -eq 0 ]; then
echo "WROLPi upgrade has completed"
else
echo "WROLPi upgrade has FAILED (exit code: $EXIT_STATUS)"
if [ "$RESTART_SERVICES_ON_FAILURE" = true ]; then
echo "Restarting WROLPi services after failed upgrade..."
systemctl start wrolpi-api || :
systemctl start wrolpi-app || :
fi
fi
' EXIT
echo "Upgrading WROLPi from branch: ${BRANCH}"
set -x
set -e
set -o pipefail
# Units may not be installed yet if a previous install/repair failed; repair.sh installs them later.
systemctl stop wrolpi-api || :
systemctl stop wrolpi-app || :
RESTART_SERVICES_ON_FAILURE=true
# Restore the previously-running commit after a failed checkout or verification.
# Services must only restart on that known-good commit, so if the rollback does
# not land there, leave them stopped.
rollback_or_no_restart() {
git -C /opt/wrolpi checkout -f -B "${BRANCH}" "${PREVIOUS_HEAD}" || :
if [ "$(git -C /opt/wrolpi rev-parse HEAD 2>/dev/null)" != "${PREVIOUS_HEAD}" ]; then
echo "ERROR: rollback to ${PREVIOUS_HEAD} failed; not restarting services."
RESTART_SERVICES_ON_FAILURE=false
fi
}
# Refuses to move a checkout back to an older WROLPi on the branch it is already on. A signature proves a commit is
# WROLPi's, not that it is the newest, so an intercepted fetch could otherwise roll a device back to an old signed
# commit. Older means an ancestor of HEAD, or a lower wrolpi/version.txt (that file is inside the signed tree, and it
# catches old signed commits from other history). A rewritten branch at the same or a higher version, a branch switch,
# and the same commit are allowed; -d allows a deliberate downgrade. Debian 12 devices never reach this: they run the
# upgrade.sh pinned at debian12-final.
refuse_rollback() {
local repo=$1 branch=$2 target=$3 current head head_version target_version
[ "${ALLOW_DOWNGRADE}" = true ] && return 0
current=$(git -C "${repo}" branch --show-current)
# A detached HEAD (a device built from a release image) is on the branch it upgrades.
[ -n "${current}" ] && [ "${current}" != "${branch}" ] && return 0
head=$(git -C "${repo}" rev-parse HEAD)
target=$(git -C "${repo}" rev-parse "${target}^{commit}")
[ "${head}" = "${target}" ] && return 0
head_version=$(git -C "${repo}" show "${head}:wrolpi/version.txt" 2>/dev/null | tr -d '[:space:]')
target_version=$(git -C "${repo}" show "${target}:wrolpi/version.txt" 2>/dev/null | tr -d '[:space:]')
if git -C "${repo}" merge-base --is-ancestor "${target}" "${head}" ||
{ [ "${target_version}" != "${head_version}" ] &&
[ "$(printf '%s\n%s\n' "${target_version}" "${head_version}" | sort -V | head -n1)" = "${target_version}" ]; }; then
echo "ERROR: ${branch} (${target}, version ${target_version:-unknown}) is older than this WROLPi (${head}," \
"version ${head_version}). Refusing to roll back; run upgrade.sh -d to allow it."
return 1
fi
}
# Fetch the latest commits without modifying the working tree.
git -C /opt/wrolpi fetch || exit 4
# Import the trusted GPG key from the current (pre-upgrade) working tree into a temporary keyring.
GNUPGHOME=$(mktemp -d)
export GNUPGHOME
gpg --batch --quiet --import /opt/wrolpi/wrolpi/roland@learningselfreliance.com.gpg
# The throwaway keyring contains only the pinned WROLPi key, so mark it
# ultimately trusted: possession of the pinned key IS the trust anchor here,
# and this silences gpg's web-of-trust WARNING noise that makes healthy
# upgrade logs look scary. Cosmetic only — verify-commit already ignores
# ownertrust when judging signature validity.
gpg --batch --list-keys --with-colons | awk -F: '/^fpr:/ {print $10":6:"; exit}' | \
gpg --batch --quiet --import-ownertrust || :
# Verify the fetched commit is signed by the trusted key before checking it out.
if ! git -C /opt/wrolpi verify-commit origin/"${BRANCH}" 2>&1; then
echo "ERROR: origin/${BRANCH} is not signed by the trusted WROLPi GPG key. Aborting upgrade."
rm -rf "$GNUPGHOME"
exit 5
fi
refuse_rollback /opt/wrolpi "${BRANCH}" "origin/${BRANCH}" || exit 7
# Signature verified, safe to apply.
PREVIOUS_HEAD=$(git -C /opt/wrolpi rev-parse HEAD)
# Force the checkout so local working-tree changes (e.g. app/package-lock.json
# rewritten by npm install during a prior upgrade) cannot abort the switch.
# Use `checkout -B <branch> origin/<branch>` so the ref can never be mistaken for a path. A tracked `release/`
# directory otherwise collides with the `release` branch name, making a bare `git checkout release` ambiguous.
# checkout -B moves the ref before checking out the tree, so a failure here may
# leave a mixed tree; roll back before the EXIT trap restarts anything.
(cd /opt/wrolpi && git checkout -f -B "${BRANCH}" "origin/${BRANCH}") || {
rollback_or_no_restart
exit 4
}
# Verify HEAD again after checkout in case the branch was swapped between fetch and checkout.
if ! git -C /opt/wrolpi verify-commit HEAD 2>&1; then
echo "ERROR: HEAD commit signature verification failed after checkout. Aborting upgrade."
rollback_or_no_restart
rm -rf "$GNUPGHOME"
exit 6
fi
rm -rf "$GNUPGHOME"
unset GNUPGHOME
# From here scripts/upgrade.sh mutates the system; a blind restart could run
# half-upgraded code, so recovery belongs to repair.sh.
RESTART_SERVICES_ON_FAILURE=false
/opt/wrolpi/scripts/upgrade.sh 2>&1 | tee /opt/wrolpi/upgrade.log
set +x
echo "Upgrade end $(date '+%Y-%m-%d %H:%M:%S')" >>/opt/wrolpi/upgrade.log