Skip to content

Commit

Permalink
refactor: do not send audit logs to tinybird (#2206)
Browse files Browse the repository at this point in the history
* refactor: do not send audit logs to tinybird

* refactor: use database for audit logs in trigger app

* chore: remove accidentally created file

* fix: cache and return properly
  • Loading branch information
chronark authored Oct 5, 2024
1 parent ea60984 commit 2288ebf
Show file tree
Hide file tree
Showing 57 changed files with 779 additions and 1,987 deletions.
17 changes: 0 additions & 17 deletions apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { getTenantId } from "@/lib/auth";
import { db, eq, schema } from "@/lib/db";
import { stripeEnv } from "@/lib/env";
import { PostHogClient } from "@/lib/posthog";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { currentUser } from "@clerk/nextjs";
import { defaultProSubscriptions } from "@unkey/billing";
import { headers } from "next/headers";
Expand Down Expand Up @@ -114,22 +113,6 @@ export default async function StripeSuccess(props: Props) {
userAgent: h.get("user-agent") ?? undefined,
},
});
await ingestAuditLogsTinybird({
workspaceId: ws.id,
actor: { type: "user", id: user.id },
event: "workspace.update",
description: "Changed plan to 'pro'",
resources: [
{
type: "workspace",
id: ws.id,
},
],
context: {
location: h.get("x-forwarded-for") ?? process.env.VERCEL_REGION ?? "unknown",
userAgent: h.get("user-agent") ?? undefined,
},
});
}
});

Expand Down
21 changes: 0 additions & 21 deletions apps/dashboard/app/new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { insertAuditLogs } from "@/lib/audit";
import { db, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { auth } from "@clerk/nextjs";
import { newId } from "@unkey/id";
import { ArrowRight, DatabaseZap, GlobeLock, KeySquare } from "lucide-react";
Expand Down Expand Up @@ -251,26 +250,6 @@ export default async function (props: Props) {
},
});
});
await ingestAuditLogsTinybird({
workspaceId: workspaceId,
event: "workspace.create",
actor: {
type: "user",
id: userId,
},
description: `Created ${workspaceId}`,
resources: [
{
type: "workspace",
id: workspaceId,
},
],

context: {
userAgent: headers().get("user-agent") ?? undefined,
location: headers().get("x-forwarded-for") ?? process.env.VERCEL_REGION ?? "unknown",
},
});

return redirect(`/new?workspaceId=${workspaceId}`);
}
Expand Down
31 changes: 1 addition & 30 deletions apps/dashboard/lib/tinybird.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { time } from "node:console";
import { env } from "@/lib/env";
import { NoopTinybird, Tinybird } from "@chronark/zod-bird";
import { newId } from "@unkey/id";
import { auditLogSchemaV1, unkeyAuditLogEvents } from "@unkey/schema/src/auditlog";
import type { unkeyAuditLogEvents } from "@unkey/schema/src/auditlog";
import { z } from "zod";
import type { MaybeArray } from "./types";

const token = env().TINYBIRD_TOKEN;
const tb = token ? new Tinybird({ token }) : new NoopTinybird();
Expand Down Expand Up @@ -504,32 +501,6 @@ export type UnkeyAuditLog = {
};
};

export function ingestAuditLogsTinybird(logs: MaybeArray<UnkeyAuditLog>) {
if (Array.isArray(logs) && logs.length === 0) {
return Promise.resolve();
}
return tb.buildIngestEndpoint({
datasource: "audit_logs__v2",
event: auditLogSchemaV1
.merge(
z.object({
event: unkeyAuditLogEvents,
auditLogId: z.string().default(newId("auditLog")),
bucket: z.string().default("unkey_mutations"),
time: z.number().default(Date.now()),
}),
)
.transform((l) => ({
...l,
actor: {
...l.actor,
meta: l.actor.meta ? JSON.stringify(l.actor.meta) : undefined,
},
resources: JSON.stringify(l.resources),
})),
})(logs);
}

export const getRatelimitsHourly = tb.buildPipe({
pipe: "get_ratelimits_hourly__v1",
parameters: z.object({
Expand Down
20 changes: 0 additions & 20 deletions apps/dashboard/lib/trpc/routers/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { z } from "zod";

import { insertAuditLogs } from "@/lib/audit";
import { db, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";
import { newId } from "@unkey/id";

Expand Down Expand Up @@ -90,25 +89,6 @@ export const createApi = rateLimitedProcedure(ratelimit.create)
userAgent: ctx.audit.userAgent,
},
});
await ingestAuditLogsTinybird({
workspaceId: ws.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.create",
description: `Created ${apiId}`,
resources: [
{
type: "api",
id: apiId,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
});

return {
Expand Down
48 changes: 0 additions & 48 deletions apps/dashboard/lib/trpc/routers/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { z } from "zod";

import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";

export const deleteApi = rateLimitedProcedure(ratelimit.delete)
Expand Down Expand Up @@ -66,25 +65,6 @@ export const deleteApi = rateLimitedProcedure(ratelimit.delete)
userAgent: ctx.audit.userAgent,
},
});
await ingestAuditLogsTinybird({
workspaceId: api.workspaceId,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.delete",
description: `Deleted ${api.id}`,
resources: [
{
type: "api",
id: api.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});

const keyIds = await tx.query.keys.findMany({
where: eq(schema.keys.keyAuthId, api.keyAuthId!),
Expand Down Expand Up @@ -122,34 +102,6 @@ export const deleteApi = rateLimitedProcedure(ratelimit.delete)
},
})),
);
await ingestAuditLogsTinybird(
keyIds.map(({ id }) => ({
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "key.delete",
description: `Deleted ${id} as part of the ${api.id} deletion`,
resources: [
{
type: "api",
id: api.id,
},
{
type: "key",
id: id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
})),
).catch((err) => {
tx.rollback();
throw err;
});
}
});
} catch (_err) {
Expand Down
25 changes: 0 additions & 25 deletions apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { z } from "zod";

import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";

export const updateAPIDeleteProtection = rateLimitedProcedure(ratelimit.update)
Expand Down Expand Up @@ -76,28 +75,4 @@ export const updateAPIDeleteProtection = rateLimitedProcedure(ratelimit.update)
},
});
});
await ingestAuditLogsTinybird({
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `API ${api.name} delete protection is now ${
input.enabled ? "enabled" : "disabled"
}.}`,
resources: [
{
type: "api",
id: api.id,
meta: {
deleteProtection: input.enabled,
},
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
});
20 changes: 0 additions & 20 deletions apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { z } from "zod";

import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";

export const updateApiIpWhitelist = rateLimitedProcedure(ratelimit.update)
Expand Down Expand Up @@ -88,23 +87,4 @@ export const updateApiIpWhitelist = rateLimitedProcedure(ratelimit.update)
},
});
});
await ingestAuditLogsTinybird({
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${api.id} IP whitelist from ${api.ipWhitelist} to ${newIpWhitelist}`,
resources: [
{
type: "api",
id: api.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
});
20 changes: 0 additions & 20 deletions apps/dashboard/lib/trpc/routers/api/updateName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { z } from "zod";

import { insertAuditLogs } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";

export const updateApiName = rateLimitedProcedure(ratelimit.update)
Expand Down Expand Up @@ -71,23 +70,4 @@ export const updateApiName = rateLimitedProcedure(ratelimit.update)
},
});
});
await ingestAuditLogsTinybird({
workspaceId: api.workspace.id,
actor: {
type: "user",
id: ctx.user.id,
},
event: "api.update",
description: `Changed ${api.id} name from ${api.name} to ${input.name}`,
resources: [
{
type: "api",
id: api.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});
});
18 changes: 0 additions & 18 deletions apps/dashboard/lib/trpc/routers/key/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { insertAuditLogs } from "@/lib/audit";
import { db, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
Expand Down Expand Up @@ -134,22 +133,5 @@ export const createKey = rateLimitedProcedure(ratelimit.create)
});
});

await ingestAuditLogsTinybird({
workspaceId: workspace.id,
actor: { type: "user", id: ctx.user.id },
event: "key.create",
description: `Created ${keyId}`,
resources: [
{
type: "key",
id: keyId,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
});

return { keyId, key };
});
4 changes: 1 addition & 3 deletions apps/dashboard/lib/trpc/routers/key/createRootKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db, eq, schema } from "@/lib/db";
import { env } from "@/lib/env";
import { type UnkeyAuditLog, ingestAuditLogsTinybird } from "@/lib/tinybird";
import type { UnkeyAuditLog } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
Expand Down Expand Up @@ -197,7 +197,5 @@ export const createRootKey = rateLimitedProcedure(ratelimit.create)
});
}

await ingestAuditLogsTinybird(auditLogs);

return { key, keyId };
});
20 changes: 0 additions & 20 deletions apps/dashboard/lib/trpc/routers/key/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { insertAuditLogs } from "@/lib/audit";
import { and, db, eq, inArray, schema } from "@/lib/db";
import { ingestAuditLogsTinybird } from "@/lib/tinybird";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
Expand Down Expand Up @@ -82,23 +81,4 @@ export const deleteKeys = rateLimitedProcedure(ratelimit.delete)
"We are unable to delete the key. Please contact support using support@unkey.dev",
});
});

await ingestAuditLogsTinybird(
workspace.keys.map((key) => ({
workspaceId: workspace.id,
actor: { type: "user", id: ctx.user.id },
event: "key.delete",
description: `Deleted ${key.id}`,
resources: [
{
type: "key",
id: key.id,
},
],
context: {
location: ctx.audit.location,
userAgent: ctx.audit.userAgent,
},
})),
);
});
Loading

0 comments on commit 2288ebf

Please sign in to comment.