Compare commits
10
Commits
6377483b63
..
next
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46fe7baed0 | ||
|
|
ea8cba7264 | ||
|
|
a8fc0c5d32 | ||
|
|
7963188c1e | ||
|
|
16ed356b68 | ||
|
|
fe5e0d4173 | ||
|
|
9ccaa8ce01 | ||
|
|
935e18c6f1 | ||
|
|
af91d8a723 | ||
|
|
f703b72ce0 |
@@ -0,0 +1,3 @@
|
|||||||
|
# Install into node_modules/: the Dockerfile's lint and Markdown stages run
|
||||||
|
# ESLint and prettier from node_modules/.bin.
|
||||||
|
nodeLinker: node-modules
|
||||||
+12
-7
@@ -67,15 +67,20 @@ RUN sed 's/}/}\n/g' static/css/tailwind.css > /tmp/committed.css \
|
|||||||
|
|
||||||
# JavaScript lint stages: ESLint, at the version package.json and yarn.lock
|
# JavaScript lint stages: ESLint, at the version package.json and yarn.lock
|
||||||
# pin, checks static/js/ against eslint.config.mjs. js-deps installs it, and
|
# pin, checks static/js/ against eslint.config.mjs. js-deps installs it, and
|
||||||
# prettier for the Markdown stages below, and stays cached until those two
|
# prettier for the Markdown stages below, and stays cached until package.json,
|
||||||
# files change. script/lint forces only js-lint to re-run, and the build stage
|
# yarn.lock or .yarnrc.yml changes. script/lint forces only js-lint to re-run,
|
||||||
# below runs it too. COPY . . brings in the CI cache barrier described in the
|
# and the build stage below runs it too. COPY . . brings in the CI cache
|
||||||
# lint stage above.
|
# barrier described in the lint stage above.
|
||||||
# node:24.21.0-alpine (LTS, with yarn 1.22.22), 2026-09-18
|
#
|
||||||
|
# The image's own corepack runs the yarn that package.json's packageManager
|
||||||
|
# field names, yarn 4.18.1 (released 2026-09-24), and checks it against the
|
||||||
|
# hash there. The image also ships yarn 1, which `corepack enable yarn`
|
||||||
|
# replaces.
|
||||||
|
# node:24.21.0-alpine (LTS), 2026-09-18
|
||||||
FROM node:24.21.0-alpine@sha256:ebfe2f90462722a7a4de65e91990e97fe0d401c70e0e762c5b53302f905ec1c1 AS js-deps
|
FROM node:24.21.0-alpine@sha256:ebfe2f90462722a7a4de65e91990e97fe0d401c70e0e762c5b53302f905ec1c1 AS js-deps
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
COPY package.json yarn.lock ./
|
COPY package.json yarn.lock .yarnrc.yml ./
|
||||||
RUN yarn install --frozen-lockfile --ignore-scripts
|
RUN corepack enable yarn && yarn install --immutable --mode=skip-build
|
||||||
|
|
||||||
FROM js-deps AS js-lint
|
FROM js-deps AS js-lint
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|||||||
@@ -1280,7 +1280,9 @@ the event log only the newest starts expanded, and an event there expands and
|
|||||||
collapses when its row's caret or its ID is clicked, and from the keyboard, but
|
collapses when its row's caret or its ID is clicked, and from the keyboard, but
|
||||||
not when its ID is selected with the mouse, and a delivery's attempts inside it
|
not when its ID is selected with the mouse, and a delivery's attempts inside it
|
||||||
expand and collapse; and at phone width the menu button opens and closes the
|
expand and collapse; and at phone width the menu button opens and closes the
|
||||||
mobile menu. It also fails if the browser reports a console warning or error, an
|
mobile menu, and neither the webhook page nor the event log, with a delivery's
|
||||||
|
attempts open, scrolls sideways or cuts anything off at the page's or a card's
|
||||||
|
edge. It also fails if the browser reports a console warning or error, an
|
||||||
uncaught exception, or anything the policy refused. `make check` and the image
|
uncaught exception, or anything the policy refused. `make check` and the image
|
||||||
build lint it but do not run it, and `make test` leaves it out (its file is
|
build lint it but do not run it, and `make test` leaves it out (its file is
|
||||||
built only with the `browser` build tag). Run it with `make test-browser` after
|
built only with the `browser` build tag). Run it with `make test-browser` after
|
||||||
@@ -1636,11 +1638,19 @@ URL, custom headers, timeout settings).
|
|||||||
|
|
||||||
**`http` target configuration:**
|
**`http` target configuration:**
|
||||||
|
|
||||||
| Key | Type | Description |
|
| Key | Type | Description |
|
||||||
| --------- | ------------- | -------------------------------------------------------------------------------------- |
|
| -------------- | ------------- | ----------------------------------------------------------------------------------------- |
|
||||||
| `url` | string | Destination the event is POSTed to |
|
| `url` | string | Destination the event is POSTed to |
|
||||||
| `headers` | object | Extra request headers, applied last so they win over the event's own forwarded headers |
|
| `headers` | object | Extra request headers, applied last so they win over the event's own forwarded headers |
|
||||||
| `timeout` | integer (sec) | Per-target request timeout; unset (or 0) uses the shared 30-second client timeout |
|
| `timeout` | integer (sec) | Per-target request timeout; unset (or 0) uses the shared 30-second client timeout |
|
||||||
|
| `forwardQuery` | boolean | Pass the query string each event arrived with on to the target; unset (or false) does not |
|
||||||
|
|
||||||
|
`forwardQuery` is off by default, and the target URL is then sent exactly as
|
||||||
|
configured. On, each delivery appends the event's query string to the target
|
||||||
|
URL, joined with `&` when the URL already has a query string of its own; a
|
||||||
|
replayed delivery and a resubmitted event's deliveries do the same. Both target
|
||||||
|
forms offer it as "Pass the query string on to this target", and the target list
|
||||||
|
shows it when it is on.
|
||||||
|
|
||||||
`timeout` is capped at **300 seconds**, and the form rejects anything above it
|
`timeout` is capped at **300 seconds**, and the form rejects anything above it
|
||||||
rather than substituting the cap. A delivery attempt holds one of the bounded
|
rather than substituting the cap. A delivery attempt holds one of the bounded
|
||||||
@@ -1702,6 +1712,7 @@ auditing, for replay, and for resubmission.
|
|||||||
| `webhook_id` | UUID | Foreign key → Webhook |
|
| `webhook_id` | UUID | Foreign key → Webhook |
|
||||||
| `entrypoint_id` | UUID | Foreign key → Entrypoint |
|
| `entrypoint_id` | UUID | Foreign key → Entrypoint |
|
||||||
| `method` | string | HTTP method of the captured request. Always `POST`: the receiver answers every other method with 405 before an Event is created |
|
| `method` | string | HTTP method of the captured request. Always `POST`: the receiver answers every other method with 405 before an Event is created |
|
||||||
|
| `raw_query` | text | The query string of the captured request, as sent, without the leading `?`; empty when there was none. A resubmitted copy carries its original's |
|
||||||
| `headers` | JSON | Complete request headers |
|
| `headers` | JSON | Complete request headers |
|
||||||
| `body` | text | Raw request body |
|
| `body` | text | Raw request body |
|
||||||
| `content_type` | string | Content-Type header value |
|
| `content_type` | string | Content-Type header value |
|
||||||
@@ -1710,9 +1721,15 @@ auditing, for replay, and for resubmission.
|
|||||||
|
|
||||||
**Relations:** Belongs to Webhook. Belongs to Entrypoint. Has many Deliveries.
|
**Relations:** Belongs to Webhook. Belongs to Entrypoint. Has many Deliveries.
|
||||||
|
|
||||||
When a request arrives at an entrypoint, the full request (method, headers,
|
When a request arrives at an entrypoint, the full request (method, query string,
|
||||||
body) is captured as an Event. The event is then queued for delivery to every
|
headers, body) is captured as an Event. The event is then queued for delivery to
|
||||||
active target configured on the parent webhook.
|
every active target configured on the parent webhook.
|
||||||
|
|
||||||
|
The event log and the event's own page show the query string with the rest of
|
||||||
|
the request. The event log leaves out one larger than 32 KiB, as it does request
|
||||||
|
headers, and links to the event's page, which shows it whole. The `database` and
|
||||||
|
`log` targets carry it with the rest of the event. An `http` target receives it
|
||||||
|
only when its `forwardQuery` setting is on.
|
||||||
|
|
||||||
#### Delivery
|
#### Delivery
|
||||||
|
|
||||||
@@ -1758,14 +1775,14 @@ the webhook's currently active targets.
|
|||||||
|
|
||||||
**Resubmit.** Replay recovers one delivery; **resubmit** re-injects one EVENT.
|
**Resubmit.** Replay recovers one delivery; **resubmit** re-injects one EVENT.
|
||||||
The event log offers a per-event **Resubmit** action that stores a NEW event
|
The event log offers a per-event **Resubmit** action that stores a NEW event
|
||||||
copying the stored one's `method`, `headers`, `body` and `content_type`
|
copying the stored one's `method`, `raw_query`, `headers`, `body` and
|
||||||
verbatim, then fans it out to the webhook's currently **active** targets —
|
`content_type` verbatim, then fans it out to the webhook's currently **active**
|
||||||
resolved fresh by the same query the receiver uses, so a target created long
|
targets — resolved fresh by the same query the receiver uses, so a target
|
||||||
after the original event arrived receives it. That is the difference that
|
created long after the original event arrived receives it. That is the
|
||||||
matters: a target added to test a backend under development has no prior
|
difference that matters: a target added to test a backend under development has
|
||||||
delivery, so there is nothing to replay to it, while a resubmit reaches it like
|
no prior delivery, so there is nothing to replay to it, while a resubmit reaches
|
||||||
any other active target. Inactive targets are skipped, exactly as the receiver
|
it like any other active target. Inactive targets are skipped, exactly as the
|
||||||
skips them.
|
receiver skips them.
|
||||||
|
|
||||||
The new event is a first-class event in the log with its own deliveries, not a
|
The new event is a first-class event in the log with its own deliveries, not a
|
||||||
marker on the one it came from, and the original's deliveries are left
|
marker on the one it came from, and the original's deliveries are left
|
||||||
@@ -2436,7 +2453,8 @@ in front of them, so a query on a fixed 200 URL would otherwise buy the same
|
|||||||
amplification as an invented path. Nothing debuggable is lost: the only query
|
amplification as an invented path. Nothing debuggable is lost: the only query
|
||||||
parameters this service reads are the sign-in page's `next`, the page to return
|
parameters this service reads are the sign-in page's `next`, the page to return
|
||||||
to, `notice`, which names the line a page shows after an action, and the event
|
to, `notice`, which names the line a page shows after an action, and the event
|
||||||
log's `show`, which picks the events it lists.
|
log's `show`, which picks the events it lists. A query string sent to an
|
||||||
|
entrypoint is not lost either: the event stores it, and the event log shows it.
|
||||||
|
|
||||||
Client-supplied request content does not leave the host by the other route
|
Client-supplied request content does not leave the host by the other route
|
||||||
either. The Sentry SDK attaches the request to every event it captures,
|
either. The Sentry SDK attaches the request to every event it captures,
|
||||||
@@ -2655,9 +2673,10 @@ What that ceiling does **not** cover, stated here so the figure is not read as
|
|||||||
more than it is:
|
more than it is:
|
||||||
|
|
||||||
- **Lines carrying an authenticated operator's own input**, which are not
|
- **Lines carrying an authenticated operator's own input**, which are not
|
||||||
truncated at all. `webhook created` logs the submitted `name` verbatim and
|
truncated at all. `webhook created` logs the submitted `name` verbatim
|
||||||
`target URL blocked by SSRF protection` logs the target host (both
|
(`internal/handlers/webhook_create.go`) and
|
||||||
`internal/handlers/source_management.go`), as do the `target_name` lines in
|
`target URL blocked by SSRF protection` logs the target host
|
||||||
|
(`internal/handlers/target_create.go`), as do the `target_name` lines in
|
||||||
`internal/delivery/engine.go` and `internal/delivery/target_http.go`. The only
|
`internal/delivery/engine.go` and `internal/delivery/target_http.go`. The only
|
||||||
bound on any of them is the 1 MB form body cap, so a 100 KB `name` writes a
|
bound on any of them is the 1 MB form body cap, so a 100 KB `name` writes a
|
||||||
single line of roughly 600 KB — measured. This is deliberate: every one of
|
single line of roughly 600 KB — measured. This is deliberate: every one of
|
||||||
@@ -2898,7 +2917,7 @@ page that was asked for.
|
|||||||
| `POST` | `/hook/{id}/edit` | Edit webhook submission |
|
| `POST` | `/hook/{id}/edit` | Edit webhook submission |
|
||||||
| `POST` | `/hook/{id}/delete` | Delete webhook |
|
| `POST` | `/hook/{id}/delete` | Delete webhook |
|
||||||
| `GET` | `/hook/{id}/events` | Full Event Log. `?show=failed` lists only the events with a failed delivery, and `?show=pending` only those with a delivery pending or retrying |
|
| `GET` | `/hook/{id}/events` | Full Event Log. `?show=failed` lists only the events with a failed delivery, and `?show=pending` only those with a delivery pending or retrying |
|
||||||
| `GET` | `/hook/{id}/events/{eventID}` | One event's own page: its details, the entrypoint it arrived at (for a resubmitted copy, the one the request it copies arrived at), its request headers, its whole body and every delivery of it |
|
| `GET` | `/hook/{id}/events/{eventID}` | One event's own page: its details, the entrypoint it arrived at (for a resubmitted copy, the one the request it copies arrived at), its query string, its request headers, its whole body and every delivery of it |
|
||||||
| `GET` | `/hook/{id}/events/{eventID}/body` | Download an event's stored body. The pages show a body as text, cut at 32 KiB in the recent events and the event log, and leave a binary one out, so this is the only route that serves the stored bytes; it is offered wherever a body is cut or binary |
|
| `GET` | `/hook/{id}/events/{eventID}/body` | Download an event's stored body. The pages show a body as text, cut at 32 KiB in the recent events and the event log, and leave a binary one out, so this is the only route that serves the stored bytes; it is offered wherever a body is cut or binary |
|
||||||
| `POST` | `/hook/{id}/deliveries/{deliveryID}/replay` | Replay a finished delivery: creates a new delivery for the same event against the target's current configuration (30 per minute per bucket, then `429`) |
|
| `POST` | `/hook/{id}/deliveries/{deliveryID}/replay` | Replay a finished delivery: creates a new delivery for the same event against the target's current configuration (30 per minute per bucket, then `429`) |
|
||||||
| `POST` | `/hook/{id}/events/{eventID}/resubmit` | Resubmit a stored event: creates a new event copying it and fans that out to every currently active target (30 per minute per bucket, then `429`) |
|
| `POST` | `/hook/{id}/events/{eventID}/resubmit` | Resubmit a stored event: creates a new event copying it and fans that out to every currently active target (30 per minute per bucket, then `429`) |
|
||||||
@@ -3010,7 +3029,17 @@ webhooker/
|
|||||||
│ │ ├── index.go # Index page handler
|
│ │ ├── index.go # Index page handler
|
||||||
│ │ ├── profile.go # User profile handler
|
│ │ ├── profile.go # User profile handler
|
||||||
│ │ ├── settings.go # Read-only Settings page handler
|
│ │ ├── settings.go # Read-only Settings page handler
|
||||||
│ │ ├── source_management.go # Webhook CRUD handlers
|
│ │ ├── webhook_list.go # Webhook list page
|
||||||
|
│ │ ├── webhook_create.go # Webhook create
|
||||||
|
│ │ ├── webhook_detail.go # Webhook detail page
|
||||||
|
│ │ ├── webhook_edit.go # Webhook edit, archive renaming
|
||||||
|
│ │ ├── webhook_delete.go # Webhook delete, event database and archive writer removal
|
||||||
|
│ │ ├── event_log.go # Event log page: loaders, filters, delivery views
|
||||||
|
│ │ ├── entrypoint.go # Entrypoint create, edit, delete and toggle
|
||||||
|
│ │ ├── target_create.go # Target create, per-type config builders
|
||||||
|
│ │ ├── target_delete.go # Target delete
|
||||||
|
│ │ ├── target_toggle.go # Target toggle
|
||||||
|
│ │ ├── shared.go # Helpers shared by several handlers
|
||||||
│ │ └── webhook.go # Webhook receiver handler
|
│ │ └── webhook.go # Webhook receiver handler
|
||||||
│ ├── healthcheck/
|
│ ├── healthcheck/
|
||||||
│ │ └── healthcheck.go # Health check service (uptime, version)
|
│ │ └── healthcheck.go # Health check service (uptime, version)
|
||||||
@@ -3051,7 +3080,8 @@ webhooker/
|
|||||||
├── Dockerfile.browser # Browser test image built by script/test-browser
|
├── Dockerfile.browser # Browser test image built by script/test-browser
|
||||||
├── Makefile # 13 of 19 targets shim script/; 6 are inline
|
├── Makefile # 13 of 19 targets shim script/; 6 are inline
|
||||||
├── go.mod / go.sum
|
├── go.mod / go.sum
|
||||||
├── package.json / yarn.lock # ESLint and prettier, pinned, for the JavaScript lint and Markdown stages
|
├── package.json / yarn.lock # ESLint, prettier and yarn, pinned, for the JavaScript lint and Markdown stages
|
||||||
|
├── .yarnrc.yml # yarn settings: install into node_modules/
|
||||||
├── eslint.config.mjs # ESLint configuration for static/js/
|
├── eslint.config.mjs # ESLint configuration for static/js/
|
||||||
├── .prettierrc # prettier settings for the Markdown
|
├── .prettierrc # prettier settings for the Markdown
|
||||||
└── .golangci.yml # golangci-lint configuration
|
└── .golangci.yml # golangci-lint configuration
|
||||||
@@ -3339,13 +3369,17 @@ ESLint never runs on the host either. It lints `static/js/` (not the extracted
|
|||||||
Alpine.js) in the Dockerfile's `js-lint` stage, which `script/lint` builds after
|
Alpine.js) in the Dockerfile's `js-lint` stage, which `script/lint` builds after
|
||||||
`Dockerfile.lint` and the image build runs before the builder stage. Its version
|
`Dockerfile.lint` and the image build runs before the builder stage. Its version
|
||||||
is pinned in `package.json` and every package's hash in `yarn.lock`. The
|
is pinned in `package.json` and every package's hash in `yarn.lock`. The
|
||||||
`js-deps` stage before it installs ESLint and stays cached until either file
|
`js-deps` stage before it installs ESLint with `yarn install --immutable`, which
|
||||||
changes, so only the lint step re-runs and ESLint is not downloaded again.
|
fails rather than change `yarn.lock`. The yarn it runs is the one the
|
||||||
`eslint.config.mjs` turns on the rules of the JavaScript styleguide
|
`packageManager` field in `package.json` pins by version and hash, which the
|
||||||
`REPO_POLICIES.md` links to that a linter can check: `no-var` and
|
node image's own corepack fetches and checks. The stage stays cached until
|
||||||
`prefer-const`. ESLint prints nothing on a pass, so `script/lint` has no summary
|
`package.json`, `yarn.lock` or `.yarnrc.yml` changes, so only the lint step
|
||||||
line to look for; it names the stage once for both `--target` and
|
re-runs and ESLint is not downloaded again. `eslint.config.mjs` turns on the
|
||||||
`--no-cache-filter`, and `--target` fails on a name that matches no stage.
|
rules of the JavaScript styleguide `REPO_POLICIES.md` links to that a linter can
|
||||||
|
check: `no-var` and `prefer-const`. ESLint prints nothing on a pass, so
|
||||||
|
`script/lint` has no summary line to look for; it names the stage once for both
|
||||||
|
`--target` and `--no-cache-filter`, and `--target` fails on a name that matches
|
||||||
|
no stage.
|
||||||
|
|
||||||
prettier formats the Markdown, and it never runs on the host either. It is
|
prettier formats the Markdown, and it never runs on the host either. It is
|
||||||
pinned in `package.json` and `yarn.lock` beside ESLint, installed by the same
|
pinned in `package.json` and `yarn.lock` beside ESLint, installed by the same
|
||||||
@@ -3371,8 +3405,9 @@ independently of the compiler's:
|
|||||||
`@source` lines name. `css-check` fails when the committed file differs from
|
`@source` lines name. `css-check` fails when the committed file differs from
|
||||||
the generated one, and `make css` writes the generated file out from
|
the generated one, and `make css` writes the generated file out from
|
||||||
`css-output` (see [Stylesheet](#stylesheet)).
|
`css-output` (see [Stylesheet](#stylesheet)).
|
||||||
3. **JavaScript lint stages** (`node:24.21.0-alpine`, with yarn) — `js-deps`
|
3. **JavaScript lint stages** (`node:24.21.0-alpine`, with the yarn
|
||||||
installs ESLint and prettier from `yarn.lock` and `js-lint` runs ESLint over
|
`package.json` pins, run through the image's corepack) — `js-deps` installs
|
||||||
|
ESLint and prettier from `yarn.lock` and `js-lint` runs ESLint over
|
||||||
`static/js/` (see [Linting](#linting)).
|
`static/js/` (see [Linting](#linting)).
|
||||||
4. **Markdown stages** (on `js-deps`) — `markdown-check` runs prettier over the
|
4. **Markdown stages** (on `js-deps`) — `markdown-check` runs prettier over the
|
||||||
Markdown and fails on any file it would change, and `make fmt` writes the
|
Markdown and fails on any file it would change, and `make fmt` writes the
|
||||||
|
|||||||
@@ -30,8 +30,10 @@ type Event struct {
|
|||||||
WebhookID string `gorm:"type:uuid;not null" json:"webhookId"`
|
WebhookID string `gorm:"type:uuid;not null" json:"webhookId"`
|
||||||
EntrypointID string `gorm:"type:uuid;not null;index:idx_events_entrypoint_id,priority:1" json:"entrypointId"`
|
EntrypointID string `gorm:"type:uuid;not null;index:idx_events_entrypoint_id,priority:1" json:"entrypointId"`
|
||||||
|
|
||||||
// Request data
|
// Request data. RawQuery is the receiving request's query string
|
||||||
|
// as sent, without the leading "?".
|
||||||
Method string `gorm:"not null" json:"method"`
|
Method string `gorm:"not null" json:"method"`
|
||||||
|
RawQuery string `gorm:"type:text" json:"rawQuery"`
|
||||||
Headers string `gorm:"type:text" json:"headers"` // JSON
|
Headers string `gorm:"type:text" json:"headers"` // JSON
|
||||||
Body string `gorm:"type:text" json:"body"`
|
Body string `gorm:"type:text" json:"body"`
|
||||||
ContentType string `json:"contentType"`
|
ContentType string `json:"contentType"`
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ type Task struct {
|
|||||||
MaxRetries int
|
MaxRetries int
|
||||||
|
|
||||||
Method string
|
Method string
|
||||||
|
RawQuery string
|
||||||
Headers string
|
Headers string
|
||||||
ContentType string
|
ContentType string
|
||||||
Body *string
|
Body *string
|
||||||
@@ -1752,6 +1753,7 @@ func buildEventFromTask(task *Task) database.Event {
|
|||||||
event := database.Event{
|
event := database.Event{
|
||||||
EntrypointID: task.EntrypointID,
|
EntrypointID: task.EntrypointID,
|
||||||
Method: task.Method,
|
Method: task.Method,
|
||||||
|
RawQuery: task.RawQuery,
|
||||||
Headers: task.Headers,
|
Headers: task.Headers,
|
||||||
ContentType: task.ContentType,
|
ContentType: task.ContentType,
|
||||||
}
|
}
|
||||||
@@ -2102,6 +2104,7 @@ func buildRecoveryTask(
|
|||||||
TargetConfig: target.Config,
|
TargetConfig: target.Config,
|
||||||
MaxRetries: target.MaxRetries,
|
MaxRetries: target.MaxRetries,
|
||||||
Method: event.Method,
|
Method: event.Method,
|
||||||
|
RawQuery: event.RawQuery,
|
||||||
Headers: event.Headers,
|
Headers: event.Headers,
|
||||||
ContentType: event.ContentType,
|
ContentType: event.ContentType,
|
||||||
Body: bodyPtr,
|
Body: bodyPtr,
|
||||||
|
|||||||
@@ -673,6 +673,11 @@ func TestRecoverPendingDeliveries(t *testing.T) {
|
|||||||
t, s.WebhookDB, s.WebhookID, targetID, 3,
|
t, s.WebhookDB, s.WebhookID, targetID, 3,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A recovered delivery still carries its event's query string.
|
||||||
|
require.NoError(t, s.WebhookDB.Model(&database.Event{}).
|
||||||
|
Where("webhook_id = ?", s.WebhookID).
|
||||||
|
Update("raw_query", eventQuery).Error)
|
||||||
|
|
||||||
s.Engine.ExportRecoverPendingDeliveries(
|
s.Engine.ExportRecoverPendingDeliveries(
|
||||||
context.Background(), s.WebhookDB,
|
context.Background(), s.WebhookDB,
|
||||||
s.WebhookID,
|
s.WebhookID,
|
||||||
@@ -687,6 +692,8 @@ func TestRecoverPendingDeliveries(t *testing.T) {
|
|||||||
database.TargetTypeLog,
|
database.TargetTypeLog,
|
||||||
task.TargetType,
|
task.TargetType,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert.Equal(t, eventQuery, task.RawQuery)
|
||||||
case <-time.After(2 * time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
t.Fatalf("expected task %d", i)
|
t.Fatalf("expected task %d", i)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -1990,6 +1991,10 @@ func assertLogLineComplete(
|
|||||||
"log line must contain the full request headers",
|
"log line must contain the full request headers",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert.Contains(t, out, "raw_query="+strconv.Quote(event.RawQuery),
|
||||||
|
"log line must contain the query string",
|
||||||
|
)
|
||||||
|
|
||||||
assert.Contains(t, out, event.EntrypointID,
|
assert.Contains(t, out, event.EntrypointID,
|
||||||
"log line must contain the entrypoint id",
|
"log line must contain the entrypoint id",
|
||||||
)
|
)
|
||||||
@@ -2012,6 +2017,7 @@ func TestDeliverLog_LogsFullContent(t *testing.T) {
|
|||||||
event := seedEvent(
|
event := seedEvent(
|
||||||
t, db, `{"log-body-marker":"abc123"}`,
|
t, db, `{"log-body-marker":"abc123"}`,
|
||||||
)
|
)
|
||||||
|
event.RawQuery = eventQuery
|
||||||
|
|
||||||
dlv := seedDelivery(
|
dlv := seedDelivery(
|
||||||
t, db, event.ID, uuid.New().String(),
|
t, db, event.ID, uuid.New().String(),
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ type TargetConfigForm struct {
|
|||||||
// Timeout is the HTTP target's per-request timeout in seconds,
|
// Timeout is the HTTP target's per-request timeout in seconds,
|
||||||
// empty when unset.
|
// empty when unset.
|
||||||
Timeout string
|
Timeout string
|
||||||
|
// ForwardQuery is the HTTP target's setting that passes each
|
||||||
|
// event's query string on to it.
|
||||||
|
ForwardQuery bool
|
||||||
// Expiry is the database (archive) target's row expiry.
|
// Expiry is the database (archive) target's row expiry.
|
||||||
Expiry string
|
Expiry string
|
||||||
// Rotation is the database (archive) target's rotation.
|
// Rotation is the database (archive) target's rotation.
|
||||||
@@ -64,9 +67,10 @@ func NewTargetConfigForm(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return TargetConfigForm{
|
return TargetConfigForm{
|
||||||
URL: cfg.URL,
|
URL: cfg.URL,
|
||||||
Headers: FormatTargetHeaders(cfg.Headers),
|
Headers: FormatTargetHeaders(cfg.Headers),
|
||||||
Timeout: FormatTargetTimeout(cfg.Timeout),
|
Timeout: FormatTargetTimeout(cfg.Timeout),
|
||||||
|
ForwardQuery: cfg.ForwardQuery,
|
||||||
}, nil
|
}, nil
|
||||||
case database.TargetTypeSlack:
|
case database.TargetTypeSlack:
|
||||||
cfg, err := parseSlackConfig(t.Config)
|
cfg, err := parseSlackConfig(t.Config)
|
||||||
|
|||||||
@@ -171,6 +171,13 @@ func httpConfigFields(t *database.Target) []ConfigField {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.ForwardQuery {
|
||||||
|
fields = append(fields, ConfigField{
|
||||||
|
Label: "Query string",
|
||||||
|
Value: "passed on to this target",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fields = append(fields, maxRetriesField(t))
|
fields = append(fields, maxRetriesField(t))
|
||||||
|
|
||||||
return fields
|
return fields
|
||||||
|
|||||||
@@ -223,7 +223,8 @@ func TestNewTargetViews_HTTP(t *testing.T) {
|
|||||||
Type: database.TargetTypeHTTP,
|
Type: database.TargetTypeHTTP,
|
||||||
Config: `{"url":"` + viewExampleHook + `",` +
|
Config: `{"url":"` + viewExampleHook + `",` +
|
||||||
`"timeout":30,` +
|
`"timeout":30,` +
|
||||||
`"headers":{"Authorization":"Bearer sekrit"}}`,
|
`"headers":{"Authorization":"Bearer sekrit"},` +
|
||||||
|
`"forwardQuery":true}`,
|
||||||
MaxRetries: 5,
|
MaxRetries: 5,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -235,6 +236,7 @@ func TestNewTargetViews_HTTP(t *testing.T) {
|
|||||||
"Destination URL": viewMaskedOrigin,
|
"Destination URL": viewMaskedOrigin,
|
||||||
"Timeout": "30s",
|
"Timeout": "30s",
|
||||||
"Headers": "1 configured",
|
"Headers": "1 configured",
|
||||||
|
"Query string": "passed on to this target",
|
||||||
viewMaxRetries: "5",
|
viewMaxRetries: "5",
|
||||||
},
|
},
|
||||||
fields,
|
fields,
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ func (t *databaseTarget) archive(d *database.Delivery) error {
|
|||||||
WebhookID: webhookID,
|
WebhookID: webhookID,
|
||||||
EntrypointID: d.Event.EntrypointID,
|
EntrypointID: d.Event.EntrypointID,
|
||||||
Method: d.Event.Method,
|
Method: d.Event.Method,
|
||||||
|
RawQuery: d.Event.RawQuery,
|
||||||
Headers: d.Event.Headers,
|
Headers: d.Event.Headers,
|
||||||
Body: d.Event.Body,
|
Body: d.Event.Body,
|
||||||
ContentType: d.Event.ContentType,
|
ContentType: d.Event.ContentType,
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ type archivedEvent struct {
|
|||||||
WebhookID string
|
WebhookID string
|
||||||
EntrypointID string
|
EntrypointID string
|
||||||
Method string
|
Method string
|
||||||
|
RawQuery string
|
||||||
Headers string
|
Headers string
|
||||||
Body string
|
Body string
|
||||||
ContentType string
|
ContentType string
|
||||||
|
|||||||
@@ -360,6 +360,7 @@ func writeRow(w io.Writer, ev *archivedEvent, period string) error {
|
|||||||
"webhook_id": ev.WebhookID,
|
"webhook_id": ev.WebhookID,
|
||||||
"entrypoint_id": ev.EntrypointID,
|
"entrypoint_id": ev.EntrypointID,
|
||||||
"method": ev.Method,
|
"method": ev.Method,
|
||||||
|
"raw_query": ev.RawQuery,
|
||||||
"headers": ev.Headers,
|
"headers": ev.Headers,
|
||||||
"body": ev.Body,
|
"body": ev.Body,
|
||||||
"content_type": ev.ContentType,
|
"content_type": ev.ContentType,
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ func TestArchiveExport_MatchesStoredRows(t *testing.T) {
|
|||||||
WebhookID: exportWebhookID,
|
WebhookID: exportWebhookID,
|
||||||
EntrypointID: "ep-1",
|
EntrypointID: "ep-1",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
|
RawQuery: eventQuery,
|
||||||
Headers: `{"X-Test":["yes"]}`,
|
Headers: `{"X-Test":["yes"]}`,
|
||||||
Body: body,
|
Body: body,
|
||||||
ContentType: testContentType,
|
ContentType: testContentType,
|
||||||
@@ -215,12 +216,13 @@ func assertExportedRow(
|
|||||||
assert.Equal(t, row.WebhookID, ev["webhook_id"])
|
assert.Equal(t, row.WebhookID, ev["webhook_id"])
|
||||||
assert.Equal(t, row.EntrypointID, ev["entrypoint_id"])
|
assert.Equal(t, row.EntrypointID, ev["entrypoint_id"])
|
||||||
assert.Equal(t, row.Method, ev["method"])
|
assert.Equal(t, row.Method, ev["method"])
|
||||||
|
assert.Equal(t, row.RawQuery, ev["raw_query"])
|
||||||
assert.Equal(t, row.Headers, ev["headers"])
|
assert.Equal(t, row.Headers, ev["headers"])
|
||||||
assert.Equal(t, row.ContentType, ev["content_type"])
|
assert.Equal(t, row.ContentType, ev["content_type"])
|
||||||
|
|
||||||
if row.Body != binaryBody {
|
if row.Body != binaryBody {
|
||||||
assert.Equal(t, row.Body, ev["body"])
|
assert.Equal(t, row.Body, ev["body"])
|
||||||
assert.Len(t, ev, 9, "the nine columns and nothing else: %v", ev)
|
assert.Len(t, ev, 10, "the ten columns and nothing else: %v", ev)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -229,7 +231,7 @@ func assertExportedRow(
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, binaryBody, string(body))
|
assert.Equal(t, binaryBody, string(body))
|
||||||
assert.Equal(t, "base64", ev["body_encoding"])
|
assert.Equal(t, "base64", ev["body_encoding"])
|
||||||
assert.Len(t, ev, 10, "the nine columns and body_encoding: %v", ev)
|
assert.Len(t, ev, 11, "the ten columns and body_encoding: %v", ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestArchiveExport_Empty proves an archive with nothing in it exports
|
// TestArchiveExport_Empty proves an archive with nothing in it exports
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ func TestDeliverDatabase_ArchivesEvent(t *testing.T) {
|
|||||||
|
|
||||||
webhookDB := testWebhookDB(t)
|
webhookDB := testWebhookDB(t)
|
||||||
event := seedEvent(t, webhookDB, `{"archived":true}`)
|
event := seedEvent(t, webhookDB, `{"archived":true}`)
|
||||||
|
event.RawQuery = eventQuery
|
||||||
d := seedDatabaseTargetDelivery(t, webhookDB, event, tgt)
|
d := seedDatabaseTargetDelivery(t, webhookDB, event, tgt)
|
||||||
|
|
||||||
env.eng.ExportDeliverDatabase(webhookDB, d)
|
env.eng.ExportDeliverDatabase(webhookDB, d)
|
||||||
@@ -113,6 +114,7 @@ func TestDeliverDatabase_ArchivesEvent(t *testing.T) {
|
|||||||
assert.Equal(t, event.ID, rows[0].EventID)
|
assert.Equal(t, event.ID, rows[0].EventID)
|
||||||
assert.Equal(t, event.WebhookID, rows[0].WebhookID)
|
assert.Equal(t, event.WebhookID, rows[0].WebhookID)
|
||||||
assert.Equal(t, event.Method, rows[0].Method)
|
assert.Equal(t, event.Method, rows[0].Method)
|
||||||
|
assert.Equal(t, eventQuery, rows[0].RawQuery)
|
||||||
assert.JSONEq(t, `{"archived":true}`, rows[0].Body)
|
assert.JSONEq(t, `{"archived":true}`, rows[0].Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -32,6 +33,11 @@ type HTTPTargetConfig struct {
|
|||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Headers map[string]string `json:"headers,omitempty"`
|
Headers map[string]string `json:"headers,omitempty"`
|
||||||
Timeout int `json:"timeout,omitempty"`
|
Timeout int `json:"timeout,omitempty"`
|
||||||
|
|
||||||
|
// ForwardQuery passes each event's query string on to the target,
|
||||||
|
// appended to URL. Off, the target URL is sent exactly as
|
||||||
|
// configured.
|
||||||
|
ForwardQuery bool `json:"forwardQuery,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// httpCore holds the retry, backoff, and circuit-breaker
|
// httpCore holds the retry, backoff, and circuit-breaker
|
||||||
@@ -444,6 +450,10 @@ func (t *httpTarget) doHTTPRequest(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.ForwardQuery {
|
||||||
|
appendQuery(req.URL, event.RawQuery)
|
||||||
|
}
|
||||||
|
|
||||||
originScoped := applyRequestHeaders(
|
originScoped := applyRequestHeaders(
|
||||||
req, event, cfg, t.eng.userAgent(),
|
req, event, cfg, t.eng.userAgent(),
|
||||||
)
|
)
|
||||||
@@ -474,6 +484,19 @@ func (t *httpTarget) doHTTPRequest(
|
|||||||
return resp.StatusCode, string(body), dur, nil
|
return resp.StatusCode, string(body), dur, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appendQuery adds an event's query string to a delivery's URL, joined
|
||||||
|
// with "&" to any query string the target URL already has.
|
||||||
|
func appendQuery(u *url.URL, rawQuery string) {
|
||||||
|
switch {
|
||||||
|
case rawQuery == "":
|
||||||
|
return
|
||||||
|
case u.RawQuery == "":
|
||||||
|
u.RawQuery = rawQuery
|
||||||
|
default:
|
||||||
|
u.RawQuery += "&" + rawQuery
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// clientForRequest returns the client for one delivery attempt.
|
// clientForRequest returns the client for one delivery attempt.
|
||||||
// originScoped is the header set applyRequestHeaders built for that
|
// originScoped is the header set applyRequestHeaders built for that
|
||||||
// attempt; a request with neither a per-target timeout nor an
|
// attempt; a request with neither a per-target timeout nor an
|
||||||
|
|||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package delivery_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
"sneak.berlin/go/webhooker/internal/delivery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// eventQuery is the query string the events in these tests arrived
|
||||||
|
// with.
|
||||||
|
const eventQuery = "a=1&b=2"
|
||||||
|
|
||||||
|
// httpTargetConfig is the stored configuration of an HTTP target at
|
||||||
|
// targetURL.
|
||||||
|
func httpTargetConfig(
|
||||||
|
t *testing.T, targetURL string, forwardQuery bool,
|
||||||
|
) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
cfg, err := json.Marshal(delivery.HTTPTargetConfig{
|
||||||
|
URL: targetURL, ForwardQuery: forwardQuery,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return string(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverWithQuery sends one event that arrived with eventQuery to an
|
||||||
|
// HTTP target configured with cfg, through the path a received event's
|
||||||
|
// delivery takes, and returns the attempt it recorded.
|
||||||
|
func deliverWithQuery(t *testing.T, cfg string) database.DeliveryResult {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
s := newISetup(t)
|
||||||
|
|
||||||
|
event := iSeedEvent(t, s.WebhookDB, s.WebhookID, "{}")
|
||||||
|
d := iSeedDelivery(
|
||||||
|
t, s.WebhookDB, event.ID, uuid.NewString(),
|
||||||
|
database.DeliveryStatusPending,
|
||||||
|
)
|
||||||
|
task := iTask(
|
||||||
|
d, event, s.WebhookID, d.TargetID, "query", cfg, 0, 1, &event.Body,
|
||||||
|
)
|
||||||
|
task.RawQuery = eventQuery
|
||||||
|
|
||||||
|
s.Engine.ExportProcessNewTask(context.TODO(), &task)
|
||||||
|
|
||||||
|
var result database.DeliveryResult
|
||||||
|
|
||||||
|
require.NoError(t, s.WebhookDB.Where(
|
||||||
|
"delivery_id = ?", d.ID,
|
||||||
|
).First(&result).Error)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDeliverHTTP_ForwardQuery proves the URL a delivery is sent to:
|
||||||
|
// with the target's setting off, the target URL exactly as configured;
|
||||||
|
// with it on, the event's query string appended, joined with "&" to a
|
||||||
|
// query string the target URL already has.
|
||||||
|
func TestDeliverHTTP_ForwardQuery(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// The target URL's path, without and with a query string of its
|
||||||
|
// own.
|
||||||
|
const (
|
||||||
|
plain = "/in"
|
||||||
|
withQuery = "/in?key=k"
|
||||||
|
)
|
||||||
|
|
||||||
|
tests := map[string]struct {
|
||||||
|
path string
|
||||||
|
forwardQuery bool
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
"off": {
|
||||||
|
path: plain, want: plain,
|
||||||
|
},
|
||||||
|
"off, the target URL has a query string": {
|
||||||
|
path: withQuery, want: withQuery,
|
||||||
|
},
|
||||||
|
"on": {
|
||||||
|
path: plain, forwardQuery: true, want: plain + "?" + eventQuery,
|
||||||
|
},
|
||||||
|
"on, the target URL has a query string": {
|
||||||
|
path: withQuery, forwardQuery: true,
|
||||||
|
want: withQuery + "&" + eventQuery,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range tests {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
received := make(chan string, 1)
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
received <- r.RequestURI
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
t.Cleanup(ts.Close)
|
||||||
|
|
||||||
|
result := deliverWithQuery(t, httpTargetConfig(
|
||||||
|
t, ts.URL+tc.path, tc.forwardQuery,
|
||||||
|
))
|
||||||
|
|
||||||
|
assert.True(t, result.Success)
|
||||||
|
require.Len(t, received, 1)
|
||||||
|
assert.Equal(t, tc.want, <-received)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDeliverHTTP_ForwardedQueryKeepsTheTargetURLMasked proves the
|
||||||
|
// credential in a target URL's own query string stays masked once the
|
||||||
|
// event's query string is appended to it: in a response or error that
|
||||||
|
// echoes the URL the target was sent, as the event log's Redactor shows
|
||||||
|
// it, and in the error a failed connection stores.
|
||||||
|
func TestDeliverHTTP_ForwardedQueryKeepsTheTargetURLMasked(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const secret = "s3cr3t"
|
||||||
|
|
||||||
|
received := make(chan string, 1)
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
received <- r.RequestURI
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
},
|
||||||
|
))
|
||||||
|
t.Cleanup(ts.Close)
|
||||||
|
|
||||||
|
target := &database.Target{
|
||||||
|
Type: database.TargetTypeHTTP,
|
||||||
|
Config: httpTargetConfig(t, ts.URL+"/in?token="+secret, true),
|
||||||
|
}
|
||||||
|
|
||||||
|
deliverWithQuery(t, target.Config)
|
||||||
|
require.Len(t, received, 1)
|
||||||
|
|
||||||
|
sent := <-received
|
||||||
|
require.Equal(t, "/in?token="+secret+"&"+eventQuery, sent)
|
||||||
|
|
||||||
|
redactor := delivery.NewRedactor(target)
|
||||||
|
|
||||||
|
for _, echoed := range []string{sent, ts.URL + sent} {
|
||||||
|
shown := redactor.Redact("rejected " + echoed)
|
||||||
|
assert.NotContains(t, shown, secret, echoed)
|
||||||
|
assert.Contains(t, shown, delivery.RedactionMarker, echoed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing listens on port 1.
|
||||||
|
failed := deliverWithQuery(t, httpTargetConfig(
|
||||||
|
t, "http://127.0.0.1:1/in?token="+secret, true,
|
||||||
|
))
|
||||||
|
require.NotEmpty(t, failed.Error)
|
||||||
|
assert.NotContains(t, failed.Error, secret)
|
||||||
|
assert.NotContains(t, failed.Error, eventQuery)
|
||||||
|
}
|
||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// logTarget is a fire-and-forget target that logs the entire
|
// logTarget is a fire-and-forget target that logs the entire
|
||||||
// inbound webhook — the full request body and headers, plus
|
// inbound webhook — the full request body, query string and
|
||||||
// the method, content type, and the webhook and entrypoint
|
// headers, plus the method, content type, and the webhook and
|
||||||
// ids — then records a single successful attempt.
|
// entrypoint ids — then records a single successful attempt.
|
||||||
//
|
//
|
||||||
// This is the one log call in the service that deliberately writes
|
// This is the one log call in the service that deliberately writes
|
||||||
// unbounded client-chosen bytes, so it is the one exception to the
|
// unbounded client-chosen bytes, so it is the one exception to the
|
||||||
@@ -46,6 +46,7 @@ func (t *logTarget) Deliver(
|
|||||||
"webhook_id", d.Event.WebhookID,
|
"webhook_id", d.Event.WebhookID,
|
||||||
"entrypoint_id", d.Event.EntrypointID,
|
"entrypoint_id", d.Event.EntrypointID,
|
||||||
"method", d.Event.Method,
|
"method", d.Event.Method,
|
||||||
|
"raw_query", d.Event.RawQuery,
|
||||||
"content_type", d.Event.ContentType,
|
"content_type", d.Event.ContentType,
|
||||||
"headers", d.Event.Headers,
|
"headers", d.Event.Headers,
|
||||||
"body", d.Event.Body,
|
"body", d.Event.Body,
|
||||||
|
|||||||
@@ -162,18 +162,22 @@ func targetSecrets(t *database.Target) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// urlSecrets returns the substrings of a destination URL that
|
// urlSecrets returns the substrings of a destination URL that
|
||||||
// must not survive into a rendered page: the whole URL, the
|
// must not survive into a rendered page: the whole URL; its
|
||||||
// parts of it MaskURL elides, and any userinfo.
|
// path, unless that is empty or "/"; its query string, and the
|
||||||
|
// request URI that carries it, which a remote echoing the
|
||||||
|
// request line shows even when the URL has no path; and its
|
||||||
|
// userinfo and password.
|
||||||
//
|
//
|
||||||
// No length floor is applied to the path, and none to the
|
// No length floor is applied to the path, the query string or
|
||||||
// userinfo. A short path or a four-byte username is treated as
|
// the userinfo. A short path or a four-byte username is
|
||||||
// a credential exactly like a long one, because the field takes
|
// treated as a credential exactly like a long one, because the
|
||||||
// an arbitrary URL and no part of it can be assumed non-secret —
|
// field takes an arbitrary URL and no part of it can be
|
||||||
// the same rule MaskURL applies. headerSecrets does carry a
|
// assumed non-secret — the same rule MaskURL applies.
|
||||||
// floor, and the difference is deliberate: a header is picked
|
// headerSecrets does carry a floor, and the difference is
|
||||||
// out by a name-shaped guess and its value may be ordinary
|
// deliberate: a header is picked out by a name-shaped guess
|
||||||
// text, whereas a URL's path and userinfo are credential
|
// and its value may be ordinary text, whereas a URL's path,
|
||||||
// material by position.
|
// query string and userinfo are credential material by
|
||||||
|
// position.
|
||||||
func urlSecrets(raw string) []string {
|
func urlSecrets(raw string) []string {
|
||||||
raw = strings.TrimSpace(raw)
|
raw = strings.TrimSpace(raw)
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
@@ -188,12 +192,11 @@ func urlSecrets(raw string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parsed.Path != "" && parsed.Path != "/" {
|
if parsed.Path != "" && parsed.Path != "/" {
|
||||||
requestURI := parsed.RequestURI()
|
secrets = append(secrets, parsed.EscapedPath())
|
||||||
secrets = append(secrets, requestURI)
|
}
|
||||||
|
|
||||||
if escaped := parsed.EscapedPath(); escaped != requestURI {
|
if parsed.RawQuery != "" {
|
||||||
secrets = append(secrets, escaped)
|
secrets = append(secrets, parsed.RequestURI(), parsed.RawQuery)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if parsed.User != nil {
|
if parsed.User != nil {
|
||||||
|
|||||||
@@ -202,6 +202,48 @@ func TestRedactor_RemovesHTTPURLQueryAndUserinfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRedactor_RemovesEchoedQueryOfURLWithoutPath covers an
|
||||||
|
// HTTP target URL whose credential is all in its query string.
|
||||||
|
// Written with or without the "/", the request line sends it
|
||||||
|
// as "/?token=…", and a target passing the event's query string
|
||||||
|
// on sends that after an "&". The event's part stays visible:
|
||||||
|
// the event's page shows it anyway.
|
||||||
|
func TestRedactor_RemovesEchoedQueryOfURLWithoutPath(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const secret = "s3cr3t"
|
||||||
|
|
||||||
|
marker := delivery.RedactionMarker
|
||||||
|
|
||||||
|
// An echoed request line, and what the event log shows of it.
|
||||||
|
echoes := map[string]string{
|
||||||
|
"POST /?token=" + secret + " HTTP/1.1": "POST " + marker +
|
||||||
|
" HTTP/1.1",
|
||||||
|
"POST ?token=" + secret + " HTTP/1.1": "POST ?" + marker +
|
||||||
|
" HTTP/1.1",
|
||||||
|
"POST /?token=" + secret + "&a=1&b=2 HTTP/1.1": "POST " +
|
||||||
|
marker + "&a=1&b=2 HTTP/1.1",
|
||||||
|
"POST ?token=" + secret + "&a=1&b=2 HTTP/1.1": "POST ?" +
|
||||||
|
marker + "&a=1&b=2 HTTP/1.1",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dest := range []string{
|
||||||
|
"https://example.com/?token=" + secret,
|
||||||
|
"https://example.com?token=" + secret,
|
||||||
|
} {
|
||||||
|
r := delivery.NewRedactor(&database.Target{
|
||||||
|
Type: database.TargetTypeHTTP,
|
||||||
|
Config: `{"url":"` + dest + `"}`,
|
||||||
|
})
|
||||||
|
|
||||||
|
for echoed, want := range echoes {
|
||||||
|
assert.Equal(
|
||||||
|
t, want, r.Redact(echoed), "%s: %s", dest, echoed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestRedactor_LeavesUnrelatedTextAlone pins that the
|
// TestRedactor_LeavesUnrelatedTextAlone pins that the
|
||||||
// redactor matches literally: it does not guess at what a
|
// redactor matches literally: it does not guess at what a
|
||||||
// secret looks like, so ordinary response content survives.
|
// secret looks like, so ordinary response content survives.
|
||||||
|
|||||||
@@ -310,6 +310,7 @@ func createReplayDelivery(
|
|||||||
TargetConfig: target.Config,
|
TargetConfig: target.Config,
|
||||||
MaxRetries: target.MaxRetries,
|
MaxRetries: target.MaxRetries,
|
||||||
Method: event.Method,
|
Method: event.Method,
|
||||||
|
RawQuery: event.RawQuery,
|
||||||
Headers: event.Headers,
|
Headers: event.Headers,
|
||||||
ContentType: event.ContentType,
|
ContentType: event.ContentType,
|
||||||
Body: replayBody(event.Body),
|
Body: replayBody(event.Body),
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ const paramDeliveryID = "deliveryID"
|
|||||||
// dispatches to it: the notifier is recorded, not run.
|
// dispatches to it: the notifier is recorded, not run.
|
||||||
const replayTargetURL = "http://93.184.216.34/hook"
|
const replayTargetURL = "http://93.184.216.34/hook"
|
||||||
|
|
||||||
|
// replayEventQuery is the query string a seeded event arrived with.
|
||||||
|
const replayEventQuery = "a=1&b=2"
|
||||||
|
|
||||||
// seedFailedDelivery records an event, a terminally failed delivery of
|
// seedFailedDelivery records an event, a terminally failed delivery of
|
||||||
// it to the given target, and the attempt that failed.
|
// it to the given target, and the attempt that failed.
|
||||||
func seedFailedDelivery(
|
func seedFailedDelivery(
|
||||||
@@ -42,6 +45,7 @@ func seedFailedDelivery(
|
|||||||
WebhookID: webhookID,
|
WebhookID: webhookID,
|
||||||
EntrypointID: "entrypoint-" + webhookID,
|
EntrypointID: "entrypoint-" + webhookID,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
|
RawQuery: replayEventQuery,
|
||||||
Headers: `{"X-Test":["yes"]}`,
|
Headers: `{"X-Test":["yes"]}`,
|
||||||
Body: `{"replay":"me"}`,
|
Body: `{"replay":"me"}`,
|
||||||
ContentType: contentTypeJSON,
|
ContentType: contentTypeJSON,
|
||||||
@@ -296,6 +300,10 @@ func assertReplayTask(
|
|||||||
"replay must use the target's current configuration",
|
"replay must use the target's current configuration",
|
||||||
)
|
)
|
||||||
assert.Equal(t, event.Method, task.Method)
|
assert.Equal(t, event.Method, task.Method)
|
||||||
|
assert.Equal(
|
||||||
|
t, replayEventQuery, task.RawQuery,
|
||||||
|
"replay re-sends the stored query string",
|
||||||
|
)
|
||||||
assert.Equal(t, event.Headers, task.Headers)
|
assert.Equal(t, event.Headers, task.Headers)
|
||||||
assert.Equal(t, event.ContentType, task.ContentType)
|
assert.Equal(t, event.ContentType, task.ContentType)
|
||||||
assert.Equal(t, 1, task.AttemptNum)
|
assert.Equal(t, 1, task.AttemptNum)
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleEntrypointCreate handles adding a new entrypoint.
|
||||||
|
func (h *Handlers) HandleEntrypointCreate() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The body size cap is enforced by the MaxBodySize
|
||||||
|
// middleware, which runs before CSRF parses the form.
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusBadRequest)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
description := r.PostFormValue("description")
|
||||||
|
|
||||||
|
entrypoint := &database.Entrypoint{
|
||||||
|
WebhookID: webhook.ID,
|
||||||
|
Path: uuid.New().String(),
|
||||||
|
Description: description,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.db.DB().Create(entrypoint).Error
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to create entrypoint", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hook/"+webhook.ID, entrypointAdded),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleEntrypointEdit handles changing an entrypoint's description.
|
||||||
|
// It writes only the description column, so the entrypoint keeps its
|
||||||
|
// URL, and an activate or deactivate saved since the page was shown
|
||||||
|
// is not undone.
|
||||||
|
func (h *Handlers) HandleEntrypointEdit() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
entrypointID := chi.URLParam(r, "entrypointID")
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The body size cap is enforced by the MaxBodySize
|
||||||
|
// middleware, which runs before CSRF parses the form.
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusBadRequest)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := h.db.DB().Model(&database.Entrypoint{}).Where(
|
||||||
|
"id = ? AND webhook_id = ?", entrypointID, webhook.ID,
|
||||||
|
).Update("description", r.PostFormValue("description"))
|
||||||
|
if result.Error != nil {
|
||||||
|
h.serverError(
|
||||||
|
w, r, "failed to edit entrypoint", result.Error,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The id came from the URL and may name another webhook's
|
||||||
|
// entrypoint, which this webhook does not have.
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hook/"+webhook.ID, entrypointSaved),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleEntrypointDelete handles deleting an entrypoint.
|
||||||
|
func (h *Handlers) HandleEntrypointDelete() http.HandlerFunc {
|
||||||
|
return h.deleteChildResource(
|
||||||
|
"entrypointID", &database.Entrypoint{},
|
||||||
|
"failed to delete entrypoint",
|
||||||
|
nil,
|
||||||
|
entrypointDeleted,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleEntrypointToggle handles toggling an entrypoint's
|
||||||
|
// active state.
|
||||||
|
func (h *Handlers) HandleEntrypointToggle() http.HandlerFunc {
|
||||||
|
return h.toggleChildResource(
|
||||||
|
"entrypointID",
|
||||||
|
func(webhookID, childID string) (bool, error) {
|
||||||
|
var ep database.Entrypoint
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND webhook_id = ?",
|
||||||
|
childID, webhookID,
|
||||||
|
).First(&ep).Error
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the active column: saving the whole row would
|
||||||
|
// write back the description read above over an edit
|
||||||
|
// saved since.
|
||||||
|
active := !ep.Active
|
||||||
|
|
||||||
|
return active, h.db.DB().Model(&ep).
|
||||||
|
Update("active", active).Error
|
||||||
|
},
|
||||||
|
"failed to toggle entrypoint",
|
||||||
|
entrypointActivated, entrypointDeactivated,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,621 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
"sneak.berlin/go/webhooker/internal/delivery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeliveryView is the display-safe projection of a delivery
|
||||||
|
// for the event log page. Its target is a TargetView, so the
|
||||||
|
// stored configuration blob — which holds the target's
|
||||||
|
// credential — has no path to the template.
|
||||||
|
type DeliveryView struct {
|
||||||
|
ID string
|
||||||
|
Status database.DeliveryStatus
|
||||||
|
Target delivery.TargetView
|
||||||
|
|
||||||
|
// Replay is set on a delivery the Replay action created.
|
||||||
|
Replay bool
|
||||||
|
|
||||||
|
// Created is how long ago the delivery was created, and
|
||||||
|
// CreatedUTC the full timestamp the page shows on hover.
|
||||||
|
Created string
|
||||||
|
CreatedUTC string
|
||||||
|
|
||||||
|
// Results is this delivery's attempts in attempt order,
|
||||||
|
// bounded by maxRenderedAttempts. Without them a failure
|
||||||
|
// renders as the status word alone and says nothing about
|
||||||
|
// why.
|
||||||
|
Results []DeliveryResultView
|
||||||
|
|
||||||
|
// AttemptCount is how many attempts were recorded, which
|
||||||
|
// is more than len(Results) once the middle was dropped.
|
||||||
|
AttemptCount int
|
||||||
|
|
||||||
|
// AttemptsOmitted is how many attempts were dropped from
|
||||||
|
// the middle of Results. The page must show it, or the
|
||||||
|
// bound would hide history rather than fold it.
|
||||||
|
AttemptsOmitted int
|
||||||
|
|
||||||
|
// Paused is set while the delivery is retrying and its
|
||||||
|
// target's circuit breaker is open, and nil otherwise.
|
||||||
|
Paused *PausedView
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// The event log's show query parameter and its two values: the events
|
||||||
|
// with a failed delivery, and those with a delivery still pending or
|
||||||
|
// retrying.
|
||||||
|
const (
|
||||||
|
showParam = "show"
|
||||||
|
showFailed = "failed"
|
||||||
|
showPending = "pending"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSourceLogs shows the request/response logs for a
|
||||||
|
// webhook.
|
||||||
|
func (h *Handlers) HandleSourceLogs() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
webhook, ok := h.ownedWebhook(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
targets, err := h.loadTargetMap(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
// Without the map every delivery renders through a
|
||||||
|
// zero redactor, so failing the page is the only
|
||||||
|
// safe answer.
|
||||||
|
h.serverError(w, r, "failed to load targets", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any other value of show lists every event, as no value
|
||||||
|
// does.
|
||||||
|
show := r.URL.Query().Get(showParam)
|
||||||
|
|
||||||
|
statuses := eventLogStatuses(show)
|
||||||
|
if statuses == nil {
|
||||||
|
show = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
evts, total, ok := h.loadEventsWithDeliveries(
|
||||||
|
w, r, webhook, targets, statuses,
|
||||||
|
)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
failed, pending, err := h.countFailedAndPendingEvents(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to count events", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
tmplKeyWebhook: &webhook,
|
||||||
|
"Events": evts,
|
||||||
|
"TotalEvents": total,
|
||||||
|
"Show": show,
|
||||||
|
"FailedEvents": failed,
|
||||||
|
"PendingEvents": pending,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderTemplate(w, r, "source_logs.html", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadTargetMap loads targets into a map of display-safe
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// The load is Unscoped because deleting a target only soft
|
||||||
|
// deletes the row while its deliveries survive in the
|
||||||
|
// per-webhook database. Both halves of the map need those rows:
|
||||||
|
// a scoped load leaves an old delivery with a zero redactor,
|
||||||
|
// which renders its response bodies unredacted, and with a zero
|
||||||
|
// view, which renders its target as a blank name.
|
||||||
|
//
|
||||||
|
// This map is historical display only. It is built for the event
|
||||||
|
// log and an event's own page, and reaches nothing but
|
||||||
|
// DeliveryView.Target: the target list on the source detail page,
|
||||||
|
// the edit form and the replay path each resolve targets
|
||||||
|
// themselves, and a deleted row is refused there as before.
|
||||||
|
func (h *Handlers) loadTargetMap(
|
||||||
|
webhookID string,
|
||||||
|
) (map[string]eventLogTarget, error) {
|
||||||
|
var targets []database.Target
|
||||||
|
|
||||||
|
err := h.db.DB().Unscoped().Where(
|
||||||
|
"webhook_id = ?", webhookID,
|
||||||
|
).Find(&targets).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
targetMap := make(
|
||||||
|
map[string]eventLogTarget, len(targets),
|
||||||
|
)
|
||||||
|
|
||||||
|
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 and a
|
||||||
|
// deleted target's configuration is masked by the same code
|
||||||
|
// that masks a live one's.
|
||||||
|
for _, v := range delivery.NewTargetViews(targets) {
|
||||||
|
entry := targetMap[v.ID]
|
||||||
|
entry.View = v
|
||||||
|
targetMap[v.ID] = entry
|
||||||
|
}
|
||||||
|
|
||||||
|
return targetMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadEventsWithDeliveries loads the recentEventLimit newest events
|
||||||
|
// and their deliveries from the per-webhook database, and the total
|
||||||
|
// number of events stored. Given delivery statuses, both cover only
|
||||||
|
// the events with a delivery in one of them. Events come back as
|
||||||
|
// capped projections rather than database.Event rows: see
|
||||||
|
// eventLogColumns for why the cut happens in SQL.
|
||||||
|
//
|
||||||
|
// The bool reports whether the load succeeded. It is false
|
||||||
|
// once this has answered the request with an error, and the
|
||||||
|
// caller must then render nothing further.
|
||||||
|
func (h *Handlers) loadEventsWithDeliveries(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook database.Webhook,
|
||||||
|
targetMap map[string]eventLogTarget,
|
||||||
|
statuses []database.DeliveryStatus,
|
||||||
|
) ([]EventLogView, int64, bool) {
|
||||||
|
if !h.dbMgr.DBExists(webhook.ID) {
|
||||||
|
return nil, 0, true
|
||||||
|
}
|
||||||
|
|
||||||
|
webhookDB, err := h.dbMgr.GetDB(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(
|
||||||
|
w, r, "failed to get webhook database", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, totalEvents, err := loadEventLogRows(
|
||||||
|
webhookDB, webhook.ID, statuses,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to load events", err)
|
||||||
|
|
||||||
|
return nil, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
result, ok := h.eventLogViews(
|
||||||
|
w, r, webhookDB, webhook.ID, rows, targetMap,
|
||||||
|
maxRenderedBodyBytes,
|
||||||
|
)
|
||||||
|
|
||||||
|
return result, totalEvents, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventLogViews projects loaded events for rendering, each with
|
||||||
|
// its deliveries, how many times it has been resubmitted and the
|
||||||
|
// entrypoint it arrived at (for a resubmitted copy, the one the
|
||||||
|
// request it copies arrived at), and with its request headers only
|
||||||
|
// when their text holds at most maxHeaderBytes. Like
|
||||||
|
// loadEventsWithDeliveries, it reports false once it has answered
|
||||||
|
// the request with an error.
|
||||||
|
func (h *Handlers) eventLogViews(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhookDB *gorm.DB,
|
||||||
|
webhookID string,
|
||||||
|
rows []eventLogRow,
|
||||||
|
targetMap map[string]eventLogTarget,
|
||||||
|
maxHeaderBytes int,
|
||||||
|
) ([]EventLogView, bool) {
|
||||||
|
result := make([]EventLogView, len(rows))
|
||||||
|
eventDeliveries := make([][]database.Delivery, len(rows))
|
||||||
|
|
||||||
|
var deliveryIDs []string
|
||||||
|
|
||||||
|
eventIDs := make([]string, len(rows))
|
||||||
|
|
||||||
|
for i := range rows {
|
||||||
|
result[i] = rows[i].view(webhookID, maxHeaderBytes)
|
||||||
|
eventIDs[i] = rows[i].ID
|
||||||
|
|
||||||
|
webhookDB.Where(
|
||||||
|
"event_id = ?", rows[i].ID,
|
||||||
|
).Find(&eventDeliveries[i])
|
||||||
|
|
||||||
|
for j := range eventDeliveries[i] {
|
||||||
|
deliveryIDs = append(
|
||||||
|
deliveryIDs, eventDeliveries[i][j].ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
attempts, err := h.loadDeliveryResults(
|
||||||
|
webhookDB, deliveryIDs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(
|
||||||
|
w, r, "failed to load delivery attempts", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
resubmits, err := resubmitCounts(webhookDB, eventIDs)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(
|
||||||
|
w, r, "failed to count event resubmissions", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
entrypoints, err := h.entrypointNames(webhookID)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to load entrypoints", err)
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range rows {
|
||||||
|
result[i].Deliveries = h.newDeliveryViews(
|
||||||
|
eventDeliveries[i], targetMap, attempts,
|
||||||
|
)
|
||||||
|
result[i].ResubmitCount = resubmits[rows[i].ID]
|
||||||
|
|
||||||
|
name, ok := entrypoints[rows[i].EntrypointID]
|
||||||
|
if !ok {
|
||||||
|
name = "deleted entrypoint"
|
||||||
|
}
|
||||||
|
|
||||||
|
result[i].Entrypoint = name
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadEventLogRows reads the event log projection of the
|
||||||
|
// recentEventLimit newest events, newest first, and the total number
|
||||||
|
// of events stored, both narrowed by statuses as eventsWithStatus
|
||||||
|
// narrows them.
|
||||||
|
func loadEventLogRows(
|
||||||
|
webhookDB *gorm.DB,
|
||||||
|
webhookID string,
|
||||||
|
statuses []database.DeliveryStatus,
|
||||||
|
) ([]eventLogRow, int64, error) {
|
||||||
|
totalEvents, err := countEventsWithStatus(
|
||||||
|
webhookDB, webhookID, statuses,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []eventLogRow
|
||||||
|
|
||||||
|
err = eventsWithStatus(webhookDB, webhookID, statuses).Select(
|
||||||
|
eventLogColumns,
|
||||||
|
maxRenderedBodyBytes, maxRenderedBodyBytes, maxRenderedBodyBytes,
|
||||||
|
).Order("created_at DESC").Limit(recentEventLimit).Find(&rows).Error
|
||||||
|
|
||||||
|
return rows, totalEvents, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventLogStatuses returns the delivery statuses the event log's show
|
||||||
|
// value lists events by, or nil for one that lists every event.
|
||||||
|
func eventLogStatuses(show string) []database.DeliveryStatus {
|
||||||
|
switch show {
|
||||||
|
case showFailed:
|
||||||
|
return []database.DeliveryStatus{database.DeliveryStatusFailed}
|
||||||
|
case showPending:
|
||||||
|
return []database.DeliveryStatus{
|
||||||
|
database.DeliveryStatusPending,
|
||||||
|
database.DeliveryStatusRetrying,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventsWithStatus selects the webhook's events, or, given statuses,
|
||||||
|
// the recentEventLimit newest of those with at least one delivery in
|
||||||
|
// one of them.
|
||||||
|
//
|
||||||
|
// Given statuses, its cost follows the matching deliveries. SQLite
|
||||||
|
// never reorders a CROSS JOIN, so it reads each join's left side
|
||||||
|
// first: the distinct event IDs of the matching deliveries, through
|
||||||
|
// idx_deliveries_status; then each of those events by ID, sorted to
|
||||||
|
// keep the newest; then the rows of only the events kept, so no other
|
||||||
|
// event's body is read. With a plain "id IN (matching deliveries)"
|
||||||
|
// condition instead, SQLite, which keeps no statistics on these
|
||||||
|
// tables, walks every event newest first.
|
||||||
|
func eventsWithStatus(
|
||||||
|
webhookDB *gorm.DB,
|
||||||
|
webhookID string,
|
||||||
|
statuses []database.DeliveryStatus,
|
||||||
|
) *gorm.DB {
|
||||||
|
if statuses == nil {
|
||||||
|
return webhookDB.Model(&database.Event{}).Where(
|
||||||
|
"webhook_id = ?", webhookID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
matching := webhookDB.Model(&database.Delivery{}).
|
||||||
|
Distinct("event_id").Where("status IN ?", statuses)
|
||||||
|
|
||||||
|
newest := webhookDB.Table("(?) AS matching", matching).
|
||||||
|
Joins("CROSS JOIN events ON events.id = matching.event_id").
|
||||||
|
Where(
|
||||||
|
"events.webhook_id = ? AND events.deleted_at IS NULL",
|
||||||
|
webhookID,
|
||||||
|
).
|
||||||
|
Order("events.created_at DESC").Limit(recentEventLimit).
|
||||||
|
Select("events.id AS event_id")
|
||||||
|
|
||||||
|
return webhookDB.Table("(?) AS newest", newest).
|
||||||
|
Joins("CROSS JOIN events ON events.id = newest.event_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// countEventsWithStatus counts the webhook's events with at least one
|
||||||
|
// delivery in one of the statuses, or every event when statuses is
|
||||||
|
// nil. Given statuses, it counts the distinct events of the matching
|
||||||
|
// deliveries and reads nothing but those deliveries, through
|
||||||
|
// idx_deliveries_status, where counting the events would read every
|
||||||
|
// event row. That is the same number, because retention deletes an
|
||||||
|
// event's deliveries with it.
|
||||||
|
func countEventsWithStatus(
|
||||||
|
webhookDB *gorm.DB,
|
||||||
|
webhookID string,
|
||||||
|
statuses []database.DeliveryStatus,
|
||||||
|
) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
if statuses == nil {
|
||||||
|
err := webhookDB.Model(&database.Event{}).Where(
|
||||||
|
"webhook_id = ?", webhookID,
|
||||||
|
).Count(&count).Error
|
||||||
|
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := webhookDB.Model(&database.Delivery{}).Distinct("event_id").
|
||||||
|
Where("status IN ?", statuses).Count(&count).Error
|
||||||
|
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// countFailedAndPendingEvents returns how many of the webhook's events
|
||||||
|
// the event log lists when it shows only those with a failed delivery,
|
||||||
|
// and when it shows only those with a delivery pending or retrying.
|
||||||
|
func (h *Handlers) countFailedAndPendingEvents(
|
||||||
|
webhookID string,
|
||||||
|
) (int64, int64, error) {
|
||||||
|
if !h.dbMgr.DBExists(webhookID) {
|
||||||
|
return 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
webhookDB, err := h.dbMgr.GetDB(webhookID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
failed, err := countEventsWithStatus(
|
||||||
|
webhookDB, webhookID, eventLogStatuses(showFailed),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pending, err := countEventsWithStatus(
|
||||||
|
webhookDB, webhookID, eventLogStatuses(showPending),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return failed, pending, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// resubmitCounts reports, for each of the page's events, how many
|
||||||
|
// events have been resubmitted from it.
|
||||||
|
//
|
||||||
|
// One grouped query covers the page rather than one query per event.
|
||||||
|
// The page shows at most recentEventLimit events, far below SQLite's
|
||||||
|
// bound parameter ceiling, so it needs no chunking as the delivery
|
||||||
|
// result load does.
|
||||||
|
func resubmitCounts(
|
||||||
|
webhookDB *gorm.DB, eventIDs []string,
|
||||||
|
) (map[string]int, error) {
|
||||||
|
counts := make(map[string]int, len(eventIDs))
|
||||||
|
|
||||||
|
if len(eventIDs) == 0 {
|
||||||
|
return counts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []struct {
|
||||||
|
ResubmittedFromID string
|
||||||
|
Total int
|
||||||
|
}
|
||||||
|
|
||||||
|
err := webhookDB.Model(&database.Event{}).
|
||||||
|
Select("resubmitted_from_id, count(*) AS total").
|
||||||
|
Where("resubmitted_from_id IN ?", eventIDs).
|
||||||
|
Group("resubmitted_from_id").
|
||||||
|
Find(&rows).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, row := range rows {
|
||||||
|
counts[row.ResubmittedFromID] = row.Total
|
||||||
|
}
|
||||||
|
|
||||||
|
return counts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliveryIDChunkSize bounds how many delivery IDs go into one
|
||||||
|
// IN clause. SQLite refuses a statement carrying more than
|
||||||
|
// SQLITE_MAX_VARIABLE_NUMBER (32766) bound parameters, and a
|
||||||
|
// page holds one delivery per target per event, so a webhook
|
||||||
|
// with enough targets would turn the whole query into an error
|
||||||
|
// and the page into zero attempts.
|
||||||
|
const deliveryIDChunkSize = 500
|
||||||
|
|
||||||
|
// loadDeliveryResults loads the recorded attempts for the
|
||||||
|
// page's deliveries, keyed by delivery ID.
|
||||||
|
//
|
||||||
|
// Each response body is cut by SQLite rather than in Go, for
|
||||||
|
// the reason deliveryResultColumns gives. How many attempts a
|
||||||
|
// delivery has is the target's MaxRetries, which the
|
||||||
|
// authenticated operator sets; how many of them reach the page
|
||||||
|
// is bounded again by maxRenderedAttempts.
|
||||||
|
func (h *Handlers) loadDeliveryResults(
|
||||||
|
webhookDB *gorm.DB,
|
||||||
|
deliveryIDs []string,
|
||||||
|
) (map[string][]deliveryResultRow, error) {
|
||||||
|
byDelivery := make(map[string][]deliveryResultRow)
|
||||||
|
|
||||||
|
for chunk := range slices.Chunk(
|
||||||
|
deliveryIDs, deliveryIDChunkSize,
|
||||||
|
) {
|
||||||
|
var rows []deliveryResultRow
|
||||||
|
|
||||||
|
err := webhookDB.Model(
|
||||||
|
&database.DeliveryResult{},
|
||||||
|
).Select(
|
||||||
|
deliveryResultColumns, maxRenderedResponseBytes,
|
||||||
|
).Where(
|
||||||
|
"delivery_id IN ?", chunk,
|
||||||
|
).Order("attempt_num ASC").Find(&rows).Error
|
||||||
|
if err != nil {
|
||||||
|
// Returning what was loaded so far renders the
|
||||||
|
// deliveries in the failed chunk as never having run,
|
||||||
|
// which is indistinguishable from ones that really
|
||||||
|
// never ran. The page fails instead.
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range rows {
|
||||||
|
byDelivery[rows[i].DeliveryID] = append(
|
||||||
|
byDelivery[rows[i].DeliveryID], rows[i],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return byDelivery, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newDeliveryViews projects deliveries for rendering,
|
||||||
|
// resolving each one's target to its display-safe view and
|
||||||
|
// each one's attempts through that target's redactor. A
|
||||||
|
// retrying delivery also reads its target's circuit breaker.
|
||||||
|
func (h *Handlers) newDeliveryViews(
|
||||||
|
deliveries []database.Delivery,
|
||||||
|
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]
|
||||||
|
created := deliveries[i].CreatedAt
|
||||||
|
|
||||||
|
results, omitted := renderedAttempts(
|
||||||
|
rows, target.Redactor,
|
||||||
|
)
|
||||||
|
|
||||||
|
views[i] = DeliveryView{
|
||||||
|
ID: deliveries[i].ID,
|
||||||
|
Status: deliveries[i].Status,
|
||||||
|
Target: target.View,
|
||||||
|
Replay: deliveries[i].Replay,
|
||||||
|
Created: humanize.Time(created),
|
||||||
|
CreatedUTC: created.UTC().Format(time.DateTime) + " UTC",
|
||||||
|
Results: results,
|
||||||
|
AttemptCount: len(rows),
|
||||||
|
AttemptsOmitted: omitted,
|
||||||
|
}
|
||||||
|
|
||||||
|
if deliveries[i].Status == database.DeliveryStatusRetrying {
|
||||||
|
views[i].Paused = h.deliveryPausedView(
|
||||||
|
deliveries[i].TargetID, rows,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return views
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxRenderedAttempts bounds how many of one delivery's
|
||||||
|
// attempts the page renders. Past it the middle is dropped and
|
||||||
|
// counted, keeping the first attempts and the last ones: how
|
||||||
|
// the delivery started failing and how it ended are what a
|
||||||
|
// reader needs, and the count says plainly that the rest was
|
||||||
|
// dropped rather than never recorded.
|
||||||
|
const (
|
||||||
|
renderedAttemptsHead = 10
|
||||||
|
renderedAttemptsTail = 10
|
||||||
|
maxRenderedAttempts = renderedAttemptsHead +
|
||||||
|
renderedAttemptsTail
|
||||||
|
)
|
||||||
|
|
||||||
|
// renderedAttempts projects a delivery's attempts through the
|
||||||
|
// target's redactor, at most maxRenderedAttempts of them, and
|
||||||
|
// reports how many it dropped.
|
||||||
|
func renderedAttempts(
|
||||||
|
rows []deliveryResultRow,
|
||||||
|
redactor delivery.Redactor,
|
||||||
|
) ([]DeliveryResultView, int) {
|
||||||
|
omitted := 0
|
||||||
|
|
||||||
|
if len(rows) > maxRenderedAttempts {
|
||||||
|
omitted = len(rows) - maxRenderedAttempts
|
||||||
|
|
||||||
|
kept := make(
|
||||||
|
[]deliveryResultRow, 0, maxRenderedAttempts,
|
||||||
|
)
|
||||||
|
kept = append(kept, rows[:renderedAttemptsHead]...)
|
||||||
|
kept = append(
|
||||||
|
kept, rows[len(rows)-renderedAttemptsTail:]...,
|
||||||
|
)
|
||||||
|
rows = kept
|
||||||
|
}
|
||||||
|
|
||||||
|
views := make([]DeliveryResultView, len(rows))
|
||||||
|
for i := range rows {
|
||||||
|
views[i] = rows[i].view(redactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
return views, omitted
|
||||||
|
}
|
||||||
@@ -17,19 +17,23 @@ import (
|
|||||||
// bytes rather than characters, so the cap bounds the page in
|
// bytes rather than characters, so the cap bounds the page in
|
||||||
// bytes whatever the payload's encoding. Cutting in SQLite
|
// bytes whatever the payload's encoding. Cutting in SQLite
|
||||||
// rather than in Go is the point of the projection — an
|
// rather than in Go is the point of the projection — an
|
||||||
// oversized body or set of request headers never becomes a Go
|
// oversized body, query string or set of request headers never
|
||||||
// string at all.
|
// becomes a Go string at all.
|
||||||
const eventLogColumns = "id, created_at, method, content_type, " +
|
const eventLogColumns = "id, created_at, method, content_type, " +
|
||||||
"resubmitted_from_id, entrypoint_id, " +
|
"resubmitted_from_id, entrypoint_id, " +
|
||||||
|
"substr(cast(raw_query as blob), 1, ?) AS raw_query, " +
|
||||||
|
"length(cast(raw_query as blob)) AS raw_query_bytes, " +
|
||||||
"substr(cast(headers as blob), 1, ?) AS headers, " +
|
"substr(cast(headers as blob), 1, ?) AS headers, " +
|
||||||
"length(cast(headers as blob)) AS headers_bytes, " +
|
"length(cast(headers as blob)) AS headers_bytes, " +
|
||||||
"substr(cast(body as blob), 1, ?) AS body, " +
|
"substr(cast(body as blob), 1, ?) AS body, " +
|
||||||
"length(cast(body as blob)) AS body_bytes"
|
"length(cast(body as blob)) AS body_bytes"
|
||||||
|
|
||||||
// eventColumns is eventLogColumns for the event's own page, which
|
// eventColumns is eventLogColumns for the event's own page, which
|
||||||
// shows the whole body and every request header.
|
// shows the whole body, the whole query string and every request
|
||||||
|
// header.
|
||||||
const eventColumns = "id, created_at, method, content_type, " +
|
const eventColumns = "id, created_at, method, content_type, " +
|
||||||
"resubmitted_from_id, entrypoint_id, headers, " +
|
"resubmitted_from_id, entrypoint_id, raw_query, " +
|
||||||
|
"length(cast(raw_query as blob)) AS raw_query_bytes, headers, " +
|
||||||
"length(cast(headers as blob)) AS headers_bytes, " +
|
"length(cast(headers as blob)) AS headers_bytes, " +
|
||||||
"cast(body as blob) AS body, " +
|
"cast(body as blob) AS body, " +
|
||||||
"length(cast(body as blob)) AS body_bytes"
|
"length(cast(body as blob)) AS body_bytes"
|
||||||
@@ -57,6 +61,13 @@ type EventLogView struct {
|
|||||||
// entrypoint's secret.
|
// entrypoint's secret.
|
||||||
Entrypoint string
|
Entrypoint string
|
||||||
|
|
||||||
|
// RawQuery is the query string the event arrived with.
|
||||||
|
// RawQueryCut reports one left out, RawQuery then empty, because
|
||||||
|
// it holds more than maxRenderedBodyBytes; only the event log
|
||||||
|
// leaves it out.
|
||||||
|
RawQuery string
|
||||||
|
RawQueryCut bool
|
||||||
|
|
||||||
// Headers is the event's request headers as text, one
|
// Headers is the event's request headers as text, one
|
||||||
// "Name: value" line per value, sorted by name. HeadersCut
|
// "Name: value" line per value, sorted by name. HeadersCut
|
||||||
// reports headers left out because they hold more than
|
// reports headers left out because they hold more than
|
||||||
@@ -85,9 +96,9 @@ func (v EventLogView) ResubmittedFrom() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eventLogRow is one row of the event log projection, or of
|
// eventLogRow is one row of the event log projection, or of
|
||||||
// eventColumns. In the event log its headers and body columns
|
// eventColumns. In the event log its query string, headers and
|
||||||
// arrive already cut to the cap by SQLite, each with its true
|
// body columns arrive already cut to the cap by SQLite, each with
|
||||||
// size beside it.
|
// its true size beside it.
|
||||||
type eventLogRow struct {
|
type eventLogRow struct {
|
||||||
ID string
|
ID string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
@@ -95,6 +106,8 @@ type eventLogRow struct {
|
|||||||
ContentType string
|
ContentType string
|
||||||
ResubmittedFromID *string
|
ResubmittedFromID *string
|
||||||
EntrypointID string
|
EntrypointID string
|
||||||
|
RawQuery string
|
||||||
|
RawQueryBytes int64
|
||||||
Headers string
|
Headers string
|
||||||
HeadersBytes int64
|
HeadersBytes int64
|
||||||
Body []byte
|
Body []byte
|
||||||
@@ -114,6 +127,13 @@ func (r *eventLogRow) view(
|
|||||||
|
|
||||||
headers, fit := requestHeaderLines(r.Headers, maxHeaderBytes)
|
headers, fit := requestHeaderLines(r.Headers, maxHeaderBytes)
|
||||||
|
|
||||||
|
rawQuery := r.RawQuery
|
||||||
|
rawQueryCut := r.RawQueryBytes > int64(len(rawQuery))
|
||||||
|
|
||||||
|
if rawQueryCut {
|
||||||
|
rawQuery = ""
|
||||||
|
}
|
||||||
|
|
||||||
return EventLogView{
|
return EventLogView{
|
||||||
ID: r.ID,
|
ID: r.ID,
|
||||||
Method: r.Method,
|
Method: r.Method,
|
||||||
@@ -123,6 +143,8 @@ func (r *eventLogRow) view(
|
|||||||
Body: newBodyView(
|
Body: newBodyView(
|
||||||
"/hook/"+webhookID+"/events/"+r.ID, r.Body, r.BodyBytes,
|
"/hook/"+webhookID+"/events/"+r.ID, r.Body, r.BodyBytes,
|
||||||
),
|
),
|
||||||
|
RawQuery: rawQuery,
|
||||||
|
RawQueryCut: rawQueryCut,
|
||||||
Headers: strings.Join(headers, "\n"),
|
Headers: strings.Join(headers, "\n"),
|
||||||
HeadersCut: !fit || r.HeadersBytes > int64(len(r.Headers)),
|
HeadersCut: !fit || r.HeadersBytes > int64(len(r.Headers)),
|
||||||
ResubmittedFromID: from,
|
ResubmittedFromID: from,
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package handlers_test
|
package handlers_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -17,14 +20,15 @@ import (
|
|||||||
|
|
||||||
// arrivedAt is how a page names the entrypoint an event arrived at.
|
// arrivedAt is how a page names the entrypoint an event arrived at.
|
||||||
func arrivedAt(name string) string {
|
func arrivedAt(name string) string {
|
||||||
return `Arrived at <span class="text-gray-900">` + name + `</span>`
|
return `Arrived at <span class="text-gray-900 wrap-anywhere">` + name +
|
||||||
|
`</span>`
|
||||||
}
|
}
|
||||||
|
|
||||||
// copiedRequestArrivedAt is how a page names, for a resubmitted copy,
|
// copiedRequestArrivedAt is how a page names, for a resubmitted copy,
|
||||||
// the entrypoint the request it copies arrived at.
|
// the entrypoint the request it copies arrived at.
|
||||||
func copiedRequestArrivedAt(name string) string {
|
func copiedRequestArrivedAt(name string) string {
|
||||||
return `The request it copies arrived at <span class="text-gray-900">` +
|
return `The request it copies arrived at ` +
|
||||||
name + `</span>`
|
`<span class="text-gray-900 wrap-anywhere">` + name + `</span>`
|
||||||
}
|
}
|
||||||
|
|
||||||
// headerBox is how a page shows an event's request header lines: as
|
// headerBox is how a page shows an event's request header lines: as
|
||||||
@@ -115,6 +119,7 @@ func TestEventRequest_EachEventShowsItsOwnEntrypointAndHeaders(
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
assert.Contains(t, page, arrivedAt("Billing sender"))
|
assert.Contains(t, page, arrivedAt("Billing sender"))
|
||||||
|
assert.Contains(t, page, "No query string.")
|
||||||
assert.Contains(t, page, headerBox(
|
assert.Contains(t, page, headerBox(
|
||||||
"Accept: */*",
|
"Accept: */*",
|
||||||
"User-Agent: shop/1 build\t7",
|
"User-Agent: shop/1 build\t7",
|
||||||
@@ -330,3 +335,70 @@ func TestEventRequest_ManyShortHeaderLines(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHandleWebhook_StoresAndShowsTheQueryString posts to an
|
||||||
|
// entrypoint's URL with a query string and proves the event stores it
|
||||||
|
// as sent, and shows it escaped in the event log and on its own page,
|
||||||
|
// in a box like the one the request headers show in.
|
||||||
|
func TestHandleWebhook_StoresAndShowsTheQueryString(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
f := newRecentEventsFixture(t)
|
||||||
|
ep := seedEntrypoint(t, f.db, f.webhook.ID)
|
||||||
|
|
||||||
|
req := httptest.NewRequestWithContext(
|
||||||
|
context.Background(), http.MethodPost,
|
||||||
|
"/h/"+ep.Path+"?a=1&b=2", strings.NewReader("{}"),
|
||||||
|
)
|
||||||
|
|
||||||
|
rctx := chi.NewRouteContext()
|
||||||
|
rctx.URLParams.Add("uuid", ep.Path)
|
||||||
|
|
||||||
|
req = req.WithContext(context.WithValue(
|
||||||
|
req.Context(), chi.RouteCtxKey, rctx,
|
||||||
|
))
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
f.h.HandleWebhook().ServeHTTP(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var stored database.Event
|
||||||
|
|
||||||
|
require.NoError(t, f.webhookDB.First(&stored).Error)
|
||||||
|
assert.Equal(t, "a=1&b=2", stored.RawQuery)
|
||||||
|
|
||||||
|
page := renderSourceLogsPage(t, f.h, f.sess, f.webhook.ID)
|
||||||
|
assert.Contains(t, page, headerBox("a=1&b=2"))
|
||||||
|
|
||||||
|
w = serveEventPage(t, f.h, f.sess, f.webhook.ID, stored.ID)
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), headerBox("a=1&b=2"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestEventRequest_QueryStringOverTheLimit proves the event log leaves
|
||||||
|
// out a query string that holds more than it shows of a body, and links
|
||||||
|
// to the event's own page, which shows it whole.
|
||||||
|
func TestEventRequest_QueryStringOverTheLimit(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
f := newRecentEventsFixture(t)
|
||||||
|
ep := f.entrypoint(t, "Billing sender")
|
||||||
|
event := f.eventAt(t, ep, `{}`, time.Now())
|
||||||
|
query := "q=" + strings.Repeat("x", bodyCap)
|
||||||
|
|
||||||
|
require.NoError(t, f.webhookDB.Model(event).Update(
|
||||||
|
"raw_query", query,
|
||||||
|
).Error)
|
||||||
|
|
||||||
|
page := renderSourceLogsPage(t, f.h, f.sess, f.webhook.ID)
|
||||||
|
assert.Contains(t, page, `<a href="/hook/`+f.webhook.ID+`/events/`+
|
||||||
|
event.ID+`" class="btn-small">Show the query string</a>`)
|
||||||
|
assert.NotContains(t, page, "q=x")
|
||||||
|
assert.Less(t, len(page), 4*bodyCap)
|
||||||
|
|
||||||
|
w := serveEventPage(t, f.h, f.sess, f.webhook.ID, event.ID)
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), headerBox(query))
|
||||||
|
assert.NotContains(t, w.Body.String(), "Show the query string")
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ type resubmitSource struct {
|
|||||||
ID string
|
ID string
|
||||||
EntrypointID string
|
EntrypointID string
|
||||||
Method string
|
Method string
|
||||||
|
RawQuery string
|
||||||
Headers string
|
Headers string
|
||||||
ContentType string
|
ContentType string
|
||||||
Body []byte
|
Body []byte
|
||||||
@@ -39,7 +40,7 @@ type resubmitSource struct {
|
|||||||
// The cast to blob is what makes the driver hand back the stored bytes
|
// The cast to blob is what makes the driver hand back the stored bytes
|
||||||
// rather than a string conversion, the same reason eventBodyQuery
|
// rather than a string conversion, the same reason eventBodyQuery
|
||||||
// casts.
|
// casts.
|
||||||
const resubmitColumns = "id, entrypoint_id, method, headers, " +
|
const resubmitColumns = "id, entrypoint_id, method, raw_query, headers, " +
|
||||||
"content_type, cast(body as blob) AS body"
|
"content_type, cast(body as blob) AS body"
|
||||||
|
|
||||||
// HandleEventResubmit re-injects a stored event as a new undelivered
|
// HandleEventResubmit re-injects a stored event as a new undelivered
|
||||||
@@ -193,6 +194,7 @@ func (h *Handlers) queueResubmit(
|
|||||||
WebhookID: webhook.ID,
|
WebhookID: webhook.ID,
|
||||||
EntrypointID: src.EntrypointID,
|
EntrypointID: src.EntrypointID,
|
||||||
Method: src.Method,
|
Method: src.Method,
|
||||||
|
RawQuery: src.RawQuery,
|
||||||
HeadersJSON: src.Headers,
|
HeadersJSON: src.Headers,
|
||||||
ContentType: src.ContentType,
|
ContentType: src.ContentType,
|
||||||
Body: src.Body,
|
Body: src.Body,
|
||||||
|
|||||||
@@ -22,9 +22,13 @@ import (
|
|||||||
// dispatches to it: the notifier is recorded, not run.
|
// dispatches to it: the notifier is recorded, not run.
|
||||||
const resubmitTargetURL = "http://93.184.216.34/hook"
|
const resubmitTargetURL = "http://93.184.216.34/hook"
|
||||||
|
|
||||||
// resubmitEventHeaders is the stored header JSON a seeded event
|
// resubmitEventHeaders and resubmitEventQuery are the stored header
|
||||||
// carries, so a test can prove the copy takes it verbatim.
|
// JSON and query string a seeded event carries, so a test can prove the
|
||||||
const resubmitEventHeaders = `{"X-Test":["yes"],"X-Trace":["abc"]}`
|
// copy takes them verbatim.
|
||||||
|
const (
|
||||||
|
resubmitEventHeaders = `{"X-Test":["yes"],"X-Trace":["abc"]}`
|
||||||
|
resubmitEventQuery = "a=1&b=2"
|
||||||
|
)
|
||||||
|
|
||||||
// seedStoredEvent records one event in a webhook's own database with
|
// seedStoredEvent records one event in a webhook's own database with
|
||||||
// no deliveries at all, which is the state a captured event is in when
|
// no deliveries at all, which is the state a captured event is in when
|
||||||
@@ -43,6 +47,7 @@ func seedStoredEvent(
|
|||||||
WebhookID: webhookID,
|
WebhookID: webhookID,
|
||||||
EntrypointID: "entrypoint-" + webhookID,
|
EntrypointID: "entrypoint-" + webhookID,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
|
RawQuery: resubmitEventQuery,
|
||||||
Headers: resubmitEventHeaders,
|
Headers: resubmitEventHeaders,
|
||||||
Body: body,
|
Body: body,
|
||||||
ContentType: contentTypeJSON,
|
ContentType: contentTypeJSON,
|
||||||
@@ -202,6 +207,7 @@ func assertEventCopy(
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
assert.Equal(t, original.Method, fresh.Method)
|
assert.Equal(t, original.Method, fresh.Method)
|
||||||
|
assert.Equal(t, resubmitEventQuery, fresh.RawQuery)
|
||||||
assert.Equal(t, original.Headers, fresh.Headers)
|
assert.Equal(t, original.Headers, fresh.Headers)
|
||||||
assert.Equal(t, original.Body, fresh.Body)
|
assert.Equal(t, original.Body, fresh.Body)
|
||||||
assert.Equal(t, int64(len(original.Body)), fresh.BodyBytes)
|
assert.Equal(t, int64(len(original.Body)), fresh.BodyBytes)
|
||||||
@@ -236,6 +242,7 @@ func assertResubmitTask(
|
|||||||
assert.Equal(t, target.ID, task.TargetID)
|
assert.Equal(t, target.ID, task.TargetID)
|
||||||
assert.Equal(t, target.Type, task.TargetType)
|
assert.Equal(t, target.Type, task.TargetType)
|
||||||
assert.Equal(t, fresh.Method, task.Method)
|
assert.Equal(t, fresh.Method, task.Method)
|
||||||
|
assert.Equal(t, fresh.RawQuery, task.RawQuery)
|
||||||
assert.Equal(t, fresh.Headers, task.Headers)
|
assert.Equal(t, fresh.Headers, task.Headers)
|
||||||
assert.Equal(t, fresh.ContentType, task.ContentType)
|
assert.Equal(t, fresh.ContentType, task.ContentType)
|
||||||
assert.Equal(t, 1, task.AttemptNum)
|
assert.Equal(t, 1, task.AttemptNum)
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// parseRetentionDays interprets a retention_days form value. It
|
||||||
|
// returns the number of days, or, for a value it refuses, the message
|
||||||
|
// the create and edit forms show; the message is empty when the value
|
||||||
|
// is accepted.
|
||||||
|
//
|
||||||
|
// An empty value yields fallback, which lets the create path apply the
|
||||||
|
// default and the edit path leave the stored value unchanged. A value
|
||||||
|
// of 0 is returned as 0 and is rewritten to the retain-forever
|
||||||
|
// sentinel by database.Webhook's BeforeSave hook. Anything unparseable
|
||||||
|
// or negative is refused rather than silently given a default.
|
||||||
|
//
|
||||||
|
// The upper bound is not cosmetic. The reaper computes its cutoff as a
|
||||||
|
// time.Duration, an int64 nanosecond count, so a day count above
|
||||||
|
// database.MaxFiniteRetentionDays overflows, puts the cutoff in the
|
||||||
|
// future, and deletes every event the webhook has. A finite value
|
||||||
|
// above that ceiling is therefore refused, and the message names the
|
||||||
|
// ceiling rather than implying the input was not a number.
|
||||||
|
//
|
||||||
|
// A value at or above the retain-forever sentinel is not out of range:
|
||||||
|
// it is what the edit form pre-fills for a retain-forever webhook, so
|
||||||
|
// submitting the form back unchanged has to keep meaning "forever"
|
||||||
|
// rather than being rejected.
|
||||||
|
func parseRetentionDays(raw string, fallback int) (int, string) {
|
||||||
|
raw = strings.TrimSpace(raw)
|
||||||
|
if raw == "" {
|
||||||
|
return fallback, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := strconv.Atoi(raw)
|
||||||
|
if err != nil || v < 0 {
|
||||||
|
return 0, "Retention must be a whole number of days, or 0 to " +
|
||||||
|
"retain events forever."
|
||||||
|
}
|
||||||
|
|
||||||
|
if v >= database.RetentionForeverDays {
|
||||||
|
return database.RetentionForeverDays, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if v > database.MaxFiniteRetentionDays {
|
||||||
|
return 0, "Retention must be at most " +
|
||||||
|
strconv.Itoa(database.MaxFiniteRetentionDays) +
|
||||||
|
" days, or 0 to retain events forever."
|
||||||
|
}
|
||||||
|
|
||||||
|
return v, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ownedWebhook resolves the request's sourceID parameter to a
|
||||||
|
// webhook the session's user owns.
|
||||||
|
//
|
||||||
|
// Ownership and existence are decided by one query, so a
|
||||||
|
// webhook belonging to another user is indistinguishable from
|
||||||
|
// one that does not exist: both are a 404, and neither confirms
|
||||||
|
// the id. Callers that reach further into a webhook's data —
|
||||||
|
// the event log page and the event body download — share this
|
||||||
|
// one check rather than restating it, so the download cannot
|
||||||
|
// come to authorize differently from the page that links to it.
|
||||||
|
//
|
||||||
|
// It reports false once it has written the response, which is a
|
||||||
|
// redirect to the login page for an unauthenticated request and
|
||||||
|
// a 404 otherwise. The caller returns without writing more.
|
||||||
|
func (h *Handlers) ownedWebhook(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
) (database.Webhook, bool) {
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return database.Webhook{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return database.Webhook{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return webhook, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteChildResource returns a handler that deletes a child
|
||||||
|
// resource (entrypoint or target) belonging to a webhook. The
|
||||||
|
// optional afterDelete hook runs with the child's id once the
|
||||||
|
// delete has removed it, before the redirect, which carries done as
|
||||||
|
// its notice.
|
||||||
|
func (h *Handlers) deleteChildResource(
|
||||||
|
idParam string,
|
||||||
|
model any,
|
||||||
|
errMsg string,
|
||||||
|
afterDelete func(childID string),
|
||||||
|
done noticeCode,
|
||||||
|
) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
childID := chi.URLParam(r, idParam)
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := h.db.DB().Where(
|
||||||
|
"id = ? AND webhook_id = ?",
|
||||||
|
childID, webhook.ID,
|
||||||
|
).Delete(model)
|
||||||
|
if result.Error != nil {
|
||||||
|
h.serverError(w, r, errMsg, result.Error)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only for a row this webhook really had: the id came from
|
||||||
|
// the URL and may name another webhook's child.
|
||||||
|
if afterDelete != nil && result.RowsAffected > 0 {
|
||||||
|
afterDelete(childID)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r,
|
||||||
|
withNotice("/hook/"+webhook.ID, done),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// toggleChildResource returns a handler that toggles the active
|
||||||
|
// state of a child resource belonging to a webhook. toggleFn returns
|
||||||
|
// the new state, and the redirect carries activated or deactivated as
|
||||||
|
// its notice to match.
|
||||||
|
func (h *Handlers) toggleChildResource(
|
||||||
|
idParam string,
|
||||||
|
toggleFn func(webhookID, childID string) (bool, error),
|
||||||
|
errMsg string,
|
||||||
|
activated, deactivated noticeCode,
|
||||||
|
) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
childID := chi.URLParam(r, idParam)
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
active, err := toggleFn(webhook.ID, childID)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, errMsg, err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
done := deactivated
|
||||||
|
if active {
|
||||||
|
done = activated
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r,
|
||||||
|
withNotice("/hook/"+webhook.ID, done),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getUserID extracts the user ID from the session.
|
||||||
|
func (h *Handlers) getUserID(
|
||||||
|
r *http.Request,
|
||||||
|
) (string, bool) {
|
||||||
|
sess, err := h.session.Get(r)
|
||||||
|
if err != nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !h.session.IsAuthenticated(sess) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.session.GetUserID(sess)
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,389 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
"sneak.berlin/go/webhooker/internal/delivery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleTargetCreate handles adding a new target to a webhook.
|
||||||
|
func (h *Handlers) HandleTargetCreate() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
h.renameMu.Lock()
|
||||||
|
defer h.renameMu.Unlock()
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The body size cap is enforced by the MaxBodySize
|
||||||
|
// middleware, which runs before CSRF parses the form.
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusBadRequest)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.processTargetCreate(w, r, webhook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// processTargetCreate validates and creates a new target. A refused
|
||||||
|
// submission shows the webhook page again, with the add target form
|
||||||
|
// open on the chosen type, the values entered, and the reason.
|
||||||
|
func (h *Handlers) processTargetCreate(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook database.Webhook,
|
||||||
|
) {
|
||||||
|
in := targetFormInputFrom(r)
|
||||||
|
|
||||||
|
target, errMsg, err := h.newTarget(r.Context(), webhook.ID, in)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to encode target config", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if errMsg != "" {
|
||||||
|
h.renderSourceDetail(w, r, webhook, in, errMsg)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.db.DB().Create(target).Error
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to create target", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hook/"+webhook.ID, targetAdded),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTarget validates a new target for a webhook and returns the row
|
||||||
|
// to create, or, when it refuses the target, the message the form
|
||||||
|
// shows. An error is the server's fault, not a refusal: the accepted
|
||||||
|
// configuration could not be encoded. Every form that creates a
|
||||||
|
// target goes through here, so they all accept and refuse the same
|
||||||
|
// things.
|
||||||
|
func (h *Handlers) newTarget(
|
||||||
|
ctx context.Context,
|
||||||
|
webhookID string,
|
||||||
|
in targetFormInput,
|
||||||
|
) (*database.Target, string, error) {
|
||||||
|
target := &database.Target{
|
||||||
|
WebhookID: webhookID,
|
||||||
|
Type: in.Type,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
errMsg, err := h.setTargetFromForm(ctx, target, in)
|
||||||
|
if err != nil || errMsg != "" {
|
||||||
|
return nil, errMsg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return target, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setTargetFromForm validates a target form against the target's type
|
||||||
|
// and, when it accepts it, sets the target's name, configuration and
|
||||||
|
// retry count from it. It returns the message the form shows for
|
||||||
|
// anything it refuses, an unknown type among them, and then leaves the
|
||||||
|
// target unchanged; an error is the server's fault, as for newTarget.
|
||||||
|
// The add target form and the target edit form both go through here,
|
||||||
|
// so the two cannot come to disagree about what a target may be.
|
||||||
|
func (h *Handlers) setTargetFromForm(
|
||||||
|
ctx context.Context,
|
||||||
|
target *database.Target,
|
||||||
|
in targetFormInput,
|
||||||
|
) (string, error) {
|
||||||
|
if in.Name == "" {
|
||||||
|
return "Name is required", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
configJSON, errMsg, err := h.buildTargetConfig(ctx, target.Type, in)
|
||||||
|
if err != nil || errMsg != "" {
|
||||||
|
return errMsg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// An empty max_retries keeps the target's count: the
|
||||||
|
// fire-and-forget default of 0 for a new target, and the stored
|
||||||
|
// count for an edited one, since the forms for target types that
|
||||||
|
// do not retry have no such field. A value that is filled in but
|
||||||
|
// invalid is refused rather than becoming that count, so a typo
|
||||||
|
// cannot destroy the count a target is delivering with.
|
||||||
|
maxRetries, err := parseMaxRetries(in.MaxRetries, target.MaxRetries)
|
||||||
|
if err != nil {
|
||||||
|
return "Invalid delivery attempts: " + retriesErrorMessage(err), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
target.Name = in.Name
|
||||||
|
target.Config = configJSON
|
||||||
|
target.MaxRetries = maxRetries
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// targetFormInput carries the raw values of a target form. Both the
|
||||||
|
// create and the edit path fill one and hand it to setTargetFromForm,
|
||||||
|
// so neither can come to validate a target differently from the
|
||||||
|
// other. Both forms are filled from one: the edit form with the
|
||||||
|
// stored values, and a refused form with the values submitted.
|
||||||
|
type targetFormInput struct {
|
||||||
|
// Name is the target's name.
|
||||||
|
Name string
|
||||||
|
// Type is the type chosen on the add target form. The edit form
|
||||||
|
// has none: a target's stored type decides.
|
||||||
|
Type database.TargetType
|
||||||
|
// URL is the destination for an HTTP target and the webhook URL
|
||||||
|
// for a Slack target.
|
||||||
|
URL string
|
||||||
|
// Headers is an HTTP target's headers, one "Name: value" per
|
||||||
|
// line.
|
||||||
|
Headers string
|
||||||
|
// Timeout is an HTTP target's per-request timeout in seconds.
|
||||||
|
Timeout string
|
||||||
|
// ForwardQuery is an HTTP target's checkbox that passes each
|
||||||
|
// event's query string on to it.
|
||||||
|
ForwardQuery bool
|
||||||
|
// MaxRetries is an HTTP or Slack target's max_retries.
|
||||||
|
MaxRetries string
|
||||||
|
// Expiry is a database (archive) target's row expiry.
|
||||||
|
Expiry string
|
||||||
|
// Rotation is a database (archive) target's rotation.
|
||||||
|
Rotation string
|
||||||
|
}
|
||||||
|
|
||||||
|
// targetFormInputFrom reads a target form from a request body. The
|
||||||
|
// body size cap is enforced by the MaxBodySize middleware, which runs
|
||||||
|
// before CSRF parses the form.
|
||||||
|
//
|
||||||
|
// Every field is read with PostFormValue, not FormValue. FormValue
|
||||||
|
// falls back to the query string, which would let
|
||||||
|
// `POST /hook/{id}/targets?url=https://hooks.slack.com/...`
|
||||||
|
// configure a target from a value the request line carries — and the
|
||||||
|
// request line, unlike the body, is what logs, proxies, Referer
|
||||||
|
// headers and error trackers record. The headers field is under the
|
||||||
|
// same rule and for the same reason: its values are authorization
|
||||||
|
// tokens.
|
||||||
|
func targetFormInputFrom(r *http.Request) targetFormInput {
|
||||||
|
return targetFormInput{
|
||||||
|
Name: r.PostFormValue("name"),
|
||||||
|
Type: database.TargetType(r.PostFormValue("type")),
|
||||||
|
URL: r.PostFormValue("url"),
|
||||||
|
Headers: r.PostFormValue("headers"),
|
||||||
|
Timeout: r.PostFormValue("timeout"),
|
||||||
|
ForwardQuery: r.PostFormValue("forward_query") != "",
|
||||||
|
MaxRetries: r.PostFormValue("max_retries"),
|
||||||
|
Expiry: r.PostFormValue("expiry"),
|
||||||
|
Rotation: r.PostFormValue("rotation"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildTargetConfig builds the JSON config string for a target from
|
||||||
|
// the submitted form values, or returns the message the form shows
|
||||||
|
// for a value it refuses. An error is the server's fault, not a
|
||||||
|
// refusal: the accepted configuration could not be encoded. Which
|
||||||
|
// fields of in apply depends on the target type; a type without a URL
|
||||||
|
// ignores any URL submitted.
|
||||||
|
func (h *Handlers) buildTargetConfig(
|
||||||
|
ctx context.Context,
|
||||||
|
targetType database.TargetType,
|
||||||
|
in targetFormInput,
|
||||||
|
) (string, string, error) {
|
||||||
|
switch targetType {
|
||||||
|
case database.TargetTypeHTTP:
|
||||||
|
return h.buildHTTPTargetConfig(ctx, in)
|
||||||
|
case database.TargetTypeSlack:
|
||||||
|
return h.buildSlackTargetConfig(ctx, in.URL)
|
||||||
|
case database.TargetTypeDatabase:
|
||||||
|
return buildDatabaseTargetConfig(in.Expiry, in.Rotation)
|
||||||
|
case database.TargetTypeLog:
|
||||||
|
return "", "", nil
|
||||||
|
default:
|
||||||
|
return "", "Invalid target type", nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildHTTPTargetConfig builds config JSON for an HTTP target: an
|
||||||
|
// SSRF-validated destination plus the optional headers, timeout and
|
||||||
|
// query string setting the delivery path honours.
|
||||||
|
func (h *Handlers) buildHTTPTargetConfig(
|
||||||
|
ctx context.Context,
|
||||||
|
in targetFormInput,
|
||||||
|
) (string, string, error) {
|
||||||
|
errMsg := h.validateTargetURL(
|
||||||
|
ctx, in.URL, "URL is required for HTTP targets",
|
||||||
|
)
|
||||||
|
if errMsg != "" {
|
||||||
|
return "", errMsg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
headers, err := delivery.ParseTargetHeaders(in.Headers)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Sprintf("Invalid headers: %v", err), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout, err := delivery.ParseTargetTimeout(in.Timeout)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Sprintf("Invalid timeout: %v", err), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
configJSON, err := marshalTargetConfig(delivery.HTTPTargetConfig{
|
||||||
|
URL: in.URL,
|
||||||
|
Headers: headers,
|
||||||
|
Timeout: timeout,
|
||||||
|
ForwardQuery: in.ForwardQuery,
|
||||||
|
})
|
||||||
|
|
||||||
|
return configJSON, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildSlackTargetConfig builds config JSON for a Slack target,
|
||||||
|
// whose whole configuration is one SSRF-validated webhook URL.
|
||||||
|
func (h *Handlers) buildSlackTargetConfig(
|
||||||
|
ctx context.Context,
|
||||||
|
targetURL string,
|
||||||
|
) (string, string, error) {
|
||||||
|
errMsg := h.validateTargetURL(
|
||||||
|
ctx, targetURL,
|
||||||
|
"Webhook URL is required for Slack targets",
|
||||||
|
)
|
||||||
|
if errMsg != "" {
|
||||||
|
return "", errMsg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
configJSON, err := marshalTargetConfig(delivery.SlackTargetConfig{
|
||||||
|
WebhookURL: targetURL,
|
||||||
|
})
|
||||||
|
|
||||||
|
return configJSON, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateTargetURL refuses an empty or SSRF-blocked destination,
|
||||||
|
// returning the message the form shows, or "" when the destination
|
||||||
|
// is accepted. missingMsg is the message for no URL at all.
|
||||||
|
//
|
||||||
|
// It is the single point at which a user-supplied destination enters
|
||||||
|
// the SSRF guard, on create and on edit alike. An edit path that
|
||||||
|
// reached storage without passing through here would reopen the hole
|
||||||
|
// the guard closes.
|
||||||
|
func (h *Handlers) validateTargetURL(
|
||||||
|
ctx context.Context,
|
||||||
|
targetURL, missingMsg string,
|
||||||
|
) string {
|
||||||
|
if targetURL == "" {
|
||||||
|
return missingMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
err := h.ssrf.ValidateTargetURL(ctx, targetURL)
|
||||||
|
if err != nil {
|
||||||
|
// The submitted URL can be a credential (a Slack
|
||||||
|
// incoming webhook URL is a bearer token), so the log
|
||||||
|
// records only its scheme and host.
|
||||||
|
h.log.Warn(
|
||||||
|
"target URL blocked by SSRF protection",
|
||||||
|
"url", delivery.MaskURL(targetURL),
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
msg := "Invalid target URL: " + err.Error()
|
||||||
|
|
||||||
|
// Only a private or reserved address's refusal says how
|
||||||
|
// to allow it. Other refusals never do: link-local, the
|
||||||
|
// unspecified addresses and the unconditional metadata
|
||||||
|
// addresses cannot be opened, and the default
|
||||||
|
// blocklist's public addresses, which listing does open,
|
||||||
|
// hand out credentials.
|
||||||
|
if errors.Is(err, delivery.ErrBlockedPrivateOrReservedIP) {
|
||||||
|
msg += ". Private and reserved addresses are refused " +
|
||||||
|
"by default; the server's ALLOWED_EGRESS_CIDRS " +
|
||||||
|
"setting allows named networks (see \"Allowing " +
|
||||||
|
"egress to your own network\" in the README)."
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshalTargetConfig serialises a target configuration for storage.
|
||||||
|
func marshalTargetConfig(cfg any) (string, error) {
|
||||||
|
configBytes, err := json.Marshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(configBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildDatabaseTargetConfig builds config JSON for a database
|
||||||
|
// (archive) target. The optional expiry and rotation are validated
|
||||||
|
// here, at creation time, so a bad value is refused instead of
|
||||||
|
// failing every subsequent delivery. Each is stored only when set,
|
||||||
|
// and with neither the config is empty (the keep-forever, one-file
|
||||||
|
// default).
|
||||||
|
func buildDatabaseTargetConfig(
|
||||||
|
expiry, rotation string,
|
||||||
|
) (string, string, error) {
|
||||||
|
expiry = strings.TrimSpace(expiry)
|
||||||
|
|
||||||
|
err := delivery.ValidateArchiveExpiry(expiry)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Sprintf("Invalid archive expiry: %v", err), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = delivery.ValidateArchiveRotation(rotation)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Sprintf("Invalid archive rotation: %v", err), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := map[string]any{}
|
||||||
|
|
||||||
|
if expiry != "" {
|
||||||
|
cfg["expiry"] = expiry
|
||||||
|
}
|
||||||
|
|
||||||
|
if rotation != "" {
|
||||||
|
cfg["rotation"] = rotation
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg) == 0 {
|
||||||
|
return "", "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
configJSON, err := marshalTargetConfig(cfg)
|
||||||
|
|
||||||
|
return configJSON, "", err
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleTargetDelete handles deleting a target. A deleted
|
||||||
|
// database target's archive writer is evicted and its handle
|
||||||
|
// closed; the archive file is left on disk.
|
||||||
|
func (h *Handlers) HandleTargetDelete() http.HandlerFunc {
|
||||||
|
return h.deleteChildResource(
|
||||||
|
"targetID", &database.Target{},
|
||||||
|
"failed to delete target",
|
||||||
|
h.evictTargetArchiveWriter,
|
||||||
|
targetDeleted,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// evictTargetArchiveWriter is evictArchiveWriter for one deleted
|
||||||
|
// target, and leaves its archive file on disk for the same reason.
|
||||||
|
// A target that is not a database target has no writer, and
|
||||||
|
// evicting it does nothing.
|
||||||
|
func (h *Handlers) evictTargetArchiveWriter(targetID string) {
|
||||||
|
if h.archives == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.archives.EvictTarget(targetID)
|
||||||
|
}
|
||||||
@@ -77,13 +77,14 @@ func (h *Handlers) HandleTargetEdit() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
form := targetFormInput{
|
form := targetFormInput{
|
||||||
Name: target.Name,
|
Name: target.Name,
|
||||||
URL: cfg.URL,
|
URL: cfg.URL,
|
||||||
Headers: cfg.Headers,
|
Headers: cfg.Headers,
|
||||||
Timeout: cfg.Timeout,
|
Timeout: cfg.Timeout,
|
||||||
MaxRetries: strconv.Itoa(target.MaxRetries),
|
ForwardQuery: cfg.ForwardQuery,
|
||||||
Expiry: cfg.Expiry,
|
MaxRetries: strconv.Itoa(target.MaxRetries),
|
||||||
Rotation: cfg.Rotation,
|
Expiry: cfg.Expiry,
|
||||||
|
Rotation: cfg.Rotation,
|
||||||
}
|
}
|
||||||
|
|
||||||
h.renderTargetEdit(
|
h.renderTargetEdit(
|
||||||
|
|||||||
@@ -442,6 +442,59 @@ func TestHandleTargetEdit_CallsTheDatabaseTypeArchive(t *testing.T) {
|
|||||||
assert.Contains(t, page, `class="label">Archive rotation</label>`)
|
assert.Contains(t, page, `class="label">Archive rotation</label>`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHandleTarget_ForwardQuery covers the HTTP target's setting that
|
||||||
|
// passes each event's query string on to it: the add target form
|
||||||
|
// stores it checked, the edit form starts with it checked and turns it
|
||||||
|
// off when saved unchecked, and both forms come back with it checked
|
||||||
|
// when refused.
|
||||||
|
func TestHandleTarget_ForwardQuery(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const checkbox = `name="forward_query" value="on" checked`
|
||||||
|
|
||||||
|
env := setupSourceTest(t)
|
||||||
|
webhook := seedWebhookWithRetention(t, env.db, 30)
|
||||||
|
targetsPath := "/hook/" + webhook.ID + "/targets"
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("name", "forwarding")
|
||||||
|
form.Set("type", string(database.TargetTypeHTTP))
|
||||||
|
form.Set("url", editOriginalURL)
|
||||||
|
form.Set("forward_query", "on")
|
||||||
|
|
||||||
|
w := serveTarget(env, http.MethodPost, targetsPath, form)
|
||||||
|
require.Equal(t, http.StatusSeeOther, w.Code, w.Body.String())
|
||||||
|
|
||||||
|
targets := targetsForWebhook(t, env.db, webhook.ID)
|
||||||
|
require.Len(t, targets, 1)
|
||||||
|
assert.True(t, storedHTTPConfig(t, env, targets[0].ID).ForwardQuery)
|
||||||
|
|
||||||
|
w = serveTarget(
|
||||||
|
env, http.MethodGet, targetsPath+"/"+targets[0].ID+"/edit", nil,
|
||||||
|
)
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), checkbox)
|
||||||
|
|
||||||
|
edit := editForm(editOriginalURL, "", "")
|
||||||
|
|
||||||
|
w = submitTargetEdit(env, webhook.ID, targets[0].ID, edit)
|
||||||
|
require.Equal(t, http.StatusSeeOther, w.Code, w.Body.String())
|
||||||
|
assert.False(t, storedHTTPConfig(t, env, targets[0].ID).ForwardQuery)
|
||||||
|
|
||||||
|
edit.Set("url", editBlockedURL)
|
||||||
|
edit.Set("forward_query", "on")
|
||||||
|
|
||||||
|
w = submitTargetEdit(env, webhook.ID, targets[0].ID, edit)
|
||||||
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), checkbox)
|
||||||
|
|
||||||
|
form.Set("url", editBlockedURL)
|
||||||
|
|
||||||
|
w = serveTarget(env, http.MethodPost, targetsPath, form)
|
||||||
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), "data-forward-query")
|
||||||
|
}
|
||||||
|
|
||||||
// TestHandleTargetEditSubmit_Rejects covers every submission that
|
// TestHandleTargetEditSubmit_Rejects covers every submission that
|
||||||
// must not reach storage.
|
// must not reach storage.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleTargetToggle handles toggling a target's active state.
|
||||||
|
func (h *Handlers) HandleTargetToggle() http.HandlerFunc {
|
||||||
|
return h.toggleChildResource(
|
||||||
|
"targetID",
|
||||||
|
func(webhookID, childID string) (bool, error) {
|
||||||
|
var tgt database.Target
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND webhook_id = ?",
|
||||||
|
childID, webhookID,
|
||||||
|
).First(&tgt).Error
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the active column: saving the whole row would
|
||||||
|
// write back the name and settings read above over an
|
||||||
|
// edit saved since.
|
||||||
|
active := !tgt.Active
|
||||||
|
|
||||||
|
return active, h.db.DB().Model(&tgt).
|
||||||
|
Update("active", active).Error
|
||||||
|
},
|
||||||
|
"failed to toggle target",
|
||||||
|
targetActivated, targetDeactivated,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -230,6 +230,7 @@ type eventSource struct {
|
|||||||
WebhookID string
|
WebhookID string
|
||||||
EntrypointID string
|
EntrypointID string
|
||||||
Method string
|
Method string
|
||||||
|
RawQuery string
|
||||||
HeadersJSON string
|
HeadersJSON string
|
||||||
ContentType string
|
ContentType string
|
||||||
Body []byte
|
Body []byte
|
||||||
@@ -245,6 +246,7 @@ func (s eventSource) event() *database.Event {
|
|||||||
WebhookID: s.WebhookID,
|
WebhookID: s.WebhookID,
|
||||||
EntrypointID: s.EntrypointID,
|
EntrypointID: s.EntrypointID,
|
||||||
Method: s.Method,
|
Method: s.Method,
|
||||||
|
RawQuery: s.RawQuery,
|
||||||
Headers: s.HeadersJSON,
|
Headers: s.HeadersJSON,
|
||||||
Body: string(s.Body),
|
Body: string(s.Body),
|
||||||
BodyBytes: int64(len(s.Body)),
|
BodyBytes: int64(len(s.Body)),
|
||||||
@@ -264,6 +266,7 @@ func requestEventSource(
|
|||||||
WebhookID: entrypoint.WebhookID,
|
WebhookID: entrypoint.WebhookID,
|
||||||
EntrypointID: entrypoint.ID,
|
EntrypointID: entrypoint.ID,
|
||||||
Method: r.Method,
|
Method: r.Method,
|
||||||
|
RawQuery: r.URL.RawQuery,
|
||||||
HeadersJSON: string(headersJSON),
|
HeadersJSON: string(headersJSON),
|
||||||
ContentType: r.Header.Get("Content-Type"),
|
ContentType: r.Header.Get("Content-Type"),
|
||||||
Body: body,
|
Body: body,
|
||||||
@@ -441,6 +444,7 @@ func buildDeliveryTasks(
|
|||||||
TargetConfig: targets[i].Config,
|
TargetConfig: targets[i].Config,
|
||||||
MaxRetries: targets[i].MaxRetries,
|
MaxRetries: targets[i].MaxRetries,
|
||||||
Method: event.Method,
|
Method: event.Method,
|
||||||
|
RawQuery: event.RawQuery,
|
||||||
Headers: event.Headers,
|
Headers: event.Headers,
|
||||||
ContentType: event.ContentType,
|
ContentType: event.ContentType,
|
||||||
Body: bodyPtr,
|
Body: bodyPtr,
|
||||||
|
|||||||
@@ -0,0 +1,263 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSourceCreate shows the form to create a new webhook.
|
||||||
|
func (h *Handlers) HandleSourceCreate() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h.renderTemplate(
|
||||||
|
w, r, "sources_new.html",
|
||||||
|
newSourceFormData("", sourceFormInput{
|
||||||
|
RetentionDays: strconv.Itoa(
|
||||||
|
database.DefaultRetentionDays,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sourceFormInput carries the raw values of the new webhook form. A
|
||||||
|
// refused submission is shown again from it, so every value entered
|
||||||
|
// comes back, retention included.
|
||||||
|
type sourceFormInput struct {
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
RetentionDays string
|
||||||
|
// HTTPURL, when not empty, asks for an HTTP target with this
|
||||||
|
// destination.
|
||||||
|
HTTPURL string
|
||||||
|
// Archive asks for a database (archive) target, whose rows expire
|
||||||
|
// after ArchiveExpiry and whose files rotate by ArchiveRotation.
|
||||||
|
Archive bool
|
||||||
|
ArchiveExpiry string
|
||||||
|
ArchiveRotation string
|
||||||
|
}
|
||||||
|
|
||||||
|
// newSourceFormData builds the template data for the webhook creation
|
||||||
|
// form. It carries the retention default, which the form's help text
|
||||||
|
// names, from database.DefaultRetentionDays rather than a hardcoded
|
||||||
|
// copy of the same policy.
|
||||||
|
func newSourceFormData(
|
||||||
|
errMsg string, in sourceFormInput,
|
||||||
|
) map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
tmplKeyError: errMsg,
|
||||||
|
"Form": in,
|
||||||
|
"DefaultRetentionDays": database.DefaultRetentionDays,
|
||||||
|
tmplKeyArchiveExpiryChoices: archiveExpiryOptions(
|
||||||
|
in.ArchiveExpiry,
|
||||||
|
),
|
||||||
|
tmplKeyArchiveRotationChoices: archiveRotationOptions(
|
||||||
|
in.ArchiveRotation,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleSourceCreateSubmit handles the webhook creation form
|
||||||
|
// submission.
|
||||||
|
func (h *Handlers) HandleSourceCreateSubmit() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The body size cap is enforced by the MaxBodySize
|
||||||
|
// middleware, which runs before CSRF parses the form.
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusBadRequest)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
in := sourceFormInput{
|
||||||
|
Name: r.PostFormValue("name"),
|
||||||
|
Description: r.PostFormValue("description"),
|
||||||
|
RetentionDays: r.PostFormValue("retention_days"),
|
||||||
|
HTTPURL: r.PostFormValue("http_url"),
|
||||||
|
Archive: r.PostFormValue("archive") != "",
|
||||||
|
ArchiveExpiry: r.PostFormValue("archive_expiry"),
|
||||||
|
ArchiveRotation: r.PostFormValue("archive_rotation"),
|
||||||
|
}
|
||||||
|
|
||||||
|
refuse := func(errMsg string) {
|
||||||
|
h.renderTemplateStatus(
|
||||||
|
w, r, "sources_new.html",
|
||||||
|
newSourceFormData(errMsg, in),
|
||||||
|
http.StatusBadRequest,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if in.Name == "" {
|
||||||
|
refuse("Name is required")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
retentionDays, errMsg := parseRetentionDays(
|
||||||
|
in.RetentionDays, database.DefaultRetentionDays,
|
||||||
|
)
|
||||||
|
if errMsg != "" {
|
||||||
|
refuse(errMsg)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
targets, errMsg, err := h.newWebhookTargets(r.Context(), in)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to encode target config", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if errMsg != "" {
|
||||||
|
refuse(errMsg)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.createWebhookWithEntrypoint(w, r, &database.Webhook{
|
||||||
|
UserID: userID,
|
||||||
|
Name: in.Name,
|
||||||
|
Description: in.Description,
|
||||||
|
RetentionDays: retentionDays,
|
||||||
|
}, targets)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// newWebhookTargets validates the targets the new webhook form asks
|
||||||
|
// for and returns the rows to create with the webhook, or the message
|
||||||
|
// the form shows for the first one it refuses. A filled-in HTTP URL
|
||||||
|
// asks for an HTTP target named "HTTP", and the archive checkbox for a
|
||||||
|
// database target named "Archive". Each goes through newTarget, as on
|
||||||
|
// the webhook page's add target form. The rows have no WebhookID yet:
|
||||||
|
// the webhook has no ID until it is created.
|
||||||
|
func (h *Handlers) newWebhookTargets(
|
||||||
|
ctx context.Context,
|
||||||
|
in sourceFormInput,
|
||||||
|
) ([]*database.Target, string, error) {
|
||||||
|
var requested []targetFormInput
|
||||||
|
|
||||||
|
if in.HTTPURL != "" {
|
||||||
|
requested = append(requested, targetFormInput{
|
||||||
|
Name: "HTTP",
|
||||||
|
Type: database.TargetTypeHTTP,
|
||||||
|
URL: in.HTTPURL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if in.Archive {
|
||||||
|
requested = append(requested, targetFormInput{
|
||||||
|
Name: "Archive",
|
||||||
|
Type: database.TargetTypeDatabase,
|
||||||
|
Expiry: in.ArchiveExpiry,
|
||||||
|
Rotation: in.ArchiveRotation,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
targets := make([]*database.Target, 0, len(requested))
|
||||||
|
|
||||||
|
for _, form := range requested {
|
||||||
|
target, errMsg, err := h.newTarget(ctx, "", form)
|
||||||
|
if err != nil || errMsg != "" {
|
||||||
|
return nil, errMsg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
targets = append(targets, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
return targets, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// createWebhookWithEntrypoint creates a webhook, its default
|
||||||
|
// entrypoint and the given targets in a transaction.
|
||||||
|
func (h *Handlers) createWebhookWithEntrypoint(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook *database.Webhook,
|
||||||
|
targets []*database.Target,
|
||||||
|
) {
|
||||||
|
err := h.commitWebhook(webhook, targets)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to create webhook", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.dbMgr.CreateDB(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
h.log.Error(
|
||||||
|
"failed to create webhook event database",
|
||||||
|
"webhook_id", webhook.ID, "error", err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.log.Info("webhook created",
|
||||||
|
"webhook_id", webhook.ID,
|
||||||
|
"name", webhook.Name, "user_id", webhook.UserID,
|
||||||
|
)
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hook/"+webhook.ID, webhookCreated),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// commitWebhook creates a webhook, its default entrypoint and the
|
||||||
|
// given targets in a transaction. Returns an error on failure (rolls
|
||||||
|
// back).
|
||||||
|
func (h *Handlers) commitWebhook(
|
||||||
|
webhook *database.Webhook,
|
||||||
|
targets []*database.Target,
|
||||||
|
) error {
|
||||||
|
tx := h.db.DB().Begin()
|
||||||
|
if tx.Error != nil {
|
||||||
|
return tx.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tx.Create(webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entrypoint := &database.Entrypoint{
|
||||||
|
WebhookID: webhook.ID,
|
||||||
|
Path: uuid.New().String(),
|
||||||
|
Description: "Default entrypoint",
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Create(entrypoint).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, target := range targets {
|
||||||
|
target.WebhookID = webhook.ID
|
||||||
|
|
||||||
|
err = tx.Create(target).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit().Error
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSourceDelete handles webhook deletion.
|
||||||
|
func (h *Handlers) HandleSourceDelete() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.deleteWebhookResources(w, r, webhook, userID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The messages deleteWebhookResources logs when a file of the event
|
||||||
|
// database cannot be removed: the database file itself, or only a
|
||||||
|
// sidecar once the database file is gone.
|
||||||
|
const (
|
||||||
|
eventDBLeftMsg = "webhook deleted, but its event database file is " +
|
||||||
|
"still on disk; remove it by hand"
|
||||||
|
sidecarLeftMsg = "webhook deleted and its events are gone, but a " +
|
||||||
|
"-wal or -shm sidecar of its event database is " +
|
||||||
|
"still on disk; remove it by hand"
|
||||||
|
)
|
||||||
|
|
||||||
|
// deleteWebhookResources soft-deletes config and hard-deletes
|
||||||
|
// the per-webhook event database.
|
||||||
|
func (h *Handlers) deleteWebhookResources(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook database.Webhook,
|
||||||
|
userID string,
|
||||||
|
) {
|
||||||
|
// The configuration delete commits before the event database
|
||||||
|
// is touched. No transaction spans the main database and the
|
||||||
|
// filesystem, so one side has to go first: committing the
|
||||||
|
// configuration first means a later failure leaves an unused
|
||||||
|
// event database file on disk, while removing the event
|
||||||
|
// database first would mean a failed commit destroys the
|
||||||
|
// history of a webhook that still exists. A leftover file can
|
||||||
|
// be removed by hand; deleted history cannot be recovered.
|
||||||
|
err := h.commitWebhookDeletion(&webhook)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to delete webhook", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.log.Info(
|
||||||
|
"webhook deleted",
|
||||||
|
"webhook_id", webhook.ID,
|
||||||
|
"user_id", userID,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Release the delivery engine's per-webhook archiving state
|
||||||
|
// so a deleted webhook's archive writer (and any handle open
|
||||||
|
// within its debounce window) does not linger for the
|
||||||
|
// process lifetime. The archive file itself is deliberately
|
||||||
|
// left on disk; see evictArchiveWriter.
|
||||||
|
h.evictArchiveWriter(webhook.ID)
|
||||||
|
|
||||||
|
err = h.dbMgr.DeleteDB(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
// The configuration is committed, so the webhook is gone,
|
||||||
|
// but a file of its event database is still on disk with
|
||||||
|
// nothing referencing it. Report the failure rather than
|
||||||
|
// redirecting as though everything succeeded: the file
|
||||||
|
// needs removing by hand, and the logged error names it.
|
||||||
|
// When only a sidecar is left, the events are already
|
||||||
|
// gone, and the message must not suggest they survive.
|
||||||
|
msg := eventDBLeftMsg
|
||||||
|
if errors.Is(err, database.ErrSidecarNotRemoved) {
|
||||||
|
msg = sidecarLeftMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
h.serverError(w, r, msg, err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hooks", webhookDeleted), http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// commitWebhookDeletion soft-deletes a webhook's entrypoints,
|
||||||
|
// targets and the webhook row in one transaction. Every
|
||||||
|
// statement is checked and any failure rolls the whole
|
||||||
|
// transaction back, so a caller that gets an error knows the
|
||||||
|
// configuration is untouched and the event database must be
|
||||||
|
// left alone.
|
||||||
|
func (h *Handlers) commitWebhookDeletion(
|
||||||
|
webhook *database.Webhook,
|
||||||
|
) error {
|
||||||
|
tx := h.db.DB().Begin()
|
||||||
|
if tx.Error != nil {
|
||||||
|
return tx.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tx.Where(
|
||||||
|
"webhook_id = ?", webhook.ID,
|
||||||
|
).Delete(&database.Entrypoint{}).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Where(
|
||||||
|
"webhook_id = ?", webhook.ID,
|
||||||
|
).Delete(&database.Target{}).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Delete(webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// evictArchiveWriter asks the delivery engine to drop the cached
|
||||||
|
// archive writers of a webhook's database targets, closing their
|
||||||
|
// archive file handles.
|
||||||
|
//
|
||||||
|
// The archive database files are NOT deleted. Unlike the event
|
||||||
|
// database — which is per-webhook working storage and is
|
||||||
|
// hard-deleted with the webhook — an archive is explicitly
|
||||||
|
// long-term storage that an operator may want to keep or move
|
||||||
|
// away for offline retention. Destroying it as a side effect of
|
||||||
|
// deleting a webhook would be a surprising and unrecoverable
|
||||||
|
// data loss, so the file is left for the operator to handle.
|
||||||
|
func (h *Handlers) evictArchiveWriter(webhookID string) {
|
||||||
|
if h.archives == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.archives.EvictWebhook(webhookID)
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
"sneak.berlin/go/webhooker/internal/reqtls"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSourceDetail shows details for a specific webhook.
|
||||||
|
func (h *Handlers) HandleSourceDetail() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderSourceDetail(w, r, webhook, targetFormInput{}, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderSourceDetail loads and renders a source detail page. With a
|
||||||
|
// targetErr, it is the page shown again for a refused add target
|
||||||
|
// form: it answers 400, and the form opens on targetForm's type with
|
||||||
|
// its values and the message.
|
||||||
|
func (h *Handlers) renderSourceDetail(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook database.Webhook,
|
||||||
|
targetForm targetFormInput,
|
||||||
|
targetErr string,
|
||||||
|
) {
|
||||||
|
var entrypoints []database.Entrypoint
|
||||||
|
|
||||||
|
h.db.DB().Where(
|
||||||
|
"webhook_id = ?", webhook.ID,
|
||||||
|
).Find(&entrypoints)
|
||||||
|
|
||||||
|
var targets []database.Target
|
||||||
|
|
||||||
|
h.db.DB().Where(
|
||||||
|
"webhook_id = ?", webhook.ID,
|
||||||
|
).Find(&targets)
|
||||||
|
|
||||||
|
entrypointViews := NewEntrypointViews(entrypoints)
|
||||||
|
|
||||||
|
var events []RecentEventView
|
||||||
|
|
||||||
|
if h.dbMgr.DBExists(webhook.ID) {
|
||||||
|
webhookDB, err := h.dbMgr.GetDB(webhook.ID)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to get webhook database", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err = loadRecentEvents(
|
||||||
|
webhookDB, webhook.ID, singleHTTPTargetID(targets),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to load recent events", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = addEntrypointEvents(
|
||||||
|
webhookDB, &webhook, entrypointViews, time.Now(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to count entrypoint events", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scheme := "http"
|
||||||
|
if reqtls.IsTLS(r) {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
// The host is the client's Host header, unvalidated. It is
|
||||||
|
// inert only because source_detail.html renders BaseURL as
|
||||||
|
// text, inside a <code> element and in an entrypoint's delete
|
||||||
|
// prompt; putting it in an href or any other URL context
|
||||||
|
// needs it constrained first.
|
||||||
|
baseURL := scheme + "://" + r.Host
|
||||||
|
|
||||||
|
// The template calls Webhook methods, which take pointer
|
||||||
|
// receivers; html/template cannot address a value stored in a map.
|
||||||
|
data := map[string]any{
|
||||||
|
tmplKeyWebhook: &webhook,
|
||||||
|
// Targets are projected to a display-safe view: a
|
||||||
|
// target's stored config blob holds a credential, and it
|
||||||
|
// must never reach a template.
|
||||||
|
"Entrypoints": entrypointViews,
|
||||||
|
"Targets": h.targetRows(&webhook, targets),
|
||||||
|
"Events": events,
|
||||||
|
"BaseURL": baseURL,
|
||||||
|
"Stats": h.loadWebhookStats(webhook.ID, entrypoints, targets),
|
||||||
|
tmplKeyTargetForm: targetForm,
|
||||||
|
"TargetError": targetErr,
|
||||||
|
// The add target form's selects start on its expiry and
|
||||||
|
// rotation through Alpine, so no choice is selected here.
|
||||||
|
tmplKeyArchiveExpiryChoices: archiveExpiryChoices(),
|
||||||
|
tmplKeyArchiveRotationChoices: archiveRotationChoices(),
|
||||||
|
}
|
||||||
|
|
||||||
|
status := http.StatusOK
|
||||||
|
if targetErr != "" {
|
||||||
|
status = http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderTemplateStatus(w, r, "source_detail.html", data, status)
|
||||||
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
"sneak.berlin/go/webhooker/internal/delivery"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSourceEdit shows the form to edit a webhook.
|
||||||
|
func (h *Handlers) HandleSourceEdit() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderWebhookEdit(
|
||||||
|
w, r, &webhook,
|
||||||
|
webhook.Name, webhook.Description,
|
||||||
|
strconv.Itoa(webhook.RetentionDays),
|
||||||
|
"", http.StatusOK,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleSourceEditSubmit handles the webhook edit form
|
||||||
|
// submission.
|
||||||
|
func (h *Handlers) HandleSourceEditSubmit() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceID := chi.URLParam(r, "sourceID")
|
||||||
|
|
||||||
|
h.renameMu.Lock()
|
||||||
|
defer h.renameMu.Unlock()
|
||||||
|
|
||||||
|
var webhook database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"id = ? AND user_id = ?", sourceID, userID,
|
||||||
|
).First(&webhook).Error
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusNotFound)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The body size cap is enforced by the MaxBodySize
|
||||||
|
// middleware, which runs before CSRF parses the form.
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
h.renderError(w, r, http.StatusBadRequest)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.applyWebhookEdit(w, r, &webhook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyWebhookEdit validates and saves webhook edits. A refused save
|
||||||
|
// shows the edit form again with the values submitted and the reason.
|
||||||
|
func (h *Handlers) applyWebhookEdit(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook *database.Webhook,
|
||||||
|
) {
|
||||||
|
// The body size cap is enforced by the MaxBodySize middleware,
|
||||||
|
// which runs before CSRF parses the form.
|
||||||
|
name := r.PostFormValue("name")
|
||||||
|
description := r.PostFormValue("description")
|
||||||
|
retention := r.PostFormValue("retention_days")
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
h.renderWebhookEdit(
|
||||||
|
w, r, webhook, name, description, retention,
|
||||||
|
"Name is required", http.StatusBadRequest,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// An empty field falls back to the stored value, so submitting the
|
||||||
|
// form without touching retention leaves the policy alone.
|
||||||
|
retentionDays, errMsg := parseRetentionDays(
|
||||||
|
retention, webhook.RetentionDays,
|
||||||
|
)
|
||||||
|
if errMsg != "" {
|
||||||
|
h.renderWebhookEdit(
|
||||||
|
w, r, webhook, name, description, retention,
|
||||||
|
errMsg, http.StatusBadRequest,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// edited is the webhook as the submission leaves it; webhook stays
|
||||||
|
// as stored, for the page shown again when the save is refused.
|
||||||
|
edited := *webhook
|
||||||
|
edited.Name = name
|
||||||
|
edited.Description = description
|
||||||
|
edited.RetentionDays = retentionDays
|
||||||
|
|
||||||
|
// A new name renames the archive files before it is saved (see
|
||||||
|
// delivery.Engine.Rename). If either step fails, the same targets'
|
||||||
|
// archives go back to the name that is still stored, without
|
||||||
|
// reading the main database again.
|
||||||
|
targets, err := h.renameWebhookArchives(
|
||||||
|
webhook.ID, webhook.Name, edited.Name,
|
||||||
|
)
|
||||||
|
if err == nil {
|
||||||
|
err = h.db.DB().Save(&edited).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
restoreErr := h.renameArchives(targets, webhook.Name)
|
||||||
|
if restoreErr != nil {
|
||||||
|
h.log.Error(
|
||||||
|
"failed to rename archives back",
|
||||||
|
"webhook_id", webhook.ID,
|
||||||
|
"error", restoreErr,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, delivery.ErrArchiveNameTaken) {
|
||||||
|
h.renderWebhookEdit(
|
||||||
|
w, r, webhook, name, description, retention,
|
||||||
|
"Not saved: "+err.Error()+
|
||||||
|
". Move that archive out of the data directory, "+
|
||||||
|
"its .db together with any -wal and -shm beside "+
|
||||||
|
"it, then save again.",
|
||||||
|
http.StatusConflict,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.serverError(w, r, "failed to update webhook", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(
|
||||||
|
w, r, withNotice("/hook/"+webhook.ID, webhookSaved),
|
||||||
|
http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderWebhookEdit renders the webhook edit page for the webhook as
|
||||||
|
// stored, its form showing name, description and retentionDays, with
|
||||||
|
// an optional error message above it.
|
||||||
|
func (h *Handlers) renderWebhookEdit(
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
webhook *database.Webhook,
|
||||||
|
name, description, retentionDays, errMsg string,
|
||||||
|
status int,
|
||||||
|
) {
|
||||||
|
data := map[string]any{
|
||||||
|
tmplKeyWebhook: webhook,
|
||||||
|
tmplKeyError: errMsg,
|
||||||
|
"Name": name,
|
||||||
|
"Description": description,
|
||||||
|
"RetentionDays": retentionDays,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderTemplateStatus(w, r, "source_edit.html", data, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// renameWebhookArchives renames the archive file of every database
|
||||||
|
// target of a webhook from the webhook name oldName to newName,
|
||||||
|
// keeping each target's own name. It does nothing when the name is
|
||||||
|
// unchanged. It returns the targets it read, so that a failed edit can
|
||||||
|
// move those same archives back with renameArchives.
|
||||||
|
func (h *Handlers) renameWebhookArchives(
|
||||||
|
webhookID, oldName, newName string,
|
||||||
|
) ([]database.Target, error) {
|
||||||
|
if h.archives == nil || oldName == newName {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var targets []database.Target
|
||||||
|
|
||||||
|
err := h.db.DB().
|
||||||
|
Where(
|
||||||
|
"webhook_id = ? AND type = ?",
|
||||||
|
webhookID, database.TargetTypeDatabase,
|
||||||
|
).
|
||||||
|
Find(&targets).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return targets, h.renameArchives(targets, newName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// renameArchives renames the archive file of each of the given
|
||||||
|
// database targets to the webhook name webhookName, keeping each
|
||||||
|
// target's own name. It tries every target even after one fails, so
|
||||||
|
// that moving the archives back after a failed edit leaves none under
|
||||||
|
// the new name, and returns every failure joined.
|
||||||
|
func (h *Handlers) renameArchives(
|
||||||
|
targets []database.Target, webhookName string,
|
||||||
|
) error {
|
||||||
|
var errs []error
|
||||||
|
|
||||||
|
for i := range targets {
|
||||||
|
err := h.archives.Rename(
|
||||||
|
targets[i].ID, webhookName, targets[i].Name,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"sneak.berlin/go/webhooker/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WebhookListItem holds data for the webhook list view.
|
||||||
|
type WebhookListItem struct {
|
||||||
|
database.Webhook
|
||||||
|
|
||||||
|
EntrypointCount int
|
||||||
|
InactiveEntrypointCount int
|
||||||
|
TargetCount int
|
||||||
|
InactiveTargetCount int
|
||||||
|
|
||||||
|
// EventCount is how many events the webhook holds, LastEventAt
|
||||||
|
// when the newest arrived (nil before the first), and
|
||||||
|
// FailedLast24Hours how many of its deliveries failed in the last
|
||||||
|
// 24 hours. When the webhook's event database could not be read,
|
||||||
|
// EventsUnreadable is set and these three are not known.
|
||||||
|
EventCount int64
|
||||||
|
LastEventAt *time.Time
|
||||||
|
FailedLast24Hours int64
|
||||||
|
EventsUnreadable bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleSourceList shows a list of user's webhooks.
|
||||||
|
func (h *Handlers) HandleSourceList() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, ok := h.getUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(
|
||||||
|
w, r, "/pages/login", http.StatusSeeOther,
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhooks []database.Webhook
|
||||||
|
|
||||||
|
err := h.db.DB().Where(
|
||||||
|
"user_id = ?", userID,
|
||||||
|
).Order("created_at DESC").Find(&webhooks).Error
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to list webhooks", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := h.buildWebhookListItems(webhooks)
|
||||||
|
if err != nil {
|
||||||
|
h.serverError(w, r, "failed to list webhooks", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]any{
|
||||||
|
"Webhooks": items,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.renderTemplate(w, r, "sources_list.html", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildWebhookListItems builds the list's entry for each webhook. It
|
||||||
|
// fails when the main database cannot be read. A webhook whose event
|
||||||
|
// database cannot be read is marked on its own entry, and the error is
|
||||||
|
// logged.
|
||||||
|
func (h *Handlers) buildWebhookListItems(
|
||||||
|
webhooks []database.Webhook,
|
||||||
|
) ([]WebhookListItem, error) {
|
||||||
|
items := make([]WebhookListItem, len(webhooks))
|
||||||
|
since := time.Now().Add(-longWindow)
|
||||||
|
|
||||||
|
for i := range webhooks {
|
||||||
|
item := &items[i]
|
||||||
|
item.Webhook = webhooks[i]
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
item.EntrypointCount, item.InactiveEntrypointCount, err =
|
||||||
|
h.countWithInactive(&database.Entrypoint{}, item.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
item.TargetCount, item.InactiveTargetCount, err =
|
||||||
|
h.countWithInactive(&database.Target{}, item.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Opening an event database that does not exist would create
|
||||||
|
// it, and it would hold nothing to count.
|
||||||
|
if !h.dbMgr.DBExists(item.ID) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.readListEventFigures(item, since)
|
||||||
|
if err != nil {
|
||||||
|
h.log.Error(
|
||||||
|
"failed to read webhook list figures",
|
||||||
|
"webhook_id", item.ID,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
item.EventsUnreadable = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// countWithInactive returns how many entrypoints or targets, as model
|
||||||
|
// says, a webhook has, and how many of them are inactive.
|
||||||
|
func (h *Handlers) countWithInactive(
|
||||||
|
model any, webhookID string,
|
||||||
|
) (int, int, error) {
|
||||||
|
var active []bool
|
||||||
|
|
||||||
|
err := h.db.DB().Model(model).
|
||||||
|
Where("webhook_id = ?", webhookID).
|
||||||
|
Pluck("active", &active).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf(
|
||||||
|
"reading active flags of webhook %s: %w", webhookID, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
inactive := 0
|
||||||
|
|
||||||
|
for _, a := range active {
|
||||||
|
if !a {
|
||||||
|
inactive++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(active), inactive, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readListEventFigures fills in the figures the list shows from the
|
||||||
|
// webhook's event database, with the statistics pane's own queries:
|
||||||
|
// the event count and last arrival from the event totals row, and the
|
||||||
|
// deliveries that failed since the given time from the deliveries'
|
||||||
|
// status index.
|
||||||
|
func (h *Handlers) readListEventFigures(
|
||||||
|
item *WebhookListItem, since time.Time,
|
||||||
|
) error {
|
||||||
|
webhookDB, err := h.dbMgr.GetDB(item.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var totals database.EventTotals
|
||||||
|
|
||||||
|
err = webhookDB.Take(&totals).Error
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("reading event totals: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
item.EventCount = totals.Events - totals.EventsRemoved
|
||||||
|
item.LastEventAt = totals.LastEventAt
|
||||||
|
|
||||||
|
byTarget, err := finishedByTarget(webhookDB, since)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range byTarget {
|
||||||
|
item.FailedLast24Hours += f.Failed
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -131,8 +131,9 @@ const (
|
|||||||
//
|
//
|
||||||
// - Lines carrying an AUTHENTICATED operator's own input, which
|
// - Lines carrying an AUTHENTICATED operator's own input, which
|
||||||
// are not truncated at all: the webhook name on "webhook
|
// are not truncated at all: the webhook name on "webhook
|
||||||
// created" and the target host on "target URL blocked by SSRF
|
// created" (internal/handlers/webhook_create.go) and the target
|
||||||
// protection" (both internal/handlers/source_management.go),
|
// host on "target URL blocked by SSRF protection"
|
||||||
|
// (internal/handlers/target_create.go),
|
||||||
// and target_name in internal/delivery/engine.go and
|
// and target_name in internal/delivery/engine.go and
|
||||||
// target_http.go. Each is bounded only by the 1 MB form body
|
// target_http.go. Each is bounded only by the 1 MB form body
|
||||||
// cap, so a 100 KB name writes one line of roughly 600 KB.
|
// cap, so a 100 KB name writes one line of roughly 600 KB.
|
||||||
|
|||||||
@@ -98,20 +98,25 @@ func TestAlpineRunsUnderTheSecurityPolicy(t *testing.T) {
|
|||||||
checkRefusedNewWebhook(ctx, t, srv.URL+"/hooks/new")
|
checkRefusedNewWebhook(ctx, t, srv.URL+"/hooks/new")
|
||||||
checkEventLog(ctx, t, page+"/events", event.ID, older.ID, target.Name)
|
checkEventLog(ctx, t, page+"/events", event.ID, older.ID, target.Name)
|
||||||
checkMobileMenu(ctx, t, page)
|
checkMobileMenu(ctx, t, page)
|
||||||
|
checkPhoneWidth(ctx, t, page, page+"/events", target.Name)
|
||||||
|
|
||||||
assert.Empty(t, problems(), "the browser reported problems")
|
assert.Empty(t, problems(), "the browser reported problems")
|
||||||
}
|
}
|
||||||
|
|
||||||
// seedBrowserWebhook seeds the webhook the browser test loads, owned by
|
// seedBrowserWebhook seeds the webhook the browser test loads, owned by
|
||||||
// userID: an entrypoint, two events, and a target whose delivery of the
|
// userID: an entrypoint, two events, and a target whose delivery of the
|
||||||
// newer event failed once with a 502. It returns the webhook, the older
|
// newer event failed once with a 502. The webhook's name and the newer
|
||||||
// and the newer event, and the target.
|
// event's content type are each too long for one line on a phone. It
|
||||||
|
// returns the webhook, the older and the newer event, and the target.
|
||||||
func seedBrowserWebhook(
|
func seedBrowserWebhook(
|
||||||
t *testing.T, env *testEnv, userID string,
|
t *testing.T, env *testEnv, userID string,
|
||||||
) (*database.Webhook, *database.Event, *database.Event, *database.Target) {
|
) (*database.Webhook, *database.Event, *database.Event, *database.Target) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
webhook := env.seedWebhook(t, userID)
|
webhook := env.seedWebhook(t, userID)
|
||||||
|
require.NoError(t, env.db.DB().Model(webhook).Update(
|
||||||
|
"name", "payment_provider_production_notifications",
|
||||||
|
).Error)
|
||||||
require.NoError(t, env.db.DB().Omit(clause.Associations).Create(
|
require.NoError(t, env.db.DB().Omit(clause.Associations).Create(
|
||||||
&database.Entrypoint{
|
&database.Entrypoint{
|
||||||
WebhookID: webhook.ID,
|
WebhookID: webhook.ID,
|
||||||
@@ -126,6 +131,9 @@ func seedBrowserWebhook(
|
|||||||
|
|
||||||
webhookDB, err := env.dbMgr.GetDB(webhook.ID)
|
webhookDB, err := env.dbMgr.GetDB(webhook.ID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, webhookDB.Model(event).Update(
|
||||||
|
"content_type", "application/vnd.paymentprovider.event+json",
|
||||||
|
).Error)
|
||||||
require.NoError(t, webhookDB.Omit(clause.Associations).Create(
|
require.NoError(t, webhookDB.Omit(clause.Associations).Create(
|
||||||
&database.DeliveryResult{
|
&database.DeliveryResult{
|
||||||
DeliveryID: dlv.ID,
|
DeliveryID: dlv.ID,
|
||||||
@@ -499,9 +507,10 @@ func checkRefusedTarget(ctx context.Context, t *testing.T, url string) {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
refusedURL = "http://127.0.0.1/hook"
|
refusedURL = "http://127.0.0.1/hook"
|
||||||
urlField = `form[action$="/targets"] input[name="url"]`
|
urlField = `form[action$="/targets"] input[name="url"]`
|
||||||
reason = `//div[@class="alert-error"]`
|
forwardQuery = `form[action$="/targets"] input[name="forward_query"]`
|
||||||
|
reason = `//div[@class="alert-error"]`
|
||||||
)
|
)
|
||||||
|
|
||||||
require.NoError(t, chromedp.Run(ctx, loadPage(url)))
|
require.NoError(t, chromedp.Run(ctx, loadPage(url)))
|
||||||
@@ -511,6 +520,7 @@ func checkRefusedTarget(ctx context.Context, t *testing.T, url string) {
|
|||||||
ctx,
|
ctx,
|
||||||
chromedp.SetValue(targetName, "refused", chromedp.ByQuery),
|
chromedp.SetValue(targetName, "refused", chromedp.ByQuery),
|
||||||
chromedp.SetValue(urlField, refusedURL, chromedp.ByQuery),
|
chromedp.SetValue(urlField, refusedURL, chromedp.ByQuery),
|
||||||
|
chromedp.Click(forwardQuery, chromedp.ByQuery),
|
||||||
))
|
))
|
||||||
|
|
||||||
click(ctx, t, saveButton)
|
click(ctx, t, saveButton)
|
||||||
@@ -518,18 +528,26 @@ func checkRefusedTarget(ctx context.Context, t *testing.T, url string) {
|
|||||||
assert.True(t, shown(ctx, reason),
|
assert.True(t, shown(ctx, reason),
|
||||||
"a refused target does not show the reason")
|
"a refused target does not show the reason")
|
||||||
|
|
||||||
var name, typed string
|
var (
|
||||||
|
name, typed string
|
||||||
|
checked bool
|
||||||
|
)
|
||||||
|
|
||||||
require.NoError(t, chromedp.Run(
|
require.NoError(t, chromedp.Run(
|
||||||
ctx,
|
ctx,
|
||||||
chromedp.Value(targetName, &name, chromedp.ByQuery),
|
chromedp.Value(targetName, &name, chromedp.ByQuery),
|
||||||
chromedp.Value(urlField, &typed, chromedp.ByQuery),
|
chromedp.Value(urlField, &typed, chromedp.ByQuery),
|
||||||
|
chromedp.JavascriptAttribute(
|
||||||
|
forwardQuery, "checked", &checked, chromedp.ByQuery,
|
||||||
|
),
|
||||||
))
|
))
|
||||||
|
|
||||||
assert.Equal(t, "refused", name,
|
assert.Equal(t, "refused", name,
|
||||||
"a refused target does not keep the name entered")
|
"a refused target does not keep the name entered")
|
||||||
assert.Equal(t, refusedURL, typed,
|
assert.Equal(t, refusedURL, typed,
|
||||||
"a refused target does not keep the url entered")
|
"a refused target does not keep the url entered")
|
||||||
|
assert.True(t, checked,
|
||||||
|
"a refused target does not keep the query string setting checked")
|
||||||
assert.True(t, shown(ctx, targetName),
|
assert.True(t, shown(ctx, targetName),
|
||||||
"a refused target does not come back with the form open")
|
"a refused target does not come back with the form open")
|
||||||
assert.True(t, hidden(ctx, typeSelect),
|
assert.True(t, hidden(ctx, typeSelect),
|
||||||
@@ -545,10 +563,15 @@ func checkRefusedTarget(ctx context.Context, t *testing.T, url string) {
|
|||||||
ctx,
|
ctx,
|
||||||
chromedp.Value(targetName, &name, chromedp.ByQuery),
|
chromedp.Value(targetName, &name, chromedp.ByQuery),
|
||||||
chromedp.Value(urlField, &typed, chromedp.ByQuery),
|
chromedp.Value(urlField, &typed, chromedp.ByQuery),
|
||||||
|
chromedp.JavascriptAttribute(
|
||||||
|
forwardQuery, "checked", &checked, chromedp.ByQuery,
|
||||||
|
),
|
||||||
))
|
))
|
||||||
|
|
||||||
assert.Empty(t, name, "after Cancel, the next Add keeps the name entered")
|
assert.Empty(t, name, "after Cancel, the next Add keeps the name entered")
|
||||||
assert.Empty(t, typed, "after Cancel, the next Add keeps the url entered")
|
assert.Empty(t, typed, "after Cancel, the next Add keeps the url entered")
|
||||||
|
assert.False(t, checked,
|
||||||
|
"after Cancel, the next Add keeps the query string setting checked")
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkTargetDeliveries loads a webhook page and checks that the row of
|
// checkTargetDeliveries loads a webhook page and checks that the row of
|
||||||
@@ -1292,3 +1315,60 @@ func checkMobileMenu(ctx context.Context, t *testing.T, url string) {
|
|||||||
click(ctx, t, button)
|
click(ctx, t, button)
|
||||||
assert.True(t, hidden(ctx, menu), "the menu button does not close the menu")
|
assert.True(t, hidden(ctx, menu), "the menu button does not close the menu")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scrollsSideways reports whether the page is wider than the window. A
|
||||||
|
// page's clientWidth is the window's width less its scroll bar.
|
||||||
|
const scrollsSideways = `document.documentElement.scrollWidth >
|
||||||
|
document.documentElement.clientWidth`
|
||||||
|
|
||||||
|
// cutOffElements lists each element, without elements inside it, that
|
||||||
|
// is shown but runs past the page's edge or its card's, by more than a
|
||||||
|
// pixel of rounding. A card hides what runs past its edge.
|
||||||
|
const cutOffElements = `[...document.querySelectorAll("body *")]
|
||||||
|
.filter((el) => {
|
||||||
|
const box = el.getBoundingClientRect();
|
||||||
|
const card = el.closest(".card")?.getBoundingClientRect();
|
||||||
|
const left = card ? card.left : 0;
|
||||||
|
const right = card ? card.right : document.documentElement.clientWidth;
|
||||||
|
return el.children.length === 0 && box.width > 0 &&
|
||||||
|
(box.left < left - 1 || box.right > right + 1);
|
||||||
|
})
|
||||||
|
.map((el) => el.outerHTML.slice(0, 120))`
|
||||||
|
|
||||||
|
// checkPhoneWidth loads the webhook page, url, and its event log,
|
||||||
|
// eventLog, in a phone-sized window, the event log with the attempts of
|
||||||
|
// the newest event's delivery to targetName shown. It checks that
|
||||||
|
// neither page scrolls sideways and that nothing shown on either, no
|
||||||
|
// status, time or control, is cut off at the page's or its card's edge.
|
||||||
|
func checkPhoneWidth(
|
||||||
|
ctx context.Context, t *testing.T, url, eventLog, targetName string,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var (
|
||||||
|
sideways bool
|
||||||
|
cutOff []string
|
||||||
|
)
|
||||||
|
|
||||||
|
measure := chromedp.Tasks{
|
||||||
|
chromedp.Evaluate(scrollsSideways, &sideways),
|
||||||
|
chromedp.Evaluate(cutOffElements, &cutOff),
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, chromedp.Run(
|
||||||
|
ctx,
|
||||||
|
chromedp.EmulateViewport(phoneWidth, phoneHeight),
|
||||||
|
loadPage(url),
|
||||||
|
measure,
|
||||||
|
))
|
||||||
|
assert.False(t, sideways, "the webhook page scrolls sideways on a phone")
|
||||||
|
assert.Empty(t, cutOff, "the webhook page cuts these off on a phone")
|
||||||
|
|
||||||
|
require.NoError(t, chromedp.Run(ctx, loadPage(eventLog)))
|
||||||
|
click(ctx, t, `//span[text()="`+targetName+`"]`)
|
||||||
|
require.True(t, shown(ctx, `//span[text()="Attempt 1"]`),
|
||||||
|
"clicking the delivery does not show its attempts")
|
||||||
|
require.NoError(t, chromedp.Run(ctx, measure))
|
||||||
|
assert.False(t, sideways, "the event log scrolls sideways on a phone")
|
||||||
|
assert.Empty(t, cutOff, "the event log cuts these off on a phone")
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -3,5 +3,6 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "10.11.0",
|
"eslint": "10.11.0",
|
||||||
"prettier": "3.9.9"
|
"prettier": "3.9.9"
|
||||||
}
|
},
|
||||||
|
"packageManager": "yarn@4.18.1+sha512.b2e1e7524f654f2749d32b4ebcb4622473cb5bcbc485df2007e12a154e50162a4d795526768bc5f5b8f81717bfd79deb2472813d86fb5ae2eb551fa9c872b08f"
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -138,6 +138,7 @@ document.addEventListener("alpine:init", function () {
|
|||||||
url: "",
|
url: "",
|
||||||
headers: "",
|
headers: "",
|
||||||
timeout: "",
|
timeout: "",
|
||||||
|
forwardQuery: false,
|
||||||
maxRetries: "",
|
maxRetries: "",
|
||||||
expiry: "",
|
expiry: "",
|
||||||
rotation: "",
|
rotation: "",
|
||||||
@@ -150,6 +151,8 @@ document.addEventListener("alpine:init", function () {
|
|||||||
this.url = refused.destination;
|
this.url = refused.destination;
|
||||||
this.headers = refused.headers;
|
this.headers = refused.headers;
|
||||||
this.timeout = refused.timeout;
|
this.timeout = refused.timeout;
|
||||||
|
this.forwardQuery =
|
||||||
|
this.$root.hasAttribute("data-forward-query");
|
||||||
this.maxRetries = refused.maxRetries;
|
this.maxRetries = refused.maxRetries;
|
||||||
this.expiry = refused.expiry;
|
this.expiry = refused.expiry;
|
||||||
this.rotation = refused.rotation;
|
this.rotation = refused.rotation;
|
||||||
@@ -169,6 +172,7 @@ document.addEventListener("alpine:init", function () {
|
|||||||
this.url = "";
|
this.url = "";
|
||||||
this.headers = "";
|
this.headers = "";
|
||||||
this.timeout = "";
|
this.timeout = "";
|
||||||
|
this.forwardQuery = false;
|
||||||
this.maxRetries = "";
|
this.maxRetries = "";
|
||||||
this.expiry = "";
|
this.expiry = "";
|
||||||
this.rotation = "";
|
this.rotation = "";
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
an event's own page. Spans only, since a button may hold no div. -->
|
an event's own page. Spans only, since a button may hold no div. -->
|
||||||
<span class="flex flex-1 flex-wrap items-center justify-between gap-3">
|
<span class="flex flex-1 flex-wrap items-center justify-between gap-3">
|
||||||
<span class="flex flex-wrap items-center gap-3">
|
<span class="flex flex-wrap items-center gap-3">
|
||||||
<span class="text-sm text-gray-700">{{.Target.DisplayName}}</span>
|
<span class="text-sm text-gray-700 wrap-anywhere">{{.Target.DisplayName}}</span>
|
||||||
{{if .Replay}}<span class="text-xs text-gray-500">replay</span>{{end}}
|
{{if .Replay}}<span class="text-xs text-gray-500">replay</span>{{end}}
|
||||||
<span class="text-xs {{if eq .Status "delivered"}}text-green-600{{else if eq .Status "failed"}}text-red-600{{else if eq .Status "retrying"}}text-yellow-600{{else}}text-gray-400{{end}}">{{with .Paused}}waiting: target paused after repeated failures, next try no earlier than {{.Until}} ({{.Relative}}){{else}}{{.Status}}{{end}}</span>
|
<span class="text-xs {{if eq .Status "delivered"}}text-green-600{{else if eq .Status "failed"}}text-red-600{{else if eq .Status "retrying"}}text-yellow-600{{else}}text-gray-400{{end}}">{{with .Paused}}waiting: target paused after repeated failures, next try no earlier than {{.Until}} ({{.Relative}}){{else}}{{.Status}}{{end}}</span>
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
{{define "event_request"}}
|
{{define "event_request"}}
|
||||||
<!-- The entrypoint an event arrived at and its request headers, as
|
<!-- The entrypoint an event arrived at, its query string and its
|
||||||
handlers.EventLogView carries them: the same in the event log and
|
request headers, as handlers.EventLogView carries them: the same in
|
||||||
the event's own page. The entrypoint's URL is never shown. A
|
the event log and the event's own page. The entrypoint's URL is
|
||||||
resubmitted copy, even a copy of a copy, did not arrive at an
|
never shown. A resubmitted copy, even a copy of a copy, did not
|
||||||
entrypoint; the request it copies did. -->
|
arrive at an entrypoint; the request it copies did. -->
|
||||||
<div class="space-y-2 text-xs">
|
<div class="space-y-2 text-xs">
|
||||||
{{if .ResubmittedFrom}}
|
{{if .ResubmittedFrom}}
|
||||||
<p class="text-gray-500">The request it copies arrived at <span class="text-gray-900">{{.Entrypoint}}</span></p>
|
<p class="text-gray-500">The request it copies arrived at <span class="text-gray-900 wrap-anywhere">{{.Entrypoint}}</span></p>
|
||||||
{{else}}
|
{{else}}
|
||||||
<p class="text-gray-500">Arrived at <span class="text-gray-900">{{.Entrypoint}}</span></p>
|
<p class="text-gray-500">Arrived at <span class="text-gray-900 wrap-anywhere">{{.Entrypoint}}</span></p>
|
||||||
|
{{end}}
|
||||||
|
{{if .RawQueryCut}}
|
||||||
|
<p class="text-gray-500">The query string is larger than the event log shows. <a href="{{.Body.EventURL}}" class="btn-small">Show the query string</a></p>
|
||||||
|
{{else if .RawQuery}}
|
||||||
|
<p class="text-gray-500">Query string</p>
|
||||||
|
<pre class="rounded-md border border-gray-200 bg-white p-2 text-xs text-gray-700 overflow-x-auto whitespace-pre-wrap break-all">{{.RawQuery}}</pre>
|
||||||
|
{{else}}
|
||||||
|
<p class="text-gray-500">No query string.</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if .HeadersCut}}
|
{{if .HeadersCut}}
|
||||||
<p class="text-gray-500">The request headers are larger than the event log shows. <a href="{{.Body.EventURL}}" class="btn-small">Show the request headers</a></p>
|
<p class="text-gray-500">The request headers are larger than the event log shows. <a href="{{.Body.EventURL}}" class="btn-small">Show the request headers</a></p>
|
||||||
|
|||||||
@@ -6,15 +6,18 @@
|
|||||||
<!-- 108rem, half again the 72rem (max-w-6xl) of the webhook list, the
|
<!-- 108rem, half again the 72rem (max-w-6xl) of the webhook list, the
|
||||||
event log, the navbar and the footer, so an entrypoint URL fits on
|
event log, the navbar and the footer, so an entrypoint URL fits on
|
||||||
one line. An inline style, because the committed tailwind.css has
|
one line. An inline style, because the committed tailwind.css has
|
||||||
no class this wide. -->
|
no class this wide. wrap-anywhere goes only on names and
|
||||||
|
descriptions: a row too wide for a phone must still run past the
|
||||||
|
edge, where the browser test sees it, rather than break its
|
||||||
|
controls mid-word. -->
|
||||||
<div class="mx-auto px-6 py-8" style="max-width: 108rem">
|
<div class="mx-auto px-6 py-8" style="max-width: 108rem">
|
||||||
<div class="mb-6">
|
<div class="mb-6">
|
||||||
<a href="/hooks" class="btn-small">← Back to webhooks</a>
|
<a href="/hooks" class="btn-small">← Back to webhooks</a>
|
||||||
<div class="flex flex-wrap justify-between items-center gap-2 mt-2">
|
<div class="flex flex-wrap justify-between items-center gap-2 mt-2">
|
||||||
<div>
|
<div>
|
||||||
<h1 class="text-2xl font-medium text-gray-900">{{.Webhook.Name}}</h1>
|
<h1 class="text-2xl font-medium text-gray-900 wrap-anywhere">{{.Webhook.Name}}</h1>
|
||||||
{{if .Webhook.Description}}
|
{{if .Webhook.Description}}
|
||||||
<p class="text-sm text-gray-500 mt-1">{{.Webhook.Description}}</p>
|
<p class="text-sm text-gray-500 mt-1 wrap-anywhere">{{.Webhook.Description}}</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
@@ -60,7 +63,7 @@
|
|||||||
{{range .Entrypoints}}
|
{{range .Entrypoints}}
|
||||||
<div class="p-4" x-data="collapsible">
|
<div class="p-4" x-data="collapsible">
|
||||||
<div class="flex flex-wrap items-center justify-between gap-2 mb-1">
|
<div class="flex flex-wrap items-center justify-between gap-2 mb-1">
|
||||||
<span x-show="closed" class="text-sm font-medium text-gray-900">{{if .Description}}{{.Description}}{{else}}Entrypoint{{end}}</span>
|
<span x-show="closed" class="text-sm font-medium text-gray-900 wrap-anywhere">{{if .Description}}{{.Description}}{{else}}Entrypoint{{end}}</span>
|
||||||
<!-- Edit shows this form in place of the
|
<!-- Edit shows this form in place of the
|
||||||
description and hides until it closes, and
|
description and hides until it closes, and
|
||||||
Cancel resets what was typed. With
|
Cancel resets what was typed. With
|
||||||
@@ -132,6 +135,7 @@
|
|||||||
data-destination="{{.TargetForm.URL}}"
|
data-destination="{{.TargetForm.URL}}"
|
||||||
data-headers="{{.TargetForm.Headers}}"
|
data-headers="{{.TargetForm.Headers}}"
|
||||||
data-timeout="{{.TargetForm.Timeout}}"
|
data-timeout="{{.TargetForm.Timeout}}"
|
||||||
|
{{if .TargetForm.ForwardQuery}}data-forward-query{{end}}
|
||||||
data-max-retries="{{.TargetForm.MaxRetries}}"
|
data-max-retries="{{.TargetForm.MaxRetries}}"
|
||||||
data-expiry="{{.TargetForm.Expiry}}"
|
data-expiry="{{.TargetForm.Expiry}}"
|
||||||
data-rotation="{{.TargetForm.Rotation}}">
|
data-rotation="{{.TargetForm.Rotation}}">
|
||||||
@@ -180,6 +184,13 @@
|
|||||||
<label class="text-sm text-gray-700">Timeout (seconds, blank = default):</label>
|
<label class="text-sm text-gray-700">Timeout (seconds, blank = default):</label>
|
||||||
<input type="number" name="timeout" :value="timeout" min="0" max="300" class="input text-sm w-24">
|
<input type="number" name="timeout" :value="timeout" min="0" max="300" class="input text-sm w-24">
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="flex items-center gap-2 text-sm text-gray-700">
|
||||||
|
<input type="checkbox" name="forward_query" value="on" :checked="forwardQuery" class="h-4 w-4">
|
||||||
|
Pass the query string on to this target
|
||||||
|
</label>
|
||||||
|
<p class="text-xs text-gray-500 mt-1">Appends the query string each event arrived with to the URL above, after any query string the URL already has.</p>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<div class="flex gap-2 items-center">
|
<div class="flex gap-2 items-center">
|
||||||
<label class="text-sm text-gray-700">Delivery attempts:</label>
|
<label class="text-sm text-gray-700">Delivery attempts:</label>
|
||||||
@@ -249,7 +260,7 @@
|
|||||||
{{range .Targets}}
|
{{range .Targets}}
|
||||||
<div class="p-4">
|
<div class="p-4">
|
||||||
<div class="flex flex-wrap items-center justify-between gap-2 mb-1">
|
<div class="flex flex-wrap items-center justify-between gap-2 mb-1">
|
||||||
<span class="text-sm font-medium text-gray-900">{{.Name}}</span>
|
<span class="text-sm font-medium text-gray-900 wrap-anywhere">{{.Name}}</span>
|
||||||
<div class="flex flex-wrap items-center gap-2">
|
<div class="flex flex-wrap items-center gap-2">
|
||||||
<span class="badge-info">{{if eq .Type "database"}}archive{{else}}{{.Type}}{{end}}</span>
|
<span class="badge-info">{{if eq .Type "database"}}archive{{else}}{{.Type}}{{end}}</span>
|
||||||
{{if .Active}}
|
{{if .Active}}
|
||||||
|
|||||||
@@ -3,10 +3,15 @@
|
|||||||
{{define "title"}}Full Event Log - {{.Webhook.Name}} - Webhooker{{end}}
|
{{define "title"}}Full Event Log - {{.Webhook.Name}} - Webhooker{{end}}
|
||||||
|
|
||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
|
<!-- wrap-anywhere goes only on names, IDs and content types: a row too
|
||||||
|
wide for a phone must still run past the edge, where the browser
|
||||||
|
test sees it, rather than break its statuses, times or controls
|
||||||
|
mid-word. So a target's name in an event's row, which shares its
|
||||||
|
element with the delivery's status, goes without. -->
|
||||||
<div class="max-w-6xl mx-auto px-6 py-8">
|
<div class="max-w-6xl mx-auto px-6 py-8">
|
||||||
<div class="mb-6">
|
<div class="mb-6">
|
||||||
<a href="/hook/{{.Webhook.ID}}" class="btn-small">← Back to {{.Webhook.Name}}</a>
|
<a href="/hook/{{.Webhook.ID}}" class="btn-small wrap-anywhere">← Back to {{.Webhook.Name}}</a>
|
||||||
<div class="flex justify-between items-center mt-2">
|
<div class="flex flex-wrap justify-between items-center gap-2 mt-2">
|
||||||
<h1 class="text-2xl font-medium text-gray-900">Full Event Log</h1>
|
<h1 class="text-2xl font-medium text-gray-900">Full Event Log</h1>
|
||||||
<!-- Under a filter, this counts the events the filter lists. -->
|
<!-- Under a filter, this counts the events the filter lists. -->
|
||||||
<span class="text-sm text-gray-500">{{if gt .TotalEvents (len .Events)}}{{len .Events}} most recent of {{.TotalEvents}} events{{else}}{{.TotalEvents}}{{if not .Show}} total{{end}} event{{if ne .TotalEvents 1}}s{{end}}{{end}}{{if eq .Show "failed"}} with a failed delivery{{else if eq .Show "pending"}} with a delivery pending or retrying{{end}}</span>
|
<span class="text-sm text-gray-500">{{if gt .TotalEvents (len .Events)}}{{len .Events}} most recent of {{.TotalEvents}} events{{else}}{{.TotalEvents}}{{if not .Show}} total{{end}} event{{if ne .TotalEvents 1}}s{{end}}{{end}}{{if eq .Show "failed"}} with a failed delivery{{else if eq .Show "pending"}} with a delivery pending or retrying{{end}}</span>
|
||||||
@@ -28,8 +33,8 @@
|
|||||||
<div role="button" tabindex="0" class="btn-small w-full flex flex-wrap justify-between gap-2" :aria-expanded="open" @mousedown="cancelPendingToggle" @click="toggleUnlessSelecting" @keydown.enter.prevent="toggle" @keydown.space.prevent="toggle">
|
<div role="button" tabindex="0" class="btn-small w-full flex flex-wrap justify-between gap-2" :aria-expanded="open" @mousedown="cancelPendingToggle" @click="toggleUnlessSelecting" @keydown.enter.prevent="toggle" @keydown.space.prevent="toggle">
|
||||||
<span class="flex flex-wrap items-center gap-3">
|
<span class="flex flex-wrap items-center gap-3">
|
||||||
<span class="badge-info">{{.Method}}</span>
|
<span class="badge-info">{{.Method}}</span>
|
||||||
<span class="text-sm font-mono text-gray-700">{{.ID}}</span>
|
<span class="text-sm font-mono text-gray-700 wrap-anywhere">{{.ID}}</span>
|
||||||
<span class="text-sm text-gray-500">{{.ContentType}}</span>
|
<span class="text-sm text-gray-500 wrap-anywhere">{{.ContentType}}</span>
|
||||||
{{if .ResubmittedFrom}}
|
{{if .ResubmittedFrom}}
|
||||||
<span class="text-xs text-gray-500" title="This event is a copy of {{.ResubmittedFromID}}">resubmitted copy</span>
|
<span class="text-xs text-gray-500" title="This event is a copy of {{.ResubmittedFromID}}">resubmitted copy</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
@@ -54,7 +59,7 @@
|
|||||||
<div x-show="open" x-cloak class="mt-3 p-3 bg-gray-50 rounded-md">
|
<div x-show="open" x-cloak class="mt-3 p-3 bg-gray-50 rounded-md">
|
||||||
<div class="mb-3 flex flex-wrap items-center justify-between gap-2">
|
<div class="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||||||
<div class="text-xs text-gray-500">
|
<div class="text-xs text-gray-500">
|
||||||
{{if .ResubmittedFrom}}Resubmitted from event <a href="/hook/{{$.Webhook.ID}}/events/{{.ResubmittedFromID}}" class="btn-small font-mono">{{.ResubmittedFromID}}</a>.{{end}}
|
{{if .ResubmittedFrom}}Resubmitted from event <a href="/hook/{{$.Webhook.ID}}/events/{{.ResubmittedFromID}}" class="btn-small font-mono wrap-anywhere">{{.ResubmittedFromID}}</a>.{{end}}
|
||||||
{{if .ResubmitCount}}Resubmitted as {{.ResubmitCount}} new event{{if ne .ResubmitCount 1}}s{{end}}.{{end}}
|
{{if .ResubmitCount}}Resubmitted as {{.ResubmitCount}} new event{{if ne .ResubmitCount 1}}s{{end}}.{{end}}
|
||||||
</div>
|
</div>
|
||||||
<form method="POST" action="/hook/{{$.Webhook.ID}}/events/{{.ID}}/resubmit" class="inline">
|
<form method="POST" action="/hook/{{$.Webhook.ID}}/events/{{.ID}}/resubmit" class="inline">
|
||||||
|
|||||||
@@ -47,6 +47,14 @@
|
|||||||
<input type="number" id="timeout" name="timeout" value="{{.TargetForm.Timeout}}" min="0" max="{{.MaxTimeout}}" class="input">
|
<input type="number" id="timeout" name="timeout" value="{{.TargetForm.Timeout}}" min="0" max="{{.MaxTimeout}}" class="input">
|
||||||
<p class="text-xs text-gray-500 mt-1">Per-request timeout, at most {{.MaxTimeout}} seconds. Leave blank to use the default.</p>
|
<p class="text-xs text-gray-500 mt-1">Per-request timeout, at most {{.MaxTimeout}} seconds. Leave blank to use the default.</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="flex items-center gap-2 text-sm font-medium text-gray-700">
|
||||||
|
<input type="checkbox" id="forward_query" name="forward_query" value="on"{{if .TargetForm.ForwardQuery}} checked{{end}} class="h-4 w-4">
|
||||||
|
Pass the query string on to this target
|
||||||
|
</label>
|
||||||
|
<p class="text-xs text-gray-500 mt-1">Appends the query string each event arrived with to the destination URL, after any query string the URL already has.</p>
|
||||||
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if eq .Target.Type "slack"}}
|
{{if eq .Target.Type "slack"}}
|
||||||
|
|||||||
Reference in New Issue
Block a user