#!/usr/bin/env bash set -euo pipefail if [[ $# != 2 ]]; then echo "usage: $0 ECHOION_UPDATER SOURCE_ROOT" >&2 exit 2 fi updater=$(realpath -e -- "$1") source_root=$(realpath -e -- "$2") temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-updater-test.XXXXXX") trap 'rm -rf -- "$temporary"' EXIT root=$temporary/root stable_helper=$root/libexec/updater/installer/bin/echoion-updater mkdir -p "$root/versions" "$root/state" "$(dirname -- "$stable_helper")" install -m 0755 "$updater" "$stable_helper" stable_helper_sha=$(sha256sum "$stable_helper") stable_helper_sha=${stable_helper_sha%% *} make_release() { local destination=$1 local version=$2 mkdir -p "$destination/bin" "$destination/lib" "$destination/service" \ "$destination/share/echoion/web" "$destination/share/echoion/tools" install -m 0755 /bin/true "$destination/bin/echoiond" install -m 0755 "$updater" "$destination/bin/echoion-updater" printf 'core %s\n' "$version" >"$destination/lib/Echoion.ts" install -m 0644 "$source_root/packaging/service/echoion.service.in" \ "$destination/service/echoion.service.in" install -m 0644 "$source_root/packaging/service/echoion-updater.service.in" \ "$destination/service/echoion-updater.service.in" install -m 0644 "$source_root/packaging/service/echoion-updater.timer.in" \ "$destination/service/echoion-updater.timer.in" install -m 0755 "$source_root/scripts/validate-release-manifest.py" \ "$destination/share/echoion/tools/validate-release-manifest.py" printf '{}\n' >"$destination/share/echoion/config.example.json" printf '\n' >"$destination/share/echoion/web/index.html" python3 "$source_root/scripts/generate-release-manifest.py" \ --root "$destination" --version "$version" } make_release "$root/versions/1.0.0" 1.0.0 make_release "$root/versions/1.1.0" 1.1.0 ln -s versions/1.1.0 "$root/current" cat >"$root/.echoion-install" <"$root/install.json" <"$root/health.log" chmod 0644 "$root/health.log" status=$("$updater" --root "$root" status --json) [[ $status == *'"current":"1.1.0"'* ]] [[ $status == *'"1.0.0"'* ]] make_release "$root/versions/0.9.0" 0.9.0 if "$updater" --root "$root" rollback-runtime --version 0.9.0 \ --retain 2 >/dev/null 2>&1; then echo "updater accepted a runtime without durable healthy state" >&2 exit 1 fi [[ $(readlink -- "$root/current") == versions/1.1.0 ]] rm -rf -- "$root/versions/0.9.0" history_victim=$temporary/history-victim printf '%s\n' 'must remain unchanged' >"$history_victim" chmod 0600 "$history_victim" mkdir -p "$root/state/updates" ln -s "$history_victim" "$root/state/updates/history.jsonl" if "$updater" --root "$root" request rollback-runtime --version 1.0.0 \ --retain 2 --origin test >/dev/null 2>&1; then echo "updater followed a symlink while opening privileged history" >&2 exit 1 fi [[ $(<"$history_victim") == 'must remain unchanged' ]] [[ $(stat -c '%a' -- "$history_victim") == 600 ]] rm -- "$root/state/updates/history.jsonl" rm -f -- "$root/state/updates/privileged-requests/"*.json queue_victim=$temporary/queue-victim mkdir "$queue_victim" mv "$root/state/updates/privileged-requests" \ "$root/state/updates/privileged-requests.safe" ln -s "$queue_victim" "$root/state/updates/privileged-requests" if "$updater" --root "$root" request rollback-runtime --version 1.0.0 \ --retain 2 --origin test >/dev/null 2>&1; then echo "updater accepted a symlinked privileged request directory" >&2 exit 1 fi [[ -z $(find "$queue_victim" -mindepth 1 -print -quit) ]] rm -- "$root/state/updates/privileged-requests" mv "$root/state/updates/privileged-requests.safe" \ "$root/state/updates/privileged-requests" if "$updater" --root "$root" check >/dev/null 2>&1 || [[ -e $root/state/updates/requests ]]; then echo "updater still exposes the dead generic request queue" >&2 exit 1 fi if "$updater" --root "$root" request rollback-runtime \ --version '../escape' --retain 2 >/dev/null 2>&1; then echo "updater accepted an unsafe request identifier" >&2 exit 1 fi if "$updater" --root "$root" request rollback-runtime \ --version 1.0.0 --retain 0 >/dev/null 2>&1; then echo "updater accepted an out-of-range retention count" >&2 exit 1 fi candidate=$root/state/updates/staged/runtime-1-2-0 make_release "$candidate" 1.2.0 # Candidate runtime code must never replace the stable privileged helper. install -m 0755 /bin/false "$candidate/bin/echoion-updater" python3 "$source_root/scripts/generate-release-manifest.py" \ --root "$candidate" --version 1.2.0 manifest_sha=$(sha256sum "$candidate/manifest.json") manifest_sha=${manifest_sha%% *} pending_victim=$temporary/pending-victim printf '%s\n' 'must remain unchanged' >"$pending_victim" ln -s "$pending_victim" "$root/state/updates/pending-runtime.json" privileged=$("$updater" --root "$root" request activate-runtime \ --staging runtime-1-2-0 --version 1.2.0 \ --manifest-sha256 "$manifest_sha" --retain 3 --origin test) [[ $privileged == "$root/state/updates/privileged-requests/"*.json ]] if "$updater" --root "$root" consume >/dev/null 2>&1; then echo "updater accepted a symlinked pending-runtime journal" >&2 exit 1 fi [[ $(<"$pending_victim") == 'must remain unchanged' ]] [[ $(readlink -- "$root/current") == versions/1.1.0 ]] [[ -d $candidate ]] rm -- "$root/state/updates/pending-runtime.json" "$updater" --root "$root" consume >/dev/null [[ $(<"$pending_victim") == 'must remain unchanged' ]] [[ $(readlink -- "$root/current") == versions/1.2.0 ]] [[ ! -e $candidate ]] [[ $(sha256sum "$stable_helper") == "$stable_helper_sha "* ]] [[ $(sha256sum "$root/current/bin/echoion-updater") != "$stable_helper_sha "* ]] grep -F '"version": "1.2.0"' "$root/state/updates/activated-runtime.json" >/dev/null grep -F '"manifestPath": "versions/1.2.0/manifest.json"' \ "$root/state/updates/activated-runtime.json" >/dev/null grep -F '"retainHealthyVersions": 3' \ "$root/state/updates/activated-runtime.json" >/dev/null grep -F '"state": "pending"' "$root/state/updates/pending-runtime.json" >/dev/null grep -F '"retainHealthyVersions": 3' \ "$root/state/updates/pending-runtime.json" >/dev/null grep -F 'version=1.2.0' "$root/.echoion-install" >/dev/null grep -F '"version":"1.2.0"' "$root/install.json" >/dev/null rm -- "$root/state/updates/status.json" mkdir "$root/state/updates/status.json" "$updater" --root "$root" request commit-runtime --version 1.2.0 \ --retain 3 --origin daemon >/dev/null "$updater" --root "$root" consume >/dev/null [[ $(readlink -- "$root/current") == versions/1.2.0 ]] [[ ! -e $root/state/updates/pending-runtime.json ]] [[ -f $root/state/updates/committed-runtime.json ]] grep -F '"state": "committed"' \ "$root/state/updates/activated-runtime.json" >/dev/null grep -F 'H 1.2.0' "$root/health.log" >/dev/null [[ ! -e $root/staging ]] rmdir "$root/state/updates/status.json" candidate=$root/state/updates/staged/runtime-1-3-0 make_release "$candidate" 1.3.0 manifest_sha=$(sha256sum "$candidate/manifest.json") manifest_sha=${manifest_sha%% *} "$updater" --root "$root" request activate-runtime \ --staging runtime-1-3-0 --version 1.3.0 \ --manifest-sha256 "$manifest_sha" --retain 3 --origin test >/dev/null "$updater" --root "$root" consume >/dev/null [[ $(readlink -- "$root/current") == versions/1.3.0 ]] [[ -f $root/state/updates/pending-runtime.json ]] # A helper pass inside the durable health grace period must not race the # daemon's 30-second health confirmation. "$updater" --root "$root" consume >/dev/null [[ $(readlink -- "$root/current") == versions/1.3.0 ]] [[ -f $root/state/updates/pending-runtime.json ]] # Once the durable deadline has expired, a pass with no matching confirmation # restores the previous version and records the failed candidate. sed -i 's/"activatedAt": "[^"]*"/"activatedAt": "2000-01-01T00:00:00Z"/' \ "$root/state/updates/pending-runtime.json" "$updater" --root "$root" consume >/dev/null [[ $(readlink -- "$root/current") == versions/1.2.0 ]] [[ ! -e $root/state/updates/pending-runtime.json ]] grep -F '"version": "1.3.0"' \ "$root/state/updates/failed-runtime.json" >/dev/null grep -F '"state": "rolled-back"' \ "$root/state/updates/activated-runtime.json" >/dev/null grep -F 'F 1.3.0' "$root/health.log" >/dev/null [[ ! -e $root/versions/1.3.0 ]] "$updater" --root "$root" request rollback-runtime --version 1.1.0 \ --retain 3 --origin test >/dev/null "$updater" --root "$root" consume >/dev/null [[ $(readlink -- "$root/current") == versions/1.1.0 ]] grep -F '"version": "1.1.0"' "$root/state/updates/activated-runtime.json" >/dev/null "$updater" --root "$root" rollback-runtime --version 1.0.0 \ --retain 3 >/dev/null [[ $(readlink -- "$root/current") == versions/1.0.0 ]] grep -F '"operation":"rollback-runtime"' "$root/state/updates/history.jsonl" >/dev/null if "$updater" --root "$root" rollback-runtime --version '../../tmp' \ >/dev/null 2>&1; then echo "updater accepted an unsafe rollback target" >&2 exit 1 fi [[ $(readlink -- "$root/current") == versions/1.0.0 ]] "$updater" --root "$root" reconcile >/dev/null grep -F '"current": "1.0.0"' "$root/state/updates/status.json" >/dev/null [[ $(sha256sum "$stable_helper") == "$stable_helper_sha "* ]] # Retention is evaluated only after a committed health confirmation or an # explicit validated rollback. Active plus one previous healthy version is # retained; known failed and older healthy versions are eligible for pruning. "$updater" --root "$root" rollback-runtime --version 1.2.0 \ --retain 1 >/dev/null [[ $(readlink -- "$root/current") == versions/1.2.0 ]] [[ -d $root/versions/1.0.0 ]] [[ ! -e $root/versions/1.1.0 ]] [[ ! -e $root/versions/1.3.0 ]] [[ ! -e $root/staging ]] echo "updater lifecycle tests passed"