feat(client): implement per-client storage and security controls

- Added storage quota, IP allowlisting, and expiration logic to client management APIs.
- Updated CSV storage manager to support auto-compression and improved partition file parsing.
- Integrated security checks into event ingestion and data retrieval routes.
This commit is contained in:
Team Dark Devil committed 2026-11-19 23:02:27 +06:00
1 parent 89e4d68690
commit 2873b9904a
32 files changed
+6075 -241

No files matched your search

+190 -3
View File
@@ -1,8 +1,9 @@
import {
SettingsManager,
type AdminSettings,
type AdminSettingsUpdateInput,
} from "@/core/adminSettings/settingsManager";
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { SecretVaultError } from "@/core/Security/secretVault";
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
@@ -16,6 +17,24 @@ const ALLOWED_SETTING_KEYS = new Set([
"enableIdempotency",
"autoPruneDays",
"jwtSecret",
"compressAfterDays",
"enableS3Backup",
"s3Endpoint",
"s3Region",
"s3Bucket",
"s3Prefix",
"s3AccessKeyId",
"s3SecretAccessKey",
"clearS3SecretKey",
"diskPauseThresholdPct",
"enableWebhookAlerts",
"webhookUrl",
"webhookSecret",
"clearWebhookSecret",
"webhookCooldownSec",
"enablePrometheusMetrics",
"prometheusMetricsToken",
"clearPrometheusToken",
]);
function parseBoundedInt(
@@ -33,6 +52,21 @@ function parseBoundedInt(
return raw;
}
function parseOptionalString(
raw: unknown,
field: string,
maxLength: number,
): string {
if (typeof raw !== "string") {
throw new Error(`${field} must be a string.`);
}
const trimmed = raw.trim();
if (trimmed.length > maxLength) {
throw new Error(`${field} must be at most ${maxLength} characters.`);
}
return trimmed;
}
export async function GET(req: NextRequest) {
try {
const auth = await verifyRequestAdminSession(
@@ -109,7 +143,7 @@ export async function POST(req: NextRequest) {
);
}
const validated: Partial<AdminSettings> = {};
const validated: AdminSettingsUpdateInput = {};
try {
if (body.maxEventsPerSec !== undefined) {
validated.maxEventsPerSec = parseBoundedInt(
@@ -159,14 +193,161 @@ export async function POST(req: NextRequest) {
3650,
);
}
if (body.compressAfterDays !== undefined) {
validated.compressAfterDays = parseBoundedInt(
body.compressAfterDays,
"compressAfterDays",
0,
3650,
);
}
if (body.diskPauseThresholdPct !== undefined) {
const pct = parseBoundedInt(
body.diskPauseThresholdPct,
"diskPauseThresholdPct",
0,
99,
);
if (pct !== 0 && pct < 50) {
throw new Error(
"diskPauseThresholdPct must be 0 (disabled) or between 50 and 99.",
);
}
validated.diskPauseThresholdPct = pct;
}
if (body.enableIdempotency !== undefined) {
if (typeof body.enableIdempotency !== "boolean") {
throw new Error("enableIdempotency must be a boolean.");
}
validated.enableIdempotency = body.enableIdempotency;
}
if (body.enableS3Backup !== undefined) {
if (typeof body.enableS3Backup !== "boolean") {
throw new Error("enableS3Backup must be a boolean.");
}
validated.enableS3Backup = body.enableS3Backup;
}
if (body.s3Endpoint !== undefined) {
const ep = parseOptionalString(body.s3Endpoint, "s3Endpoint", 512);
if (ep) {
let parsedEp: URL;
try {
parsedEp = new URL(
/^https?:\/\//i.test(ep) ? ep : `https://${ep}`,
);
} catch {
throw new Error("s3Endpoint must be a valid http(s) URL.");
}
if (parsedEp.protocol !== "http:" && parsedEp.protocol !== "https:") {
throw new Error("s3Endpoint must use http:// or https://.");
}
}
validated.s3Endpoint = ep;
}
if (body.s3Region !== undefined) {
validated.s3Region =
parseOptionalString(body.s3Region, "s3Region", 64) || "auto";
}
if (body.s3Bucket !== undefined) {
validated.s3Bucket = parseOptionalString(
body.s3Bucket,
"s3Bucket",
128,
);
}
if (body.s3Prefix !== undefined) {
validated.s3Prefix = parseOptionalString(
body.s3Prefix,
"s3Prefix",
256,
);
}
if (body.s3AccessKeyId !== undefined) {
validated.s3AccessKeyId = parseOptionalString(
body.s3AccessKeyId,
"s3AccessKeyId",
256,
);
}
if (body.s3SecretAccessKey !== undefined) {
validated.s3SecretAccessKey = parseOptionalString(
body.s3SecretAccessKey,
"s3SecretAccessKey",
512,
);
}
if (body.clearS3SecretKey !== undefined) {
if (typeof body.clearS3SecretKey !== "boolean") {
throw new Error("clearS3SecretKey must be a boolean.");
}
validated.clearS3SecretKey = body.clearS3SecretKey;
}
if (body.enableWebhookAlerts !== undefined) {
if (typeof body.enableWebhookAlerts !== "boolean") {
throw new Error("enableWebhookAlerts must be a boolean.");
}
validated.enableWebhookAlerts = body.enableWebhookAlerts;
}
if (body.webhookUrl !== undefined) {
const wh = parseOptionalString(body.webhookUrl, "webhookUrl", 1024);
if (wh) {
let parsedWh: URL;
try {
parsedWh = new URL(wh);
} catch {
throw new Error("webhookUrl must be a valid http(s) URL.");
}
if (parsedWh.protocol !== "http:" && parsedWh.protocol !== "https:") {
throw new Error("webhookUrl must use http:// or https://.");
}
}
validated.webhookUrl = wh;
}
if (body.webhookSecret !== undefined) {
validated.webhookSecret = parseOptionalString(
body.webhookSecret,
"webhookSecret",
512,
);
}
if (body.clearWebhookSecret !== undefined) {
if (typeof body.clearWebhookSecret !== "boolean") {
throw new Error("clearWebhookSecret must be a boolean.");
}
validated.clearWebhookSecret = body.clearWebhookSecret;
}
if (body.webhookCooldownSec !== undefined) {
validated.webhookCooldownSec = parseBoundedInt(
body.webhookCooldownSec,
"webhookCooldownSec",
10,
86400,
);
}
if (body.enablePrometheusMetrics !== undefined) {
if (typeof body.enablePrometheusMetrics !== "boolean") {
throw new Error("enablePrometheusMetrics must be a boolean.");
}
validated.enablePrometheusMetrics = body.enablePrometheusMetrics;
}
if (body.prometheusMetricsToken !== undefined) {
validated.prometheusMetricsToken = parseOptionalString(
body.prometheusMetricsToken,
"prometheusMetricsToken",
512,
);
}
if (body.clearPrometheusToken !== undefined) {
if (typeof body.clearPrometheusToken !== "boolean") {
throw new Error("clearPrometheusToken must be a boolean.");
}
validated.clearPrometheusToken = body.clearPrometheusToken;
}
if (body.jwtSecret !== undefined) {
if (typeof body.jwtSecret !== "string" || body.jwtSecret.trim().length < 32) {
if (
typeof body.jwtSecret !== "string" ||
body.jwtSecret.trim().length < 32
) {
throw new Error(
"jwtSecret must be a string with at least 32 characters.",
);
@@ -208,6 +389,12 @@ export async function POST(req: NextRequest) {
: {}),
});
} catch (err: unknown) {
if (err instanceof SecretVaultError) {
return NextResponse.json(
{ error: err.message, code: err.code },
{ status: 500 },
);
}
const message =
err instanceof Error ? err.message : "Settings update failed";
return NextResponse.json({ error: message }, { status: 500 });
+119
View File
@@ -0,0 +1,119 @@
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import { dispatchWebhookAlertAsync } from "@/core/alerts/webhookNotifier";
import { SecretVaultError } from "@/core/Security/secretVault";
import { requireAdminApiSession } from "@/lib/adminAuth";
import { NextRequest, NextResponse } from "next/server";
export async function POST(req: NextRequest) {
const auth = await requireAdminApiSession(req);
if (!auth.valid) {
return auth.response;
}
let body: Record<string, unknown> = {};
try {
const text = await req.text();
if (text.trim()) {
const parsed = JSON.parse(text);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
body = parsed as Record<string, unknown>;
}
}
} catch {
return NextResponse.json(
{ error: "Invalid JSON request payload.", code: "INVALID_JSON" },
{ status: 400 },
);
}
try {
const savedConfig = await SettingsManager.getDecryptedWebhookConfigAsync();
const webhookUrl =
typeof body.webhookUrl === "string"
? body.webhookUrl.trim()
: savedConfig.webhookUrl.trim();
const clearWebhookSecret = body.clearWebhookSecret === true;
const webhookSecret = clearWebhookSecret
? ""
: typeof body.webhookSecret === "string" && body.webhookSecret.trim()
? body.webhookSecret.trim()
: savedConfig.webhookSecret;
if (!webhookUrl) {
return NextResponse.json(
{
success: false,
code: "VALIDATION_ERROR",
error: "Webhook URL is required to send a test alert.",
},
{ status: 400 },
);
}
try {
const parsedUrl = new URL(webhookUrl);
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
throw new Error("Webhook URL must use http:// or https://.");
}
} catch {
return NextResponse.json(
{
success: false,
code: "VALIDATION_ERROR",
error: "Webhook URL must be a valid http:// or https:// endpoint.",
},
{ status: 400 },
);
}
const result = await dispatchWebhookAlertAsync(
{
event: "TEST_ALERT",
severity: "info",
title: "Webhook Verification",
message: "Test alert dispatched from DataStack Admin Console.",
details: {
triggeredBy: auth.auth.user?.email || "admin",
},
},
{
enabled: true,
webhookUrl,
webhookSecret,
cooldownSec: savedConfig.cooldownSec,
},
{ bypassCooldown: true },
);
if (!result.ok) {
return NextResponse.json(
{
success: false,
code: "WEBHOOK_DELIVERY_FAILED",
error: result.error || "Webhook target failed to accept test alert.",
status: result.status,
},
{ status: 502 },
);
}
return NextResponse.json({
success: true,
status: result.status,
message: `Webhook test alert delivered (HTTP ${result.status ?? 200}).`,
});
} catch (err: unknown) {
if (err instanceof SecretVaultError) {
return NextResponse.json(
{ success: false, code: err.code, error: err.message },
{ status: 400 },
);
}
const message =
err instanceof Error ? err.message : "Webhook test alert failed";
return NextResponse.json(
{ success: false, code: "WEBHOOK_DELIVERY_FAILED", error: message },
{ status: 502 },
);
}
}