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.
150 lines
6.9 KiB
Markdown
150 lines
6.9 KiB
Markdown
# Permission Matrix — BitReport Error Reporting Admin
|
|
|
|
> **Source of truth:** `api/src/types/auth.ts` → `ROLE_PERMISSIONS`.
|
|
> This document is generated manually from that table. If they diverge, the code wins.
|
|
|
|
## Phase 1 Roles
|
|
|
|
| Role | Description |
|
|
|------|-------------|
|
|
| `superadmin` | Full access. Can manage other admin users and all system settings. |
|
|
| `admin` | Project and error management. Cannot manage other admin users. |
|
|
| `viewer` | Read-only across all resources. Cannot mutate anything. |
|
|
|
|
## Permission Matrix
|
|
|
|
| Permission | superadmin | admin | viewer |
|
|
|------------|:----------:|:-----:|:------:|
|
|
| **Projects** | | | |
|
|
| `projects:read` | ✅ | ✅ | ✅ |
|
|
| `projects:create` | ✅ | ✅ | ❌ |
|
|
| `projects:update` | ✅ | ✅ | ❌ |
|
|
| `projects:delete` | ✅ | ❌ | ❌ |
|
|
| **Errors** | | | |
|
|
| `errors:read` | ✅ | ✅ | ✅ |
|
|
| `errors:triage` | ✅ | ✅ | ❌ |
|
|
| `errors:delete` | ✅ | ✅ | ❌ |
|
|
| **Ingest Tokens** | | | |
|
|
| `ingest_tokens:read` | ✅ | ✅ | ✅ |
|
|
| `ingest_tokens:create` | ✅ | ✅ | ❌ |
|
|
| `ingest_tokens:revoke` | ✅ | ✅ | ❌ |
|
|
| **Sources (Active Collectors)** | | | |
|
|
| `sources:read` | ✅ | ✅ | ✅ |
|
|
| `sources:create` | ✅ | ✅ | ❌ |
|
|
| `sources:update` | ✅ | ✅ | ❌ |
|
|
| `sources:delete` | ✅ | ✅ | ❌ |
|
|
| **Admin Users** | | | |
|
|
| `users:read` | ✅ | ❌ | ❌ |
|
|
| `users:create` | ✅ | ❌ | ❌ |
|
|
| `users:update` | ✅ | ❌ | ❌ |
|
|
| `users:delete` | ✅ | ❌ | ❌ |
|
|
| **Settings** | | | |
|
|
| `settings:read` | ✅ | ✅ | ✅ |
|
|
| `settings:update` | ✅ | ❌ | ❌ |
|
|
| **Audit Logs** | | | |
|
|
| `audit_logs:read` | ✅ | ✅ | ✅ |
|
|
|
|
## Public CRM-shaped API (`/api/error-monitoring/*`)
|
|
|
|
These routes mirror the CRM error-monitoring module for Paperclip and automation. They use **query/body secrets** and optionally the **same admin JWT** as `/admin/*`.
|
|
|
|
### Read: `GET /api/error-monitoring/recent-errors`
|
|
|
|
Alias: `GET /v1/paperclip/recent-errors` (same handler; served by the admin API via the `admin-ui` rewrite — BIT-31442).
|
|
|
|
| Credential | Notes |
|
|
|------------|--------|
|
|
| `api_key` query | Must match `ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY` |
|
|
| `view_errors` query | Must match `DEBUG_VIEW_ERRORS_KEY` (read-oriented) |
|
|
| `X-Api-Key` header (BIT-31442) | Carries either the resolve/reporting key or `DEBUG_VIEW_ERRORS_KEY`; alternative to the query params to avoid key leakage in URLs/logs |
|
|
| Admin session | Not used on this path; query/header credentials only |
|
|
|
|
At least one of the server secrets above must be configured or the endpoint returns **503**.
|
|
|
|
### Mutations: resolve, bulk-resolve, purge-resolved
|
|
|
|
| Route | Required RBAC permission (session) | Automation (`ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY`) |
|
|
|-------|-----------------------------------|---------------------------------------------------------------------|
|
|
| `POST …/resolve` | `errors:triage` | Body `api_key` or header `X-Error-Resolve-Key` |
|
|
| `POST …/bulk-resolve` | `errors:triage` | Same |
|
|
| `POST …/purge-resolved` | `errors:delete` | Same key (destructive; protect key like a root password) |
|
|
|
|
| Role | `resolve` / `bulk-resolve` | `purge-resolved` |
|
|
|------|---------------------------|------------------|
|
|
| `viewer` | Forbidden (**403** if session present) | Forbidden |
|
|
| `admin` | Allowed (session or key) | Allowed (session or key) |
|
|
| `superadmin` | Allowed | Allowed |
|
|
|
|
**401 vs 403:** Missing or wrong automation key → **401**. Valid JWT cookie but role without permission → **403**. Expired or invalid JWT (and no valid key) → **401**.
|
|
|
|
If **no** resolve key is set in the environment, only **session + permission** can mutate (automation via shared key is disabled).
|
|
|
|
## Design Notes
|
|
|
|
### Why admins cannot manage users
|
|
Phase 1 keeps user management exclusive to `superadmin` to prevent privilege escalation:
|
|
an `admin` cannot create another `admin` or grant themselves `superadmin`.
|
|
|
|
### Why viewers can read audit logs
|
|
Transparency: all authenticated staff should be able to see what actions were taken,
|
|
even if they cannot perform those actions. Audit logs contain no secrets.
|
|
|
|
### Why `projects:delete` requires superadmin
|
|
Deleting a project is destructive and irreversible — it cascades to errors, tokens,
|
|
and sources. Only the highest privilege level can trigger this.
|
|
|
|
### Future: per-project scoping (phase 2)
|
|
In phase 2, a `project_members` table will add per-project roles:
|
|
```sql
|
|
CREATE TABLE project_members (
|
|
user_id UUID REFERENCES admin_users(id),
|
|
project_id UUID REFERENCES projects(id),
|
|
role VARCHAR(20) CHECK (role IN ('admin', 'viewer')),
|
|
PRIMARY KEY (user_id, project_id)
|
|
);
|
|
```
|
|
`superadmin` will retain global access; `admin` and `viewer` will be scoped per project.
|
|
|
|
## Audit Events
|
|
|
|
The following operations MUST emit audit log entries (see `api/src/plugins/audit.ts`):
|
|
|
|
| Action | Trigger | Key metadata |
|
|
|--------|---------|--------------|
|
|
| `auth.login` | Successful login | `role` |
|
|
| `auth.login_failed` | Failed login attempt | `reason` (user_not_found / bad_password / inactive) |
|
|
| `auth.logout` | Explicit logout | — |
|
|
| `users.create` | New admin created | `new_role`, `new_email` |
|
|
| `users.update` | Role/email changed | `old_role`, `new_role` |
|
|
| `users.delete` | Admin deleted | `deleted_email`, `deleted_role` |
|
|
| `projects.create` | Project registered | `project_id`, `name` |
|
|
| `projects.update` | Project settings changed | `fields_changed` |
|
|
| `projects.delete` | Project deleted | `project_id`, `name` |
|
|
| `ingest_tokens.create` | Token issued | `project_id`, `token_id` |
|
|
| `ingest_tokens.revoke` | Token revoked | `project_id`, `token_id` |
|
|
| `sources.create` | Collector source added | `project_id`, `source_type` |
|
|
| `sources.delete` | Source removed | `project_id`, `source_id` |
|
|
| `errors.triage` | Error state changed | `from_status`, `to_status`, `error_id` |
|
|
| `errors.delete` | Error deleted | `error_id`, `project_id` |
|
|
| `error_monitoring.resolve` | Hub `POST …/resolve` succeeded | `unique_id`, `project_id`, `from_state`, `via` |
|
|
| `error_monitoring.bulk_resolve` | Hub `POST …/bulk-resolve` succeeded | `by`, `resolved`, `via` |
|
|
| `error_monitoring.purge_resolved_dry_run` | Hub purge dry run | `would_delete`, `via` |
|
|
| `error_monitoring.purge_resolved` | Hub `POST …/purge-resolved` deleted rows | `deleted`, `via` |
|
|
| `settings.update` | System settings changed | `keys_changed` |
|
|
|
|
## Rate Limits
|
|
|
|
| Endpoint | Limit | Window | Key |
|
|
|----------|-------|--------|-----|
|
|
| `POST /admin/auth/login` | 10 requests | 5 minutes | per IP |
|
|
| `POST /api/ingest/*` | 1000 requests | 1 minute | per token (fallback: IP) |
|
|
| `POST /api/error-monitoring/resolve`, `…/bulk-resolve`, `…/purge-resolved` | 120 requests (env `RATE_MUTATION_MAX`) | 1 minute (`RATE_MUTATION_WINDOW_MS`) | per client IP (`errmon_mut:ip:*`) |
|
|
| All others | 500 requests | 1 minute | per IP |
|
|
|
|
## Payload Limits
|
|
|
|
| Endpoint group | Max body size |
|
|
|---------------|---------------|
|
|
| `/api/ingest/*` | 512 KB |
|
|
| Admin API (`/admin/*`) | 64 KB |
|