Render delivery attempt detail in the event log (closes #202)
All checks were successful
check / check (push) Successful in 6m0s

Expanding a delivery on the event log page now shows each recorded
attempt: attempt number, outcome, status code, duration, error and
response body. Previously a failure rendered as "target: failed" and
diagnosing it meant opening the per-webhook SQLite file by hand.

The response body is cut by SQLite via substr over a blob cast, the
same projection the event body uses, so an oversized stored response
never becomes a Go string. The page reports the cut with a marker.

Response bodies and errors are remote content, so both go through a
new delivery.Redactor that strips the target's own destination URL,
path, query and userinfo before rendering. Configured HTTP header
values are deliberately not redacted; they are as often routine as
secret, and replacing them would mangle ordinary responses. Target
configuration keeps reaching the template only as a TargetView.
This commit is contained in:
2026-08-20 04:14:20 +00:00
parent 10c8dd2331
commit d9e8e28846
7 changed files with 861 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/go-chi/chi"
"github.com/google/uuid"
"gorm.io/gorm"
"sneak.berlin/go/webhooker/internal/database"
"sneak.berlin/go/webhooker/internal/delivery"
)
@@ -100,6 +101,22 @@ type DeliveryView struct {
ID string
Status database.DeliveryStatus
Target delivery.TargetView
// Results is every recorded attempt at this delivery, in
// attempt order. Without it a failure renders as the
// status word alone and says nothing about why.
Results []DeliveryResultView
}
// eventLogTarget is what the event log needs to know about
// one target: the display-safe view its template renders, and
// the redactor that keeps that target's own credential out of
// the text its remote peer chose. The two are kept together
// so a caller cannot pick up one without the other, and apart
// from TargetView so the secrets never reach a template.
type eventLogTarget struct {
View delivery.TargetView
Redactor delivery.Redactor
}
// HandleSourceList shows a list of user's webhooks.
@@ -794,26 +811,35 @@ func (h *Handlers) HandleSourceLogs() http.HandlerFunc {
}
// loadTargetMap loads targets into a map of display-safe
// views keyed by target ID. The projection happens here so
// that no caller can hand a raw target, configuration blob
// and all, to a template.
// views keyed by target ID, each paired with its redactor.
// The projection happens here so that no caller can hand a
// raw target, configuration blob and all, to a template: the
// raw rows do not leave this function.
func (h *Handlers) loadTargetMap(
webhookID string,
) map[string]delivery.TargetView {
) map[string]eventLogTarget {
var targets []database.Target
h.db.DB().Where(
"webhook_id = ?", webhookID,
).Find(&targets)
views := delivery.NewTargetViews(targets)
targetMap := make(
map[string]delivery.TargetView, len(views),
map[string]eventLogTarget, len(targets),
)
for _, v := range views {
targetMap[v.ID] = v
for i := range targets {
targetMap[targets[i].ID] = eventLogTarget{
Redactor: delivery.NewRedactor(&targets[i]),
}
}
// The views come from NewTargetViews rather than being
// rebuilt here, so the masking rules stay in one place.
for _, v := range delivery.NewTargetViews(targets) {
entry := targetMap[v.ID]
entry.View = v
targetMap[v.ID] = entry
}
return targetMap
@@ -840,7 +866,7 @@ func (h *Handlers) parsePage(r *http.Request) int {
func (h *Handlers) loadEventsWithDeliveries(
w http.ResponseWriter,
webhook database.Webhook,
targetMap map[string]delivery.TargetView,
targetMap map[string]eventLogTarget,
page int,
) ([]EventLogView, int64) {
var totalEvents int64
@@ -877,37 +903,96 @@ func (h *Handlers) loadEventsWithDeliveries(
).Find(&rows)
result = make([]EventLogView, len(rows))
eventDeliveries := make([][]database.Delivery, len(rows))
var deliveryIDs []string
for i := range rows {
result[i] = rows[i].view()
var deliveries []database.Delivery
webhookDB.Where(
"event_id = ?", rows[i].ID,
).Find(&deliveries)
).Find(&eventDeliveries[i])
for j := range eventDeliveries[i] {
deliveryIDs = append(
deliveryIDs, eventDeliveries[i][j].ID,
)
}
}
attempts := h.loadDeliveryResults(webhookDB, deliveryIDs)
for i := range rows {
result[i].Deliveries = newDeliveryViews(
deliveries, targetMap,
eventDeliveries[i], targetMap, attempts,
)
}
return result, totalEvents
}
// loadDeliveryResults loads every recorded attempt for the
// page's deliveries in one query, keyed by delivery ID.
//
// Each response body is cut by SQLite rather than in Go, for
// the reason deliveryResultColumns gives. What the cut does
// not bound is how many attempts a delivery has: that is the
// target's MaxRetries, which the authenticated operator sets
// — the same class of operator-chosen dimension as the number
// of targets a webhook has, which this page already accepts.
// No part of it is chosen by the unauthenticated sender.
func (h *Handlers) loadDeliveryResults(
webhookDB *gorm.DB,
deliveryIDs []string,
) map[string][]deliveryResultRow {
if len(deliveryIDs) == 0 {
return nil
}
var rows []deliveryResultRow
webhookDB.Model(&database.DeliveryResult{}).Select(
deliveryResultColumns, maxRenderedResponseBytes,
).Where(
"delivery_id IN ?", deliveryIDs,
).Order("attempt_num ASC").Find(&rows)
byDelivery := make(map[string][]deliveryResultRow)
for i := range rows {
byDelivery[rows[i].DeliveryID] = append(
byDelivery[rows[i].DeliveryID], rows[i],
)
}
return byDelivery
}
// newDeliveryViews projects deliveries for rendering,
// resolving each one's target to its display-safe view.
// resolving each one's target to its display-safe view and
// each one's attempts through that target's redactor.
func newDeliveryViews(
deliveries []database.Delivery,
targetMap map[string]delivery.TargetView,
targetMap map[string]eventLogTarget,
attempts map[string][]deliveryResultRow,
) []DeliveryView {
views := make([]DeliveryView, len(deliveries))
for i := range deliveries {
target := targetMap[deliveries[i].TargetID]
rows := attempts[deliveries[i].ID]
results := make([]DeliveryResultView, len(rows))
for j := range rows {
results[j] = rows[j].view(target.Redactor)
}
views[i] = DeliveryView{
ID: deliveries[i].ID,
Status: deliveries[i].Status,
Target: targetMap[deliveries[i].TargetID],
ID: deliveries[i].ID,
Status: deliveries[i].Status,
Target: target.View,
Results: results,
}
}