Repository navigation
Expand file tree
/
Copy pathreceiver.py
More file actions
153 lines (124 loc) · 5.73 KB
/
Copy pathreceiver.py
File metadata and controls
153 lines (124 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Webhook processing: the receiver (web service)
A FastAPI app that receives GitHub webhooks and hands each one to a workflow
task. For every delivery it:
1. Verifies the X-Hub-Signature-256 header against the raw request body.
2. Stores the payload in Postgres, keyed by the X-GitHub-Delivery ID.
3. Answers 200 without doing anything else if that delivery was already
processed (a redelivery).
4. Otherwise starts process_delivery with the delivery ID and answers 202.
It does no processing itself, so it answers in well under GitHub's 10-second
limit.
"""
import hashlib
import hmac
import json
import logging
import os
import time
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from psycopg.types.json import Jsonb
from render import RenderAsync
import db
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
SLUG = os.getenv("WORKFLOW_SLUG", "webhook-processing")
# The events process_delivery handles. GitHub sends only the events you pick
# in the webhook's settings; anything else gets a 200 and is dropped.
HANDLED_EVENTS = {"push", "issues"}
render: RenderAsync | None = None
secret = b""
@asynccontextmanager
async def lifespan(app: FastAPI):
global render, secret
# Without a secret, anyone who finds the URL could send fake events.
# Refuse to start rather than accept unsigned requests.
if not os.getenv("GITHUB_WEBHOOK_SECRET"):
raise RuntimeError("GITHUB_WEBHOOK_SECRET is not set")
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
secret = os.environ["GITHUB_WEBHOOK_SECRET"].encode()
await db.apply_schema()
# Reads RENDER_API_KEY, or RENDER_USE_LOCAL_DEV to use the local task server.
render = RenderAsync()
yield
app = FastAPI(title="Webhook processing", lifespan=lifespan)
def signature_ok(body: bytes, header: str | None) -> bool:
"""Check X-Hub-Signature-256: "sha256=" + the HMAC-SHA256 hex digest of the body."""
if not header:
return False
expected = "sha256=" + hmac.new(secret, body, hashlib.sha256).hexdigest()
# compare_digest takes the same time wherever the strings differ, so the
# response time doesn't leak how much of a forged signature was right.
return hmac.compare_digest(expected, header)
@app.post("/webhooks/github")
async def github_webhook(request: Request):
started = time.perf_counter()
# Verify against the exact bytes GitHub signed. Parsing and re-encoding
# the JSON first would change them.
body = await request.body()
if not signature_ok(body, request.headers.get("X-Hub-Signature-256")):
logger.warning("[RECEIVER] Rejected a request with a missing or bad signature")
return JSONResponse(status_code=401, content={"detail": "Bad signature"})
event = request.headers.get("X-GitHub-Event", "")
delivery_id = request.headers.get("X-GitHub-Delivery", "")
if not event or not delivery_id:
return JSONResponse(status_code=400, content={"detail": "Missing GitHub headers"})
# GitHub sends a ping when you create the webhook.
if event == "ping":
return {"ok": True}
if event not in HANDLED_EVENTS:
return {"ignored": event}
if not request.headers.get("Content-Type", "").startswith("application/json"):
return JSONResponse(
status_code=415,
content={"detail": "Set the webhook's content type to application/json"},
)
try:
payload = json.loads(body)
except ValueError:
return JSONResponse(status_code=400, content={"detail": "Body isn't JSON"})
# Store the payload and pass only the delivery ID to the task. GitHub
# payloads can be up to 25 MB; task arguments are capped at 4 MB.
async with await db.connect() as conn:
cur = await conn.execute(
"""
INSERT INTO webhook_deliveries (delivery_id, event, payload)
VALUES (%s, %s, %s)
ON CONFLICT (delivery_id)
DO UPDATE SET receipts = webhook_deliveries.receipts + 1
RETURNING status, receipts
""",
(delivery_id, event, Jsonb(payload)),
)
row = await cur.fetchone()
# A redelivery of a delivery that's already processed: acknowledge it
# and start nothing.
if row["status"] == "processed":
logger.info(f"[RECEIVER] {delivery_id} ({event}) duplicate, receipt {row['receipts']}")
return {"delivery_id": delivery_id, "duplicate": True}
# New, or sent again before it was processed (for example after a
# failed run): start processing. If two runs end up processing the
# same delivery, process_delivery applies it only once.
try:
run = await render.workflows.start_task(f"{SLUG}/process_delivery", [delivery_id])
except Exception:
# Fail the delivery so it shows as failed in GitHub and can be
# redelivered. The payload is stored, so replay_pending can also
# pick it up.
logger.exception(f"[RECEIVER] Couldn't start process_delivery for {delivery_id}")
return JSONResponse(status_code=503, content={"detail": "Couldn't start processing"})
await conn.execute(
"UPDATE webhook_deliveries SET last_run_id = %s WHERE delivery_id = %s",
(run.id, delivery_id),
)
elapsed_ms = round((time.perf_counter() - started) * 1000)
logger.info(f"[RECEIVER] {delivery_id} ({event}) -> {run.id} in {elapsed_ms} ms")
return JSONResponse(status_code=202, content={"delivery_id": delivery_id, "run_id": run.id})
@app.get("/healthz")
async def healthz():
return {"ok": True}