From 5c977d69775b1b8eb4c0d1ffcdd00d9ed11011b7 Mon Sep 17 00:00:00 2001 From: Team Dark Devil Date: Fri, 20 Nov 2026 18:48:54 +0600 Subject: [PATCH] feat(auth): implement RBAC audit logging and session security Integrate granular audit logging for admin actions, transition to role-based API access via `requireAdminApiSession`, and update session tokens to persist user roles and IDs for consistent multi-admin management. --- MEMORY.md | 20 +- app/api/audit/route.ts | 41 + app/api/auth/login/route.ts | 30 +- app/api/auth/profile/route.ts | 30 +- app/api/clients/[id]/route.ts | 124 +- app/api/clients/route.ts | 87 +- app/api/prometheus/route.ts | 18 +- app/api/settings/route.ts | 43 +- app/api/settings/webhook-test/route.ts | 19 +- app/api/storage/archive/route.ts | 19 +- app/api/storage/route.ts | 2 +- app/api/storage/s3-test/route.ts | 2 +- app/api/team/[id]/route.ts | 136 ++ app/api/team/route.ts | 96 ++ app/vcon/audit/page.tsx | 5 + app/vcon/sdk/page.tsx | 257 +++- app/vcon/team/page.tsx | 5 + components/layout/Sidebar.tsx | 2 + components/sections/AuditLogViewer.tsx | 236 ++++ components/sections/TeamWorkspace.tsx | 447 ++++++ core/Database/database.ts | 26 + core/auth/adminSession.ts | 54 +- core/auth/adminUsers.ts | 940 ++++++++++++- core/auth/auditLog.ts | 281 ++++ lib/adminAuth.ts | 82 +- middleware.ts | 1 + prisma/schema.prisma | 96 +- sdk/node/datastack/README.md | 84 ++ sdk/node/datastack/index.ts | 1245 +++++++++++++++++ sdk/node/datastack/package.json | 35 + sdk/node/datastack/spool.ts | 176 +++ sdk/node/datastack/types.ts | 190 +++ server/index.js | 24 +- tests/phase07-deployment.test.ts | 130 ++ tests/v2-phase02-alerts-ip-prometheus.test.ts | 286 ++-- tests/v2-phase03-rbac-audit.test.ts | 621 ++++++++ tests/v2-phase04-node-sdk.test.ts | 398 ++++++ 37 files changed, 5939 insertions(+), 349 deletions(-) create mode 100644 app/api/audit/route.ts create mode 100644 app/api/team/[id]/route.ts create mode 100644 app/api/team/route.ts create mode 100644 app/vcon/audit/page.tsx create mode 100644 app/vcon/team/page.tsx create mode 100644 components/sections/AuditLogViewer.tsx create mode 100644 components/sections/TeamWorkspace.tsx create mode 100644 core/auth/auditLog.ts create mode 100644 sdk/node/datastack/README.md create mode 100644 sdk/node/datastack/index.ts create mode 100644 sdk/node/datastack/package.json create mode 100644 sdk/node/datastack/spool.ts create mode 100644 sdk/node/datastack/types.ts create mode 100644 tests/v2-phase03-rbac-audit.test.ts create mode 100644 tests/v2-phase04-node-sdk.test.ts diff --git a/MEMORY.md b/MEMORY.md index 5ea198e..25c11de 100644 --- a/MEMORY.md +++ b/MEMORY.md @@ -1,7 +1,7 @@ # MEMORY.md — DataStack Cross-IDE Persistent Project Memory -> **Last Synchronized**: 2026-11-19 -> **Status**: **V2-Phase-01 Complete & Verified (`BASELINE-2026-11-19-V2`) — `1 / 4` V2 Phases Done (Storage `.csv.gz` Auto-Compression, S3/R2 Off-Site Archive with AES-256-GCM Secret Vault, Per-Client `storageQuotaMb` & VPS Disk Pause Guard `HTTP 507`) — Verified (`89/89` TS tests, `10/10` Python tests, ESLint-enabled build & lint `0` errors / `0` warnings).** +> **Last Synchronized**: 2026-11-20 +> **Status**: **V2-Phase-04 & `V2-Deploy-Fix-01` Complete & Verified (`BASELINE-2026-11-19-V2`) — `4 / 4` V2 Phases + Final Coolify Redeploy Hardening Done (`V2-Phase-01` Storage `.csv.gz` Compression, S3/R2 Off-Site Archive & Per-Client/Disk Quotas; `V2-Phase-02` Outbound Webhook Alerting, Per-Client IP/CIDR Allowlist & Token Expiry, Prometheus `/api/prometheus` Metrics Endpoint & Streaming `.ndjson` Export; `V2-Phase-03` Multi-Admin RBAC `owner`/`admin`/`operator`/`viewer` via `admin_team_members` & Immutable Admin Audit Log `admin_audit_logs`; `V2-Phase-04` Official Node.js / TypeScript Client SDK `sdk/node/datastack` & `/vcon/sdk` Multi-Language Guide; `V2-Deploy-Fix-01` `DEPLOY-V2-BUG-01..03`) — Verified (`102/102` TS tests, `10/10` Python tests, ESLint-enabled build & lint `0` errors / `0` warnings).** > **Scope Rule**: Maintain this file accurately across VS Code, Google Antigravity, Cursor, and any other IDE after every modification, feature update, or bug fix (see `.ai-rules` and `AGENTS.md`). --- @@ -314,6 +314,22 @@ All planned hardening & deployment phases (**Phase-01 through Phase-07**) are ** - **Per-Client Storage Quota (`storageQuotaMb`) & VPS Disk Pause Guard (`diskPauseThresholdPct`)**: Additive PostgreSQL columns (`client_sdks.storage_quota_mb` + 9 `admin_settings` columns in `core/Database/database.ts`, synced in `server/index.js` while preserving `ddlCount === 8`), `30s` TTL cached `statfs` + directory byte counters, and HTTP `507 Insufficient Storage` (`DISK_SPACE_LOW` / `STORAGE_QUOTA_EXCEEDED` — `V2-E04`, `V2-E05`) on `POST /api/events` and Port `4000` `sync_data`/`batch_sync` while keeping all Admin read/export/settings routes online. - **Admin UI Controls (`SettingsWorkspace.tsx`, `NewClientModal.tsx`, `ClientsTable.tsx`, `lib/vconUiHelpers.ts`)**: Added live VPS disk usage bar, `Auto-compress (.csv → .csv.gz)`, `Disk pause threshold (0 or 50–99%)`, full `S3 / R2 off-site archive` configuration with `Test S3 Connection` & `Archive Now`, and per-client `Storage quota (MB, 0 = unlimited)` in modal and table. - **Verified**: `89/89` TS tests (`4/4` in `tests/v2-phase01-storage-quotas.test.ts`), `10/10` Python SDK tests, `npm run build` clean, `npm run lint` (`0` errors / `0` warnings), and Chromium E2E across `/vcon/settings` (Storage tab) and `/vcon/clients` (`0` console errors). + - **`V2-Phase-02` Complete & Verified (`2026-11-19`)** (`V2-FEAT-02`, `V2-FEAT-04`, `V2-FEAT-06`, `V2-E06`–`V2-E09`): + - **Per-Client IP / CIDR Allowlist & Token Expiration (`core/Security/ipAllowlist.ts`, `core/sdk/sdkManager.ts`, `app/api/clients/**`, `app/api/events/route.ts`, `server/index.js`, `server/socket/handlers.js`)**: Validates and normalizes comma-separated IPv4, IPv4 CIDR (`/0..32`), IPv6, IPv6 CIDR (`/0..128`), IPv4-mapped IPv6 (`::ffff:`), loopback equivalence (`127.0.0.1` ↔ `::1`), and wildcard `*`; validates ISO `expiresAt` timestamps. Enforced across both Port `3000` `POST /api/events` and Port `4000` Socket.IO handshakes/events (`403 CLIENT_IP_FORBIDDEN` — `V2-E07`; `401 CLIENT_TOKEN_EXPIRED` — `V2-E08`), synced live via `/internal/client-sync` and additive PostgreSQL columns (`client_sdks.allowed_ips`, `client_sdks.expires_at`). + - **Outbound Webhook Alerting (`core/alerts/webhookNotifier.ts`, `core/adminSettings/settingsManager.ts`, `app/api/settings/webhook-test/route.ts`)**: Sends Slack (`text`), Discord (`content`), and structured JSON webhook payloads signed with optional HMAC-SHA256 (`X-DataStack-Signature: sha256=`) and deduplicated per `event:clientId` via configurable `webhookCooldownSec` (`10..86400s`, default `300s`). Triggers non-blocking alerts on `DISK_SPACE_LOW`, `STORAGE_QUOTA_EXCEEDED`, `S3_ARCHIVE_FAILED`, `CLIENT_IP_FORBIDDEN`, and `CLIENT_TOKEN_EXPIRED`, plus on-demand `POST /api/settings/webhook-test` (`200` on delivery, `502 WEBHOOK_DELIVERY_FAILED` on upstream error — `V2-E06`). `webhook_secret_enc` is encrypted via AES-256-GCM Secret Vault and masked in `GET /api/settings` (`hasWebhookSecret`, `webhookSecretMask`). + - **Prometheus Scrape Endpoint (`app/api/prometheus/route.ts`)**: `GET /api/prometheus` outputs Prometheus 0.0.4 text exposition format (`datastack_up`, `datastack_ingestion_bridge_up`, `datastack_active_sockets`, `datastack_events_total`, `datastack_buffer_events_total`, `datastack_storage_files_total`, `datastack_storage_bytes_total`, `datastack_storage_rows_total`, `datastack_disk_used_percent`, `datastack_disk_free_bytes`, `datastack_disk_total_bytes`, `datastack_clients_total`, `datastack_client_events_total`, `datastack_client_storage_bytes`). Protected by constant-time SHA-256 Bearer token comparison (`prometheus_metrics_token_enc` encrypted at rest) or active Admin Console session cookie (`401 PROMETHEUS_UNAUTHORIZED` — `V2-E09`). + - **Streaming `.ndjson` Export (`app/api/export/route.ts`, `components/sections/ExportWorkspace.tsx`)**: Added `format=ndjson` (`Content-Type: application/x-ndjson; charset=utf-8`, `.ndjson` extension) streaming newline-delimited JSON objects across both `.csv` and `.csv.gz` partitions, with `NDJSON` format radio selector in `/vcon/export`. + - **Admin UI Controls (`SettingsWorkspace.tsx`, `NewClientModal.tsx`, `ClientsTable.tsx`, `lib/vconUiHelpers.ts`)**: Added `Outbound webhook alerts` and `Prometheus metrics (/api/prometheus)` groups under `/vcon/settings` → **Security** tab, and `Allowed IPs / CIDR` + `Token expiration` controls in `/vcon/clients` New/Edit modals and table badges. + - **Verified**: `93/93` TS tests (`4/4` in `tests/v2-phase02-alerts-ip-prometheus.test.ts`), `10/10` Python SDK tests, `npm run build` clean, `npm run lint` (`0` errors / `0` warnings), and Chromium E2E across `/vcon/settings` (`0` console errors). + - **`V2-Phase-03` Complete & Verified (`2026-11-19`)** (`V2-FEAT-05`, `V2-E10`–`V2-E11`): + - **Multi-Admin Role-Based Access Control (`core/Database/database.ts`, `core/auth/adminSession.ts`, `core/auth/adminUsers.ts`, `lib/adminAuth.ts`, `app/api/team/route.ts`, `app/api/team/[id]/route.ts`)**: Added canonical `admin_team_members` table in PostgreSQL (plus in-memory fallback) while preserving `admin_users` (`id = 1 CHECK (id = 1)`) as the immutable Primary Owner (`owner-1`, protected by `400 CANNOT_REVOKE_PRIMARY_OWNER` — `V2-E11`). Supports roles `owner` (rank 3 — full platform settings, secrets & team management), `admin` / `operator` (rank 2 — manage SDK clients, rotate tokens, manual buffer flushes, S3 test/archive), and `viewer` (rank 1 — read-only dashboards, events, clients, settings & exports). Enforced centrally via `requireAdminApiSession(req, { minRole })` returning `403 INSUFFICIENT_ROLE_PERMISSION` (`V2-E10`). Role/status/password changes on any team member immediately bump `token_version` to revoke existing sessions. + - **Immutable Admin Audit Log (`core/auth/auditLog.ts`, `app/api/audit/route.ts`)**: Added canonical `admin_audit_logs` table in PostgreSQL (plus bounded 1,000-entry in-memory fallback) with automatic secret key redaction. Records governance events (`ADMIN_LOGIN`, `ADMIN_PROFILE_UPDATED`, `ADMIN_PASSWORD_UPDATED`, `TEAM_MEMBER_CREATED`, `TEAM_MEMBER_UPDATED`, `TEAM_MEMBER_REVOKED`, `TEAM_MEMBER_DELETED`, `CLIENT_CREATED`, `CLIENT_UPDATED`, `CLIENT_TOKEN_ROTATED`, `CLIENT_DELETED`, `SETTINGS_UPDATED`, `S3_ARCHIVE_TRIGGERED`, `WEBHOOK_TEST_TRIGGERED`) without ever inserting telemetry events into PostgreSQL. Exposed filterable `GET /api/audit` (`?action=`, `?actor=`, `?q=`, `?limit=`). + - **Admin Console UI (`components/sections/TeamWorkspace.tsx`, `components/sections/AuditLogViewer.tsx`, `app/vcon/team/page.tsx`, `app/vcon/audit/page.tsx`, `components/layout/Sidebar.tsx`)**: Added `/vcon/team` (Team & RBAC directory, add member modal, role selector, session revocation & deletion) and `/vcon/audit` (filterable governance trail viewer) linked from `Sidebar.tsx`. + - **Verified**: `97/97` TS tests (`4/4` in `tests/v2-phase03-rbac-audit.test.ts`), `10/10` Python SDK tests, `npm run build` clean, `npm run lint` (`0` errors / `0` warnings), and Chromium E2E across `/vcon/team` and `/vcon/audit` (`0` console errors). + - **`V2-Phase-04` Complete & Verified (`2026-11-20`)** (`V2-FEAT-07`, `V2-E12`): + - **Official Node.js / TypeScript Client SDK (`sdk/node/datastack/` — `@datastack/sdk` v2.2.0)**: Created `sdk/node/datastack/package.json`, `types.ts`, `spool.ts`, `index.ts`, and `README.md`. Supports `datastack.config.json` / `DATASTACK_*` env / constructor options with strict `normalizeClientId` vs JWT `clientId`/`sub` verification (`DataStackConfigError` — `V2-E12`), Socket.IO primary streaming (`connect()`, `send()`/`sync()`, `sendBatch()`/`batchSync()`), automatic HTTP `POST /api/events` fallback, recursive `413 PAYLOAD_TOO_LARGE` batch splitting, partial-batch `429 RATE_LIMIT_EXCEEDED` retry, typed terminal error mapping (`401 CLIENT_TOKEN_EXPIRED` & `403 CLIENT_IP_FORBIDDEN` -> `DataStackAuthError`, `507 STORAGE_QUOTA_EXCEEDED` & `DISK_SPACE_LOW` -> `DataStackQuotaExceededError`), durable `.datastack_spool/spool.jsonl` local disk spooling (`DiskSpooler`) & `flushSpool()`, direct `.csv`/`.json`/`.jsonl`/`.ndjson` file ingestion (`sendFile()` with deterministic SHA-256 `eventId` via `makeDeterministicFileEventId`), and graceful `await client.shutdown(timeoutMs)` drain. + - **Multi-Language SDK Guide (`app/vcon/sdk/page.tsx`)**: Added interactive language tabs (`Python SDK (v2.2.0)`, `Node.js / TypeScript SDK (v2.2.0)`, `HTTP / cURL`) with live client JWT token population and copy-ready `config.datastack.py` / `datastack.config.json` snippets. + - **Verified**: `101/101` TS tests (`4/4` in `tests/v2-phase04-node-sdk.test.ts`), `10/10` Python SDK tests, `npm run build` clean, and `npm run lint` (`0` errors / `0` warnings). --- diff --git a/app/api/audit/route.ts b/app/api/audit/route.ts new file mode 100644 index 0000000..ad31905 --- /dev/null +++ b/app/api/audit/route.ts @@ -0,0 +1,41 @@ +import { listAdminAuditLogsAsync } from "@/core/auth/auditLog"; +import { requireAdminApiSession } from "@/lib/adminAuth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(req: NextRequest) { + const auth = await requireAdminApiSession(req, { minRole: "viewer" }); + if (!auth.valid) { + return auth.response; + } + + try { + const { searchParams } = new URL(req.url); + const rawLimit = Number(searchParams.get("limit") || "100"); + const limit = Number.isFinite(rawLimit) + ? Math.min(Math.max(Math.floor(rawLimit), 1), 500) + : 100; + const action = searchParams.get("action") || undefined; + const actorEmail = + searchParams.get("actor") || + searchParams.get("actorEmail") || + undefined; + const search = + searchParams.get("q") || searchParams.get("search") || undefined; + + const logs = await listAdminAuditLogsAsync({ + limit, + action, + actorEmail, + search, + }); + + return NextResponse.json({ + logs, + total: logs.length, + }); + } catch (err: unknown) { + const message = + err instanceof Error ? err.message : "Unable to fetch audit logs."; + return NextResponse.json({ error: message }, { status: 500 }); + } +} diff --git a/app/api/auth/login/route.ts b/app/api/auth/login/route.ts index 9c72d08..7037c40 100644 --- a/app/api/auth/login/route.ts +++ b/app/api/auth/login/route.ts @@ -1,4 +1,5 @@ import { + type AdminRole, createAdminSessionToken, getAdminSessionSecret, getSessionCookieOptions, @@ -9,6 +10,7 @@ import { clearAdminLoginAttempts, consumeAdminLoginAttempt, } from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { extractClientIp } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; @@ -79,7 +81,13 @@ export async function POST(request: NextRequest) { ); } - let authResult: { authenticated: boolean; tokenVersion: number }; + let authResult: { + authenticated: boolean; + tokenVersion: number; + role: AdminRole; + memberId: string; + email: string; + }; try { authResult = await authenticateAdminCredentials(email, password); } catch (err) { @@ -117,13 +125,29 @@ export async function POST(request: NextRequest) { const remember = body.remember === true; const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS; const expiresAt = Date.now() + maxAge * 1000; + const effectiveEmail = authResult.email || email; const token = await createAdminSessionToken( - email, + effectiveEmail, sessionSecret, expiresAt, authResult.tokenVersion, + authResult.role, + authResult.memberId, ); - const response = NextResponse.json({ success: true }); + + await recordAdminAuditLogAsync({ + actorEmail: effectiveEmail, + actorRole: authResult.role || "owner", + action: "ADMIN_LOGIN", + target: effectiveEmail, + ipAddress: clientIp, + metadata: { remember }, + }); + + const response = NextResponse.json({ + success: true, + role: authResult.role || "owner", + }); response.cookies.set( SESSION_COOKIE, diff --git a/app/api/auth/profile/route.ts b/app/api/auth/profile/route.ts index 13f5f66..682bd58 100644 --- a/app/api/auth/profile/route.ts +++ b/app/api/auth/profile/route.ts @@ -1,5 +1,6 @@ import { createAdminSessionToken, + formatAdminRoleLabel, getSessionCookieOptions, } from "@/core/auth/adminSession"; import { @@ -7,6 +8,8 @@ import { updateAdminAccount, verifyRequestAdminSession, } from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; +import { extractClientIp } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; const SESSION_COOKIE = "datastack_admin_session"; @@ -33,10 +36,13 @@ export async function GET(request: NextRequest) { try { const account = await getAdminAccountInfo(auth.session.email); + const effectiveRole = auth.session.role || account.role || "owner"; return NextResponse.json({ profile: { email: account.email, - role: "Owner", + role: formatAdminRoleLabel(effectiveRole), + roleKey: effectiveRole, + memberId: auth.session.memberId || account.memberId || "owner-1", storageMode: account.storageMode, createdAt: account.createdAt, sessionExpiresAt: new Date(auth.session.expiresAt).toISOString(), @@ -125,24 +131,42 @@ export async function PUT(request: NextRequest) { ); } - // Preserve remaining session duration (e.g., 30-day "Remember me" session) const remainingSeconds = Math.max( SESSION_SECONDS, Math.ceil((auth.session.expiresAt - Date.now()) / 1000), ); const expiresAt = Date.now() + remainingSeconds * 1000; + const effectiveRole = result.role || auth.session.role || "owner"; + const effectiveMemberId = + result.memberId || auth.session.memberId || "owner-1"; const nextToken = await createAdminSessionToken( result.email, auth.sessionSecret, expiresAt, result.tokenVersion, + effectiveRole, + effectiveMemberId, ); + await recordAdminAuditLogAsync({ + actorEmail: result.email, + actorRole: effectiveRole, + action: newPassword ? "ADMIN_PASSWORD_UPDATED" : "ADMIN_PROFILE_UPDATED", + target: result.email, + ipAddress: extractClientIp(request), + metadata: { + emailChanged: result.email !== account.email, + passwordRotated: Boolean(newPassword), + }, + }); + const response = NextResponse.json({ success: true, profile: { email: result.email, - role: "Owner", + role: formatAdminRoleLabel(effectiveRole), + roleKey: effectiveRole, + memberId: effectiveMemberId, storageMode: account.storageMode, createdAt: account.createdAt, sessionExpiresAt: new Date(expiresAt).toISOString(), diff --git a/app/api/clients/[id]/route.ts b/app/api/clients/[id]/route.ts index 284a57c..cffc1ff 100644 --- a/app/api/clients/[id]/route.ts +++ b/app/api/clients/[id]/route.ts @@ -1,12 +1,12 @@ -import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { SDKManager } from "@/core/sdk/sdkManager"; import { validateAllowedIpsInput, validateClientExpiresAtInput, } from "@/core/Security/ipAllowlist"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; -const SESSION_COOKIE = "datastack_admin_session"; const ALLOWED_PATCH_KEYS = new Set([ "action", "name", @@ -21,14 +21,12 @@ export async function GET( req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const includeToken = req.nextUrl.searchParams.get("includeToken") === "true"; + const auth = await requireAdminApiSession(req, { + minRole: includeToken ? "operator" : "viewer", + }); if (!auth.valid) { - return NextResponse.json( - { error: "Authentication required." }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } const { id } = await params; @@ -40,7 +38,6 @@ export async function GET( } const { secret, token, ...clientSummary } = client; void secret; - const includeToken = req.nextUrl.searchParams.get("includeToken") === "true"; return NextResponse.json({ client: { ...clientSummary, @@ -65,14 +62,9 @@ export async function PATCH( req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "operator" }); if (!auth.valid) { - return NextResponse.json( - { error: "Authentication required." }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } const { id } = await params; @@ -90,7 +82,10 @@ export async function PATCH( if (!rawBody || typeof rawBody !== "object" || Array.isArray(rawBody)) { return NextResponse.json( - { error: "Update payload must be a JSON object.", code: "VALIDATION_ERROR" }, + { + error: "Update payload must be a JSON object.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -120,8 +115,23 @@ export async function PATCH( try { const refreshed = await SDKManager.regenerateTokenAsync(normalizedId); if (!refreshed) { - return NextResponse.json({ error: "Client not found" }, { status: 404 }); + return NextResponse.json( + { error: "Client not found" }, + { status: 404 }, + ); } + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "CLIENT_TOKEN_ROTATED", + target: refreshed.clientId, + ipAddress: extractClientIp(req), + metadata: { + clientId: refreshed.clientId, + }, + }); + return NextResponse.json({ client: refreshed }); } catch (err: unknown) { const message = @@ -130,7 +140,9 @@ export async function PATCH( return NextResponse.json( { error: message, - code: isDbWrite ? "DATABASE_WRITE_FAILED" : "TOKEN_REGENERATION_FAILED", + code: isDbWrite + ? "DATABASE_WRITE_FAILED" + : "TOKEN_REGENERATION_FAILED", }, { status: isDbWrite ? 503 : 500 }, ); @@ -152,7 +164,10 @@ export async function PATCH( body.name.trim().length > 120 ) { return NextResponse.json( - { error: "Client name must be a non-empty string (max 120 characters).", code: "VALIDATION_ERROR" }, + { + error: "Client name must be a non-empty string (max 120 characters).", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -166,7 +181,10 @@ export async function PATCH( body.rateLimit > 100000 ) { return NextResponse.json( - { error: "rateLimit must be an integer between 1 and 100000.", code: "VALIDATION_ERROR" }, + { + error: "rateLimit must be an integer between 1 and 100000.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -180,7 +198,10 @@ export async function PATCH( body.storageQuotaMb > 1048576 ) { return NextResponse.json( - { error: "storageQuotaMb must be an integer between 0 and 1048576.", code: "VALIDATION_ERROR" }, + { + error: "storageQuotaMb must be an integer between 0 and 1048576.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -196,7 +217,10 @@ export async function PATCH( const ipValidation = validateAllowedIpsInput(body.allowedIps); if (!ipValidation.valid) { return NextResponse.json( - { error: ipValidation.error || "Invalid allowedIps.", code: "VALIDATION_ERROR" }, + { + error: ipValidation.error || "Invalid allowedIps.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -206,16 +230,22 @@ export async function PATCH( const expValidation = validateClientExpiresAtInput(body.expiresAt); if (!expValidation.valid) { return NextResponse.json( - { error: expValidation.error || "Invalid expiresAt.", code: "VALIDATION_ERROR" }, + { + error: expValidation.error || "Invalid expiresAt.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } - updates.expiresAt = expValidation.normalized; + updates.expiresAt = expValidation.iso ?? null; } if (body.status !== undefined) { if (body.status !== "active" && body.status !== "revoked") { return NextResponse.json( - { error: "status must be 'active' or 'revoked'.", code: "VALIDATION_ERROR" }, + { + error: "status must be 'active' or 'revoked'.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -224,7 +254,10 @@ export async function PATCH( if (Object.keys(updates).length === 0) { return NextResponse.json( - { error: "No valid fields provided for update.", code: "VALIDATION_ERROR" }, + { + error: "No valid fields provided for update.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -234,6 +267,16 @@ export async function PATCH( if (!updated) { return NextResponse.json({ error: "Client not found" }, { status: 404 }); } + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "CLIENT_UPDATED", + target: updated.clientId, + ipAddress: extractClientIp(req), + metadata: updates, + }); + return NextResponse.json({ client: updated }); } catch (err: unknown) { const message = err instanceof Error ? err.message : "Update failed"; @@ -252,14 +295,9 @@ export async function DELETE( req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "operator" }); if (!auth.valid) { - return NextResponse.json( - { error: "Authentication required." }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } const { id } = await params; @@ -272,13 +310,27 @@ export async function DELETE( if (!result.deleted) { return NextResponse.json({ error: "Client not found" }, { status: 404 }); } + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "CLIENT_DELETED", + target: normalizedId, + ipAddress: extractClientIp(req), + metadata: { + purgeFiles, + purgedFiles: result.purgedFiles, + }, + }); + return NextResponse.json({ success: true, message: `Client ${normalizedId} deleted`, purgedFiles: result.purgedFiles, }); } catch (err: unknown) { - const message = err instanceof Error ? err.message : "Client deletion failed"; + const message = + err instanceof Error ? err.message : "Client deletion failed"; const isDbWrite = message.includes("DATABASE_WRITE_FAILED"); return NextResponse.json( { diff --git a/app/api/clients/route.ts b/app/api/clients/route.ts index bca320a..eb5b6c4 100644 --- a/app/api/clients/route.ts +++ b/app/api/clients/route.ts @@ -1,22 +1,16 @@ -import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { SDKManager } from "@/core/sdk/sdkManager"; import { validateAllowedIpsInput, validateClientExpiresAtInput, } from "@/core/Security/ipAllowlist"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; -const SESSION_COOKIE = "datastack_admin_session"; - export async function GET(req: NextRequest) { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "viewer" }); if (!auth.valid) { - return NextResponse.json( - { error: "Authentication required." }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } try { @@ -37,14 +31,9 @@ export async function GET(req: NextRequest) { } export async function POST(req: NextRequest) { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "operator" }); if (!auth.valid) { - return NextResponse.json( - { error: "Authentication required." }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } let body: Record; @@ -71,9 +60,17 @@ export async function POST(req: NextRequest) { expiresAt, } = body; - if (typeof clientId !== "string" || typeof name !== "string" || !clientId.trim() || !name.trim()) { + if ( + typeof clientId !== "string" || + typeof name !== "string" || + !clientId.trim() || + !name.trim() + ) { return NextResponse.json( - { error: "clientId and name are required strings", code: "VALIDATION_ERROR" }, + { + error: "clientId and name are required strings", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -87,17 +84,32 @@ export async function POST(req: NextRequest) { } const parsedRateLimit = rateLimit === undefined ? 100 : Number(rateLimit); - if (!Number.isInteger(parsedRateLimit) || parsedRateLimit < 1 || parsedRateLimit > 100000) { + if ( + !Number.isInteger(parsedRateLimit) || + parsedRateLimit < 1 || + parsedRateLimit > 100000 + ) { return NextResponse.json( - { error: "rateLimit must be an integer between 1 and 100000.", code: "VALIDATION_ERROR" }, + { + error: "rateLimit must be an integer between 1 and 100000.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } - const parsedQuotaMb = storageQuotaMb === undefined ? 0 : Number(storageQuotaMb); - if (!Number.isInteger(parsedQuotaMb) || parsedQuotaMb < 0 || parsedQuotaMb > 1048576) { + const parsedQuotaMb = + storageQuotaMb === undefined ? 0 : Number(storageQuotaMb); + if ( + !Number.isInteger(parsedQuotaMb) || + parsedQuotaMb < 0 || + parsedQuotaMb > 1048576 + ) { return NextResponse.json( - { error: "storageQuotaMb must be an integer between 0 and 1048576.", code: "VALIDATION_ERROR" }, + { + error: "storageQuotaMb must be an integer between 0 and 1048576.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -113,7 +125,10 @@ export async function POST(req: NextRequest) { const ipValidation = validateAllowedIpsInput(allowedIps); if (!ipValidation.valid) { return NextResponse.json( - { error: ipValidation.error || "Invalid allowedIps.", code: "VALIDATION_ERROR" }, + { + error: ipValidation.error || "Invalid allowedIps.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } @@ -125,11 +140,14 @@ export async function POST(req: NextRequest) { const expValidation = validateClientExpiresAtInput(expiresAt); if (!expValidation.valid) { return NextResponse.json( - { error: expValidation.error || "Invalid expiresAt.", code: "VALIDATION_ERROR" }, + { + error: expValidation.error || "Invalid expiresAt.", + code: "VALIDATION_ERROR", + }, { status: 400 }, ); } - normalizedExpiresAt = expValidation.normalized; + normalizedExpiresAt = expValidation.iso ?? null; } const existing = await SDKManager.getClientAsync(safeClientId); @@ -149,6 +167,21 @@ export async function POST(req: NextRequest) { expiresAt: normalizedExpiresAt, }); + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "CLIENT_CREATED", + target: newClient.clientId, + ipAddress: extractClientIp(req), + metadata: { + name: newClient.name, + rateLimit: newClient.rateLimit, + storageQuotaMb: newClient.storageQuotaMb, + allowedIps: newClient.allowedIps, + expiresAt: newClient.expiresAt, + }, + }); + return NextResponse.json({ client: newClient }, { status: 201 }); } catch (err: unknown) { const message = diff --git a/app/api/prometheus/route.ts b/app/api/prometheus/route.ts index 25a75aa..c6b7bf7 100644 --- a/app/api/prometheus/route.ts +++ b/app/api/prometheus/route.ts @@ -73,12 +73,13 @@ export async function GET(req: NextRequest) { ); } - const [clients, storageStats, diskHealth, bufferStats] = await Promise.all([ + const [clients, allFiles, diskHealth, bufferStats] = await Promise.all([ SDKManager.listClientsAsync(), - CSVStorageManager.getStatsAsync(), - CSVStorageManager.getDiskSpaceHealthAsync(), + CSVStorageManager.listAllFilesAsync(), + CSVStorageManager.getDiskUsageAsync(), DataBufferEngine.getBufferStatsAsync(), ]); + const storageStats = CSVStorageManager.formatStorageStats(allFiles); let activeSockets = 0; let bridgeUp = 0; @@ -117,7 +118,9 @@ export async function GET(req: NextRequest) { let activeClientsCount = 0; let revokedClientsCount = 0; + let totalEventsCounter = 0; for (const c of clients) { + totalEventsCounter += Math.max(0, Number(c.totalEvents) || 0); if (c.status === "revoked") { revokedClientsCount += 1; } else { @@ -135,6 +138,9 @@ export async function GET(req: NextRequest) { "# HELP datastack_active_sockets Currently connected Socket.IO SDK clients on Port 4000.", "# TYPE datastack_active_sockets gauge", `datastack_active_sockets ${activeSockets}`, + "# HELP datastack_events_total Cumulative events ingested across all registered clients.", + "# TYPE datastack_events_total counter", + `datastack_events_total ${Math.max(totalEventsCounter, storageStats.totalRows)}`, "# HELP datastack_buffer_events_total Total events currently queued in memory or Redis awaiting CSV flush.", "# TYPE datastack_buffer_events_total gauge", `datastack_buffer_events_total ${totalBufferedEvents}`, @@ -143,7 +149,7 @@ export async function GET(req: NextRequest) { `datastack_storage_files_total ${storageStats.totalFiles}`, "# HELP datastack_storage_bytes_total Total bytes occupied by CSV and gzipped CSV partition files on disk.", "# TYPE datastack_storage_bytes_total gauge", - `datastack_storage_bytes_total ${storageStats.totalSizeBytes}`, + `datastack_storage_bytes_total ${storageStats.totalBytes}`, "# HELP datastack_storage_rows_total Total telemetry rows persisted across all partition files.", "# TYPE datastack_storage_rows_total gauge", `datastack_storage_rows_total ${storageStats.totalRows}`, @@ -152,7 +158,7 @@ export async function GET(req: NextRequest) { `datastack_disk_used_percent ${diskHealth.usedPct}`, "# HELP datastack_disk_free_bytes Free bytes available on the underlying storage volume.", "# TYPE datastack_disk_free_bytes gauge", - `datastack_disk_free_bytes ${diskHealth.freeBytes}`, + `datastack_disk_free_bytes ${diskHealth.availableBytes}`, "# HELP datastack_disk_total_bytes Total capacity in bytes of the underlying storage volume.", "# TYPE datastack_disk_total_bytes gauge", `datastack_disk_total_bytes ${diskHealth.totalBytes}`, @@ -177,7 +183,7 @@ export async function GET(req: NextRequest) { ); const perClientBytes = new Map(); - for (const f of storageStats.files) { + for (const f of allFiles) { perClientBytes.set( f.clientId, (perClientBytes.get(f.clientId) || 0) + f.sizeBytes, diff --git a/app/api/settings/route.ts b/app/api/settings/route.ts index fdaea49..a589257 100644 --- a/app/api/settings/route.ts +++ b/app/api/settings/route.ts @@ -2,12 +2,11 @@ import { SettingsManager, type AdminSettingsUpdateInput, } from "@/core/adminSettings/settingsManager"; -import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { SecretVaultError } from "@/core/Security/secretVault"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; -const SESSION_COOKIE = "datastack_admin_session"; - const ALLOWED_SETTING_KEYS = new Set([ "maxEventsPerSec", "maxPayloadSizeKb", @@ -69,19 +68,9 @@ function parseOptionalString( export async function GET(req: NextRequest) { try { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "viewer" }); if (!auth.valid) { - return NextResponse.json( - { - error: - auth.reason === "UNAUTHORIZED" - ? "Authentication required." - : "Admin authentication is temporarily unavailable.", - }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } const settings = await SettingsManager.getPublicSettingsAsync(); @@ -94,19 +83,9 @@ export async function GET(req: NextRequest) { export async function POST(req: NextRequest) { try { - const auth = await verifyRequestAdminSession( - req.cookies.get(SESSION_COOKIE)?.value, - ); + const auth = await requireAdminApiSession(req, { minRole: "owner" }); if (!auth.valid) { - return NextResponse.json( - { - error: - auth.reason === "UNAUTHORIZED" - ? "Authentication required." - : "Admin authentication is temporarily unavailable.", - }, - { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, - ); + return auth.response; } let rawBody: unknown; @@ -368,6 +347,16 @@ export async function POST(req: NextRequest) { } await SettingsManager.updateSettingsAsync(validated); + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "SETTINGS_UPDATED", + target: "admin_settings", + ipAddress: extractClientIp(req), + metadata: { + updatedFields: Object.keys(validated), + }, + }); const settings = await SettingsManager.getPublicSettingsAsync(); const bridgeSync = SettingsManager.getLastBridgeSyncStatus(); return NextResponse.json({ diff --git a/app/api/settings/webhook-test/route.ts b/app/api/settings/webhook-test/route.ts index e038c9c..3e56bb9 100644 --- a/app/api/settings/webhook-test/route.ts +++ b/app/api/settings/webhook-test/route.ts @@ -1,11 +1,12 @@ import { SettingsManager } from "@/core/adminSettings/settingsManager"; import { dispatchWebhookAlertAsync } from "@/core/alerts/webhookNotifier"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { SecretVaultError } from "@/core/Security/secretVault"; -import { requireAdminApiSession } from "@/lib/adminAuth"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { - const auth = await requireAdminApiSession(req); + const auth = await requireAdminApiSession(req, { minRole: "owner" }); if (!auth.valid) { return auth.response; } @@ -73,7 +74,7 @@ export async function POST(req: NextRequest) { title: "Webhook Verification", message: "Test alert dispatched from DataStack Admin Console.", details: { - triggeredBy: auth.auth.user?.email || "admin", + triggeredBy: auth.auth.session.email || "admin", }, }, { @@ -85,6 +86,18 @@ export async function POST(req: NextRequest) { { bypassCooldown: true }, ); + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "WEBHOOK_TEST_TRIGGERED", + target: webhookUrl, + ipAddress: extractClientIp(req), + metadata: { + ok: result.ok, + status: result.status, + }, + }); + if (!result.ok) { return NextResponse.json( { diff --git a/app/api/storage/archive/route.ts b/app/api/storage/archive/route.ts index c0daaf6..8f3a206 100644 --- a/app/api/storage/archive/route.ts +++ b/app/api/storage/archive/route.ts @@ -1,4 +1,5 @@ import { SettingsManager } from "@/core/adminSettings/settingsManager"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; import { SecretVaultError } from "@/core/Security/secretVault"; import { CSVStorageManager } from "@/core/Storage/csvStorage"; import { @@ -6,11 +7,11 @@ import { normalizeS3EndpointUrl, uploadFileToS3, } from "@/core/Storage/s3Archive"; -import { requireAdminApiSession } from "@/lib/adminAuth"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { - const auth = await requireAdminApiSession(req); + const auth = await requireAdminApiSession(req, { minRole: "operator" }); if (!auth.valid) { return auth.response; } @@ -135,6 +136,20 @@ export async function POST(req: NextRequest) { }); } + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "S3_ARCHIVE_TRIGGERED", + target: s3Config.bucket, + ipAddress: extractClientIp(req), + metadata: { + archivedCount: archiveResult.archivedCount, + prunedCount: archiveResult.prunedCount, + compressedCount, + pruneAfterUpload, + }, + }); + return NextResponse.json({ success: archiveResult.errors.length === 0, compressedCount, diff --git a/app/api/storage/route.ts b/app/api/storage/route.ts index 0fbdc9a..2c974e1 100644 --- a/app/api/storage/route.ts +++ b/app/api/storage/route.ts @@ -50,7 +50,7 @@ export async function GET(req: NextRequest) { } export async function POST(req: NextRequest) { - const auth = await requireAdminApiSession(req); + const auth = await requireAdminApiSession(req, { minRole: 'operator' }); if (!auth.valid) { return auth.response; } diff --git a/app/api/storage/s3-test/route.ts b/app/api/storage/s3-test/route.ts index 2ae5643..a60f980 100644 --- a/app/api/storage/s3-test/route.ts +++ b/app/api/storage/s3-test/route.ts @@ -11,7 +11,7 @@ import { requireAdminApiSession } from "@/lib/adminAuth"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { - const auth = await requireAdminApiSession(req); + const auth = await requireAdminApiSession(req, { minRole: "operator" }); if (!auth.valid) { return auth.response; } diff --git a/app/api/team/[id]/route.ts b/app/api/team/[id]/route.ts new file mode 100644 index 0000000..b23b383 --- /dev/null +++ b/app/api/team/[id]/route.ts @@ -0,0 +1,136 @@ +import { + deleteAdminTeamMemberAsync, + PrimaryOwnerProtectedError, + TeamMemberValidationError, + updateAdminTeamMemberAsync, +} from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function PATCH( + req: NextRequest, + { params }: { params: Promise<{ id: string }> }, +) { + const auth = await requireAdminApiSession(req, { minRole: "owner" }); + if (!auth.valid) { + return auth.response; + } + + let body: Record; + try { + const parsed = await req.json(); + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + return NextResponse.json( + { + error: "Team member update payload must be a JSON object.", + code: "VALIDATION_ERROR", + }, + { status: 400 }, + ); + } + body = parsed as Record; + } catch { + return NextResponse.json( + { error: "Invalid JSON request payload.", code: "INVALID_JSON" }, + { status: 400 }, + ); + } + + try { + const { id } = await params; + const updated = await updateAdminTeamMemberAsync(id, { + role: typeof body.role === "string" ? body.role : undefined, + status: typeof body.status === "string" ? body.status : undefined, + password: typeof body.password === "string" ? body.password : undefined, + revokeSessions: body.revokeSessions === true, + }); + + const action = + body.status === "disabled" || body.revokeSessions === true + ? "TEAM_MEMBER_REVOKED" + : "TEAM_MEMBER_UPDATED"; + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action, + target: updated.email, + ipAddress: extractClientIp(req), + metadata: { + memberId: updated.id, + role: updated.role, + status: updated.status, + passwordReset: Boolean(body.password), + }, + }); + + return NextResponse.json({ success: true, member: updated }); + } catch (err: unknown) { + if (err instanceof PrimaryOwnerProtectedError) { + return NextResponse.json( + { error: err.message, code: err.code }, + { status: 400 }, + ); + } + if (err instanceof TeamMemberValidationError) { + const status = err.code === "TEAM_MEMBER_NOT_FOUND" ? 404 : 400; + return NextResponse.json( + { error: err.message, code: err.code }, + { status }, + ); + } + const message = + err instanceof Error ? err.message : "Unable to update team member."; + return NextResponse.json({ error: message }, { status: 500 }); + } +} + +export async function DELETE( + req: NextRequest, + { params }: { params: Promise<{ id: string }> }, +) { + const auth = await requireAdminApiSession(req, { minRole: "owner" }); + if (!auth.valid) { + return auth.response; + } + + try { + const { id } = await params; + const deleted = await deleteAdminTeamMemberAsync(id); + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "TEAM_MEMBER_DELETED", + target: deleted.email, + ipAddress: extractClientIp(req), + metadata: { + memberId: deleted.id, + }, + }); + + return NextResponse.json({ + success: true, + deletedId: deleted.id, + email: deleted.email, + }); + } catch (err: unknown) { + if (err instanceof PrimaryOwnerProtectedError) { + return NextResponse.json( + { error: err.message, code: err.code }, + { status: 400 }, + ); + } + if (err instanceof TeamMemberValidationError) { + const status = err.code === "TEAM_MEMBER_NOT_FOUND" ? 404 : 400; + return NextResponse.json( + { error: err.message, code: err.code }, + { status }, + ); + } + const message = + err instanceof Error ? err.message : "Unable to delete team member."; + return NextResponse.json({ error: message }, { status: 500 }); + } +} diff --git a/app/api/team/route.ts b/app/api/team/route.ts new file mode 100644 index 0000000..cc44daf --- /dev/null +++ b/app/api/team/route.ts @@ -0,0 +1,96 @@ +import { + createAdminTeamMemberAsync, + listAdminTeamMembersAsync, + TeamMemberValidationError, +} from "@/core/auth/adminUsers"; +import { recordAdminAuditLogAsync } from "@/core/auth/auditLog"; +import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(req: NextRequest) { + const auth = await requireAdminApiSession(req, { minRole: "viewer" }); + if (!auth.valid) { + return auth.response; + } + + try { + const members = await listAdminTeamMembersAsync(); + return NextResponse.json({ + members, + currentRole: auth.auth.session.role || "owner", + currentEmail: auth.auth.session.email, + }); + } catch (err: unknown) { + const message = + err instanceof Error ? err.message : "Unable to list team members."; + return NextResponse.json({ error: message }, { status: 500 }); + } +} + +export async function POST(req: NextRequest) { + const auth = await requireAdminApiSession(req, { minRole: "owner" }); + if (!auth.valid) { + return auth.response; + } + + let body: Record; + try { + const parsed = await req.json(); + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + return NextResponse.json( + { + error: "Team member payload must be a JSON object.", + code: "VALIDATION_ERROR", + }, + { status: 400 }, + ); + } + body = parsed as Record; + } catch { + return NextResponse.json( + { error: "Invalid JSON request payload.", code: "INVALID_JSON" }, + { status: 400 }, + ); + } + + try { + const email = typeof body.email === "string" ? body.email : ""; + const password = typeof body.password === "string" ? body.password : ""; + const role = typeof body.role === "string" ? body.role : "viewer"; + + const created = await createAdminTeamMemberAsync({ + email, + password, + role, + invitedBy: auth.auth.session.email, + }); + + await recordAdminAuditLogAsync({ + actorEmail: auth.auth.session.email, + actorRole: auth.auth.session.role || "owner", + action: "TEAM_MEMBER_CREATED", + target: created.email, + ipAddress: extractClientIp(req), + metadata: { + memberId: created.id, + role: created.role, + }, + }); + + return NextResponse.json( + { success: true, member: created }, + { status: 201 }, + ); + } catch (err: unknown) { + if (err instanceof TeamMemberValidationError) { + const status = err.code === "TEAM_MEMBER_EXISTS" ? 409 : 400; + return NextResponse.json( + { error: err.message, code: err.code }, + { status }, + ); + } + const message = + err instanceof Error ? err.message : "Unable to create team member."; + return NextResponse.json({ error: message }, { status: 500 }); + } +} diff --git a/app/vcon/audit/page.tsx b/app/vcon/audit/page.tsx new file mode 100644 index 0000000..3e693d5 --- /dev/null +++ b/app/vcon/audit/page.tsx @@ -0,0 +1,5 @@ +import { AuditLogViewer } from '@/components/sections/AuditLogViewer'; + +export default function AuditPage() { + return ; +} diff --git a/app/vcon/sdk/page.tsx b/app/vcon/sdk/page.tsx index e2309bf..e1f3480 100644 --- a/app/vcon/sdk/page.tsx +++ b/app/vcon/sdk/page.tsx @@ -20,8 +20,11 @@ interface SdkClientItem { token?: string; } +type SdkLanguageTab = 'python' | 'node' | 'curl'; + export default function SdkGuidePage() { const { success, error } = useToast(); + const [activeTab, setActiveTab] = useState('python'); const [clients, setClients] = useState(() => getCached('vcon:clients', []) ); @@ -128,13 +131,14 @@ export default function SdkGuidePage() { const sampleToken = loadingToken ? 'LOADING_CLIENT_JWT_TOKEN...' : resolvedToken || 'YOUR_SIGNED_CLIENT_JWT_TOKEN'; + const clientIdValue = activeClient?.clientId || ''; - const configSnippet = `# config.datastack.py — Place in your Python project root + const pythonConfigSnippet = `# config.datastack.py — Place in your Python project root DATASTACK_CONFIG = { "SERVER_URL": "${serverUrl}", "API_URL": "${apiUrl}", "CLIENT_TOKEN": "${sampleToken}", - "CLIENT_ID": "${activeClient?.clientId || ''}", + "CLIENT_ID": "${clientIdValue}", "AUTO_CONNECT": True, "ENABLE_HTTP_FALLBACK": True, "HEARTBEAT_INTERVAL_SEC": 5.0, @@ -173,6 +177,109 @@ print("SDK Status:", client.status()) client.disconnect()`; + const nodeConfigSnippet = `// datastack.config.json — Place in your Node.js / TypeScript project root +{ + "serverUrl": "${serverUrl}", + "apiUrl": "${apiUrl}", + "clientToken": "${sampleToken}", + "clientId": "${clientIdValue}", + "autoConnect": true, + "enableHttpFallback": true, + "heartbeatIntervalMs": 5000, + "maxRetries": 3, + "batchChunkSize": 250, + "offlineSpoolEnabled": true, + "offlineSpoolDir": ".datastack_spool" +}`; + + const nodeSnippet = `import { DataStack } from '@datastack/sdk'; + +// Loads datastack.config.json or explicit constructor options +const client = new DataStack({ + serverUrl: '${serverUrl}', + apiUrl: '${apiUrl}', + token: '${sampleToken}', + clientId: '${clientIdValue}', + autoConnect: true, +}); + +// 1. Single Event (Socket.IO Primary + Auto HTTP Fallback + Offline Spool) +const res = await client.send({ + event_type: 'checkout_completed', + order_id: 'ord_9942', + amount_usd: 149.5, +}); +console.log('Event response:', res); + +// 2. Batch Events (Auto-chunked & 413/429 resilient) +const batchRes = await client.sendBatch([ + { sensor: 'temp', value: 24.5, unit: 'C' }, + { sensor: 'humidity', value: 61.2, unit: '%' }, +]); +console.log('Batch response:', batchRes); + +// 3. Direct File Ingestion (.csv, .json, .jsonl, .ndjson with SHA-256 deterministic eventId) +const fileRes = await client.sendFile('./events_dataset.csv', { chunkSize: 250 }); +console.log('File ingestion:', fileRes); + +// 4. Graceful Shutdown Drain (awaits in-flight + drains offline spool) +await client.shutdown(5000);`; + + const curlConfigSnippet = `# Environment exports for HTTP / cURL ingestion +export DATASTACK_API_URL="${apiUrl}" +export DATASTACK_CLIENT_ID="${clientIdValue}" +export DATASTACK_CLIENT_TOKEN="${sampleToken}"`; + + const curlSnippet = `# 1. Single Event Ingestion via HTTP POST /api/events +curl -X POST "${apiUrl}/api/events" \\ + -H "Content-Type: application/json" \\ + -H "Authorization: Bearer ${sampleToken}" \\ + -d '{ + "clientId": "${clientIdValue}", + "data": { + "event_type": "deployment_finished", + "service": "billing-api", + "status": "healthy" + } + }' + +# 2. Batch Event Ingestion via HTTP POST /api/events +curl -X POST "${apiUrl}/api/events" \\ + -H "Content-Type: application/json" \\ + -H "Authorization: Bearer ${sampleToken}" \\ + -d '{ + "clientId": "${clientIdValue}", + "batch": [ + { "data": { "metric": "cpu_pct", "value": 31.4 } }, + { "data": { "metric": "mem_pct", "value": 58.2 } } + ] + }'`; + + const activeConfigSnippet = + activeTab === 'node' + ? nodeConfigSnippet + : activeTab === 'curl' + ? curlConfigSnippet + : pythonConfigSnippet; + const activeCodeSnippet = + activeTab === 'node' + ? nodeSnippet + : activeTab === 'curl' + ? curlSnippet + : pythonSnippet; + const activeConfigLabel = + activeTab === 'node' + ? 'datastack.config.json' + : activeTab === 'curl' + ? 'Environment Variables' + : 'config.datastack.py'; + const activeCodeLabel = + activeTab === 'node' + ? 'Node.js / TypeScript SDK snippet' + : activeTab === 'curl' + ? 'cURL REST snippet' + : 'Python SDK snippet'; + const copySnippet = async (text: string, label: string) => { try { if (!navigator?.clipboard?.writeText) { @@ -192,7 +299,7 @@ client.disconnect()`; return ( {resolvedToken && ( @@ -211,7 +318,7 @@ client.disconnect()`; variant="secondary" icon="FileCode" disabled={loadingToken} - onClick={() => void copySnippet(configSnippet, 'config.datastack.py')} + onClick={() => void copySnippet(activeConfigSnippet, activeConfigLabel)} > Copy Config @@ -219,7 +326,7 @@ client.disconnect()`; size="xs" variant="secondary" icon="Copy" - onClick={() => void copySnippet(pythonSnippet, 'Python SDK snippet')} + onClick={() => void copySnippet(activeCodeSnippet, activeCodeLabel)} > Copy Code @@ -228,7 +335,16 @@ client.disconnect()`; > - -
- - pip install /path/to/datastack/sdk/python/datastack - +
+
+ +
-

- Self-hosted package bundled in sdk/python/datastack. From inside the repo run{' '} - pip install ./sdk/python/datastack, or build a portable wheel with{' '} - pip wheel ./sdk/python/datastack -w dist/ for external projects. -

- + + {activeTab === 'node' + ? 'sdk/node/datastack' + : activeTab === 'curl' + ? 'POST /api/events' + : 'sdk/python/datastack'} + +
+ + {activeTab === 'python' && ( + +
+ + pip install /path/to/datastack/sdk/python/datastack + +
+

+ Self-hosted package bundled in sdk/python/datastack. From inside the repo run{' '} + pip install ./sdk/python/datastack, or build a portable wheel with{' '} + pip wheel ./sdk/python/datastack -w dist/ for external projects. +

+
+ )} + + {activeTab === 'node' && ( + +
+ + npm install /path/to/datastack/sdk/node/datastack + +
+

+ Official Node.js & TypeScript package bundled in{' '} + sdk/node/datastack (Node >= 20). Supports Socket.IO primary streaming, automatic{' '} + POST /api/events fallback, deterministic{' '} + sendFile() SHA-256 deduplication, offline disk spooling, and graceful{' '} + await client.shutdown(). +

+
+ )} @@ -296,14 +479,14 @@ client.disconnect()`; size="xs" variant="ghost" icon="Copy" - aria-label="Copy config.datastack.py" + aria-label={`Copy ${activeConfigLabel}`} disabled={loadingToken} - onClick={() => void copySnippet(configSnippet, 'config.datastack.py')} + onClick={() => void copySnippet(activeConfigSnippet, activeConfigLabel)} />
} > - {configSnippet} + {activeConfigSnippet} void copySnippet(pythonSnippet, 'Python SDK snippet')} + aria-label={`Copy ${activeCodeLabel}`} + onClick={() => void copySnippet(activeCodeSnippet, activeCodeLabel)} /> } > - {pythonSnippet} + {activeCodeSnippet}
); diff --git a/app/vcon/team/page.tsx b/app/vcon/team/page.tsx new file mode 100644 index 0000000..f3d1af0 --- /dev/null +++ b/app/vcon/team/page.tsx @@ -0,0 +1,5 @@ +import { TeamWorkspace } from '@/components/sections/TeamWorkspace'; + +export default function TeamPage() { + return ; +} diff --git a/components/layout/Sidebar.tsx b/components/layout/Sidebar.tsx index 5730f28..da638c8 100644 --- a/components/layout/Sidebar.tsx +++ b/components/layout/Sidebar.tsx @@ -34,6 +34,8 @@ const NAVIGATION_SECTIONS = [ items: [ { name: 'Python SDK', href: '/vcon/sdk', icon: 'FileCode2' }, { name: 'Simulator', href: '/vcon/simulator', icon: 'Play' }, + { name: 'Team & RBAC', href: '/vcon/team', icon: 'Users' }, + { name: 'Audit Log', href: '/vcon/audit', icon: 'FileText' }, { name: 'Settings', href: '/vcon/settings', icon: 'Sliders' }, { name: 'Profile', href: '/vcon/profile', icon: 'User' }, ], diff --git a/components/sections/AuditLogViewer.tsx b/components/sections/AuditLogViewer.tsx new file mode 100644 index 0000000..73368c4 --- /dev/null +++ b/components/sections/AuditLogViewer.tsx @@ -0,0 +1,236 @@ +'use client'; + +import { PageShell } from '@/components/layout/PageShell'; +import { Button } from '@/components/ui/Button'; +import { Input, Select } from '@/components/ui/Input'; +import { Panel } from '@/components/ui/Panel'; +import { StatStrip } from '@/components/ui/StatStrip'; +import { + Table, + TableBody, + TableCell, + TableEmpty, + TableHead, + TableHeader, + TableRow, + TableWrap, +} from '@/components/ui/Table'; +import { useToast } from '@/components/ui/Toast'; +import Link from 'next/link'; +import React, { useCallback, useEffect, useState } from 'react'; + +interface AuditLogItem { + id: string; + actorEmail: string; + actorRole: string; + action: string; + target: string; + ipAddress: string; + metadata: Record; + createdAt: string; +} + +const ACTION_FILTER_OPTIONS = [ + { value: 'ALL', label: 'All Governance Actions' }, + { value: 'ADMIN_LOGIN', label: 'ADMIN_LOGIN' }, + { value: 'ADMIN_PASSWORD_UPDATED', label: 'ADMIN_PASSWORD_UPDATED' }, + { value: 'TEAM_MEMBER_CREATED', label: 'TEAM_MEMBER_CREATED' }, + { value: 'TEAM_MEMBER_UPDATED', label: 'TEAM_MEMBER_UPDATED' }, + { value: 'TEAM_MEMBER_REVOKED', label: 'TEAM_MEMBER_REVOKED' }, + { value: 'TEAM_MEMBER_DELETED', label: 'TEAM_MEMBER_DELETED' }, + { value: 'CLIENT_CREATED', label: 'CLIENT_CREATED' }, + { value: 'CLIENT_UPDATED', label: 'CLIENT_UPDATED' }, + { value: 'CLIENT_TOKEN_ROTATED', label: 'CLIENT_TOKEN_ROTATED' }, + { value: 'CLIENT_DELETED', label: 'CLIENT_DELETED' }, + { value: 'SETTINGS_UPDATED', label: 'SETTINGS_UPDATED' }, + { value: 'S3_ARCHIVE_TRIGGERED', label: 'S3_ARCHIVE_TRIGGERED' }, + { value: 'WEBHOOK_TEST_TRIGGERED', label: 'WEBHOOK_TEST_TRIGGERED' }, +]; + +export function AuditLogViewer() { + const { error } = useToast(); + const [logs, setLogs] = useState([]); + const [loading, setLoading] = useState(true); + const [actionFilter, setActionFilter] = useState('ALL'); + const [searchQuery, setSearchQuery] = useState(''); + + const fetchAuditLogs = useCallback(async () => { + setLoading(true); + try { + const params = new URLSearchParams(); + params.set('limit', '150'); + if (actionFilter && actionFilter !== 'ALL') { + params.set('action', actionFilter); + } + if (searchQuery.trim()) { + params.set('q', searchQuery.trim()); + } + const res = await fetch(`/api/audit?${params.toString()}`, { + credentials: 'include', + cache: 'no-store', + }); + if (res.status === 401) { + window.location.assign('/vcon/login'); + return; + } + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Failed to load audit logs.'); + } + setLogs(Array.isArray(data.logs) ? data.logs : []); + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to load audit logs.'); + } finally { + setLoading(false); + } + }, [actionFilter, searchQuery, error]); + + useEffect(() => { + void fetchAuditLogs(); + }, [fetchAuditLogs]); + + const uniqueActors = new Set(logs.map((l) => l.actorEmail)).size; + const mutationCount = logs.filter((l) => l.action !== 'ADMIN_LOGIN').length; + + return ( + + + + + + + } + > +
+ + + +
+ setSearchQuery(e.target.value)} + /> +
+
+ + + + + + Timestamp (UTC) + Actor + Action + Target Resource + Source IP + Metadata + + + + {logs.length === 0 ? ( + + ) : ( + logs.map((row) => ( + + + + {row.createdAt + ? row.createdAt.replace('T', ' ').slice(0, 19) + : '—'} + + + +
+ + {row.actorEmail} + + + {row.actorRole} + +
+
+ + + {row.action} + + + + + {row.target} + + + + + {row.ipAddress || 'unknown'} + + + + + {JSON.stringify(row.metadata || {})} + + +
+ )) + )} +
+
+
+ + +
+ ); +} + +export default AuditLogViewer; diff --git a/components/sections/TeamWorkspace.tsx b/components/sections/TeamWorkspace.tsx new file mode 100644 index 0000000..c03ae71 --- /dev/null +++ b/components/sections/TeamWorkspace.tsx @@ -0,0 +1,447 @@ +'use client'; + +import { PageShell } from '@/components/layout/PageShell'; +import { Button } from '@/components/ui/Button'; +import { Input, Select } from '@/components/ui/Input'; +import { Modal } from '@/components/ui/Modal'; +import { Panel } from '@/components/ui/Panel'; +import { StatStrip } from '@/components/ui/StatStrip'; +import { + Table, + TableBody, + TableCell, + TableEmpty, + TableHead, + TableHeader, + TableRow, + TableWrap, +} from '@/components/ui/Table'; +import { useToast } from '@/components/ui/Toast'; +import Link from 'next/link'; +import React, { useCallback, useEffect, useState } from 'react'; + +interface TeamMemberItem { + id: string; + email: string; + role: 'owner' | 'admin' | 'operator' | 'viewer'; + status: 'active' | 'disabled'; + tokenVersion: number; + invitedBy: string; + isPrimaryOwner: boolean; + createdAt: string; + updatedAt: string; +} + +export function TeamWorkspace() { + const { success, error } = useToast(); + const [members, setMembers] = useState([]); + const [currentRole, setCurrentRole] = useState('owner'); + const [loading, setLoading] = useState(true); + + const [createOpen, setCreateOpen] = useState(false); + const [newEmail, setNewEmail] = useState(''); + const [newPassword, setNewPassword] = useState(''); + const [newRole, setNewRole] = useState('viewer'); + const [creating, setCreating] = useState(false); + + const [busyId, setBusyId] = useState(null); + + const fetchMembers = useCallback(async () => { + setLoading(true); + try { + const res = await fetch('/api/team', { + credentials: 'include', + cache: 'no-store', + }); + if (res.status === 401) { + window.location.assign('/vcon/login'); + return; + } + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Failed to load team members.'); + } + setMembers(Array.isArray(data.members) ? data.members : []); + if (data.currentRole) { + setCurrentRole(data.currentRole); + } + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to load team members.'); + } finally { + setLoading(false); + } + }, [error]); + + useEffect(() => { + void fetchMembers(); + }, [fetchMembers]); + + const isOwner = currentRole === 'owner'; + + const handleCreateMember = async (e: React.FormEvent) => { + e.preventDefault(); + if (!newEmail.trim() || !newPassword) { + error('Email and password are required.'); + return; + } + setCreating(true); + try { + const res = await fetch('/api/team', { + method: 'POST', + credentials: 'include', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + email: newEmail.trim(), + password: newPassword, + role: newRole, + }), + }); + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Failed to create team member.'); + } + success(`Added ${data.member?.email || newEmail} (${newRole}).`); + setNewEmail(''); + setNewPassword(''); + setNewRole('viewer'); + setCreateOpen(false); + await fetchMembers(); + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to create team member.'); + } finally { + setCreating(false); + } + }; + + const handleRoleChange = async (member: TeamMemberItem, nextRole: string) => { + if (member.isPrimaryOwner) { + error('Primary Owner role cannot be modified.'); + return; + } + setBusyId(member.id); + try { + const res = await fetch(`/api/team/${encodeURIComponent(member.id)}`, { + method: 'PATCH', + credentials: 'include', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ role: nextRole }), + }); + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Role update failed.'); + } + success(`Updated ${member.email} role to ${nextRole}.`); + await fetchMembers(); + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to update role.'); + } finally { + setBusyId(null); + } + }; + + const handleToggleStatus = async (member: TeamMemberItem) => { + if (member.isPrimaryOwner) { + error('Primary Owner cannot be disabled.'); + return; + } + const nextStatus = member.status === 'active' ? 'disabled' : 'active'; + setBusyId(member.id); + try { + const res = await fetch(`/api/team/${encodeURIComponent(member.id)}`, { + method: 'PATCH', + credentials: 'include', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ status: nextStatus, revokeSessions: true }), + }); + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Status update failed.'); + } + success( + nextStatus === 'disabled' + ? `Revoked access and active sessions for ${member.email}.` + : `Re-enabled access for ${member.email}.` + ); + await fetchMembers(); + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to update member status.'); + } finally { + setBusyId(null); + } + }; + + const handleDeleteMember = async (member: TeamMemberItem) => { + if (member.isPrimaryOwner) { + error('Primary Owner cannot be deleted.'); + return; + } + setBusyId(member.id); + try { + const res = await fetch(`/api/team/${encodeURIComponent(member.id)}`, { + method: 'DELETE', + credentials: 'include', + }); + const data = await res.json(); + if (!res.ok) { + throw new Error(data?.error || 'Delete failed.'); + } + success(`Removed ${member.email} from team.`); + await fetchMembers(); + } catch (err: unknown) { + error(err instanceof Error ? err.message : 'Unable to delete team member.'); + } finally { + setBusyId(null); + } + }; + + const activeCount = members.filter((m) => m.status === 'active').length; + const ownerCount = members.filter((m) => m.role === 'owner').length; + const operatorCount = members.filter( + (m) => m.role === 'admin' || m.role === 'operator' + ).length; + const viewerCount = members.filter((m) => m.role === 'viewer').length; + + return ( + + + + + + {isOwner && ( + + )} + + } + > +
+ + + + + + + + Email + Role + Status + Token Version + Created + Actions + + + + {members.length === 0 ? ( + + ) : ( + members.map((row) => ( + + +
+ + {row.email} + + {row.isPrimaryOwner && ( + + Primary Owner + + )} +
+
+ + {row.isPrimaryOwner || !isOwner ? ( + + {row.role} + + ) : ( +
+
+
+
+ + setCreateOpen(false)} + title="Add Administrator / Team Member" + > +
+ setNewEmail(e.target.value)} + required + /> + setNewPassword(e.target.value)} + required + /> +