#!/usr/bin/env bash set -euo pipefail if [[ $# != 1 ]]; then echo "usage: $0 SOURCE_ROOT" >&2 exit 2 fi source_root=$(realpath -e -- "$1") temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-release-archive.XXXXXX") trap 'rm -rf -- "$temporary"' EXIT stage="$temporary/stage" mkdir -p "$stage/bin" "$stage/lib" install -m 0755 /bin/true "$stage/bin/echoiond" install -m 0755 /bin/true "$stage/bin/echoion-updater" install -m 0644 /bin/true "$stage/lib/Echoion.ts" printf '%s\n' \ '{"schemaVersion":2,"version":"1.2.3","buildType":"RelWithDebInfo","buildSignature":"test:1.2.3"}' \ >"$stage/manifest.json" cat >"$stage/install.sh" <<'INSTALL' #!/bin/sh set -eu payload_root=$(CDPATH= cd -P -- "$(dirname -- "$0")" && pwd -P) [ "$(stat -c '%a' -- "$payload_root/bin/echoiond")" = 755 ] [ "$(stat -c '%a' -- "$payload_root/lib/Echoion.ts")" = 644 ] [ "$1" = --record ] record=$2 shift 2 printf '%s\n' "$@" >"$record" INSTALL printf '%s\n' '#!/bin/sh' 'exit 0' >"$stage/uninstall.sh" chmod 0755 "$stage/install.sh" "$stage/uninstall.sh" base=echoion-1.2.3-linux-x86_64 archive_one="$temporary/one.tar.zst" installer_one="$temporary/one.run" archive_two="$temporary/two.tar.zst" installer_two="$temporary/two.run" bash "$source_root/scripts/package-release-payload.sh" \ "$stage" "$base" 1700000000 "$archive_one" "$installer_one" "$temporary" bash "$source_root/scripts/package-release-payload.sh" \ "$stage" "$base" 1700000000 "$archive_two" "$installer_two" "$temporary" cmp "$archive_one" "$archive_two" cmp "$installer_one" "$installer_two" [[ $(stat -c '%a' -- "$archive_one") == 644 ]] [[ $(stat -c '%a' -- "$archive_two") == 644 ]] [[ $(stat -c '%a' -- "$installer_one") == 755 ]] [[ $(stat -c '%a' -- "$installer_two") == 755 ]] feed_payload="$temporary/update-payload.json" core_feed_payload="$temporary/core-update-payload.json" plugin_feed_payload="$temporary/plugin-update-payload.json" feed_envelope="$temporary/update-envelope.json" private_key="$temporary/update-private.pem" public_key="$temporary/update-public.pem" signature="$temporary/update-signature.bin" SOURCE_DATE_EPOCH=1700000000 python3 \ "$source_root/scripts/generate-update-feed.py" \ --artifact "$archive_one" \ --manifest "$stage/manifest.json" \ --version 1.2.3 \ --url https://updates.example.invalid/echoion-1.2.3-linux-x86_64.tar.zst \ --channel beta \ --feed-id echoion-test \ --sequence 42 \ --output-payload "$feed_payload" python3 - "$feed_payload" "$archive_one" "$stage/manifest.json" <<'PY' import hashlib import json from pathlib import Path import sys payload = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) artifact = Path(sys.argv[2]) manifest = Path(sys.argv[3]) release = payload["releases"][0] assert payload["schemaVersion"] == 1 assert payload["releases"][0]["buildType"] == "RelWithDebInfo" assert payload["releases"][0]["buildSignature"] == "test:1.2.3" assert payload["feedId"] == "echoion-test" assert payload["sequence"] == 42 assert release["component"] == "runtime" assert release["artifactId"] == "" assert release["channel"] == "beta" assert release["packageType"] == "tar.zst" assert release["size"] == artifact.stat().st_size assert release["sha256"] == hashlib.sha256(artifact.read_bytes()).hexdigest() assert release["releaseManifestSha256"] == hashlib.sha256( manifest.read_bytes() ).hexdigest() PY SOURCE_DATE_EPOCH=1700000000 python3 \ "$source_root/scripts/generate-update-feed.py" \ --artifact "$stage/lib/Echoion.ts" \ --component core \ --version 1.2.3 \ --url https://updates.example.invalid/Echoion-1.2.3.so \ --feed-id echoion-core-test \ --sequence 43 \ --output-payload "$core_feed_payload" SOURCE_DATE_EPOCH=1700000000 python3 \ "$source_root/scripts/generate-update-feed.py" \ --artifact "$stage/lib/Echoion.ts" \ --component plugin \ --artifact-id lyrics \ --version 2.3.4 \ --url https://updates.example.invalid/lyrics-2.3.4.so \ --feed-id echoion-plugin-test \ --sequence 44 \ --output-payload "$plugin_feed_payload" python3 - "$core_feed_payload" "$plugin_feed_payload" <<'PY' import json from pathlib import Path import sys core = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))["releases"][0] plugin = json.loads(Path(sys.argv[2]).read_text(encoding="utf-8"))["releases"][0] assert core["component"] == "core" assert core["artifactId"] == "" assert core["packageType"] == "shared-library" assert "releaseManifestSha256" not in core assert plugin["component"] == "plugin" assert plugin["artifactId"] == "lyrics" assert plugin["packageType"] == "shared-library" assert "releaseManifestSha256" not in plugin PY openssl genpkey -algorithm ED25519 -out "$private_key" >/dev/null 2>&1 SOURCE_DATE_EPOCH=1700000000 python3 \ "$source_root/scripts/generate-update-feed.py" \ --artifact "$archive_one" \ --manifest "$stage/manifest.json" \ --version 1.2.3 \ --url https://updates.example.invalid/echoion-1.2.3-linux-x86_64.tar.zst \ --sequence 42 \ --output-payload "$feed_payload" \ --output-envelope "$feed_envelope" \ --key-id test-key \ --private-key "$private_key" python3 - "$feed_payload" "$feed_envelope" "$signature" <<'PY' import base64 import json from pathlib import Path import sys payload = Path(sys.argv[1]).read_bytes() envelope = json.loads(Path(sys.argv[2]).read_text(encoding="utf-8")) def decode(value: str) -> bytes: return base64.urlsafe_b64decode(value + "=" * (-len(value) % 4)) assert envelope["schemaVersion"] == 3 assert envelope["keyId"] == "test-key" assert envelope["document"] == json.loads(payload) assert decode(envelope["signedPayload"]) == payload Path(sys.argv[3]).write_bytes(decode(envelope["signature"])) PY openssl pkey -in "$private_key" -pubout -out "$public_key" >/dev/null 2>&1 openssl pkeyutl -verify -rawin -pubin -inkey "$public_key" \ -in "$feed_payload" -sigfile "$signature" >/dev/null mapfile -t members < <(tar --zstd -tf "$archive_one") ((${#members[@]} > 0)) for member in "${members[@]}"; do [[ $member == "$base" || $member == "$base/"* ]] || { echo "archive member escaped top-level directory: $member" >&2 exit 1 } done printf '%s\n' "${members[@]}" | grep -Fx "$base/bin/echoiond" record="$temporary/installer-arguments" ( umask 077 "$installer_one" --record "$record" alpha 'two words' ) printf '%s\n' alpha 'two words' >"$temporary/expected-arguments" cmp "$temporary/expected-arguments" "$record" path_installer_directory="$temporary/path-installer" mkdir "$path_installer_directory" install -m 0755 "$installer_one" "$path_installer_directory/echoion-installer" path_record="$temporary/path-installer-arguments" ( cd / PATH="$path_installer_directory:$PATH" \ echoion-installer --record "$path_record" alpha 'two words' ) cmp "$temporary/expected-arguments" "$path_record" echo "release archive layout and .run regressions passed"