validate.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. # Validates a deployed Turnstile siteverify Worker end-to-end.
  3. #
  4. # Reads:
  5. # $CLOUDFLARE_API_TOKEN (required for hostname check)
  6. #
  7. # Args:
  8. # --worker-url <url> Deployed Worker URL (from worker-deploy.sh)
  9. # --account-id <id> Cloudflare account ID
  10. # --sitekey <key> Widget sitekey (from widget-create.sh)
  11. # --expected-domains <a,b,c> Comma-separated domains that must appear in the widget's domains array
  12. #
  13. # Outputs JSON. Exit 0 if all three checks pass, 1 otherwise.
  14. # ok: {"status":"ok"}
  15. # fail: {"status":"error","check":"health|dummy_siteverify|worker_metadata|hostname","detail":"<msg>"}
  16. set -uo pipefail
  17. while [[ $# -gt 0 ]]; do
  18. case $1 in
  19. --worker-url) WORKER_URL="$2"; shift 2 ;;
  20. --account-id) ACCOUNT_ID="$2"; shift 2 ;;
  21. --sitekey) SITEKEY="$2"; shift 2 ;;
  22. --expected-domains) EXPECTED_DOMAINS="$2"; shift 2 ;;
  23. *) echo "validate: unknown arg $1" >&2; exit 2 ;;
  24. esac
  25. done
  26. : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN must be set}"
  27. : "${WORKER_URL:?--worker-url required}"
  28. : "${ACCOUNT_ID:?--account-id required}"
  29. : "${SITEKEY:?--sitekey required}"
  30. : "${EXPECTED_DOMAINS:?--expected-domains required}"
  31. # Check 1: health endpoint
  32. health=$(curl -sSf "${WORKER_URL}/health" 2>/dev/null || echo "")
  33. if [ -z "$health" ] || ! echo "$health" | grep -q '"ok":true'; then
  34. echo "validate: health check failed; $WORKER_URL/health did not return {ok:true,version:...}" >&2
  35. echo "{\"status\":\"error\",\"check\":\"health\",\"detail\":\"worker /health did not respond ok:true\"}"
  36. exit 1
  37. fi
  38. # Check 2: dummy siteverify; should return success:false + error-codes array
  39. dummy=$(curl -sS -X POST "${WORKER_URL}/" \
  40. -H "Content-Type: application/json" \
  41. -d '{"token":"XXXX.DUMMY.TOKEN.XXXX"}' 2>/dev/null || echo "")
  42. success=$(echo "$dummy" | (jq -r '.success // "missing"' 2>/dev/null || echo "missing"))
  43. errors=$(echo "$dummy" | (jq -r '.["error-codes"] | length // 0' 2>/dev/null || echo "0"))
  44. if [ "$success" != "false" ] || [ "$errors" = "0" ]; then
  45. echo "validate: dummy siteverify check failed; expected success:false + error-codes; got: $dummy" >&2
  46. echo "{\"status\":\"error\",\"check\":\"dummy_siteverify\",\"detail\":\"unexpected response shape\"}"
  47. exit 1
  48. fi
  49. # Check 2b: confirm the Worker is the managed template (not a customer-written
  50. # replacement) by looking for the _worker metadata field. If absent, the user
  51. # deployed a custom Worker; surface it so the agent can alert them.
  52. worker_meta=$(echo "$dummy" | (jq -r '._worker.worker_version // "missing"' 2>/dev/null || echo "missing"))
  53. if [ "$worker_meta" = "missing" ]; then
  54. echo "validate: _worker metadata missing from response; this is not the managed Spin Worker template." >&2
  55. echo "{\"status\":\"error\",\"check\":\"worker_metadata\",\"detail\":\"_worker field missing; user may have deployed a custom Worker\"}"
  56. exit 1
  57. fi
  58. # Check 3: hostname / widget domains registered
  59. widget=$(curl -sS \
  60. "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/challenges/widgets/$SITEKEY" \
  61. -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" 2>/dev/null)
  62. registered=$(echo "$widget" | (jq -r '.result.domains[]' 2>/dev/null || python3 -c "import sys,json; [print(d) for d in json.load(sys.stdin)['result']['domains']]"))
  63. missing=""
  64. IFS=',' read -ra DOMS <<< "$EXPECTED_DOMAINS"
  65. for d in "${DOMS[@]}"; do
  66. if ! echo "$registered" | grep -qFx "$d"; then
  67. missing="${missing}${d} "
  68. fi
  69. done
  70. if [ -n "$missing" ]; then
  71. echo "validate: hostname check failed; domains not on widget: $missing" >&2
  72. echo "{\"status\":\"error\",\"check\":\"hostname\",\"detail\":\"missing domains: ${missing% }\"}"
  73. exit 1
  74. fi
  75. echo '{"status":"ok"}'