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
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).
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.
api/src/routes/public/recentErrors.test.ts — new tests: alias path, X-Api-Key with resolve key, with view key, invalid key → 401.
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)
## 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
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 master2026-10-21 20:40:24 +01:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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-errorsGET /v1/paperclip/recent-errors(alias)on the admin API (port 3200). But the
admin-uiNext.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
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).api/src/lib/errorMonitoringAuth.ts—authorizeReadQuerynow also accepts anX-Api-Keyheader carrying eitherERROR_RESOLVE_API_KEY/ERROR_REPORTING_API_KEYorDEBUG_VIEW_ERRORS_KEY, so programmatic callers avoid leaking keys in URLs/access logs. Backward compatible with existing query params.api/src/routes/public/recentErrors.test.ts— new tests: alias path,X-Api-Keywith resolve key, with view key, invalid key → 401.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→ cleanDeploy
No manual steps. CI runs on PR; merge to master triggers the webhook deploy. The proxy fix takes effect on the next
admin-uirebuild/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 datahttps://errors.bitreport.pt/v1/paperclip/recent-errorswith headerX-Api-Key: <key>→ 200https://errors.bitreport.pt/api/error-monitoring/recent-errors?...→ unchanged (still works)Refs: BIT-31442, BIT-31157, BIT-31155, BIT-31156, BIT-31154