#!/usr/bin/env bash set -euo pipefail PARENT_URL="${1:-}" UPDATE_JOB_ID="${2:-}" CONF="/etc/pizur-monitor/agent.json" BASE="/usr/local/lib/pizur-monitor-agent" LABEL="local.pizurhouse.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 on this host." >&2 exit 1 fi if [[ -z "$PARENT_URL" ]]; then PARENT_URL="$(python3 -c 'import json; print(json.load(open("/etc/pizur-monitor/agent.json"))["parent_url"])')" fi if [[ "$PARENT_URL" != https://* ]]; then echo "The Parent URL must use HTTPS." >&2 exit 1 fi CA_CERT_PATH="$(python3 -c 'import json; print(json.load(open("/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 "macOS agent update failed with exit code $result."; fi } trap cleanup_update EXIT report_update running "Verifying the signed macOS agent release." CA_ARGS=() if [[ -n "$CA_CERT_PATH" && -f "$CA_CERT_PATH" ]]; then CA_ARGS=(--cacert "$CA_CERT_PATH"); fi PARENT_URL="${PARENT_URL%/}" for artifact in agent.py agent.py.sig release-signing-public.pem; do curl --proto '=https' --tlsv1.2 --fail --silent --show-error --max-redirs 0 \ "${CA_ARGS[@]}" "$PARENT_URL/downloads/$artifact" -o "$VERIFY_DIR/$artifact" done actual_release_key="$(shasum -a 256 "$VERIFY_DIR/release-signing-public.pem" | awk '{print toupper($1)}')" 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 0755 "$VERIFY_DIR/agent.py" "$BASE/agent.py.new" mv -f "$BASE/agent.py.new" "$BASE/agent.py" if ! launchctl kickstart -k "system/$LABEL" || ! launchctl print "system/$LABEL" >/dev/null 2>&1; then mv -f "$BASE/agent.py.previous" "$BASE/agent.py" launchctl kickstart -k "system/$LABEL" || true echo "Upgrade failed; restored the previous macOS agent." >&2 exit 1 fi rm -f "$BASE/agent.py.previous" report_update succeeded "macOS agent upgraded to the latest signed release." echo "Pizur Monitor macOS agent upgraded."