Files
Paul O'Reilly f8c8805f26 fix(harnesses): stage tokens for agent UID, fix three broken auth paths
Mirrors agent-runtimes audit + fix. All three M22 Phase 9 wrapper-script
auth paths were broken since the cutover: each one ran as the agent UID
trying to `cat` a 0400 root-only ESO mount. Same root cause we hit on
minimax/z-ai earlier today.

Changes:

1. anthropic-cloud-paul-oauth/v1 (NEW in framework)
   - Mirrors agent-runtimes — was previously only present there.
   - init.sh stages oauth_token into ~/.claude/.credentials.json (Claude
     Code's native subscription-OAuth schema). No env, no apiKeyHelper,
     no wrapper. Restores the equivalent of what harness_init.py used to
     do for the legacy SOPS path.
   - The legacy `bin/anthropic-wrapper.sh` was dead code (never wired).

2. gitea-https/v1
   - init.sh stages the token to $HOME/.config/git/gitea-https-token
     (0600 agent:agent) and points the per-host git credential helper at
     the staged copy. Previously the helper `cat`d the ESO mount path
     and silently failed at every git invocation.

3. gitea-admin/v1
   - init.sh stages the token to $HOME/.config/gitea-admin/token
     (0600 agent:agent). Wrapper updated to read from the staged copy.
   - Removes stale `requires: anthropic-cloud/v1` (the only anthropic
     harness in agent-runtimes is anthropic-cloud-paul-oauth/v1).

Pattern matches gitea-ssh / minimax / z-ai: ESO mount stays root-only,
init.sh runs as root and `install -m 0600 -o agent -g agent`s a single
explicit copy. Per-secret enumeration; future ESO Secret keys remain
inaccessible by default.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-07 21:05:33 +12:00

61 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
# gitea-admin init — stage the API token for the agent user and configure
# the git credential helper.
#
# Threat model: keep the ESO mount root-only (mode 0400). init.sh runs as
# root and `install`s a per-secret 0600 agent-owned copy at
# $AGENT_HOME/.config/gitea-admin/token. The wrapper at
# bin/gitea-admin-wrapper.sh reads the staged copy (not the ESO mount) at
# exec time. Without staging, every wrapper invocation would fail at the
# `cat` step because the wrapper runs under the agent UID and the ESO file
# is root:root mode 0400.
#
# This was previously broken: the wrapper `cat`d the ESO mount path which
# the agent could not read. Discovered during the M22 Phase 9 audit; the
# wrapper-script approach was never re-validated post-cutover. Stage-and-
# wrapper-points-at-stage matches gitea-ssh / gitea-https / minimax / z-ai.
#
# Rotation handling: per-container init. Long-running sessions need a
# future scripts.control_loop hook to re-stage between operations.
set -euo pipefail
ESO_TOKEN="/run/agent/secrets/gitea-admin/token"
if [ ! -r "$ESO_TOKEN" ]; then
echo "ERROR: $ESO_TOKEN not readable. Check ESO ExternalSecret acct-<gitea-admin-id>." >&2
exit 1
fi
AGENT_USER="${AGENT_USER:-agent}"
AGENT_HOME=$(getent passwd "$AGENT_USER" | cut -d: -f6)
if [ -z "$AGENT_HOME" ] || [ ! -d "$AGENT_HOME" ]; then
AGENT_HOME="/home/$AGENT_USER"
fi
# Stage the token into a per-secret path owned by agent, mode 0600.
# Wrapper reads from this path; ESO mount is never accessed at runtime by
# the agent UID.
STAGED_DIR="$AGENT_HOME/.config/gitea-admin"
STAGED_TOKEN="$STAGED_DIR/token"
mkdir -p "$STAGED_DIR"
chown "$AGENT_USER:" "$STAGED_DIR" 2>/dev/null || true
chmod 0700 "$STAGED_DIR"
install -m 0600 -o "$AGENT_USER" -g "$AGENT_USER" "$ESO_TOKEN" "$STAGED_TOKEN"
# Ensure SSH directory exists with correct permissions (legacy SSH path).
mkdir -p "$AGENT_HOME/.ssh"
chown "$AGENT_USER:" "$AGENT_HOME/.ssh" 2>/dev/null || true
chmod 0700 "$AGENT_HOME/.ssh"
# Non-secret configuration: GITEA_BASE_URL is set in harness env.
# Configure git to use the gitea-admin credential helper wrapper. Run as
# the agent user so ~/.gitconfig is owned correctly; otherwise root would
# write into /root/.gitconfig and the agent's git wouldn't see the helper.
WRAPPER_PATH="/opt/harness/contexts/gitea-admin/v1/bin/gitea-admin-wrapper.sh"
su - "$AGENT_USER" -c "git config --global credential.helper '!$WRAPPER_PATH git-credential-helper'"
echo "gitea-admin: token staged at $STAGED_TOKEN (0600 $AGENT_USER:$AGENT_USER)"
echo "gitea-admin: git credential helper configured"