2026-08-27 18:56:57 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# install-agent.sh — Heimdall Agent installer for Linux
|
|
|
|
|
set -euo pipefail
|
2026-10-23 18:54:14 +02:00
|
|
|
CENTRAL_URL="" SITE_TOKEN="" AGENT_NAME="" STATE_DIR="/var/lib/heimdall" UPDATE_PUBLIC_KEY="" ALLOW_INSECURE_HTTP=false
|
|
|
|
|
INSTALL_ROOT="/opt/heimdall/agent" SERVICE_NAME="heimdall-agent" SERVICE_USER="heimdall"
|
|
|
|
|
while [[ $# -gt 0 ]]; do case "$1" in --central-url) CENTRAL_URL="$2"; shift 2;; --site-token) SITE_TOKEN="$2"; shift 2;; --update-public-key) UPDATE_PUBLIC_KEY="$2"; shift 2;; --agent-name) AGENT_NAME="$2"; shift 2;; --state-dir) STATE_DIR="$2"; shift 2;; --allow-insecure-http) ALLOW_INSECURE_HTTP=true; shift;; -h|--help) echo "Usage: $0 --central-url URL --site-token TOKEN --update-public-key LOCAL_PEM [--agent-name NAME] [--state-dir PATH] [--allow-insecure-http]"; exit 0;; *) echo "Unknown: $1" >&2; exit 1;; esac; done
|
|
|
|
|
[[ -z "$CENTRAL_URL" || -z "$SITE_TOKEN" || -z "$UPDATE_PUBLIC_KEY" ]] && { echo "Error: --central-url, --site-token and --update-public-key required" >&2; exit 1; }
|
|
|
|
|
[[ -f "$UPDATE_PUBLIC_KEY" ]] || { echo 'Publisher public key file is missing' >&2; exit 1; }
|
|
|
|
|
command -v jq >/dev/null && command -v openssl >/dev/null && command -v python3 >/dev/null && command -v dotnet >/dev/null || { echo 'jq, openssl, python3 and the .NET 10 runtime are required' >&2; exit 1; }
|
|
|
|
|
DOTNET_BIN=$(command -v dotnet)
|
2026-08-27 18:56:57 +02:00
|
|
|
CENTRAL_URL="${CENTRAL_URL%/}"
|
2026-10-22 06:56:06 +02:00
|
|
|
if [[ "$CENTRAL_URL" != https://* && ! ( "$ALLOW_INSECURE_HTTP" == true && "$CENTRAL_URL" == http://* ) ]]; then
|
|
|
|
|
echo "Error: --central-url must use HTTPS (or explicitly opt in with --allow-insecure-http on an isolated network)" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2026-08-27 18:56:57 +02:00
|
|
|
ARCH=$(uname -m); case "$ARCH" in x86_64|amd64) PLATFORM="linux-x64";; aarch64|arm64) PLATFORM="linux-arm64";; armv7l|armhf) PLATFORM="linux-arm";; *) echo "Unsupported: $ARCH" >&2; exit 1;; esac
|
|
|
|
|
echo "Installing Heimdall Agent for $PLATFORM..."
|
|
|
|
|
TMP_DIR=$(mktemp -d); trap 'rm -rf "$TMP_DIR"' EXIT
|
2026-10-23 18:54:14 +02:00
|
|
|
curl -fsS "$CENTRAL_URL/api/updates/latest" -o "$TMP_DIR/release.json" || { echo "Failed to fetch release" >&2; exit 1; }
|
|
|
|
|
jq -r '.signedManifest // empty' "$TMP_DIR/release.json" | base64 -d > "$TMP_DIR/manifest.json" || { echo 'Invalid signed manifest encoding' >&2; exit 1; }
|
|
|
|
|
jq -r '.manifestSignature // empty' "$TMP_DIR/release.json" | base64 -d > "$TMP_DIR/manifest.sig" || { echo 'Invalid release signature encoding' >&2; exit 1; }
|
|
|
|
|
openssl dgst -sha256 -verify "$UPDATE_PUBLIC_KEY" -signature "$TMP_DIR/manifest.sig" "$TMP_DIR/manifest.json" >/dev/null || { echo 'Release signature verification failed' >&2; exit 1; }
|
|
|
|
|
VERSION=$(jq -r '.version' "$TMP_DIR/manifest.json")
|
|
|
|
|
[[ "$VERSION" == "$(jq -r '.version' "$TMP_DIR/release.json")" && "$VERSION" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+([._-][0-9A-Za-z.-]+)?$ ]] || { echo 'Invalid signed release version' >&2; exit 1; }
|
|
|
|
|
ASSET_PLATFORM=$(jq -r --arg platform "$PLATFORM" 'if .assets[$platform] then $platform elif .assets.portable then "portable" else empty end' "$TMP_DIR/manifest.json")
|
|
|
|
|
[[ -n "$ASSET_PLATFORM" ]] || { echo "No signed archive for $PLATFORM" >&2; exit 1; }
|
|
|
|
|
EXPECTED_SHA256=$(jq -r --arg platform "$ASSET_PLATFORM" '.assets[$platform]' "$TMP_DIR/manifest.json")
|
|
|
|
|
DOWNLOAD_URL=$(jq -r --arg platform "$ASSET_PLATFORM" '.assets[$platform].url // empty' "$TMP_DIR/release.json")
|
|
|
|
|
RELEASE_SHA256=$(jq -r --arg platform "$ASSET_PLATFORM" '.assets[$platform].sha256 // empty' "$TMP_DIR/release.json")
|
|
|
|
|
[[ "$EXPECTED_SHA256" =~ ^[[:xdigit:]]{64}$ && "${EXPECTED_SHA256,,}" == "${RELEASE_SHA256,,}" && "$DOWNLOAD_URL" == https://* ]] || { echo 'Release archive differs from signed manifest' >&2; exit 1; }
|
|
|
|
|
curl -fsSL --proto '=https' --proto-redir '=https' "$DOWNLOAD_URL" -o "$TMP_DIR/agent.tar.gz" || { echo "Download failed" >&2; exit 1; }
|
|
|
|
|
ACTUAL=$(sha256sum "$TMP_DIR/agent.tar.gz" | cut -d' ' -f1); [[ "${ACTUAL,,}" == "${EXPECTED_SHA256,,}" ]] || { echo "SHA256 mismatch" >&2; exit 1; }
|
|
|
|
|
echo "Signed archive verified: $VERSION"
|
|
|
|
|
mkdir -p "$TMP_DIR/agent"
|
|
|
|
|
python3 - "$TMP_DIR/agent.tar.gz" "$TMP_DIR/agent" <<'PY'
|
|
|
|
|
import pathlib, shutil, sys, tarfile
|
|
|
|
|
root=pathlib.Path(sys.argv[2]).resolve()
|
|
|
|
|
with tarfile.open(sys.argv[1], 'r:gz') as archive:
|
|
|
|
|
members=archive.getmembers()
|
|
|
|
|
if len(members)>20000 or sum(m.size for m in members)>2_000_000_000:
|
|
|
|
|
raise SystemExit('Archive exceeds extraction limits')
|
|
|
|
|
for member in members:
|
|
|
|
|
if not (member.isfile() or member.isdir()):
|
|
|
|
|
raise SystemExit('Archive contains a link or unsupported entry')
|
|
|
|
|
target=(root/member.name).resolve()
|
|
|
|
|
if target!=root and root not in target.parents:
|
|
|
|
|
raise SystemExit('Archive path escapes extraction root')
|
|
|
|
|
if member.isdir():
|
|
|
|
|
target.mkdir(parents=True,exist_ok=True)
|
|
|
|
|
else:
|
|
|
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
|
|
|
with archive.extractfile(member) as source, open(target,'xb') as output:
|
|
|
|
|
shutil.copyfileobj(source,output)
|
|
|
|
|
PY
|
|
|
|
|
[[ -f "$TMP_DIR/agent/Heimdall.Agent.dll" ]] || { echo 'Archive has no agent assembly' >&2; exit 1; }
|
2026-08-27 18:56:57 +02:00
|
|
|
id "$SERVICE_USER" &>/dev/null || sudo useradd --system --no-create-home --shell /usr/sbin/nologin "$SERVICE_USER" 2>/dev/null || true
|
|
|
|
|
sudo mkdir -p "$STATE_DIR" && sudo chown "$SERVICE_USER:$SERVICE_USER" "$STATE_DIR" && sudo chmod 700 "$STATE_DIR"
|
2026-10-23 18:54:14 +02:00
|
|
|
sudo install -d -o "$SERVICE_USER" -g "$SERVICE_USER" -m 750 "$INSTALL_ROOT" "$INSTALL_ROOT/releases"
|
|
|
|
|
[[ ! -e "$INSTALL_ROOT/releases/$VERSION" ]] || { echo 'Release already installed' >&2; exit 1; }
|
|
|
|
|
sudo mv "$TMP_DIR/agent" "$INSTALL_ROOT/releases/$VERSION"
|
|
|
|
|
sudo chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_ROOT/releases/$VERSION"
|
|
|
|
|
sudo ln -sfn "$INSTALL_ROOT/releases/$VERSION" "$INSTALL_ROOT/current"
|
|
|
|
|
sudo install -d -o root -g root -m 755 /etc/heimdall
|
|
|
|
|
sudo install -o root -g root -m 644 "$UPDATE_PUBLIC_KEY" /etc/heimdall/update-public-key.pem
|
|
|
|
|
sudo install -d -o root -g root -m 755 /usr/local/libexec
|
|
|
|
|
sudo install -o root -g root -m 755 "$(dirname "$0")/agent-supervisor.py" /usr/local/libexec/heimdall-agent-supervisor.py
|
2026-10-22 06:57:50 +02:00
|
|
|
sudo install -d -o root -g root -m 700 /etc/heimdall/credentials
|
|
|
|
|
printf '%s' "$SITE_TOKEN" | sudo tee /etc/heimdall/credentials/site-token >/dev/null
|
|
|
|
|
sudo chmod 600 /etc/heimdall/credentials/site-token
|
|
|
|
|
AGENT_NAME_CREDENTIAL=""
|
|
|
|
|
if [[ -n "$AGENT_NAME" ]]; then
|
|
|
|
|
printf '%s' "$AGENT_NAME" | sudo tee /etc/heimdall/credentials/agent-name >/dev/null
|
|
|
|
|
sudo chmod 600 /etc/heimdall/credentials/agent-name
|
|
|
|
|
AGENT_NAME_CREDENTIAL="LoadCredential=agent-name:/etc/heimdall/credentials/agent-name"
|
|
|
|
|
fi
|
2026-08-27 18:56:57 +02:00
|
|
|
cat <<EOF | sudo tee /etc/systemd/system/$SERVICE_NAME.service >/dev/null
|
|
|
|
|
[Unit]
|
|
|
|
|
Description=Heimdall Agent
|
|
|
|
|
After=network-online.target
|
|
|
|
|
[Service]
|
|
|
|
|
Type=simple
|
|
|
|
|
User=$SERVICE_USER
|
2026-10-23 18:54:14 +02:00
|
|
|
ExecStart=/usr/bin/python3 /usr/local/libexec/heimdall-agent-supervisor.py
|
|
|
|
|
WorkingDirectory=$INSTALL_ROOT/current
|
|
|
|
|
Restart=always
|
|
|
|
|
RestartSec=5
|
|
|
|
|
Environment=ASPNETCORE_URLS=http://127.0.0.1:8081
|
|
|
|
|
Environment=HEIMDALL_DOTNET=$DOTNET_BIN
|
2026-08-27 18:56:57 +02:00
|
|
|
Environment=ASPNETCORE_ENVIRONMENT=Production
|
|
|
|
|
Environment=Agent__CentralUrl=$CENTRAL_URL
|
2026-10-22 06:56:06 +02:00
|
|
|
Environment=Agent__AllowInsecureHttp=$ALLOW_INSECURE_HTTP
|
2026-08-27 18:56:57 +02:00
|
|
|
Environment=Agent__StateDirectory=$STATE_DIR
|
2026-10-23 18:54:14 +02:00
|
|
|
Environment=Agent__UpdateInstallRoot=$INSTALL_ROOT
|
|
|
|
|
Environment=Agent__UpdatePublicKeyPath=/etc/heimdall/update-public-key.pem
|
2026-10-22 06:57:50 +02:00
|
|
|
LoadCredential=site-token:/etc/heimdall/credentials/site-token
|
|
|
|
|
$AGENT_NAME_CREDENTIAL
|
2026-08-27 18:56:57 +02:00
|
|
|
[Install]
|
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
EOF
|
|
|
|
|
sudo systemctl daemon-reload && sudo systemctl enable "$SERVICE_NAME" && sudo systemctl start "$SERVICE_NAME"
|
|
|
|
|
echo "Done! Version: $VERSION, Service: $SERVICE_NAME"
|