Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
88 changes: 51 additions & 37 deletions apps/dashboard/app/(app)/audit/[bucket]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Suspense } from "react";
import { BucketSelect } from "./bucket-select";
import { Filter } from "./filter";
import { Row } from "./row";
import { SelectAuditLogTarget, type SelectAuditLog } from "@unkey/db/src/schema";

export const dynamic = "force-dynamic";
export const runtime = "edge";

Expand All @@ -31,18 +33,42 @@ type Props = {
};
};

type AuditLogWithTargets = SelectAuditLog & { targets: Array<SelectAuditLogTarget> };

/**
* Parse searchParam string arrays
*/
const filterParser = parseAsArrayOf(parseAsString).withDefault([]);

/**
* Utility to map log with targets to log entry
*/
const toLogEntry = (l: AuditLogWithTargets) => ({
id: l.id,
event: l.event,
time: l.time,
actor: {
id: l.actorId,
name: l.actorName,
type: l.actorType,
},
location: l.remoteIp,
description: l.display,
targets: l.targets.map((t) => ({
id: t.id,
type: t.type,
name: t.name,
})),
});

export default async function AuditPage(props: Props) {
const tenantId = getTenantId();
const workspace = await db.query.workspaces.findFirst({
where: (table, { eq, and, isNull }) =>
and(eq(table.tenantId, tenantId), isNull(table.deletedAt)),
with: {
ratelimitNamespaces: {
where: (table, { isNull }) => isNull(table.deletedAt),
columns: {
id: true,
name: true,
Expand Down Expand Up @@ -87,19 +113,6 @@ export default async function AuditPage(props: Props) {
},
},
});
if (!bucket) {
return (
<EmptyPlaceholder>
<EmptyPlaceholder.Icon>
<Box />
</EmptyPlaceholder.Icon>
<EmptyPlaceholder.Title>Bucket Not Found</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
The specified audit log bucket does not exist or you do not have access to it.
</EmptyPlaceholder.Description>
</EmptyPlaceholder>
);
}

return (
<div>
Expand Down Expand Up @@ -159,29 +172,25 @@ export default async function AuditPage(props: Props) {
</EmptyPlaceholder>
}
>
<AuditLogTable
logs={bucket.logs.map((l) => ({
id: l.id,
event: l.event,
time: l.time,
actor: {
id: l.actorId,
name: l.actorName,
type: l.actorType,
},
location: l.remoteIp,
description: l.display,
targets: l.targets.map((t) => ({
id: t.id,
type: t.type,
name: t.name,
})),
}))}
before={props.searchParams.before ? Number(props.searchParams.before) : undefined}
selectedEvents={selectedEvents}
selectedUsers={selectedUsers}
selectedRootKeys={selectedRootKeys}
/>
{!bucket ? (
<EmptyPlaceholder>
<EmptyPlaceholder.Icon>
<Box />
</EmptyPlaceholder.Icon>
<EmptyPlaceholder.Title>Bucket Not Found</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
The specified audit log bucket does not exist or you do not have access to it.
</EmptyPlaceholder.Description>
</EmptyPlaceholder>
) : (
<AuditLogTable
logs={bucket.logs.map(toLogEntry)}
before={props.searchParams.before ? Number(props.searchParams.before) : undefined}
selectedEvents={selectedEvents}
selectedUsers={selectedUsers}
selectedRootKeys={selectedRootKeys}
/>
)}
Comment thread
unrenamed marked this conversation as resolved.
</Suspense>
</main>
</div>
Expand Down Expand Up @@ -359,7 +368,12 @@ const UserFilter: React.FC<{ tenantId: string }> = async ({ tenantId }) => {

const RootKeyFilter: React.FC<{ workspaceId: string }> = async ({ workspaceId }) => {
const rootKeys = await db.query.keys.findMany({
where: (table, { eq }) => eq(table.forWorkspaceId, workspaceId),
where: (table, { eq, and, or, isNull, gt }) =>
and(
eq(table.forWorkspaceId, workspaceId),
isNull(table.deletedAt),
or(isNull(table.expires), gt(table.expires, new Date())),
),
columns: {
id: true,
name: true,
Expand Down
5 changes: 3 additions & 2 deletions apps/dashboard/app/(app)/audit/[bucket]/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Badge } from "@/components/ui/badge";
import { Code } from "@/components/ui/code";
import { TableCell, TableRow } from "@/components/ui/table";
import { cn } from "@/lib/utils";
import { format } from "date-fns";
import { ChevronDown, KeySquare, Minus } from "lucide-react";
import { useState } from "react";

Expand Down Expand Up @@ -85,10 +86,10 @@ export const Row: React.FC<Props> = ({ auditLog, user }) => {
<TableCell>
<div className="flex items-center gap-2">
<span className="text-sm text-content">
{new Date(auditLog.time).toLocaleDateString()}
{format(new Date(auditLog.time), 'dd/MM/yyyy')}
Comment thread
chronark marked this conversation as resolved.
Outdated
</span>
<span className="text-xs text-content-subtle">
{new Date(auditLog.time).toLocaleTimeString()}
{format(new Date(auditLog.time), 'dd/MM/yyyy')}
</span>
</div>
</TableCell>
Expand Down
3 changes: 3 additions & 0 deletions internal/db/src/schema/audit_logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ export const auditLogTargetRelations = relations(auditLogTarget, ({ one }) => ({
references: [auditLog.id],
}),
}));

export type SelectAuditLog = typeof auditLog.$inferSelect;
Comment thread
chronark marked this conversation as resolved.
export type SelectAuditLogTarget = typeof auditLogTarget.$inferSelect;