171 lines
6.9 KiB
Bash
Executable File
171 lines
6.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Send a test event to the event capture intake and report what landed:
|
|
# POST /webhooks/capture/{source} on the dashboard listener, stored redacted for
|
|
# the Event inspector. With --secret the body is signed in X-Hub-Signature-256,
|
|
# so a signed source records it as authenticated and its bindings can dispatch.
|
|
#
|
|
# Usage:
|
|
# tools/webhook-send.sh # one generic capture event
|
|
# tools/webhook-send.sh --kind github-issue # realistic GitHub delivery
|
|
# tools/webhook-send.sh --kind sentry --source sentry-prod
|
|
# tools/webhook-send.sh --body '{"a":1}' # an exact payload
|
|
# tools/webhook-send.sh --file /tmp/event.json
|
|
# tools/webhook-send.sh --count 5 --delay 1
|
|
# tools/webhook-send.sh --kind github-issue --source gh --secret "$SOURCE_SECRET"
|
|
# tools/webhook-send.sh --list # kinds, and exit
|
|
#
|
|
# --url is the dashboard the send goes to and is read back from.
|
|
#
|
|
# Environment:
|
|
# ARCHIE_URL dashboard base URL (default http://127.0.0.1:8484)
|
|
# ARCHIE_WEBHOOK_SECRET the source's secret, when --secret is not given
|
|
#
|
|
# Limits the capture intake enforces (defaults from [capture] in
|
|
# config.example.toml):
|
|
# rate 1/s per remote address, burst 5 -> a fast --count > 5 needs --delay
|
|
# bodies over 256 KiB are rejected with 413
|
|
# Both are per-daemon-configurable, so a 429/413 here is the daemon's policy,
|
|
# not a broken sender.
|
|
set -euo pipefail
|
|
|
|
BASE_URL="${ARCHIE_URL:-http://127.0.0.1:8484}"
|
|
TARGET_URL=""
|
|
SOURCE="manual-test"
|
|
KIND="generic"
|
|
COUNT=1
|
|
DELAY=0
|
|
BODY=""
|
|
FILE=""
|
|
QUIET=0
|
|
SECRET="${ARCHIE_WEBHOOK_SECRET:-}"
|
|
GH_EVENT="issues"
|
|
|
|
usage() {
|
|
sed -n '2,40p' "$0" | sed 's/^# \{0,1\}//'
|
|
}
|
|
|
|
list_kinds() {
|
|
cat <<'KINDS'
|
|
Kinds (what each one posts):
|
|
generic {"hello":"world","n":1} small JSON
|
|
github-issue GitHub "issues" delivery (action/issue/repository/sender)
|
|
github-push GitHub "push" delivery (ref/repository/commits/pusher)
|
|
sentry Sentry "issue created" delivery
|
|
form application/x-www-form-urlencoded body
|
|
large ~64 KB JSON, to exercise the intake's body bound
|
|
empty no body at all
|
|
KINDS
|
|
}
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--source) SOURCE="${2:?--source needs a value}"; shift 2 ;;
|
|
--kind) KIND="${2:?--kind needs a value}"; shift 2 ;;
|
|
--count) COUNT="${2:?--count needs a value}"; shift 2 ;;
|
|
--delay) DELAY="${2:?--delay needs a value}"; shift 2 ;;
|
|
--body) BODY="${2:?--body needs a value}"; shift 2 ;;
|
|
--file) FILE="${2:?--file needs a value}"; shift 2 ;;
|
|
--url) TARGET_URL="${2:?--url needs a value}"; shift 2 ;;
|
|
--secret) SECRET="${2:?--secret needs a value}"; shift 2 ;;
|
|
--event) GH_EVENT="${2:?--event needs a value}"; shift 2 ;;
|
|
--quiet) QUIET=1; shift ;;
|
|
--list) list_kinds; exit 0 ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) echo "unknown argument: $1" >&2; echo >&2; usage >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
BASE_URL="${TARGET_URL:-$BASE_URL}"
|
|
|
|
CONTENT_TYPE="application/json"
|
|
if [ -n "$BODY" ]; then
|
|
PAYLOAD="$BODY"
|
|
elif [ -n "$FILE" ]; then
|
|
PAYLOAD="$(cat "$FILE")"
|
|
else
|
|
case "$KIND" in
|
|
generic) PAYLOAD='{"hello":"world","n":1}' ;;
|
|
github-issue)
|
|
# The receiver only queues an issue event that is open, not a pull
|
|
# request, and whose action could newly make it eligible.
|
|
PAYLOAD="$(printf '{"action":"opened","issue":{"number":42,"title":"Test issue from webhook-send.sh","body":"Filed by tools/webhook-send.sh.","state":"open","html_url":"https://github.com/samcharles93/archie-core/issues/42","assignees":[],"labels":[]},"repository":{"name":"archie-core","full_name":"samcharles93/archie-core","html_url":"https://github.com/samcharles93/archie-core","owner":{"login":"samcharles93"}},"sender":{"login":"webhook-send"}}')"
|
|
;;
|
|
github-push)
|
|
PAYLOAD='{"ref":"refs/heads/main","repository":{"name":"archie-core","full_name":"samcharles93/archie-core","owner":{"login":"samcharles93"}},"commits":[{"id":"c0ffee1","message":"Test commit from webhook-send.sh"}],"pusher":{"name":"webhook-send"}}'
|
|
;;
|
|
sentry)
|
|
PAYLOAD='{"action":"created","data":{"issue":{"title":"Test error from webhook-send.sh","culprit":"app/main.go"}},"project":"archie"}'
|
|
;;
|
|
form)
|
|
CONTENT_TYPE="application/x-www-form-urlencoded"
|
|
PAYLOAD='event=test&source=webhook-send.sh&detail=nothing-to-see'
|
|
;;
|
|
large)
|
|
PAYLOAD="$(printf '{"filler":"'; head -c 65536 /dev/zero | tr '\0' 'x'; printf '"}')"
|
|
;;
|
|
empty) PAYLOAD="" ;;
|
|
*) echo "unknown kind: $KIND" >&2; echo >&2; list_kinds >&2; exit 2 ;;
|
|
esac
|
|
fi
|
|
|
|
# sign prints the value the receiver's VerifyHMAC accepts: sha256= followed by
|
|
# the hex HMAC-SHA256 of the exact bytes sent. openssl first, python3 second:
|
|
# both compute the same digest as internal/webhookguard/hmac.go.
|
|
sign() {
|
|
local body="$1"
|
|
if command -v openssl >/dev/null 2>&1; then
|
|
printf '%s' "$body" | openssl dgst -sha256 -hmac "$SECRET" | awk '{print $NF}'
|
|
return
|
|
fi
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
printf '%s' "$body" | python3 -c 'import hmac,hashlib,sys; print(hmac.new(sys.argv[1].encode(), sys.stdin.buffer.read(), hashlib.sha256).hexdigest())' "$SECRET"
|
|
return
|
|
fi
|
|
echo "signing needs openssl or python3 on PATH" >&2
|
|
return 1
|
|
}
|
|
|
|
send_capture() {
|
|
local url="${BASE_URL%/}/webhooks/capture/${SOURCE}"
|
|
local code
|
|
local args=(-sS -o /tmp/webhook-send.out -w '%{http_code}' -X POST "$url" -H "Content-Type: ${CONTENT_TYPE}" -H "X-GitHub-Event: ${GH_EVENT}")
|
|
[ -n "$SECRET" ] && args+=(-H "X-Hub-Signature-256: sha256=$(sign "$PAYLOAD")")
|
|
[ -n "$PAYLOAD" ] && args+=(--data-binary "$PAYLOAD")
|
|
code="$(curl "${args[@]}")" || {
|
|
echo "cannot reach ${BASE_URL} — is the daemon running? (task dev, or archied)" >&2
|
|
return 1
|
|
}
|
|
if [ "$code" -eq 429 ]; then
|
|
echo "POST ${url} -> 429: the intake's rate limit (default 1/s, burst 5 per address)." >&2
|
|
echo "Space sends out with --delay 1, or raise [capture] rate_per_second / rate_burst." >&2
|
|
return 1
|
|
fi
|
|
if [ "$code" -eq 413 ]; then
|
|
echo "POST ${url} -> 413: body over [capture] max_body_bytes (default 256 KiB)." >&2
|
|
return 1
|
|
fi
|
|
if [ "$code" -lt 200 ] || [ "$code" -ge 300 ]; then
|
|
echo "POST ${url} -> ${code}: $(cat /tmp/webhook-send.out)" >&2
|
|
return 1
|
|
fi
|
|
[ "$QUIET" -eq 1 ] || echo "POST ${url} -> ${code} (source: ${SOURCE}, kind: ${KIND}$([ -n "$SECRET" ] && echo ', signed'))"
|
|
}
|
|
|
|
i=0
|
|
while [ "$i" -lt "$COUNT" ]; do
|
|
send_capture
|
|
i=$((i + 1))
|
|
[ "$i" -lt "$COUNT" ] && [ "$DELAY" != "0" ] && sleep "$DELAY"
|
|
done
|
|
|
|
# Read it back, so a silent drop is visible here rather than only in the UI.
|
|
if [ "$QUIET" -eq 0 ]; then
|
|
sleep 0.3
|
|
echo
|
|
echo "Latest captures:"
|
|
curl -sS "${BASE_URL%/}/api/captures?limit=${COUNT}" \
|
|
| tr ',' '\n' | grep -E '"id"|"source"|"received_at"|"content_type"' | sed 's/^ *//' | head -20
|
|
echo
|
|
echo "Open ${BASE_URL%/}/captures to inspect them."
|
|
fi
|