Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add audit logging functionality

Kiran K387e34314d4d
diff --git a/apps/web/lib/api/audit-logs/record-audit-log.ts b/apps/web/lib/api/audit-logs/record-audit-log.ts
new file mode 100644
index 0000000000..482ff52213
--- /dev/null
+++ b/apps/web/lib/api/audit-logs/record-audit-log.ts
@@ -0,0 +1,48 @@
+import { tb } from "@/lib/tinybird";
+import { ipAddress } from "@vercel/functions";
+import { headers } from "next/headers";
+import { z } from "zod";
+import { createId } from "../create-id";
+import { getIP } from "../utils";
+import { auditLogSchemaTB, recordAuditLogInputSchema } from "./schemas";
+
+export const recordAuditLogTB = tb.buildIngestEndpoint({
+ datasource: "dub_audit_logs",
+ event: auditLogSchemaTB,
+ wait: true,
+});
+
+// TODO:
+// Support array of logs
+export const recordAuditLog = async (
+ data: z.infer<typeof recordAuditLogInputSchema>,
+) => {
+ const headersList = headers();
+ const location = data.req ? ipAddress(data.req) : getIP();
+ const userAgent = headersList.get("user-agent");
+
+ const auditLog: z.infer<typeof auditLogSchemaTB> = {
+ id: createId({ prefix: "audit_" }),
+ timestamp: new Date().toISOString(),
+ workspace_id: data.workspaceId,
+ program_id: data.programId,
+ action: data.action,
+ actor_id: data.actor.id,
+ actor_type: data.actor.type || "user",
+ actor_name: data.actor.name || "",
+ description: data.description || "",
+ location: location || "",
+ user_agent: userAgent || "",
+ targets: data.targets ? JSON.stringify(data.targets) : "",
+ metadata: data.metadata ? JSON.stringify(data.metadata) : "",
+ };
+
+ if (process.env.NODE_ENV === "development") {
+ console.info("Inserting audit log", auditLog);
+ return;
+ }
+
+ await recordAuditLogTB(auditLog).catch((error) => {
+ console.error("Failed to record audit log", error);
+ });
+};
diff --git a/apps/web/lib/api/audit-logs/schemas.ts b/apps/web/lib/api/audit-logs/schemas.ts
new file mode 100644
index 0000000000..54d3fa92ad
--- /dev/null
+++ b/apps/web/lib/api/audit-logs/schemas.ts
@@ -0,0 +1,77 @@
+import { DiscountSchema } from "@/lib/zod/schemas/discount";
+import { RewardSchema } from "@/lib/zod/schemas/rewards";
+import { z } from "zod";
+
+// Schema that represents the audit log schema in Tinybird
+export const auditLogSchemaTB = z.object({
+ id: z.string(),
+ timestamp: z.string(),
+ workspace_id: z.string(),
+ program_id: z.string(),
+ action: z.string(),
+ actor_id: z.string(),
+ actor_type: z.string(),
+ actor_name: z.string(),
+ description: z.string(),
+ targets: z.string().nullable(),
+ location: z.string().nullable(),
+ user_agent: z.string().nullable(),
+ metadata: z.string().nullable(),
+});
+
+// Schema that represents the audit log in the CSV file
+export const AuditLogSchema = z.object({
+ id: z.string(),
+ timestamp: z.string(),
+ workspaceId: z.string(),
+ programId: z.string(),
+ action: z.string(),
+ actorId: z.string(),
+ actorType: z.string(),
+ actorName: z.string(),
+ description: z.string(),
+ targets: z.array(z.record(z.string(), z.any())).nullable(),
+ location: z.string().nullable(),
+ userAgent: z.string().nullable(),
+ metadata: z.record(z.string(), z.any()).nullable(),
+});
+
+const RewardEvent = z.object({
+ type: z.literal("reward"),
+ id: z.string(),
+ metadata: RewardSchema.pick({
+ event: true,
+ type: true,
+ amount: true,
+ maxDuration: true,
+ }),
+});
+
+const DiscountEvent = z.object({
+ type: z.literal("discount"),
+ id: z.string(),
+ metadata: DiscountSchema.pick({
+ type: true,
+ amount: true,
+ maxDuration: true,
+ }),
+});
+
+export const AuditLogEvent = z.union([RewardEvent, DiscountEvent]);
+
+export const recordAuditLogInputSchema = z.object({
+ workspaceId: z.string(),
+ programId: z.string(),
+ action: z.string(),
+ actor: z.object({
+ id: z.string(),
+ name: z.string().nullable(),
+ type: z.string().optional(),
+ }),
+ description: z.string().optional(),
+ location: z.string().optional(),
+ userAgent: z.string().optional(),
+ targets: z.array(AuditLogEvent).nullable(),
+ metadata: z.record(z.string(), z.any()).nullable(),
+ req: z.instanceof(Request).optional(),
+});
diff --git a/apps/web/lib/api/create-id.ts b/apps/web/lib/api/create-id.ts
index 5808806ad9..5820a8ea22 100644
--- a/apps/web/lib/api/create-id.ts
+++ b/apps/web/lib/api/create-id.ts
@@ -26,6 +26,7 @@ const prefixes = [
"rw_",
"disc_",
"dub_embed_",
+ "audit_",
] as const;
// ULID uses base32 encoding
diff --git a/packages/tinybird/datasources/dub_audit_logs.datasource b/packages/tinybird/datasources/dub_audit_logs.datasource
new file mode 100644
index 0000000000..9633ab0580
--- /dev/null
+++ b/packages/tinybird/datasources/dub_audit_logs.datasource
@@ -0,0 +1,19 @@
+SCHEMA >
+ `id` String `json:$.id`,
+ `timestamp` DateTime64(3) `json:$.timestamp`,
+ `workspace_id` String `json:$.workspace_id`,
+ `program_id` String `json:$.program_id`,
+ `action` LowCardinality(String) `json:$.action`,
+ `actor_id` String `json:$.actor_id`,
+ `actor_type` LowCardinality(String) `json:$.actor_type`,
+ `actor_name` String `json:$.actor_name`,
+ `targets` String `json:$.targets`,
+ `description` String `json:$.description`,
+ `location` String `json:$.location`,
+ `user_agent` String `json:$.user_agent`,
+ `metadata` String `json:$.metadata`
+
+ENGINE "MergeTree"
+ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
+ENGINE_SORTING_KEY "workspace_id, program_id, timestamp"
+ENGINE_TTL "timestamp + INTERVAL 1 YEAR"
\ No newline at end of file