#!/bin/sh set -eu PARENT_URL=${1:-} UPDATE_JOB_ID=${2:-} CONF=/usr/local/etc/pizur-monitor/agent.json BASE=/usr/local/lib/pizur-monitor-agent EXPECTED_RELEASE_KEY_SHA256=C0F5B6B1AAACD1DDF1E51BC42B894532C167BA10B494996F660C0F4AB8ACFA59 if [ "$(id -u)" -ne 0 ]; then echo "Run with sudo to upgrade the system agent." >&2; exit 1; fi if [ ! -f "$CONF" ] || [ ! -f "$BASE/agent.py" ]; then echo "Pizur Monitor agent is not installed." >&2; exit 1; fi if [ -z "$PARENT_URL" ]; then PARENT_URL=$(python3 -c 'import json; print(json.load(open("/usr/local/etc/pizur-monitor/agent.json"))["parent_url"])'); fi if [ "${PARENT_URL#https://}" = "$PARENT_URL" ]; then echo "The Parent URL must use HTTPS." >&2; exit 1; fi PARENT_URL=${PARENT_URL%/} CA_CERT_PATH=$(python3 -c 'import json; print(json.load(open("/usr/local/etc/pizur-monitor/agent.json")).get("ca_cert", ""))') VERIFY_DIR=$(mktemp -d) report_update() { [ -n "$UPDATE_JOB_ID" ] || return 0 python3 - "$CONF" "$UPDATE_JOB_ID" "$1" "$2" <<'PY' || true import json, ssl, sys, urllib.request config_path, job_id, state, message = sys.argv[1:] try: config = json.load(open(config_path, encoding="utf-8")) context = ssl.create_default_context(cafile=config.get("ca_cert") or None) payload = json.dumps({"state": state, "message": message[:500]}).encode() request = urllib.request.Request(config["parent_url"].rstrip("/") + "/v1/agent-update-jobs/" + job_id + "/status", data=payload, headers={"Authorization": "Bearer " + config["agent_token"], "Content-Type": "application/json"}) urllib.request.urlopen(request, timeout=240, context=context).read() except Exception: pass PY } cleanup_update() { result=$? rm -rf "$VERIFY_DIR" if [ "$result" -ne 0 ]; then report_update failed "FreeBSD agent update failed with exit code $result."; fi } trap cleanup_update EXIT trap 'exit 1' HUP INT TERM report_update running "Verifying the signed FreeBSD agent release." fetch_artifact() { if [ -n "$CA_CERT_PATH" ] && [ -f "$CA_CERT_PATH" ]; then curl --proto '=https' --tlsv1.2 --fail --silent --show-error --max-redirs 0 --cacert "$CA_CERT_PATH" \ "$PARENT_URL/downloads/$1" -o "$VERIFY_DIR/$1" else curl --proto '=https' --tlsv1.2 --fail --silent --show-error --max-redirs 0 \ "$PARENT_URL/downloads/$1" -o "$VERIFY_DIR/$1" fi } for artifact in agent.py agent.py.sig release-signing-public.pem; do fetch_artifact "$artifact"; done actual_release_key=$(openssl dgst -sha256 "$VERIFY_DIR/release-signing-public.pem" | awk '{print toupper($NF)}') if [ "$actual_release_key" != "$EXPECTED_RELEASE_KEY_SHA256" ]; then echo "Agent release signing key fingerprint mismatch." >&2; exit 1; fi openssl dgst -sha256 -verify "$VERIFY_DIR/release-signing-public.pem" \ -signature "$VERIFY_DIR/agent.py.sig" "$VERIFY_DIR/agent.py" >/dev/null || { echo "Agent package signature verification failed." >&2; exit 1; } cp -p "$BASE/agent.py" "$BASE/agent.py.previous" install -m 0555 "$VERIFY_DIR/agent.py" "$BASE/agent.py.new" mv -f "$BASE/agent.py.new" "$BASE/agent.py" if ! service pizur_monitor_agent restart; then mv -f "$BASE/agent.py.previous" "$BASE/agent.py" service pizur_monitor_agent restart || true echo "Upgrade failed; restored the previous FreeBSD agent." >&2 exit 1 fi rm -f "$BASE/agent.py.previous" report_update succeeded "FreeBSD agent upgraded to the latest signed release." echo "Pizur Monitor FreeBSD agent upgraded."