spawn/shared/common.sh
Sprite da7724da68 refactor: Extract SSH key management helpers to reduce nesting
Created helper functions in shared/common.sh to simplify ensure_ssh_key():
- generate_ssh_key_if_missing(key_path): Generate SSH key if needed
- get_ssh_fingerprint(pub_path): Get MD5 fingerprint
- json_escape(string): JSON-escape strings for API bodies

Refactored ensure_ssh_key() in all cloud providers to use these helpers:
- Reduced function length from 35-52 lines to 24-28 lines
- Eliminated nested conditionals
- Made the code flow more linear and readable
- Centralized SSH key generation and fingerprint logic

Benefits:
- Single source of truth for SSH key operations
- Reduced code duplication (~40 lines per provider → 3 helper functions)
- Easier to maintain and test
- More consistent error handling

All tests pass (42/42).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-07 20:03:43 +00:00

366 lines
12 KiB
Bash

#!/bin/bash
# Shared bash functions used across all spawn scripts
# Provider-agnostic utilities for logging, input, OAuth, etc.
#
# This file is meant to be sourced by cloud provider-specific common.sh files.
# It does not set bash flags (like set -euo pipefail) as those should be set
# by the scripts that source this file.
# ============================================================
# Color definitions and logging
# ============================================================
# Use non-readonly vars to avoid errors if sourced multiple times
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print colored messages (to stderr so they don't pollute command substitution output)
log_info() {
echo -e "${GREEN}$1${NC}" >&2
}
log_warn() {
echo -e "${YELLOW}$1${NC}" >&2
}
log_error() {
echo -e "${RED}$1${NC}" >&2
}
# ============================================================
# Input handling
# ============================================================
# Safe read function that works in both interactive and non-interactive modes
safe_read() {
local prompt="$1"
local result=""
if [[ -t 0 ]]; then
# stdin is a terminal - read directly
read -p "$prompt" result
elif echo -n "" > /dev/tty 2>/dev/null; then
# /dev/tty is functional - use it
read -p "$prompt" result < /dev/tty
else
# No interactive input available
log_error "Cannot read input: no TTY available"
return 1
fi
echo "$result"
}
# ============================================================
# Network utilities
# ============================================================
# Listen on a port with netcat (handles busybox/Termux nc requiring -p flag)
nc_listen() {
local port=$1
shift
# Detect if nc requires -p flag (busybox nc on Termux)
if nc --help 2>&1 | grep -q "BusyBox\|busybox" || nc --help 2>&1 | grep -q "\-p "; then
nc -l -p "$port" "$@"
else
nc -l "$port" "$@"
fi
}
# Open browser to URL (supports macOS, Linux, Termux)
open_browser() {
local url=$1
if command -v termux-open-url &> /dev/null; then
termux-open-url "$url" </dev/null
elif command -v open &> /dev/null; then
open "$url" </dev/null
elif command -v xdg-open &> /dev/null; then
xdg-open "$url" </dev/null
else
log_warn "Please open: ${url}"
fi
}
# Validate model ID to prevent command injection
validate_model_id() {
local model_id="$1"
if [[ -z "$model_id" ]]; then return 0; fi
if [[ ! "$model_id" =~ ^[a-zA-Z0-9/_:.-]+$ ]]; then
log_error "Invalid model ID: contains unsafe characters"
log_error "Model IDs should only contain: letters, numbers, /, -, _, :, ."
return 1
fi
return 0
}
# ============================================================
# OpenRouter authentication
# ============================================================
# Manually prompt for API key
get_openrouter_api_key_manual() {
echo ""
log_warn "Manual API Key Entry"
echo -e "${YELLOW}Get your API key from: https://openrouter.ai/settings/keys${NC}"
echo ""
local api_key=""
while [[ -z "$api_key" ]]; do
api_key=$(safe_read "Enter your OpenRouter API key: ") || return 1
# Basic validation - OpenRouter keys typically start with "sk-or-"
if [[ -z "$api_key" ]]; then
log_error "API key cannot be empty"
elif [[ ! "$api_key" =~ ^sk-or-v1-[a-f0-9]{64}$ ]]; then
log_warn "Warning: API key format doesn't match expected pattern (sk-or-v1-...)"
local confirm=$(safe_read "Use this key anyway? (y/N): ") || return 1
if [[ "$confirm" =~ ^[Yy]$ ]]; then
break
else
api_key=""
fi
fi
done
log_info "API key accepted!"
echo "$api_key"
}
# Generate OAuth success response HTML
create_oauth_response_html() {
cat << 'HTML_EOF'
HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<html><head><style>@keyframes checkmark{0%{transform:scale(0) rotate(-45deg);opacity:0}60%{transform:scale(1.2) rotate(-45deg);opacity:1}100%{transform:scale(1) rotate(-45deg);opacity:1}}@keyframes fadein{from{opacity:0;transform:translateY(10px)}to{opacity:1;transform:translateY(0)}}body{font-family:system-ui,-apple-system,sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#1a1a2e}.card{text-align:center;color:#fff}.check{width:80px;height:80px;border-radius:50%;background:#00d4aa22;display:flex;align-items:center;justify-content:center;margin:0 auto 24px}.check::after{content:"";display:block;width:28px;height:14px;border-left:4px solid #00d4aa;border-bottom:4px solid #00d4aa;animation:checkmark .5s ease forwards}h1{color:#00d4aa;margin:0 0 8px;font-size:1.6rem}p{margin:0 0 6px;color:#ffffffcc;font-size:1rem}.sub{color:#ffffff66;font-size:.85rem;animation:fadein .5s ease .5s both}</style></head><body><div class="card"><div class="check"></div><h1>Authentication Successful!</h1><p>Redirecting back to terminal...</p><p class="sub">This tab will close automatically</p></div><script>setTimeout(function(){try{window.close()}catch(e){}setTimeout(function(){document.querySelector(".sub").textContent="You can safely close this tab"},500)},3000)</script></body></html>
HTML_EOF
}
# Start OAuth callback server in background, returns server PID
start_oauth_server() {
local port="$1"
local code_file="$2"
local success_response=$(create_oauth_response_html)
(
while true; do
local response_file=$(mktemp)
echo -e "$success_response" > "$response_file"
local request=$(nc_listen "$port" < "$response_file" 2>/dev/null | head -1)
local nc_status=$?
rm -f "$response_file"
if [[ $nc_status -ne 0 ]]; then
break
fi
if [[ "$request" == *"/callback?code="* ]]; then
local code=$(echo "$request" | sed -n 's/.*code=\([^ &]*\).*/\1/p')
echo "$code" > "$code_file"
break
fi
done
) </dev/null &
echo $!
}
# Wait for OAuth code with timeout, returns 0 if code received
wait_for_oauth_code() {
local code_file="$1"
local timeout="${2:-120}"
local elapsed=0
while [[ ! -f "$code_file" ]] && [[ $elapsed -lt $timeout ]]; do
sleep 1
((elapsed++))
done
[[ -f "$code_file" ]]
}
# Exchange OAuth code for API key
exchange_oauth_code() {
local oauth_code="$1"
local key_response=$(curl -s -X POST "https://openrouter.ai/api/v1/auth/keys" \
-H "Content-Type: application/json" \
-d "{\"code\": \"$oauth_code\"}")
local api_key=$(echo "$key_response" | grep -o '"key":"[^"]*"' | sed 's/"key":"//;s/"$//')
if [[ -z "$api_key" ]]; then
log_error "Failed to exchange OAuth code: ${key_response}"
return 1
fi
echo "$api_key"
}
# Clean up OAuth session resources
cleanup_oauth_session() {
local server_pid="$1"
local oauth_dir="$2"
if [[ -n "$server_pid" ]]; then
kill "$server_pid" 2>/dev/null || true
wait "$server_pid" 2>/dev/null || true
fi
if [[ -n "$oauth_dir" && -d "$oauth_dir" ]]; then
rm -rf "$oauth_dir"
fi
}
# Try OAuth flow (orchestrates the helper functions above)
try_oauth_flow() {
local callback_port=${1:-5180}
log_warn "Attempting OAuth authentication..."
if ! command -v nc &> /dev/null; then
log_warn "netcat (nc) not found - OAuth server unavailable"
return 1
fi
local callback_url="http://localhost:${callback_port}/callback"
local auth_url="https://openrouter.ai/auth?callback_url=${callback_url}"
local oauth_dir=$(mktemp -d)
local code_file="$oauth_dir/code"
log_warn "Starting local OAuth server on port ${callback_port}..."
local server_pid=$(start_oauth_server "$callback_port" "$code_file")
sleep 1
if ! kill -0 "$server_pid" 2>/dev/null; then
log_warn "Failed to start OAuth server (port may be in use)"
cleanup_oauth_session "" "$oauth_dir"
return 1
fi
log_warn "Opening browser to authenticate with OpenRouter..."
open_browser "$auth_url"
if ! wait_for_oauth_code "$code_file" 120; then
log_warn "OAuth timeout - no response received"
cleanup_oauth_session "$server_pid" "$oauth_dir"
return 1
fi
local oauth_code=$(cat "$code_file")
cleanup_oauth_session "$server_pid" "$oauth_dir"
log_warn "Exchanging OAuth code for API key..."
local api_key=$(exchange_oauth_code "$oauth_code") || return 1
log_info "Successfully obtained OpenRouter API key via OAuth!"
echo "$api_key"
}
# Main function: Try OAuth, fallback to manual entry
get_openrouter_api_key_oauth() {
local callback_port=${1:-5180}
# Try OAuth flow first
local api_key=$(try_oauth_flow "$callback_port")
if [[ -n "$api_key" ]]; then
echo "$api_key"
return 0
fi
# OAuth failed, offer manual entry
echo ""
log_warn "OAuth authentication failed or unavailable"
log_warn "You can enter your API key manually instead"
echo ""
local manual_choice=$(safe_read "Would you like to enter your API key manually? (Y/n): ") || {
log_error "Cannot prompt for manual entry in non-interactive mode"
log_warn "Set OPENROUTER_API_KEY environment variable for non-interactive usage"
return 1
}
if [[ ! "$manual_choice" =~ ^[Nn]$ ]]; then
api_key=$(get_openrouter_api_key_manual)
echo "$api_key"
return 0
else
log_error "Authentication cancelled by user"
return 1
fi
}
# ============================================================
# SSH key management helpers
# ============================================================
# Generate SSH key if it doesn't exist
# Usage: generate_ssh_key_if_missing KEY_PATH
generate_ssh_key_if_missing() {
local key_path="$1"
if [[ -f "$key_path" ]]; then
return 0
fi
log_warn "Generating SSH key..."
mkdir -p "$(dirname "$key_path")"
ssh-keygen -t ed25519 -f "$key_path" -N "" -q
log_info "SSH key generated at $key_path"
}
# Get MD5 fingerprint of SSH public key
# Usage: get_ssh_fingerprint PUB_KEY_PATH
get_ssh_fingerprint() {
local pub_path="$1"
ssh-keygen -lf "$pub_path" -E md5 2>/dev/null | awk '{print $2}' | sed 's/MD5://'
}
# JSON-escape a string (for embedding in JSON bodies)
# Usage: json_escape STRING
json_escape() {
local string="$1"
python3 -c "import json; print(json.dumps('$string'))" 2>/dev/null || echo "\"$string\""
}
# Extract SSH key IDs from cloud provider API response
# Usage: extract_ssh_key_ids API_RESPONSE KEY_FIELD
# KEY_FIELD: "ssh_keys" (DigitalOcean/Vultr) or "data" (Linode)
extract_ssh_key_ids() {
local api_response="$1"
local key_field="${2:-ssh_keys}"
python3 -c "
import json, sys
data = json.loads(sys.stdin.read())
ids = [k['id'] for k in data.get('$key_field', [])]
print(json.dumps(ids))
" <<< "$api_response"
}
# ============================================================
# SSH connectivity helpers
# ============================================================
# Generic SSH wait function - polls until a remote command succeeds
# Usage: generic_ssh_wait IP SSH_OPTS TEST_CMD DESCRIPTION MAX_ATTEMPTS INTERVAL
generic_ssh_wait() {
local ip="$1"
local ssh_opts="$2"
local test_cmd="$3"
local description="$4"
local max_attempts="${5:-30}"
local interval="${6:-5}"
local attempt=1
log_warn "Waiting for $description to $ip..."
while [[ "$attempt" -le "$max_attempts" ]]; do
if ssh $ssh_opts "root@$ip" "$test_cmd" >/dev/null 2>&1; then
log_info "$description ready"
return 0
fi
log_warn "Waiting for $description... ($attempt/$max_attempts)"
sleep "$interval"
((attempt++))
done
log_error "$description failed after $max_attempts attempts"
return 1
}