diff --git a/package/gitea-runner-controller/hcloud.sh b/package/gitea-runner-controller/hcloud.sh index 5552cf3c..fdcae700 100644 --- a/package/gitea-runner-controller/hcloud.sh +++ b/package/gitea-runner-controller/hcloud.sh @@ -584,13 +584,18 @@ gcr_vm_rebuild_request() { '{image:$image, user_data:$user_data}')" || return 1 gcr_hcloud_req POST "/servers/$rebuild_vm_id/actions/rebuild" \ "$rebuild_payload" || return 1 - jq -er --arg vm "$rebuild_vm_id" \ + rebuild_action_id="$(jq -er --arg vm "$rebuild_vm_id" \ '.action | select((.id | type) == "number") - | select(.command == "rebuild") + | select(.command == "rebuild_server") | select(.status == "running" or .status == "success") | select([.resources[]? | select(.type == "server") | (.id | tostring)] == [$vm]) - | .id' "$GCR_LAST_BODY" + | .id' "$GCR_LAST_BODY")" || { + gcr_log warn --ns=hcloud \ + "rebuild response validation failed server=$rebuild_vm_id expected_command=rebuild_server" + return 1 + } + printf '%s' "$rebuild_action_id" } # Prints running or success. Provider-reported errors and malformed/mismatched @@ -605,11 +610,15 @@ gcr_vm_rebuild_action_status() { --arg action "$rebuild_action_id" \ '.action | select((.id | tostring) == $action) - | select(.command == "rebuild") + | select(.command == "rebuild_server") | select([.resources[]? | select(.type == "server") | (.id | tostring)] == [$vm]) | .status | select(. == "running" or . == "success" or . == "error")' \ - "$GCR_LAST_BODY")" || return 1 + "$GCR_LAST_BODY")" || { + gcr_log warn --ns=hcloud \ + "rebuild action validation failed server=$rebuild_vm_id action=$rebuild_action_id expected_command=rebuild_server" + return 1 + } [ "$rebuild_status" != error ] || return 1 printf '%s' "$rebuild_status" } @@ -620,7 +629,7 @@ gcr_vm_verify_rebuilt() { verify_vm_id="$1"; verify_type="$2"; verify_location="$3" verify_arch="$4"; verify_image="$5" gcr_hcloud_req GET "/servers/$verify_vm_id" || return 1 - jq -er --arg vm "$verify_vm_id" --arg type "$verify_type" \ + verify_ip="$(jq -er --arg vm "$verify_vm_id" --arg type "$verify_type" \ --arg location "$verify_location" --arg arch "$verify_arch" \ --arg image "$verify_image" \ '.server @@ -628,10 +637,15 @@ gcr_vm_verify_rebuilt() { | select(.status == "running") | select((.server_type.name // "") == $type) | select((.server_type.architecture // "") == $arch) - | select((.datacenter.location.name // "") == $location) + | select((.location.name // .datacenter.location.name // "") == $location) | select((.image.id | tostring) == $image) | .public_net.ipv4.ip - | select(type == "string" and length > 0)' "$GCR_LAST_BODY" + | select(type == "string" and length > 0)' "$GCR_LAST_BODY")" || { + gcr_log warn --ns=hcloud \ + "rebuilt server validation failed server=$verify_vm_id expected_type=$verify_type expected_location=$verify_location expected_arch=$verify_arch expected_image=$verify_image" + return 1 + } + printf '%s' "$verify_ip" } # Stop idle runners so Gitea cannot schedule work before atomic reuse claim. diff --git a/test/package/gitea-runner-controller/test/idle-reuse.sh b/test/package/gitea-runner-controller/test/idle-reuse.sh index 1b67f404..9b8488c6 100644 --- a/test/package/gitea-runner-controller/test/idle-reuse.sh +++ b/test/package/gitea-runner-controller/test/idle-reuse.sh @@ -140,7 +140,7 @@ gcr_gitea_set_runner_disabled() { "$1" "$2" "$3" >> "$calls" runner_disabled="$3" } -original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}' +original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-highmem","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"ccx53","budget_rate":"0.8550","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}' gcr_record_put 301 1 "$original" gcr_deallocate 301 1 completed:success test "$(gcr_record_field "$(gcr_record_get 301 1)" status)" = retiring_vm @@ -151,7 +151,7 @@ idle="$(gcr_record_get 301 1)" jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600 and .repo == "" and .label == "" and .runner_name == "" and .bootstrapped == false and .created_at == 1000 and .ttl_min == 480 and - .server_type == "cx53" and .location == "nbg1" and .arch == "amd64" and + .server_type == "ccx53" and .location == "nbg1" and .arch == "amd64" and .image_id == "424242")' "$(gcr_record_path 301 1)" >/dev/null test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 3 grep -q '^disable-runner repo=hinterland/hearth id=17 value=true$' "$calls" @@ -194,12 +194,12 @@ gcr_record_put 301 1 "$idle" # Compatible job in another repository claims generic idle by persisted profile. gcr_gitea_list_runners() { :; } -gcr_alloc 303 1 hectic-lab/util.nix '["gross-nix-x86-perf"]' +gcr_alloc 303 1 hectic-lab/util.nix '["gross-nix-x86-highmem"]' test "$RESPONSE_BODY" = 'rebuilding gcr-301-1' test ! -e "$(gcr_record_path 301 1)" claimed="$(gcr_record_get 303 1)" jq -e 'select(.job_id == "303" and .repo == "hectic-lab/util.nix" and - .label == "gross-nix-x86-perf" and .vm_id == 71 and + .label == "gross-nix-x86-highmem" and .vm_id == 71 and .vm_name == "gcr-301-1" and .runner_name == "gcr-303-1" and .status == "rebuild_pending" and .bootstrapped == false and .rebuild_image_id == "424242" and .rebuild_action_id == "" and @@ -213,6 +213,7 @@ test "$(gcr_count_active_repo hectic-lab/util.nix)" = 1 gcr_gitea_job_state() { printf 'queued:'; } gcr_hcloud_req() { method="$1"; path="$2"; body="${3:-}" + printf 'hcloud %s %s\n' "$method" "$path" >> "$calls" test ! -e "$GCR_STATE_DIR/jobs/.lock.admission" test ! -e "$GCR_STATE_DIR/jobs/.lock.idle-pool" GCR_LAST_BODY="$GCR_STATE_DIR/last-body.json" @@ -229,16 +230,16 @@ EOF request_rec="$(gcr_record_get 303 1)" test "$(gcr_record_field "$request_rec" status)" = rebuild_requesting test -z "$(gcr_record_field "$request_rec" rebuild_action_id)" - printf '%s\n' '{"action":{"id":501,"status":"running","command":"rebuild","resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY" + printf '%s\n' '{"action":{"id":501,"status":"running","command":"rebuild_server","progress":0,"started":"2026-01-16T00:31:45+00:00","finished":null,"error":null,"resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY" ;; GET:/actions/501) poll_rec="$(gcr_record_get 303 1)" test "$(gcr_record_field "$poll_rec" status)" = rebuild_running test "$(gcr_record_field "$poll_rec" rebuild_action_id)" = 501 - printf '%s\n' '{"action":{"id":501,"status":"success","command":"rebuild","resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY" + printf '%s\n' '{"action":{"id":501,"status":"success","command":"rebuild_server","progress":100,"started":"2026-01-16T00:31:45+00:00","finished":"2026-01-16T00:32:15+00:00","error":null,"resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY" ;; GET:/servers/71) - printf '%s\n' '{"server":{"id":71,"status":"running","server_type":{"name":"cx53","architecture":"amd64"},"datacenter":{"location":{"name":"nbg1"}},"image":{"id":424242},"public_net":{"ipv4":{"ip":"192.0.2.71"}}}}' > "$GCR_LAST_BODY" + printf '%s\n' '{"server":{"id":71,"status":"running","server_type":{"id":117,"name":"ccx53","architecture":"amd64"},"location":{"id":1,"name":"nbg1","city":"Nuremberg","country":"DE","network_zone":"eu-central"},"image":{"id":424242},"public_net":{"ipv4":{"ip":"192.0.2.71"}}}}' > "$GCR_LAST_BODY" ;; *) return 1 ;; esac @@ -247,8 +248,10 @@ gcr_sweep_rebuilds ready="$(gcr_record_get 303 1)" jq -e 'select(.status == "pending_vm" and .bootstrapped == false and .last_rebuild_action_id == 501 and .ip == "192.0.2.71" and - .created_at == 1000 and .ttl_min == 480 and .budget_rate == "0.032")' \ + .server_type == "ccx53" and .created_at == 1000 and .ttl_min == 480 and + .budget_rate == "0.8550")' \ "$(gcr_record_path 303 1)" >/dev/null +test "$(grep -c '^hcloud POST /servers/71/actions/rebuild$' "$calls")" = 1 if grep -q '^token$' "$calls"; then printf 'registration token must not be fetched before rebuild succeeds\n' >&2 exit 1 @@ -265,12 +268,54 @@ gcr_bootstrap_pending bootstrapped="$(gcr_record_get 303 1)" test "$(gcr_record_field "$bootstrapped" bootstrapped)" = true test "$(grep -c '^token$' "$calls")" = 1 -grep -q '^bootstrap ip=192.0.2.71 label=gross-nix-x86-perf token=fresh-token runner=gcr-303-1$' "$calls" +grep -q '^bootstrap ip=192.0.2.71 label=gross-nix-x86-highmem token=fresh-token runner=gcr-303-1$' "$calls" if grep -q '^bootstrap .*runner=gcr-301-1$' "$calls"; then printf 'old runner identity must never be bootstrapped\n' >&2 exit 1 fi +# Official action identity is fail-closed. Diagnostics name expected contract +# without printing provider bodies, credentials, user data, or private keys. +contract_body="$GCR_STATE_DIR/contract-body.json" +contract_log="$GCR_STATE_DIR/contract.log" +gcr_hcloud_req() { + test "$1:$2" = "$contract_request" + GCR_LAST_BODY="$contract_body" +} +export GCR_LOG=warn +printf '%s\n' '{"action":{"id":601,"status":"running","command":"rebuild","resources":[{"id":71,"type":"server"}]},"root_password":"LEAK_ROOT","user_data":"LEAK_CLOUD_INIT","private_key":"LEAK_PRIVATE_KEY","token":"LEAK_TOKEN"}' > "$contract_body" +contract_request='POST:/servers/71/actions/rebuild' +if gcr_vm_rebuild_request 71 424242 '#cloud-config' 2> "$contract_log"; then + printf 'legacy rebuild command must be rejected\n' >&2 + exit 1 +fi +grep -q 'rebuild response validation failed server=71 expected_command=rebuild_server' "$contract_log" + +for contract_case in wrong-command wrong-action wrong-server; do + case "$contract_case" in + wrong-command) + printf '%s\n' '{"action":{"id":601,"status":"running","command":"create_server","resources":[{"id":71,"type":"server"}]},"token":"LEAK_TOKEN"}' > "$contract_body" + ;; + wrong-action) + printf '%s\n' '{"action":{"id":999,"status":"running","command":"rebuild_server","resources":[{"id":71,"type":"server"}]},"user_data":"LEAK_CLOUD_INIT"}' > "$contract_body" + ;; + wrong-server) + printf '%s\n' '{"action":{"id":601,"status":"running","command":"rebuild_server","resources":[{"id":72,"type":"server"}]},"private_key":"LEAK_PRIVATE_KEY"}' > "$contract_body" + ;; + esac + contract_request='GET:/actions/601' + if gcr_vm_rebuild_action_status 71 601 2>> "$contract_log"; then + printf 'mismatched rebuild action must be rejected: %s\n' "$contract_case" >&2 + exit 1 + fi +done +test "$(grep -c 'rebuild action validation failed server=71 action=601 expected_command=rebuild_server' "$contract_log")" = 3 +if grep -Eq 'LEAK_ROOT|LEAK_CLOUD_INIT|LEAK_PRIVATE_KEY|LEAK_TOKEN|#cloud-config' "$contract_log"; then + printf 'rebuild validation diagnostics leaked response or request data\n' >&2 + exit 1 +fi +export GCR_LOG=error + # Incompatible image/profile remains idle and normal allocation creates anew. incompatible='{"job_id":"304","run_attempt":"1","repo":"","label":"","created_at":1000,"ttl_min":180,"vm_id":73,"vm_name":"gcr-304-1","runner_name":"","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"313131","bootstrapped":false,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}' gcr_record_put 304 1 "$incompatible"