Files
buzz/scripts/setup-goose-agent.sh
Bradley Axen e186c6d916 Add just goose for one-command agent setup + fix API token auth
Adds an idempotent setup script and justfile recipe so that connecting
a Goose agent to a local Sprout relay is a single command:

    just goose

What this does:
- scripts/setup-goose-agent.sh: mints a Nostr keypair + API token on
  first run, persists to .sprout-agent.env (gitignored, chmod 600).
  Subsequent runs are a no-op.
- justfile: 'just goose' sources the agent env and launches goose with
  the sprout-mcp-server extension.
- .gitignore: excludes .sprout-agent.env.

Also fixes a bug where API token authentication was not wired up in the
relay's NIP-42 auth handler. The AuthService intentionally has no DB
access, so sprout_ tokens need to be intercepted in handle_auth before
calling verify_auth_event. The handler now:
1. Extracts the auth_token tag from the NIP-42 event
2. If it starts with sprout_, hashes it, looks it up via
   Db::get_api_token_by_hash, and delegates to
   AuthService::verify_api_token_against_hash
3. Updates last_used_at on success
4. Falls through to the existing JWT/no-token paths otherwise
2026-03-10 14:21:03 -07:00

103 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================================
# setup-goose-agent.sh — Idempotent setup for a Goose agent identity
# =============================================================================
# Creates .sprout-agent.env with a minted API token and Nostr keypair.
# If the file already exists, does nothing. Safe to run repeatedly.
#
# Usage: ./scripts/setup-goose-agent.sh
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
AGENT_ENV="${REPO_ROOT}/.sprout-agent.env"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
log() { echo -e "${BLUE}[setup-agent]${NC} $*"; }
success(){ echo -e "${GREEN}[setup-agent]${NC}$*"; }
warn() { echo -e "${YELLOW}[setup-agent]${NC} ⚠️ $*"; }
error() { echo -e "${RED}[setup-agent]${NC}$*" >&2; }
# ---- Already set up? --------------------------------------------------------
if [[ -f "${AGENT_ENV}" ]]; then
success "Agent identity already exists at .sprout-agent.env — skipping."
exit 0
fi
# ---- Preflight ---------------------------------------------------------------
# Need the relay DB to mint a token
if ! docker inspect --format='{{.State.Health.Status}}' sprout-mysql 2>/dev/null | grep -q healthy; then
error "MySQL is not running. Run 'just setup' first."
exit 1
fi
# Build sprout-admin if needed
ADMIN_BIN="${REPO_ROOT}/target/debug/sprout-admin"
if [[ ! -x "${ADMIN_BIN}" ]]; then
log "Building sprout-admin..."
cargo build -p sprout-admin 2>&1 | tail -3
fi
# Build sprout-mcp-server if needed
MCP_BIN="${REPO_ROOT}/target/debug/sprout-mcp-server"
if [[ ! -x "${MCP_BIN}" ]]; then
log "Building sprout-mcp-server..."
cargo build -p sprout-mcp 2>&1 | tail -3
fi
# ---- Mint token --------------------------------------------------------------
log "Minting agent token..."
# Source .env for DATABASE_URL
if [[ -f "${REPO_ROOT}/.env" ]]; then
set -o allexport
source "${REPO_ROOT}/.env"
set +o allexport
fi
export DATABASE_URL="${DATABASE_URL:-mysql://sprout:sprout_dev@localhost:3306/sprout}"
OUTPUT=$("${ADMIN_BIN}" mint-token \
--name "goose-agent" \
--scopes "messages:read,messages:write,channels:read,channels:write" \
2>&1)
# Parse the nsec and token from the box-drawing output
NSEC=$(echo "${OUTPUT}" | grep -oE 'nsec1[a-z0-9]+')
TOKEN=$(echo "${OUTPUT}" | grep -oE 'sprout_[0-9a-f]+')
if [[ -z "${NSEC}" || -z "${TOKEN}" ]]; then
error "Failed to parse credentials from mint-token output:"
echo "${OUTPUT}"
exit 1
fi
# ---- Write .sprout-agent.env -------------------------------------------------
cat > "${AGENT_ENV}" <<EOF
# Sprout agent credentials — generated by setup-goose-agent.sh
# DO NOT commit this file.
SPROUT_RELAY_URL=ws://localhost:3000
SPROUT_PRIVATE_KEY=${NSEC}
SPROUT_API_TOKEN=${TOKEN}
EOF
chmod 600 "${AGENT_ENV}"
success "Agent identity created at .sprout-agent.env"
echo ""
echo -e " ${BLUE}Relay:${NC} ws://localhost:3000"
echo -e " ${BLUE}Private key:${NC} ${NSEC:0:20}..."
echo -e " ${BLUE}API token:${NC} ${TOKEN:0:20}..."
echo ""
echo -e " Run ${GREEN}just goose${NC} to start a Goose session with Sprout."
echo ""