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:
1 parent
89e4d68690
commit
2873b9904a
32 files changed
+6075
-241
No files matched your search
@@ -1,9 +1,21 @@
|
||||
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
|
||||
import { SDKManager } from "@/core/sdk/sdkManager";
|
||||
import {
|
||||
validateAllowedIpsInput,
|
||||
validateClientExpiresAtInput,
|
||||
} from "@/core/Security/ipAllowlist";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const SESSION_COOKIE = "datastack_admin_session";
|
||||
const ALLOWED_PATCH_KEYS = new Set(["action", "name", "rateLimit", "status"]);
|
||||
const ALLOWED_PATCH_KEYS = new Set([
|
||||
"action",
|
||||
"name",
|
||||
"rateLimit",
|
||||
"storageQuotaMb",
|
||||
"allowedIps",
|
||||
"expiresAt",
|
||||
"status",
|
||||
]);
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
@@ -125,7 +137,14 @@ export async function PATCH(
|
||||
}
|
||||
}
|
||||
|
||||
const updates: { name?: string; rateLimit?: number; status?: "active" | "revoked" } = {};
|
||||
const updates: {
|
||||
name?: string;
|
||||
rateLimit?: number;
|
||||
storageQuotaMb?: number;
|
||||
allowedIps?: string;
|
||||
expiresAt?: string | null;
|
||||
status?: "active" | "revoked";
|
||||
} = {};
|
||||
if (body.name !== undefined) {
|
||||
if (
|
||||
typeof body.name !== "string" ||
|
||||
@@ -153,6 +172,46 @@ export async function PATCH(
|
||||
}
|
||||
updates.rateLimit = body.rateLimit;
|
||||
}
|
||||
if (body.storageQuotaMb !== undefined) {
|
||||
if (
|
||||
typeof body.storageQuotaMb !== "number" ||
|
||||
!Number.isInteger(body.storageQuotaMb) ||
|
||||
body.storageQuotaMb < 0 ||
|
||||
body.storageQuotaMb > 1048576
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: "storageQuotaMb must be an integer between 0 and 1048576.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
updates.storageQuotaMb = body.storageQuotaMb;
|
||||
}
|
||||
if (body.allowedIps !== undefined) {
|
||||
if (typeof body.allowedIps !== "string") {
|
||||
return NextResponse.json(
|
||||
{ error: "allowedIps must be a string.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const ipValidation = validateAllowedIpsInput(body.allowedIps);
|
||||
if (!ipValidation.valid) {
|
||||
return NextResponse.json(
|
||||
{ error: ipValidation.error || "Invalid allowedIps.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
updates.allowedIps = ipValidation.normalized;
|
||||
}
|
||||
if (body.expiresAt !== undefined) {
|
||||
const expValidation = validateClientExpiresAtInput(body.expiresAt);
|
||||
if (!expValidation.valid) {
|
||||
return NextResponse.json(
|
||||
{ error: expValidation.error || "Invalid expiresAt.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
updates.expiresAt = expValidation.normalized;
|
||||
}
|
||||
if (body.status !== undefined) {
|
||||
if (body.status !== "active" && body.status !== "revoked") {
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
|
||||
import { SDKManager } from "@/core/sdk/sdkManager";
|
||||
import {
|
||||
validateAllowedIpsInput,
|
||||
validateClientExpiresAtInput,
|
||||
} from "@/core/Security/ipAllowlist";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const SESSION_COOKIE = "datastack_admin_session";
|
||||
@@ -58,7 +62,14 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const { clientId, name, rateLimit } = body;
|
||||
const {
|
||||
clientId,
|
||||
name,
|
||||
rateLimit,
|
||||
storageQuotaMb,
|
||||
allowedIps,
|
||||
expiresAt,
|
||||
} = body;
|
||||
|
||||
if (typeof clientId !== "string" || typeof name !== "string" || !clientId.trim() || !name.trim()) {
|
||||
return NextResponse.json(
|
||||
@@ -83,6 +94,44 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
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" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
let normalizedAllowedIps = "";
|
||||
if (allowedIps !== undefined && allowedIps !== null) {
|
||||
if (typeof allowedIps !== "string") {
|
||||
return NextResponse.json(
|
||||
{ error: "allowedIps must be a string.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const ipValidation = validateAllowedIpsInput(allowedIps);
|
||||
if (!ipValidation.valid) {
|
||||
return NextResponse.json(
|
||||
{ error: ipValidation.error || "Invalid allowedIps.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
normalizedAllowedIps = ipValidation.normalized;
|
||||
}
|
||||
|
||||
let normalizedExpiresAt: string | null = null;
|
||||
if (expiresAt !== undefined) {
|
||||
const expValidation = validateClientExpiresAtInput(expiresAt);
|
||||
if (!expValidation.valid) {
|
||||
return NextResponse.json(
|
||||
{ error: expValidation.error || "Invalid expiresAt.", code: "VALIDATION_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
normalizedExpiresAt = expValidation.normalized;
|
||||
}
|
||||
|
||||
const existing = await SDKManager.getClientAsync(safeClientId);
|
||||
if (existing) {
|
||||
return NextResponse.json(
|
||||
@@ -95,6 +144,9 @@ export async function POST(req: NextRequest) {
|
||||
clientId: safeClientId,
|
||||
name: name.trim().slice(0, 120),
|
||||
rateLimit: parsedRateLimit,
|
||||
storageQuotaMb: parsedQuotaMb,
|
||||
allowedIps: normalizedAllowedIps,
|
||||
expiresAt: normalizedExpiresAt,
|
||||
});
|
||||
|
||||
return NextResponse.json({ client: newClient }, { status: 201 });
|
||||
|
||||
Reference in new issue
Block a user