diff --git a/infra/gitea-runners/runbook.md b/infra/gitea-runners/runbook.md index 2eb78bdf..b0553237 100644 --- a/infra/gitea-runners/runbook.md +++ b/infra/gitea-runners/runbook.md @@ -595,9 +595,11 @@ registration and filesystem are never reused. For every creation and reuse rebuild, the controller generates a unique SSH host key, injects it through authenticated Hetzner `user_data`, and pins that exact public key before sending a registration token. Shared image host keys -and trust-on-first-use are not used. Runner images must process Hetzner -cloud-init `write_files` during create and rebuild; verify this image gate before -dispatching jobs. If host-key injection fails, bootstrap remains fail-closed. +and trust-on-first-use are not used. Runner images must process cloud-init's +native `ssh_keys` with `ssh_deletekeys: true` and `ssh_genkeytypes: [ed25519]` +during create and rebuild. Before release, verify cloud-init completion and +agreement between generated, pinned, and live host-key fingerprints on the +target image. If host-key injection fails, bootstrap remains fail-closed. Reuse preserves original Hetzner VM creation time, hard TTL, and budget charge; it creates no server and makes no second reservation. Provider or registration diff --git a/package/gitea-runner-controller/controller.sh b/package/gitea-runner-controller/controller.sh index 71b63dfc..fccc0af7 100644 --- a/package/gitea-runner-controller/controller.sh +++ b/package/gitea-runner-controller/controller.sh @@ -631,7 +631,7 @@ gcr_bootstrap_pending() { gcr_log info --ns=alloc "bootstrapping vm=$vm_id ip=$ip job=$job_id" if gcr_vm_bootstrap_ssh "$ip" "$label" "$reg_token" "$runner_name" \ - "$known_hosts_file"; then + "$known_hosts_file" "$job_id" "$attempt" "$vm_id"; then rec="$(printf '%s' "$rec" | jq -c '.bootstrapped = true | .ip = $ip' --arg ip "$ip")" gcr_record_put "$job_id" "$attempt" "$rec" gcr_event "vm-bootstrapped" "$job_id" "{\"vm_id\":$vm_id,\"ip\":\"$ip\"}" diff --git a/package/gitea-runner-controller/hcloud.sh b/package/gitea-runner-controller/hcloud.sh index 69a5a303..37dbb631 100644 --- a/package/gitea-runner-controller/hcloud.sh +++ b/package/gitea-runner-controller/hcloud.sh @@ -32,35 +32,51 @@ gcr_vm_host_key_path() { printf '%s/ssh-host-keys/%s' "$GCR_STATE_DIR" "$(gcr_alloc_key "$1" "$2")" } +gcr_vm_host_key_pair_valid() { + host_key_path="$1" + [ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ] || return 1 + derived_public="$(ssh-keygen -y -f "$host_key_path" 2>/dev/null)" || return 1 + derived_public="$(printf '%s\n' "$derived_public" | awk 'NR == 1 {print $1 " " $2}')" + expected_public="$(awk 'NR == 1 {print $1 " " $2}' "$host_key_path.pub")" + [ -n "$derived_public" ] && [ "$derived_public" = "$expected_public" ] +} + gcr_vm_host_key_prepare() { host_key_path="$(gcr_vm_host_key_path "$1" "$2")" if [ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ]; then + if ! gcr_vm_host_key_pair_valid "$host_key_path"; then + gcr_log error --ns=hcloud "existing SSH host-key pair is invalid job=$1 attempt=$2" + return 1 + fi + chmod 0600 "$host_key_path" + chmod 0644 "$host_key_path.pub" return 0 fi rm -f "$host_key_path" "$host_key_path.pub" ssh-keygen -q -t ed25519 -N '' -C '' -f "$host_key_path" || return 1 chmod 0600 "$host_key_path" chmod 0644 "$host_key_path.pub" + gcr_vm_host_key_pair_valid "$host_key_path" } gcr_vm_host_key_userdata() { host_key_path="$(gcr_vm_host_key_path "$1" "$2")" - [ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ] || return 1 - host_private_b64="$(base64 < "$host_key_path" | tr -d '\n')" || return 1 - host_public_b64="$(base64 < "$host_key_path.pub" | tr -d '\n')" || return 1 + gcr_vm_host_key_pair_valid "$host_key_path" || { + gcr_log error --ns=hcloud "SSH host-key pair is invalid job=$1 attempt=$2" + return 1 + } + host_public_key="$(awk 'NR == 1 {print $1 " " $2}' "$host_key_path.pub")" \ + || return 1 + [ -n "$host_public_key" ] || return 1 cat </dev/null || systemctl restart ssh.service' ] EOF @@ -69,7 +85,10 @@ EOF gcr_vm_known_hosts_file() { known_job="$1"; known_attempt="$2"; known_ip="$3" known_key_path="$(gcr_vm_host_key_path "$known_job" "$known_attempt")" - [ -s "$known_key_path.pub" ] || return 1 + gcr_vm_host_key_pair_valid "$known_key_path" || { + gcr_log error --ns=hcloud "cannot pin invalid SSH host-key pair job=$known_job attempt=$known_attempt" + return 1 + } known_type="$(cut -d' ' -f1 < "$known_key_path.pub")" known_key="$(cut -d' ' -f2 < "$known_key_path.pub")" [ -n "$known_type" ] && [ -n "$known_key" ] || return 1 @@ -681,6 +700,7 @@ gcr_vm_collect_diagnostics() { printf "== kernel failure signals ==\n"; dmesg -T 2>/dev/null | grep -Ei "out of memory|oom-kill|killed process|no space|I/O error|EXT4-fs error|xfs.*error|nvme.*error" | tail -n 80 printf "== runner service ==\n"; systemctl show gitea-runner.service -p ActiveState -p SubState -p Result -p ExecMainStatus -p ExecMainCode -p NRestarts 2>/dev/null printf "== bootstrap service ==\n"; systemctl show gcr-bootstrap.service -p ActiveState -p SubState -p Result -p ExecMainStatus -p ExecMainCode 2>/dev/null + printf "== cloud-init ==\n"; if command -v cloud-init >/dev/null 2>&1; then cloud-init status --long 2>/dev/null | head -n 20; else printf "cloud-init unavailable\\n"; fi printf "== process sample ==\n"; ps -eo pid,ppid,stat,etime,comm 2>/dev/null | head -n 80' \ > "$diag_out" 2>&1; then gcr_redact < "$diag_out" >&2 @@ -693,6 +713,44 @@ gcr_vm_collect_diagnostics() { return 0 } +gcr_ssh_error_category() { + error_text="$(cat "$1")" + case "$error_text" in + *"REMOTE HOST IDENTIFICATION HAS CHANGED"*|*"Host key verification failed"*) + printf 'host-key-verification' + ;; + *"Permission denied"*|*"Too many authentication failures"*) + printf 'authentication' + ;; + *"Connection refused"*|*"Connection timed out"*|*"Operation timed out"*|\ + *"No route to host"*|*"Network is unreachable"*|*"Connection reset"*|\ + *"Could not resolve hostname"*|*"Name or service not known"*) + printf 'connectivity-or-sshd' + ;; + *"Bad configuration option"*|*"Could not open"*|*"Load key"*) + printf 'ssh-client-error' + ;; + *) + printf 'unknown' + ;; + esac +} + +gcr_ssh_run_captured() { + captured_error_file="$1"; shift + ( + if ! ulimit -f 8; then + exit 125 + fi + "$@" + ) 2>"$captured_error_file" +} + +gcr_ssh_temp_cleanup() { + [ -z "${1:-}" ] || rm -f "$1" + [ -z "${2:-}" ] || rm -f "$2" +} + # Retry durable repository-runner retirement. Generic idle is written only # after exact-name absence is confirmed and the persisted VM profile is valid. gcr_vm_retire_pending() { @@ -919,10 +977,16 @@ systemctl enable --now gitea-runner.service BSEOF } -# gcr_vm_bootstrap_ssh IP LABEL REG_TOKEN — blocking; returns ssh exit status. -gcr_vm_bootstrap_ssh() { +# gcr_vm_bootstrap_ssh IP LABEL REG_TOKEN RUNNER_NAME KNOWN_HOSTS JOB ATTEMPT VM +# Waits for strict-pinned SSH readiness, then pushes the bootstrap script. +gcr_vm_bootstrap_ssh() ( ip="$1"; label="$2"; reg_token="$3"; runner_name="$4" known_hosts_file="${5:-${GCR_SSH_KNOWN_HOSTS_FILE:-}}" + job_id="${6:-}"; attempt="${7:-}"; vm_id="${8:-}" + key_tmp="" + ssh_error_file="" + trap 'gcr_ssh_temp_cleanup "${key_tmp:-}" "${ssh_error_file:-}"' EXIT + trap 'exit 1' HUP INT TERM test -n "${GCR_SSH_PRIVKEY_FILE:-}" && test -r "$GCR_SSH_PRIVKEY_FILE" || { gcr_log error --ns=hcloud "GCR_SSH_PRIVKEY_FILE missing or unreadable" return 1 @@ -937,12 +1001,22 @@ gcr_vm_bootstrap_ssh() { chmod 0600 "$key_tmp" SSH_OPTS="-i $key_tmp -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$known_hosts_file -o ConnectTimeout=5 -o BatchMode=yes" + ssh_error_file="$(mktemp "${TMPDIR:-/tmp}/gcr-ssh-error.XXXXXX")" + chmod 0600 "$ssh_error_file" waited=0 - until ssh $SSH_OPTS "root@$ip" true 2>/dev/null; do + ssh_status=0 + while :; do + if gcr_ssh_run_captured "$ssh_error_file" \ + ssh $SSH_OPTS "root@$ip" true; then + break + else + ssh_status="$?" + fi waited=$((waited + 5)) [ "$waited" -ge 900 ] && { - gcr_log warn --ns=hcloud "sshd never came up on $ip" - rm -f "$key_tmp" + ssh_category="$(gcr_ssh_error_category "$ssh_error_file")" + gcr_log warn --ns=hcloud \ + "SSH readiness failed stage=ssh-readiness category=$ssh_category exit=$ssh_status after=${waited}s ip=$ip" return 1 } sleep 5 @@ -950,10 +1024,28 @@ gcr_vm_bootstrap_ssh() { ttl_min="$(printf '%s' "$(gcr_label_profile "$label")" | awk '{print $2}')" script="$(gcr_bootstrap_script "$label" "$reg_token" "$ttl_min" "$runner_name")" - if printf '%s' "$script" | ssh $SSH_OPTS "root@$ip" sh -s; then - rm -f "$key_tmp" + if printf '%s' "$script" | gcr_ssh_run_captured "$ssh_error_file" \ + ssh $SSH_OPTS "root@$ip" sh -s; then return 0 + else + ssh_status="$?" + fi + ssh_category="$(gcr_ssh_error_category "$ssh_error_file")" + if [ "$ssh_status" -eq 255 ]; then + diagnostics_reason=bootstrap-transport-failed + if [ "$ssh_category" = unknown ]; then + ssh_category=ssh-client-error + fi + gcr_log warn --ns=hcloud \ + "SSH bootstrap failed stage=ssh-transport category=$ssh_category exit=$ssh_status ip=$ip" + else + diagnostics_reason=bootstrap-command-failed + gcr_log warn --ns=hcloud \ + "SSH bootstrap failed stage=remote-bootstrap-command category=remote-bootstrap-command-failed exit=$ssh_status ip=$ip" + fi + if [ -n "$job_id" ] && [ -n "$attempt" ] && [ -n "$vm_id" ]; then + gcr_vm_collect_diagnostics "$vm_id" "$ip" "$job_id" \ + "$diagnostics_reason" "$attempt" || true fi - rm -f "$key_tmp" return 1 -} +) diff --git a/test/package/gitea-runner-controller/test/highmem-label.sh b/test/package/gitea-runner-controller/test/highmem-label.sh index b74d20c6..ed1d9bc1 100644 --- a/test/package/gitea-runner-controller/test/highmem-label.sh +++ b/test/package/gitea-runner-controller/test/highmem-label.sh @@ -93,7 +93,9 @@ gcr_record_del 9001 1 test "$(wc -l < "$calls" | tr -d ' ')" = '3' jq -e 'select(.server_type == "ccx53" and .location == "nbg1" and (.user_data | startswith("#cloud-config\n")) and - (.user_data | contains("/etc/ssh/ssh_host_ed25519_key")) and + (.user_data | contains("ssh_deletekeys: true")) and + (.user_data | contains("ssh_genkeytypes: [ed25519]")) and + (.user_data | contains("ed25519_private: |")) and .labels["gcr.arch"] == "amd64" and .labels["gcr.image-id"] == "424242" and .labels["gcr.label"] == "gross-nix-x86-highmem" and diff --git a/test/package/gitea-runner-controller/test/idle-reuse.sh b/test/package/gitea-runner-controller/test/idle-reuse.sh index 9bee4aad..b175b2e5 100644 --- a/test/package/gitea-runner-controller/test/idle-reuse.sh +++ b/test/package/gitea-runner-controller/test/idle-reuse.sh @@ -204,7 +204,9 @@ gcr_hcloud_req() { POST:/servers/71/actions/rebuild) jq -e 'select(.image == "424242" and (.user_data | startswith("#cloud-config\n")) and - (.user_data | contains("/etc/ssh/ssh_host_ed25519_key")) and + (.user_data | contains("ssh_deletekeys: true")) and + (.user_data | contains("ssh_genkeytypes: [ed25519]")) and + (.user_data | contains("ed25519_private: |")) and keys == ["image", "user_data"])' </dev/null $body EOF diff --git a/test/package/gitea-runner-controller/test/ssh-bootstrap.sh b/test/package/gitea-runner-controller/test/ssh-bootstrap.sh new file mode 100644 index 00000000..7f1fde67 --- /dev/null +++ b/test/package/gitea-runner-controller/test/ssh-bootstrap.sh @@ -0,0 +1,225 @@ +#!/bin/dash +set -eu + +. "$LOG_SH" +. "$STATE_SH" +. "$DECIDE_SH" +. "$HCLOUD_SH" + +gcr_state_init +export GCR_LOG=warn + +key_path="$(gcr_vm_host_key_path 940 1)" +other_key_path="$GCR_STATE_DIR/other-host-key" +gcr_vm_host_key_prepare 940 1 +test "$(stat -c '%a' "$key_path")" = 600 +test "$(stat -c '%a' "$key_path.pub")" = 644 +gcr_vm_host_key_pair_valid "$key_path" + +ssh-keygen -q -t ed25519 -N '' -C '' -f "$other_key_path" +cp "$other_key_path.pub" "$key_path.pub" +if gcr_vm_host_key_prepare 940 1; then + printf 'mismatched existing host-key pair must fail closed\n' >&2 + exit 1 +fi +if gcr_vm_host_key_userdata 940 1 >/dev/null; then + printf 'invalid host-key pair must not enter cloud-init user-data\n' >&2 + exit 1 +fi +if gcr_vm_known_hosts_file 940 1 192.0.2.94 >/dev/null; then + printf 'invalid host-key pair must not create a known_hosts pin\n' >&2 + exit 1 +fi +ssh-keygen -y -f "$key_path" > "$key_path.pub" +chmod 0644 "$key_path.pub" +gcr_vm_host_key_pair_valid "$key_path" +host_key_userdata="$(gcr_vm_host_key_userdata 940 1)" +public_key_line="$(awk 'NR == 1 {print $1 " " $2}' "$key_path.pub")" +printf '%s\n' "$host_key_userdata" | grep -Eq '^ssh_deletekeys: true$' +printf '%s\n' "$host_key_userdata" | grep -Eq '^ssh_genkeytypes: \[ed25519\]$' +printf '%s\n' "$host_key_userdata" | grep -Fq ' ed25519_private: |' +printf '%s\n' "$host_key_userdata" | grep -Fq " ed25519_public: $public_key_line" +rendered_private_key="$(printf '%s\n' "$host_key_userdata" \ + | sed -n '/^ ed25519_private: |$/,/^ ed25519_public:/p' \ + | sed '1d;$d;s/^ //')" +test "$rendered_private_key" = "$(cat "$key_path")" +printf '%s\n' "$host_key_userdata" \ + | grep -Fq 'systemctl restart sshd.service 2>/dev/null || systemctl restart ssh.service' + +known_hosts_file="$(gcr_vm_known_hosts_file 940 1 192.0.2.94)" +test "$(stat -c '%a' "$known_hosts_file")" = 600 +grep -Eq '^192\.0\.2\.94 ssh-ed25519 [A-Za-z0-9+/=]+$' "$known_hosts_file" +long_error_file="$GCR_STATE_DIR/long-ssh-error" +large_stderr_called="$GCR_STATE_DIR/large-stderr-called" +large_stderr() { + printf 'called\n' > "$large_stderr_called" + awk 'BEGIN { for (i = 0; i < 10000; i++) printf "x" }' >&2 + return 255 +} +if gcr_ssh_run_captured "$long_error_file" large_stderr 2>/dev/null; then + printf 'SSH error capture must preserve command failure\n' >&2 + exit 1 +fi +test "$(cat "$large_stderr_called")" = called +test "$(wc -c < "$long_error_file" | tr -d ' ')" -le 4096 + +export GCR_SSH_PRIVKEY_FILE="$GCR_STATE_DIR/ssh-client-key" +printf 'test-only private key placeholder\n' > "$GCR_SSH_PRIVKEY_FILE" +chmod 0600 "$GCR_SSH_PRIVKEY_FILE" +export GCR_NIX_VERSION=2.24.11 +export GCR_NIX_TARBALL_SHA256=sha256 +export GCR_ARM_NIX_TARBALL_SHA256=sha256 +export GCR_ACT_RUNNER_VERSION=0.2.11 +export GCR_ACT_RUNNER_SHA256=sha256 +export GCR_GITEA_URL=https://gitea.example.test + +sleep_calls_file="$GCR_STATE_DIR/sleep-calls" +sleep() { + test "$1" = 5 + printf '5\n' >> "$sleep_calls_file" +} + +ssh_mode=host-key +ssh_calls_file="$GCR_STATE_DIR/ssh-calls" +strict_pin_file="$GCR_STATE_DIR/strict-pin-seen" +temp_paths="$GCR_STATE_DIR/bootstrap-temp-paths" +mktemp() { + created_temp="$(command mktemp "$@")" || return 1 + printf '%s\n' "$created_temp" >> "$temp_paths" + printf '%s' "$created_temp" +} +assert_bootstrap_temps_removed() { + while IFS= read -r temp_path; do + test ! -e "$temp_path" + done < "$temp_paths" +} +printf '0\n' > "$ssh_calls_file" +ssh() { + ssh_calls="$(cat "$ssh_calls_file")" + ssh_calls=$((ssh_calls + 1)) + printf '%s\n' "$ssh_calls" > "$ssh_calls_file" + case "$*" in + *StrictHostKeyChecking=yes*"UserKnownHostsFile=$known_hosts_file"*) + printf '1\n' > "$strict_pin_file" + ;; + *) + printf 'SSH bootstrap did not use strict per-job host-key pinning\n' >&2 + return 99 + ;; + esac + case "$ssh_mode" in + host-key) + printf '%s\n' \ + 'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!' \ + 'Host key verification failed.' \ + 'REGISTRATION_TOKEN=secret-value' \ + 'Authorization: token secret-value' >&2 + return 255 + ;; + retry) + if [ "$ssh_calls" -le 2 ]; then + printf 'ssh: connect to host 192.0.2.94 port 22: Connection refused\n' >&2 + return 255 + fi + if [ "$ssh_calls" -eq 3 ]; then + return 0 + fi + cat >/dev/null + printf 'bootstrap\n' >> "$retry_script_calls" + return 0 + ;; + remote-failure) + if [ "$ssh_calls" -eq 1 ]; then + return 0 + fi + cat >/dev/null + printf '%s\n' 'REGISTRATION_TOKEN=secret-value' \ + 'Authorization: token secret-value' 'cloud-init status: error' >&2 + return 1 + ;; + transport-failure) + if [ "$ssh_calls" -eq 1 ]; then + return 0 + fi + cat >/dev/null + printf '%s\n' 'Connection reset by peer' 'REGISTRATION_TOKEN=secret-value' >&2 + return 255 + ;; + esac +} + +host_key_log="$GCR_STATE_DIR/host-key.log" +: > "$temp_paths" +if gcr_vm_bootstrap_ssh 192.0.2.94 gross-x86 fresh-token gcr-940-1 \ + "$known_hosts_file" 940 1 994 > /dev/null 2> "$host_key_log"; then + printf 'host-key mismatch must block SSH bootstrap\n' >&2 + exit 1 +fi +grep -q 'category=host-key-verification' "$host_key_log" +if grep -Eq 'REMOTE HOST IDENTIFICATION|Host key verification failed|REGISTRATION_TOKEN|Authorization:|secret-value' "$host_key_log"; then + printf 'raw SSH stderr leaked into controller log\n' >&2 + exit 1 +fi +test "$(cat "$strict_pin_file")" = 1 +test "$(cat "$ssh_calls_file")" = 180 +test "$(wc -l < "$sleep_calls_file" | tr -d ' ')" = 179 +assert_bootstrap_temps_removed +if grep -Eq 'StrictHostKeyChecking=(no|accept-new)|sshd never came up' "$host_key_log"; then + printf 'unsafe SSH policy or stale failure message found\n' >&2 + exit 1 +fi + +ssh_mode=retry +printf '0\n' > "$ssh_calls_file" +rm -f "$sleep_calls_file" +retry_script_calls="$GCR_STATE_DIR/retry-script-calls" +: > "$temp_paths" +if ! gcr_vm_bootstrap_ssh 192.0.2.94 gross-x86 fresh-token gcr-940-1 \ + "$known_hosts_file" 940 1 994 > /dev/null 2> "$GCR_STATE_DIR/retry.log"; then + printf 'SSH readiness should succeed after two transient failures\n' >&2 + exit 1 +fi +test "$(cat "$ssh_calls_file")" = 4 +test "$(wc -l < "$sleep_calls_file" | tr -d ' ')" = 2 +test "$(wc -l < "$retry_script_calls" | tr -d ' ')" = 1 +assert_bootstrap_temps_removed + +diag_calls="$GCR_STATE_DIR/diagnostics-called" +gcr_vm_collect_diagnostics() { + printf '%s\n' "$*" >> "$diag_calls" +} +ssh_mode=remote-failure +printf '0\n' > "$ssh_calls_file" +remote_failure_log="$GCR_STATE_DIR/remote-bootstrap.log" +: > "$temp_paths" +if gcr_vm_bootstrap_ssh 192.0.2.94 gross-x86 fresh-token gcr-940-1 \ + "$known_hosts_file" 940 1 994 > /dev/null 2> "$remote_failure_log"; then + printf 'remote bootstrap command failure must be reported\n' >&2 + exit 1 +fi +grep -q 'category=remote-bootstrap-command-failed' "$remote_failure_log" +if grep -Eq 'REGISTRATION_TOKEN|Authorization:|secret-value|cloud-init status: error' "$remote_failure_log"; then + printf 'remote bootstrap stderr leaked into controller log\n' >&2 + exit 1 +fi +test "$(wc -l < "$diag_calls" | tr -d ' ')" = 1 +test "$(cat "$diag_calls")" = '994 192.0.2.94 940 bootstrap-command-failed 1' +assert_bootstrap_temps_removed + +ssh_mode=transport-failure +printf '0\n' > "$ssh_calls_file" +: > "$temp_paths" +transport_failure_log="$GCR_STATE_DIR/transport-failure.log" +if gcr_vm_bootstrap_ssh 192.0.2.94 gross-x86 fresh-token gcr-940-1 \ + "$known_hosts_file" 940 1 994 > /dev/null 2> "$transport_failure_log"; then + printf 'SSH transport failure after readiness must be reported\n' >&2 + exit 1 +fi +grep -q 'category=connectivity-or-sshd' "$transport_failure_log" +if grep -Eq 'Connection reset|REGISTRATION_TOKEN|secret-value' "$transport_failure_log"; then + printf 'transport stderr leaked into controller log\n' >&2 + exit 1 +fi +test "$(wc -l < "$diag_calls" | tr -d ' ')" = 2 +grep -q '994 192.0.2.94 940 bootstrap-transport-failed 1' "$diag_calls" +assert_bootstrap_temps_removed