#!/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. # # 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-." >&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"