#!/bin/bash # gitea-https init — stage the token for the agent user and configure the # git credential helper to read from the staged copy. # # 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/git/gitea-https-token. The git credential helper # reads from the staged copy at every git invocation; the ESO mount path # is never accessed by the agent. # # This was previously a `cat /run/agent/secrets/gitea-https/token` from # inside an init.sh-generated helper script, which silently failed because # the helper runs as agent and the ESO file is root:root mode 0400. The # stage-and-helper-points-at-stage pattern matches gitea-ssh / minimax / # z-ai / anthropic-cloud-paul-oauth. # # Rotation handling: per-container init. Ephemeral container agents # always pick up the latest mounted token. Long-running sessions need a # future scripts.control_loop hook to re-stage between git invocations. set -euo pipefail ESO_TOKEN="/run/agent/secrets/gitea-https/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. STAGED_DIR="$AGENT_HOME/.config/git" STAGED_TOKEN="$STAGED_DIR/gitea-https-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" # Generate the credential helper. It reads from the STAGED copy, not the # ESO mount, so it works under the agent's UID. CRED_HELPER="/opt/harness/contexts/gitea-https/v1/git-credential-gitea.sh" cat > "$CRED_HELPER" <