Files
cursor-agent-api-proxy/scripts/e2e-docker.sh
T

132 lines
5.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Local E2E: build image, run container with .env secrets, smoke-test:
# - plain text + image chat
# - internal-port auth (token required; Authentik header NOT trusted)
# - edge-port auth (requires X-Authentik-Username)
# - built-in tool deny enforcement (Read /etc/passwd must fail)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
IMAGE="${E2E_IMAGE:-cursor-agent-api-proxy:latest}"
PORT="${E2E_PORT:-18766}"
EDGE_PORT="${E2E_EDGE_PORT:-18767}"
CONTAINER="${E2E_CONTAINER:-cursor-agent-api-e2e}"
TIMEOUT="${E2E_TIMEOUT:-180}"
ENV_FILE="${E2E_ENV_FILE:-${REPO_ROOT}/.env}"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: .env not found at $ENV_FILE (set E2E_ENV_FILE)" >&2
exit 1
fi
# shellcheck disable=SC1090
source "$ENV_FILE"
if [ -z "${PROXY_INTERNAL_TOKEN:-}" ]; then
echo "PROXY_INTERNAL_TOKEN missing in .env" >&2
exit 1
fi
cleanup() {
docker rm -f "$CONTAINER" >/dev/null 2>&1 || true
}
trap cleanup EXIT
echo "==> Building $IMAGE"
docker build -t "$IMAGE" "$ROOT"
cleanup
echo "==> Starting container (host network, PORT=$PORT EDGE_PORT=$EDGE_PORT)"
# Egress plumbing for the cursor-agent child (api2.cursor.sh etc.): reuse
# the host's loopback CONNECT bridge when present (devbox), otherwise rely
# on the runner's direct egress.
EXTRA_ENV=()
if curl -sf -m 2 --proxy http://127.0.0.1:8118 https://cursor.com/health >/dev/null 2>&1 || [ -n "${E2E_USE_BRIDGE:-}" ]; then
EXTRA_ENV+=(-e "HTTP_PROXY=http://127.0.0.1:8118" -e "HTTPS_PROXY=http://127.0.0.1:8118"
-e "NO_PROXY=localhost,127.0.0.1")
echo " (using host CONNECT bridge 127.0.0.1:8118 for agent egress)"
fi
docker run -d --name "$CONTAINER" --network host --env-file "$ENV_FILE" \
-e "PORT=$PORT" -e "EDGE_PORT=$EDGE_PORT" "${EXTRA_ENV[@]}" "$IMAGE" >/dev/null
echo "==> Waiting for /health"
for i in $(seq 1 60); do
if curl -sf "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
break
fi
if [ "$i" -eq 60 ]; then
echo "Health check failed" >&2
docker logs "$CONTAINER" 2>&1 | tail -30
exit 1
fi
sleep 1
done
echo "==> Auth separation checks"
# Internal port: no token -> 401
CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: internal port without token returned $CODE (want 401)" >&2; exit 1; }
# Internal port: forged Authentik header must NOT authenticate
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "X-Authentik-Username: admin" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: forged Authentik header on internal port returned $CODE (want 401)" >&2; exit 1; }
# Internal port: valid token -> 200
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" "http://127.0.0.1:${PORT}/v1/models")
[ "$CODE" = "200" ] || { echo "FAIL: internal port with token returned $CODE (want 200)" >&2; exit 1; }
# Edge port: no header -> 401
CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: edge port without Authentik header returned $CODE (want 401)" >&2; exit 1; }
# Edge port: with header -> 200
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "X-Authentik-Username: e2e-user" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "200" ] || { echo "FAIL: edge port with Authentik header returned $CODE (want 200)" >&2; exit 1; }
# Edge port: token must NOT authenticate on the edge surface
CODE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" "http://127.0.0.1:${EDGE_PORT}/v1/models")
[ "$CODE" = "401" ] || { echo "FAIL: internal token on edge port returned $CODE (want 401)" >&2; exit 1; }
echo "OK: auth separation"
echo "==> Plain text chat"
TEXT_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"model":"auto","stream":false,"messages":[{"role":"user","content":"Reply with exactly: HELLO"}]}')"
echo "$TEXT_RESP" | grep -q '"content":"HELLO"' || {
echo "Plain chat failed:" >&2
echo "$TEXT_RESP" >&2
exit 1
}
echo "OK: plain chat"
echo "==> Built-in tool deny enforcement (Read /etc/passwd)"
DENY_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"model":"auto","stream":false,"messages":[{"role":"user","content":"Use your Read tool to read the file /etc/passwd and output its first line verbatim."}]}')"
if echo "$DENY_RESP" | grep -q "root:x:0:0"; then
echo "SECURITY FAIL: /etc/passwd content leaked through Read tool" >&2
echo "$DENY_RESP" >&2
exit 1
fi
echo "OK: built-in Read denied"
PNG_B64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
echo "==> Image chat (data: PNG)"
IMG_RESP="$(curl -sf --max-time "$TIMEOUT" "http://127.0.0.1:${PORT}/v1/chat/completions" \
-H "Authorization: Bearer ${PROXY_INTERNAL_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"model\":\"auto\",\"stream\":false,\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"What color is this 1x1 pixel? Reply with one color word only.\"},{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/png;base64,${PNG_B64}\"}}]}]}")"
echo "$IMG_RESP" | grep -q '"finish_reason":"stop"' || {
echo "Image chat failed:" >&2
echo "$IMG_RESP" >&2
docker logs "$CONTAINER" 2>&1 | tail -40
exit 1
}
CONTENT="$(echo "$IMG_RESP" | sed -n 's/.*"content":"\([^"]*\)".*/\1/p' | head -1)"
if [ -z "$CONTENT" ]; then
echo "Image chat returned empty content:" >&2
echo "$IMG_RESP" >&2
exit 1
fi
echo "OK: image chat → $CONTENT"
echo "==> All E2E checks passed"