feat(BIT-183): admin auth, RBAC, audit logging, rate limiting

Implements BIT-154.7 — Auth administrativa, RBAC e quotas:

- api/src/types/auth.ts: 3 roles (superadmin/admin/viewer), 18 permissions,
  ROLE_PERMISSIONS map as single source of truth
- api/src/plugins/auth.ts: JWT via HTTP-only __Host- cookies (15min access /
  7d refresh), token rotation, timing-safe login
- api/src/plugins/rbac.ts: requirePermission() + requireRole() Fastify
  decorators for route-level enforcement
- api/src/plugins/audit.ts: auditLog() helper + Fastify plugin; writes to
  audit_logs table; failure never aborts main operation
- api/src/plugins/rate-limit.ts: per-route limits (login: 10/5min IP,
  ingest: 1000/min token, global: 500/min) + payload size enforcement
- api/src/routes/admin/auth.ts: POST login, POST refresh, POST logout,
  GET /me — with audit events on all operations
- db/migrations/001–004: admin_users, refresh_tokens, audit_logs tables +
  dev seed (bcrypt superadmin)
- web/src/app/(admin)/login: Velzon Bootstrap 5 login page (Next.js client
  component, credentials:include, HTTP-only cookie flow)
- docs/permission-matrix.md: full permission matrix + audit event catalogue
  + rate/payload limit table (BIT-183 acceptance criterion)

Co-Authored-By: Paperclip <[email protected]>
This commit is contained in:
1sonhadorandPaperclip committed 2026-03-22 04:22:57 +00:00
1 parent d455a26333
commit eeb5039a3c
21 files changed
+1455

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
# 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` | ✅ | ✅ | ✅ |
## 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` |
| `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) |
| All others | 500 requests | 1 minute | per IP |
## Payload Limits
| Endpoint group | Max body size |
|---------------|---------------|
| `/api/ingest/*` | 512 KB |
| Admin API (`/admin/*`) | 64 KB |