From 1c6fd1b8390d3a1449cc79a9ea92786ad5d0f205 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 18 Nov 2026 12:57:39 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20contributor=20container:=20s?= =?UTF-8?q?upport=20rootless=20Docker,=20fall=20back=20to=20podman=20on=20?= =?UTF-8?q?permission=20failure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #19076 Signed-off-by: Andrew Anderson --- .github/workflows/v2-ci.yml | 6 + Justfile | 73 ++++++++- ...fixed-19076-contributor-rootless-docker.md | 1 + .../test_contributor_runtime_selection.sh | 145 ++++++++++++++++++ src/docs/contributor-relay.md | 2 + 5 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 changelog.d/fixed-19076-contributor-rootless-docker.md create mode 100644 src/deploy/test_contributor_runtime_selection.sh diff --git a/.github/workflows/v2-ci.yml b/.github/workflows/v2-ci.yml index ed8e4e519c..508d120495 100644 --- a/.github/workflows/v2-ci.yml +++ b/.github/workflows/v2-ci.yml @@ -1212,6 +1212,12 @@ jobs: HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' run: bash src/deploy/test_contributor_container_resources.sh + - name: Contributor runtime selection and rootless Docker (#19076) + working-directory: . + env: + HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' + run: bash src/deploy/test_contributor_runtime_selection.sh + # #7836: container mode staged the contributor's ENTIRE ~/.claude — every # transcript on the machine, the prompt history, paste cache, memory and # CLAUDE.md — into the sandbox where third-party test suites run for diff --git a/Justfile b/Justfile index 380517925b..ca86893daa 100644 --- a/Justfile +++ b/Justfile @@ -1610,6 +1610,20 @@ contribute-hive backend="" mode="docker": check-version # not the Mac). The relay only dials out, so the default network # works; reach a localhost LiteLLM proxy via host.containers.internal. fi + # Rootless Docker (#19076): the daemon runs in a user namespace where the + # host user is container root and the image's `dev` user (uid 1000) is an + # unmapped sub-uid, so the host-owned 0600 contributor.env (and the config + # dir) are unreadable/unwritable inside. Docker has no keep-id, so the + # bind sources are staged and chowned to uid 1000 through the userns + # (see rootless_stage_owner below) instead of widening any file mode. + DOCKER_ROOTLESS="" + CONFIG_MOUNT_SRC="{{config_dir}}" + GH_MOUNT_SRC="${HOME}/.config/gh" + ROOTLESS_STAGES=() + if [[ "$RUNTIME" == "docker" ]] \ + && "$RUNTIME" info --format '{{ "{{" }}.SecurityOptions{{ "}}" }}' 2>/dev/null | grep -q 'name=rootless'; then + DOCKER_ROOTLESS=1 + fi if [[ "${HIVE_SKIP_PULL:-}" != "true" ]]; then echo "Pulling {{hive_image}} (${RUNTIME})..." "$RUNTIME" pull {{hive_image}} 2>/dev/null || echo "Pull failed — using local image" @@ -2011,7 +2025,28 @@ contribute-hive backend="" mode="docker": check-version # Remove the ephemeral CLI config staging dir (H6). Any config the # container wrote — including a malicious injection — dies with it and # never touches the contributor's real host config. + # Rootless Docker: hand the staged trees back to the host user first, + # otherwise the sub-uid-owned files cannot be removed from the host. + if [ -n "${DOCKER_ROOTLESS}" ] && [ "${#ROOTLESS_STAGES[@]}" -gt 0 ]; then + rootless_stage_owner 0:0 "${ROOTLESS_STAGES[@]}" >/dev/null 2>&1 || true + fi [ -n "${CLI_STAGE:-}" ] && rm -rf "${CLI_STAGE}" 2>/dev/null || true + [ -n "${CONFIG_STAGE:-}" ] && rm -rf "${CONFIG_STAGE}" 2>/dev/null || true + [ -n "${GH_STAGE:-}" ] && rm -rf "${GH_STAGE}" 2>/dev/null || true + } + # rootless_stage_owner ...: chown bind-mount sources from + # inside the daemon's user namespace (container root == host user), so + # uid 1000 in the container owns them while modes stay exactly as copied + # (0600 stays 0600). 0:0 maps ownership back to the host user. + rootless_stage_owner() { + local owner="$1" d i=0 vols=() paths=() + shift + for d in "$@"; do + vols+=(-v "${d}:/stage/${i}") + paths+=("/stage/${i}") + i=$((i + 1)) + done + "$RUNTIME" run --rm --user 0:0 --entrypoint chown "${vols[@]}" {{hive_image}} -R "$owner" "${paths[@]}" } trap cleanup_container EXIT report_container_termination() { @@ -2029,14 +2064,34 @@ contribute-hive backend="" mode="docker": check-version echo " Check the host's OOM logs for system-wide memory pressure." fi } + if [[ -n "$DOCKER_ROOTLESS" ]]; then + CONFIG_STAGE="$(mktemp -d "${TMPDIR:-/tmp}/hive-config-stage.XXXXXX")" + chmod 700 "${CONFIG_STAGE}" + cp -a "{{config_dir}}/." "${CONFIG_STAGE}/" + CONFIG_MOUNT_SRC="${CONFIG_STAGE}" + ROOTLESS_STAGES+=("${CONFIG_STAGE}") + if [[ -d "${HOME}/.config/gh" ]]; then + GH_STAGE="$(mktemp -d "${TMPDIR:-/tmp}/hive-gh-stage.XXXXXX")" + chmod 700 "${GH_STAGE}" + cp -a "${HOME}/.config/gh/." "${GH_STAGE}/" + GH_MOUNT_SRC="${GH_STAGE}" + ROOTLESS_STAGES+=("${GH_STAGE}") + fi + ROOTLESS_STAGES+=("${CLI_STAGE}") + echo "Rootless Docker detected: staging config for the container user (modes unchanged)." + if ! rootless_stage_owner 1000:1000 "${ROOTLESS_STAGES[@]}"; then + echo "ERROR: could not prepare config for rootless Docker; use HIVE_CONTAINER_RUNTIME=podman." >&2 + exit 1 + fi + fi "$RUNTIME" run -d \ --name "${CONTAINER_NAME}" \ ${RUNTIME_FLAGS} \ "${RESOURCE_FLAGS[@]}" \ ${NET_FLAGS} \ - -v "{{config_dir}}:/home/dev/.config/hive${VOLSUF}" \ + -v "${CONFIG_MOUNT_SRC}:/home/dev/.config/hive${VOLSUF}" \ ${CLI_MOUNTS} \ - -v "${HOME}/.config/gh:/home/dev/.config/gh${ROSUF}" \ + -v "${GH_MOUNT_SRC}:/home/dev/.config/gh${ROSUF}" \ -e HIVE_HUB="{{hive_hub}}" \ -e AGENT_BACKEND="${BACKEND}" \ -e GH_TOKEN="${GH_TOKEN:-}" \ @@ -2070,6 +2125,18 @@ contribute-hive backend="" mode="docker": check-version # the user only sees an opaque runtime error. CONTAINER_STATE=$("$RUNTIME" inspect -f '{{ "{{" }}.State.Running{{ "}}" }}' "${CONTAINER_NAME}" 2>/dev/null || echo "missing") if [[ "$CONTAINER_STATE" != "true" ]]; then + # Auto-detected docker failed on a permission error (UID mapping): fall + # back to podman once, with a one-line reason. Never overrides an + # explicit HIVE_CONTAINER_RUNTIME, and cannot loop. + if [[ "$RUNTIME" == "docker" && -z "{{container_runtime}}" && -z "${HIVE_RUNTIME_FALLBACK:-}" ]] \ + && command -v podman >/dev/null 2>&1 \ + && "$RUNTIME" logs "${CONTAINER_NAME}" 2>&1 | grep -q 'Permission denied'; then + echo "docker failed with 'Permission denied' on the mounted config (UID mapping); retrying with podman." + cleanup_container + trap - EXIT + HIVE_CONTAINER_RUNTIME=podman HIVE_RUNTIME_FALLBACK=1 \ + exec "{{just_executable()}}" --justfile "{{justfile()}}" contribute-hive "{{backend}}" "{{mode}}" + fi CONTAINER_EXIT=$("$RUNTIME" inspect -f '{{ "{{" }}.State.ExitCode{{ "}}" }}' "${CONTAINER_NAME}" 2>/dev/null || echo "unknown") echo "" echo "ERROR: the contributor container exited during startup." @@ -2113,7 +2180,7 @@ contribute-hive backend="" mode="docker": check-version else echo "Common causes:" echo " * GH_TOKEN empty/expired — re-run: just contribute-setup {{backend}}" - echo " * config mounts unreadable (rootless podman UID mapping)" + echo " * config mounts unreadable (rootless docker/podman UID mapping)" echo " * missing HIVE_REGISTRATION_TOKEN" fi echo "" diff --git a/changelog.d/fixed-19076-contributor-rootless-docker.md b/changelog.d/fixed-19076-contributor-rootless-docker.md new file mode 100644 index 0000000000..0ab600829d --- /dev/null +++ b/changelog.d/fixed-19076-contributor-rootless-docker.md @@ -0,0 +1 @@ +- `just contribute-hive` under rootless Docker no longer fails with `contributor.env: Permission denied`: the config is staged and chowned through the user namespace with file modes unchanged (the token file stays 0600), and an auto-detected docker that fails on a permission error falls back to podman with a one-line reason ([#19076](https://github.com/hivecommons/hive/issues/19076)). diff --git a/src/deploy/test_contributor_runtime_selection.sh b/src/deploy/test_contributor_runtime_selection.sh new file mode 100644 index 0000000000..9131c985da --- /dev/null +++ b/src/deploy/test_contributor_runtime_selection.sh @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +# Exercises the real `just contribute-hive` container recipe against stubbed +# Docker and Podman CLIs to pin runtime selection and the launch arguments for +# rootful Docker, rootless Docker, Podman, and the permission-failure fallback +# (#19076). No image, daemon, credential, or network is used. +# +# Run: bash src/deploy/test_contributor_runtime_selection.sh +set -uo pipefail + +# shellcheck source=src/deploy/test_lib.sh +. "$(cd "$(dirname "$0")" && pwd)/test_lib.sh" +PASS=0 +FAIL=0 +pass() { echo " PASS: $1"; PASS=$((PASS + 1)); } +fail() { + echo " FAIL: $1" + [ $# -gt 1 ] && [ -n "${2:-}" ] && echo " $2" + FAIL=$((FAIL + 1)) +} +has() { # label file needle + if grep -qF -- "$3" "$2" 2>/dev/null; then pass "$1"; else fail "$1" "missing: '$3'"; fi +} +hasnt() { + if grep -qF -- "$3" "$2" 2>/dev/null; then fail "$1" "unexpectedly present: '$3'"; else pass "$1"; fi +} + +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +if ! command -v just >/dev/null 2>&1; then + hive_test_skip "'just' not installed; cannot exercise the recipe" + hive_test_report; exit $? +fi + +echo "=== contributor runtime selection (#19076) ===" + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT +FAKE_HOME="$WORK/home" +STUB_BIN="$WORK/bin" +CALLS="$WORK/calls" +mkdir -p "$FAKE_HOME/.config/hive" "$STUB_BIN" "$WORK/runtime-dir" +printf '%s\n' 'HIVE_HUB=wss://hive.example.test/contribute' \ + 'HIVE_REGISTRATION_TOKEN=placeholder-registration-token' 'AGENT_BACKEND=codex' \ + > "$FAKE_HOME/.config/hive/contributor.env" +chmod 600 "$FAKE_HOME/.config/hive/contributor.env" +printf '%s\n' 'GH_TOKEN=placeholder-github-token' > "$FAKE_HOME/.config/hive/gh-auth.env" + +# One stub serves both names. Every invocation appends one line: +# +# and `chown` helper runs also record the mode of the staged contributor.env. +cat > "$STUB_BIN/runtime" <<'STUB' +#!/usr/bin/env bash +name="$(basename "$0")" +echo "$name $*" >> "$STUB_CALLS" +case "${1:-}" in + info) echo "${STUB_INFO:-[name=seccomp,profile=builtin]}" ;; + run) + case "$*" in + *"--entrypoint chown"*) + for a in "$@"; do + case "$a" in + *hive-config-stage*:/stage/*) + src="${a%%:/stage/*}" + { stat -c '%a' "$src/contributor.env" 2>/dev/null \ + || stat -f '%Lp' "$src/contributor.env"; } >> "$STUB_CALLS.mode" + ;; + esac + done ;; + esac + echo stub-id ;; + inspect) + running="true" + [ "$name" = "docker" ] && running="${STUB_DOCKER_RUNNING:-true}" + case "$*" in + *'.State.Running'*) echo "$running" ;; + *'.State.ExitCode'*) echo 1 ;; + *'.State.OOMKilled'*) echo false ;; + *) exit 1 ;; + esac ;; + logs) [ "$name" = "docker" ] && echo "${STUB_DOCKER_LOGS:-}"; exit 0 ;; + rm|pull) exit 0 ;; + *) exit 1 ;; +esac +STUB +chmod +x "$STUB_BIN/runtime" +ln -s runtime "$STUB_BIN/docker" +ln -s runtime "$STUB_BIN/podman" +printf '#!/usr/bin/env bash\n[ "${1:-} ${2:-}" = "api user" ] && echo test-contributor\nexit 0\n' > "$STUB_BIN/gh" +printf '#!/usr/bin/env bash\nexit 0\n' > "$STUB_BIN/sleep" +chmod +x "$STUB_BIN/gh" "$STUB_BIN/sleep" + +# scenario [VAR=value...]; output lands in $OUT. +scenario() { + local forced="$1"; shift + : > "$CALLS"; rm -f "$CALLS.mode" + OUT="$(cd "$ROOT" && env -u HIVE_CONTAINER_RUNTIME -u HIVE_RUNTIME_FALLBACK \ + HOME="$FAKE_HOME" PATH="$STUB_BIN:$PATH" XDG_RUNTIME_DIR="$WORK/runtime-dir" \ + STUB_CALLS="$CALLS" HIVE_SKIP_VERSION_CHECK=true HIVE_SKIP_PULL=true \ + ${forced:+HIVE_CONTAINER_RUNTIME=$forced} "$@" just contribute-hive codex 2>&1)" + RC=$? +} +runline() { grep -E "^$1 run -d" "$CALLS" | head -1; } + +echo "" +echo "-- rootful docker: unchanged --" +scenario docker +L="$(runline docker)"; echo "$L" > "$WORK/l" +has "uses docker" "$WORK/l" "docker run -d" +hasnt "no userns flag" "$WORK/l" "--userns" +has "mounts the real config dir" "$WORK/l" "-v $FAKE_HOME/.config/hive:/home/dev/.config/hive " +hasnt "no stage helper" "$CALLS" "--entrypoint chown" + +echo "" +echo "-- rootless docker: staged, chowned through the userns, modes kept --" +scenario docker STUB_INFO='[name=seccomp,profile=builtin name=rootless name=cgroupns]' +L="$(runline docker)"; echo "$L" > "$WORK/l" +has "chown helper runs as userns root" "$CALLS" "--user 0:0 --entrypoint chown" +has "helper chowns to uid 1000" "$CALLS" "-R 1000:1000" +hasnt "container does not mount the real config dir" "$WORK/l" "-v $FAKE_HOME/.config/hive:" +has "container mounts the staged config dir" "$WORK/l" "hive-config-stage." +has "staged contributor.env stays 0600" "$CALLS.mode" "600" +has "operator is told why" <(echo "$OUT") "Rootless Docker detected" + +echo "" +echo "-- podman: unchanged --" +scenario podman +L="$(runline podman)"; echo "$L" > "$WORK/l" +has "keep-id pinned to uid 1000" "$WORK/l" "--userns=keep-id:uid=1000,gid=1000" +has "real config dir with SELinux label" "$WORK/l" "-v $FAKE_HOME/.config/hive:/home/dev/.config/hive:Z" +hasnt "no stage helper" "$CALLS" "--entrypoint chown" + +echo "" +echo "-- auto-detect: docker wins; permission failure falls back to podman --" +scenario "" +has "auto-detect picks docker first" "$CALLS" "docker run -d" +scenario "" STUB_DOCKER_RUNNING=false STUB_DOCKER_LOGS='source: /home/dev/.config/hive/contributor.env: Permission denied' +has "docker attempted first" "$CALLS" "docker run -d" +has "podman launched after the failure" "$CALLS" "podman run -d" +has "one-line reason printed" <(echo "$OUT") "retrying with podman" + +echo "" +echo "-- explicit runtime is never overridden --" +scenario docker STUB_DOCKER_RUNNING=false STUB_DOCKER_LOGS='contributor.env: Permission denied' +hasnt "no podman fallback when docker was forced" "$CALLS" "podman run -d" + +hive_test_report diff --git a/src/docs/contributor-relay.md b/src/docs/contributor-relay.md index e821480cf9..3309d1c643 100644 --- a/src/docs/contributor-relay.md +++ b/src/docs/contributor-relay.md @@ -63,6 +63,8 @@ just contribute-hive claude local # host mode — relay + CLI directly on your ``` Containerized mode auto-detects the runtime — docker first, then podman — and can be forced with `export HIVE_CONTAINER_RUNTIME=podman`. + +**Rootless Docker.** `just contribute-hive` detects rootless Docker (`docker info` lists `name=rootless` under security options). In that mode the host user is container root and the image's `dev` user is an unmapped sub-uid, so the 0600 `contributor.env` bind mount would be unreadable (`contributor.env: Permission denied`). The recipe instead copies `~/.config/hive` and `~/.config/gh` into 0700 staging directories, chowns them to uid 1000 through the user namespace (file modes are unchanged, so `contributor.env` stays 0600), mounts the copies, and removes them on exit. Files the relay writes in the container (`relay-status.json`, `hubs-seen.json`, the pid file) therefore land in the staged copy, not in `~/.config/hive`. If an auto-detected docker still fails at startup with `Permission denied` and podman is installed, the recipe retries once with podman and prints a one-line reason. An explicit `HIVE_CONTAINER_RUNTIME` is never overridden. Rootful Docker and Podman launch exactly as before. The resolved runtime is passed into the container, so the "attach to the CLI" hints printed from inside it (the status line, and the banner shown when the CLI needs a login) name the engine that actually launched it ([#5145](https://github.com/hivecommons/hive/issues/5145)). In host mode there is no container, and those hints are a plain `tmux attach -t `. The `just contribute-hive` container defaults to the same workload ceiling as From 3dbf49e1d76df6baa08b0d30dde57de6c208ca4f Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 18 Nov 2026 13:00:07 -0500 Subject: [PATCH 2/3] ci: regenerate integration candidate workflows for runtime-selection test (#19076) Signed-off-by: Andrew Anderson --- .github/workflows/integration-candidate.yml | 5 +++++ .github/workflows/integration-technical-v2-ci.yml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/.github/workflows/integration-candidate.yml b/.github/workflows/integration-candidate.yml index 3384a26fcd..4574b8baa6 100644 --- a/.github/workflows/integration-candidate.yml +++ b/.github/workflows/integration-candidate.yml @@ -621,6 +621,11 @@ jobs: env: HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' run: bash src/deploy/test_contributor_container_resources.sh + - name: Contributor runtime selection and rootless Docker (#19076) + working-directory: . + env: + HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' + run: bash src/deploy/test_contributor_runtime_selection.sh - name: Contributor containers stage only the Claude credential and settings (#7836) working-directory: . env: diff --git a/.github/workflows/integration-technical-v2-ci.yml b/.github/workflows/integration-technical-v2-ci.yml index 8faea22075..7a70591e8b 100644 --- a/.github/workflows/integration-technical-v2-ci.yml +++ b/.github/workflows/integration-technical-v2-ci.yml @@ -475,6 +475,11 @@ jobs: env: HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' run: bash src/deploy/test_contributor_container_resources.sh + - name: Contributor runtime selection and rootless Docker (#19076) + working-directory: . + env: + HIVE_TEST_REQUIRE_BEHAVIOURAL: '1' + run: bash src/deploy/test_contributor_runtime_selection.sh - name: Contributor containers stage only the Claude credential and settings (#7836) working-directory: . env: From df9a4fd6fd5826da8e2fc78f9221cb57cae86d96 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 18 Nov 2026 13:07:07 -0500 Subject: [PATCH 3/3] test: handle runtime-fallback interpolations in container-exit block harness (#19076) Signed-off-by: Andrew Anderson --- src/pkg/dashboard/contribute_container_exit_cause_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pkg/dashboard/contribute_container_exit_cause_test.go b/src/pkg/dashboard/contribute_container_exit_cause_test.go index f3500d0c02..4023727b64 100644 --- a/src/pkg/dashboard/contribute_container_exit_cause_test.go +++ b/src/pkg/dashboard/contribute_container_exit_cause_test.go @@ -47,6 +47,12 @@ func runContainerExitReport(t *testing.T, backend, containerLogs string) string // reads, so check for stray interpolations before, not after. block = strings.ReplaceAll(block, "{{hive_image}}", "ghcr.io/example/hive-contributor:test") block = strings.ReplaceAll(block, "{{backend}}", backend) + // The runtime-fallback re-exec (#19076) interpolates these; they are inert + // here because the fake runtime's log never says "Permission denied". + block = strings.ReplaceAll(block, "{{container_runtime}}", "") + block = strings.ReplaceAll(block, "{{mode}}", "docker") + block = strings.ReplaceAll(block, "{{just_executable()}}", "just") + block = strings.ReplaceAll(block, "{{justfile()}}", "Justfile") if leftover := strings.ReplaceAll(strings.ReplaceAll(block, `{{ "{{" }}`, ""), `{{ "}}" }}`, ""); strings.Contains(leftover, "{{") { t.Fatalf("unhandled just interpolation in the container-exit block: %s", block) }