Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 25 additions & 44 deletions apps/dashboard/lib/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from "zod";

import type { MaybeArray } from "@/lib/types";
import { type Database, type Transaction, schema } from "@unkey/db";
import type { auditLog, auditLogTarget } from "@unkey/db/src/schema";
import { newId } from "@unkey/id";

export const AUDIT_LOG_BUCKET = "unkey_mutations";
Expand Down Expand Up @@ -103,31 +104,15 @@ export async function insertAuditLogs(
const logs = Array.isArray(logOrLogs) ? logOrLogs : [logOrLogs];

if (logs.length === 0) {
console.info("No audit logs to insert");
return Promise.resolve();
}

console.info({
message: "Inserting audit logs",
count: logs.length,
events: logs.map((log) => log.event),
workspaceIds: [...new Set(logs.map((log) => log.workspaceId))],
});
const auditLogs: (typeof auditLog.$inferInsert)[] = [];
const auditLogTargets: (typeof auditLogTarget.$inferInsert)[] = [];

for (const log of logs) {
const auditLogId = newId("auditLog");

console.info({
message: "Inserting audit log entry",
auditLogId,
workspaceId: log.workspaceId,
event: log.event,
actorType: log.actor.type,
actorId: log.actor.id,
resourceCount: log.resources.length,
});

await db.insert(schema.auditLog).values({
auditLogs.push({
id: auditLogId,
workspaceId: log.workspaceId,
bucketId: "DEPRECATED",
Expand All @@ -143,32 +128,28 @@ export async function insertAuditLogs(
actorMeta: log.actor.meta,
});

if (log.resources.length > 0) {
console.info({
message: "Inserting audit log resources",
auditLogId,
resourceCount: log.resources.length,
resourceTypes: log.resources.map((r) => r.type),
});

await db.insert(schema.auditLogTarget).values(
log.resources.map((r) => ({
workspaceId: log.workspaceId,
auditLogId,
bucketId: "DEPRECATED",
bucket: AUDIT_LOG_BUCKET,
displayName: "",
type: r.type,
id: r.id,
name: r.name,
meta: r.meta,
})),
);
if (log.resources.length === 0) {
continue;
}

auditLogTargets.push(
...log.resources.map((r) => ({
workspaceId: log.workspaceId,
auditLogId,
bucketId: "DEPRECATED",
bucket: AUDIT_LOG_BUCKET,
displayName: "",
type: r.type,
id: r.id,
name: r.name,
meta: r.meta,
})),
);
}

console.info({
message: "Successfully inserted all audit logs",
count: logs.length,
});
await db.insert(schema.auditLog).values(auditLogs);

if (auditLogTargets.length > 0) {
await db.insert(schema.auditLogTarget).values(auditLogTargets);
}
}
12 changes: 8 additions & 4 deletions apps/dashboard/lib/trpc/routers/key/createRootKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { UnkeyAuditLog } from "@/lib/audit";
import { db, eq, schema } from "@/lib/db";
import { and, db, eq, schema } from "@/lib/db";
import { env } from "@/lib/env";
import { TRPCError } from "@trpc/server";
import { newId } from "@unkey/id";
Expand Down Expand Up @@ -53,7 +53,6 @@ export const createRootKey = t.procedure
}

const keyId = newId("key");

const { key, hash, start } = await newKey({
prefix: "unkey",
byteLength: 16,
Expand Down Expand Up @@ -102,20 +101,23 @@ export const createRootKey = t.procedure
let identityId: string | undefined = undefined;
await tx.query.identities
.findFirst({
where: (table, { eq }) => eq(table.externalId, ctx.user.id),
where: (table, { eq }) =>
and(eq(table.workspaceId, ctx.workspace.id), eq(table.externalId, ctx.user.id)),
})
.then((res) => {
if (res) {
identityId = res.id;
}
});

if (!identityId) {
identityId = newId("identity");
await tx.insert(schema.identities).values({
id: identityId,
workspaceId: ctx.workspace.id,
externalId: ctx.user.id,
});

auditLogs.push({
workspaceId: ctx.workspace.id,
actor: { type: "user", id: ctx.user.id },
Expand All @@ -133,15 +135,16 @@ export const createRootKey = t.procedure
},
});
}

await tx.update(schema.keys).set({ identityId }).where(eq(schema.keys.id, keyId));

const { permissions, auditLogs: createPermissionLogs } = await upsertPermissions(
ctx,
env().UNKEY_WORKSPACE_ID,
input.permissions,
);
auditLogs.push(...createPermissionLogs);

auditLogs.push(...createPermissionLogs);
auditLogs.push(
...permissions.map((p) => ({
workspaceId: ctx.workspace.id,
Expand Down Expand Up @@ -174,6 +177,7 @@ export const createRootKey = t.procedure
workspaceId: env().UNKEY_WORKSPACE_ID,
})),
);

await insertAuditLogs(tx, auditLogs);
});
} catch (_err) {
Expand Down