fix(hub): BIT-31442 route /v1/paperclip alias to admin API + accept X-Api-Key header #17

Merged
spark merged 1 commits from fix/BIT-31442-recent-errors-alias-proxy-and-xapikey into master 2026-10-21 20:40:24 +01:00
Owner

BIT-31442 — Expor rota API recent-errors com auth programática

Root cause

The recent-errors handler (api/src/routes/public/recentErrors.ts) registers both:

  • GET /api/error-monitoring/recent-errors
  • GET /v1/paperclip/recent-errors (alias)

on the admin API (port 3200). But the admin-ui Next.js rewrite sent all /v1/* to ingest-api (port 3041), which has no such route. Result:

  • /api/error-monitoring/recent-errors → 401 without key (alive, correct)
  • /v1/paperclip/recent-errors → 404 (alias never reached the admin API)

Auth was query-param only (api_key / view_errors), which is why earlier Bearer/X-Api-Key tests returned 401/redirect.

Changes

  1. admin-ui/next.config.ts — add a specific /v1/paperclip/:path* → admin API rewrite before the generic /v1/:path* → ingest-api rule (Next.js evaluates rewrites in order; first match wins).
  2. api/src/lib/errorMonitoringAuth.ts — authorizeReadQuery now also accepts an X-Api-Key header carrying either ERROR_RESOLVE_API_KEY/ERROR_REPORTING_API_KEY or DEBUG_VIEW_ERRORS_KEY, so programmatic callers avoid leaking keys in URLs/access logs. Backward compatible with existing query params.
  3. api/src/routes/public/recentErrors.test.ts — new tests: alias path, X-Api-Key with resolve key, with view key, invalid key → 401.
  4. docs/HTTP_CONTRACTS.md, docs/permission-matrix.md — document the header and the proxy ordering requirement.

Verification

  • api: npm test -- --run src/routes/public/recentErrors.test.ts → 10/10 pass (5 new + 5 existing)
  • api: errorMonitoringAuth.test.ts → 8/8 pass (shared lib unchanged behaviour for query path)
  • admin-ui: tsc --noEmit → clean

Deploy

No manual steps. CI runs on PR; merge to master triggers the webhook deploy. The proxy fix takes effect on the next admin-ui rebuild/deploy.

Acceptance

After deploy, in production:

  • https://errors.bitreport.pt/v1/paperclip/recent-errors?project_slug=bizdocs&view_errors=<DEBUG_VIEW_ERRORS_KEY> → 200 with data
  • https://errors.bitreport.pt/v1/paperclip/recent-errors with header X-Api-Key: <key> → 200
  • https://errors.bitreport.pt/api/error-monitoring/recent-errors?... → unchanged (still works)

Refs: BIT-31442, BIT-31157, BIT-31155, BIT-31156, BIT-31154

## BIT-31442 — Expor rota API recent-errors com auth programática ### Root cause The recent-errors handler (`api/src/routes/public/recentErrors.ts`) registers **both**: - `GET /api/error-monitoring/recent-errors` - `GET /v1/paperclip/recent-errors` (alias) on the **admin API (port 3200)**. But the `admin-ui` Next.js rewrite sent **all** `/v1/*` to **ingest-api (port 3041)**, which has no such route. Result: - `/api/error-monitoring/recent-errors` → **401** without key (alive, correct) - `/v1/paperclip/recent-errors` → **404** (alias never reached the admin API) Auth was query-param only (`api_key` / `view_errors`), which is why earlier Bearer/X-Api-Key tests returned 401/redirect. ### Changes 1. **`admin-ui/next.config.ts`** — add a specific `/v1/paperclip/:path*` → admin API rewrite **before** the generic `/v1/:path*` → ingest-api rule (Next.js evaluates rewrites in order; first match wins). 2. **`api/src/lib/errorMonitoringAuth.ts`** — `authorizeReadQuery` now also accepts an `X-Api-Key` header carrying either `ERROR_RESOLVE_API_KEY`/`ERROR_REPORTING_API_KEY` or `DEBUG_VIEW_ERRORS_KEY`, so programmatic callers avoid leaking keys in URLs/access logs. Backward compatible with existing query params. 3. **`api/src/routes/public/recentErrors.test.ts`** — new tests: alias path, `X-Api-Key` with resolve key, with view key, invalid key → 401. 4. **`docs/HTTP_CONTRACTS.md`, `docs/permission-matrix.md`** — document the header and the proxy ordering requirement. ### Verification - `api`: `npm test -- --run src/routes/public/recentErrors.test.ts` → 10/10 pass (5 new + 5 existing) - `api`: `errorMonitoringAuth.test.ts` → 8/8 pass (shared lib unchanged behaviour for query path) - `admin-ui`: `tsc --noEmit` → clean ### Deploy No manual steps. CI runs on PR; merge to master triggers the webhook deploy. The proxy fix takes effect on the next `admin-ui` rebuild/deploy. ### Acceptance After deploy, in production: - `https://errors.bitreport.pt/v1/paperclip/recent-errors?project_slug=bizdocs&view_errors=<DEBUG_VIEW_ERRORS_KEY>` → 200 with data - `https://errors.bitreport.pt/v1/paperclip/recent-errors` with header `X-Api-Key: <key>` → 200 - `https://errors.bitreport.pt/api/error-monitoring/recent-errors?...` → unchanged (still works) Refs: BIT-31442, BIT-31157, BIT-31155, BIT-31156, BIT-31154
spark added 1 commit 2026-10-21 18:16:47 +01:00
fix(hub): BIT-31442 route /v1/paperclip alias to admin API + accept X-Api-Key header
CI / Test ingest-api (pull_request) Successful in 41s
CI / Test api (pull_request) Successful in 44s
CI / Lint and typecheck admin-ui (pull_request) Successful in 1m24s
CI / Validate Docker build (pull_request) Successful in 6m2s
d4059d801a
The recent-errors handler (api/src/routes/public/recentErrors.ts) registers
both GET /api/error-monitoring/recent-errors and the alias
GET /v1/paperclip/recent-errors on the admin API (port 3200). However the
admin-ui Next.js rewrite sent all /v1/* to ingest-api (port 3041), which has
no such route, so the alias returned 404 in production while the
/api/error-monitoring path worked (401 without a key).

Changes:
- admin-ui/next.config.ts: add a specific /v1/paperclip/:path* -> admin API
  rewrite before the generic /v1/:path* -> ingest-api rule (rewrite order
  matters; first match wins).
- api/src/lib/errorMonitoringAuth.ts: authorizeReadQuery now also accepts an
  X-Api-Key header carrying either ERROR_RESOLVE_API_KEY/ERROR_REPORTING_API_KEY
  or DEBUG_VIEW_ERRORS_KEY, so programmatic callers avoid leaking keys in
  URLs/access logs. Backward compatible with the existing query params.
- api/src/routes/public/recentErrors.test.ts: cover the alias path and the
  X-Api-Key header (resolve key, view key, invalid key).
- docs/HTTP_CONTRACTS.md, docs/permission-matrix.md: document the header and
  the proxy ordering requirement.
spark merged commit d987d2aaf4 into master 2026-10-21 20:40:24 +01:00
Sign in to join this conversation.