diff --git a/.github/workflows/v2-ci.yml b/.github/workflows/v2-ci.yml index b6f69f5a70..458052a823 100644 --- a/.github/workflows/v2-ci.yml +++ b/.github/workflows/v2-ci.yml @@ -996,6 +996,19 @@ jobs: working-directory: . run: bash src/deploy/test_ttyd_url_arg.sh + # The browser terminal's attach helper falls back to the DEFAULT tmux + # socket when agents share one UID. It assumed that socket lived under + # dev's passwd uid (/tmp/tmux-1001), but under OpenShift's restricted + # SCC the container runs as an arbitrary uid with no passwd entry, so + # every session was alive on /tmp/tmux-/default while the + # dashboard polled /tmp/tmux-1001/default for 900s. This stubs `id`, + # starts real tmux servers at the paths each uid layout would use, and + # asserts the helper attaches to the right one. Skips where tmux is + # absent. + - name: ttyd finds the default tmux socket under an arbitrary container UID + working-directory: . + run: bash src/deploy/test_ttyd_default_socket_uid.sh + # #5435: hive-discord.service ran `node bot.js` with # WorkingDirectory=/tmp/hive/discord, resolving the executed code out of a # world-writable parent that is cleared on every reboot — a local user who diff --git a/changelog.d/fixed-14789-terminal-default-tmux-socket-arbitrary-uid.md b/changelog.d/fixed-14789-terminal-default-tmux-socket-arbitrary-uid.md new file mode 100644 index 0000000000..1ffc4a4dbd --- /dev/null +++ b/changelog.d/fixed-14789-terminal-default-tmux-socket-arbitrary-uid.md @@ -0,0 +1 @@ +- The dashboard terminal now attaches to agents when the container runs as an arbitrary UID (e.g. OpenShift's restricted SCC) and agents share one tmux server: the attach helper looked for the default tmux socket only under dev's passwd uid (`/tmp/tmux-1001`), so it polled "waiting for agent to start" for every agent while all sessions were alive on `/tmp/tmux-/default`. It now checks the current uid's socket, dev's, and any other `tmux-*/default` under `$TMUX_TMPDIR`, picking the one that holds the requested session ([#14789](https://github.com/hivecommons/hive/issues/14789)). diff --git a/src/deploy/test_ttyd_default_socket_uid.sh b/src/deploy/test_ttyd_default_socket_uid.sh new file mode 100644 index 0000000000..d3027d813a --- /dev/null +++ b/src/deploy/test_ttyd_default_socket_uid.sh @@ -0,0 +1,266 @@ +#!/usr/bin/env bash +# The browser terminal must find the default tmux socket whatever UID the +# container runs as. +# Run: bash src/deploy/test_ttyd_default_socket_uid.sh +# +# THE BUG. With no per-agent UID map ("no UID map found, agents will share dev +# UID") every hive- session lives on the DEFAULT tmux server of the UID +# the container runs as, i.e. $TMUX_TMPDIR/tmux-$(id -u)/default. The attach +# helper's fallback instead looked at /tmp/tmux-$(id -u dev || 1001)/default — +# dev's PASSWD uid. On Compose/Podman the two are the same number and nobody +# noticed. Under OpenShift's restricted SCC the container runs as an arbitrary +# UID (1000840000 on the spoke that reported this) that has no passwd entry, so +# the sessions were at /tmp/tmux-1000840000/default, the helper polled +# /tmp/tmux-1001/default forever, and the dashboard showed "waiting for agent +# 'X' to start (tmux session not created yet)" for every agent while `tmux ls` +# showed them all alive. +# +# HOW THIS TESTS IT. The helper derives the UID from `id -u`, so `id` is +# stubbed on PATH to answer with an OpenShift-style UID (and, per case, with or +# without a passwd entry for dev). TMUX_TMPDIR points the helper's socket +# lookup at a scratch directory where a real tmux server is started with +# `tmux -S` at exactly the path the container would use. The helper is then +# run for real against it: resolving the socket is asserted POSITIVELY through +# the wheel rebind the helper leaves on the server (the one setting it does not +# restore on detach), because an absence-only check passes just as happily when +# the script never reaches attach at all. +set -uo pipefail + +PASS=0 +FAIL=0 +pass() { echo " PASS: $1"; PASS=$((PASS + 1)); } +fail() { echo " FAIL: $1"; [ $# -gt 1 ] && echo " $2"; FAIL=$((FAIL + 1)); } +finish() { + echo "" + echo "=== Results: $PASS passed, $FAIL failed ===" + [ "$FAIL" -eq 0 ] +} + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +ATTACH="${ROOT}/src/deploy/ttyd-tmux.sh" + +echo "=== ttyd-tmux.sh finds the default tmux socket under an arbitrary container UID ===" + +if [ ! -f "$ATTACH" ]; then + fail "locate $ATTACH" "the layout moved — this test cannot verify anything" + finish; exit $? +fi + +if ! command -v tmux >/dev/null 2>&1; then + echo " SKIP: tmux not installed — these assertions need a real tmux server" + if [ "${HIVE_TEST_REQUIRE_BEHAVIOURAL:-}" = "1" ]; then + fail "tmux is required when HIVE_TEST_REQUIRE_BEHAVIOURAL=1" + fi + finish; exit $? +fi +if command -v su-exec >/dev/null 2>&1; then + # The helper would su-exec to the socket owner when the stubbed uid differs + # from it; that path needs root and is not what is under test here. + echo " SKIP: su-exec is on PATH — the uid stub would route through it" + finish; exit $? +fi + +# The UID of the reporting OpenShift spoke. Nothing on a developer box or CI +# runner owns this, so the helper's historical /tmp/tmux-1001 assumption cannot +# be satisfied by accident. +ARBITRARY_UID=1000840000 +DEV_UID=1001 +OTHER_UID=4242 + +WORK="$(mktemp -d)" +SOCKET_ROOT="${WORK}/sockets" +STUB_BIN="${WORK}/bin" +mkdir -p "$SOCKET_ROOT" "$STUB_BIN" +declare -a SERVERS=() +cleanup() { + local s + for s in "${SERVERS[@]+"${SERVERS[@]}"}"; do + tmux -S "$s" kill-server 2>/dev/null + done + rm -rf "$WORK" +} +trap cleanup EXIT + +# stub_id : `id -u` answers the first, `id -u dev` +# the second (or fails, like getent on a container with no passwd entry for an +# arbitrary UID). Anything else falls through to the real id. +REAL_ID="$(command -v id)" +stub_id() { + cat > "${STUB_BIN}/id" <&2; exit 1 +fi +exec "$REAL_ID" "\$@" +EOF + chmod +x "${STUB_BIN}/id" +} + +run_with_timeout() { + local seconds="$1" + shift + if command -v timeout >/dev/null 2>&1; then + timeout "$seconds" "$@" + else + perl -e ' + my $seconds = shift @ARGV; + my $pid = fork(); + exit 127 unless defined $pid; + if ($pid == 0) { setpgrp(0, 0); exec @ARGV; exit 127; } + $SIG{ALRM} = sub { + kill "TERM", -$pid; select undef, undef, undef, 0.2; kill "KILL", -$pid; + waitpid($pid, 0); exit 124; + }; + alarm $seconds; waitpid($pid, 0); + exit 127 if $? == -1; exit 128 + ($? & 127) if $? & 127; exit $? >> 8; + ' "$seconds" "$@" + fi +} + +# start_default_server : a real tmux server at the path the +# container's default server would use for that uid. +start_default_server() { + local dir="${SOCKET_ROOT}/tmux-$1" sock + mkdir -p "$dir" && chmod 700 "$dir" + sock="${dir}/default" + tmux -S "$sock" -f /dev/null new-session -d -s "$2" 'sleep 300' 2>/dev/null || return 1 + SERVERS+=("$sock") + [ -S "$sock" ] +} + +# run_attach : the helper, for real, with a short wait so a lookup +# that still fails reports quickly instead of polling for 900s. Invoked through +# `bash` because the file ships mode 644 and is chmod'ed at image build time. +run_attach() { + PATH="${STUB_BIN}:${PATH}" TMUX_TMPDIR="$SOCKET_ROOT" \ + HIVE_TTYD_WAIT_SEC=2 HIVE_TTYD_POLL_SEC=1 \ + run_with_timeout 15 bash "$ATTACH" "$1" &1 +} + +# assert_resolves