"use client"; import { Button } from "@app/components/ui/button"; import { toast } from "@app/hooks/useToast"; import { useState, useTransition, useMemo } from "react"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { useParams, useRouter, useSearchParams } from "next/navigation"; import { useTranslations } from "next-intl"; import { LogDataTable } from "@app/components/LogDataTable"; import { ColumnDef } from "@tanstack/react-table"; import { DateTimeValue } from "@app/components/DateTimePicker"; import { ArrowUpRight, Key, User } from "lucide-react"; import Link from "next/link"; import { ColumnFilter } from "@app/components/ColumnFilter"; import SettingsSectionTitle from "@app/components/SettingsSectionTitle"; import { build } from "@server/build"; import { getSevenDaysAgo } from "@app/lib/getSevenDaysAgo"; import axios from "axios"; import { useStoredPageSize } from "@app/hooks/useStoredPageSize"; import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert"; import { usePaidStatus } from "@app/hooks/usePaidStatus"; import { tierMatrix } from "@server/lib/billing/tierMatrix"; import { logQueries } from "@app/lib/queries"; import { useQuery } from "@tanstack/react-query"; import type { QueryAccessAuditLogResponse } from "@server/routers/auditLogs/types"; export default function GeneralPage() { const router = useRouter(); const searchParams = useSearchParams(); const api = createApiClient(useEnvContext()); const t = useTranslations(); const { orgId } = useParams(); const { isPaidUser } = usePaidStatus(); const [isExporting, startTransition] = useTransition(); const [filters, setFilters] = useState<{ action?: string; type?: string; resourceId?: string; location?: string; actor?: string; }>({ action: searchParams.get("action") || undefined, type: searchParams.get("type") || undefined, resourceId: searchParams.get("resourceId") || undefined, location: searchParams.get("location") || undefined, actor: searchParams.get("actor") || undefined }); const [currentPage, setCurrentPage] = useState(0); const [pageSize, setPageSize] = useStoredPageSize("access-audit-logs", 20); const getDefaultDateRange = () => { const startParam = searchParams.get("start"); const endParam = searchParams.get("end"); if (startParam && endParam) { return { startDate: { date: new Date(startParam) }, endDate: { date: new Date(endParam) } }; } return { startDate: { date: getSevenDaysAgo() }, endDate: { date: new Date() } }; }; const [dateRange, setDateRange] = useState<{ startDate: DateTimeValue; endDate: DateTimeValue; }>(getDefaultDateRange()); const queryFilters = useMemo(() => { let timeStart: string | undefined; let timeEnd: string | undefined; if (dateRange.startDate?.date) { const dt = new Date(dateRange.startDate.date); if (dateRange.startDate.time) { const [h, m, s] = dateRange.startDate.time .split(":") .map(Number); dt.setHours(h, m, s || 0); } timeStart = dt.toISOString(); } if (dateRange.endDate?.date) { const dt = new Date(dateRange.endDate.date); if (dateRange.endDate.time) { const [h, m, s] = dateRange.endDate.time.split(":").map(Number); dt.setHours(h, m, s || 0); } else { const now = new Date(); dt.setHours( now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds() ); } timeEnd = dt.toISOString(); } return { timeStart, timeEnd, page: currentPage, pageSize, ...filters, resourceId: filters.resourceId ? Number(filters.resourceId) : undefined }; }, [dateRange, currentPage, pageSize, filters]); const { data, isFetching, isLoading, refetch } = useQuery({ ...logQueries.access({ orgId: orgId as string, filters: queryFilters }), enabled: isPaidUser(tierMatrix.accessLogs) && build !== "oss" }); const rows = isLoading ? generateSampleAccessLogs() : (data?.log ?? []); const totalCount = data?.pagination?.total ?? 0; const filterAttributes = data?.filterAttributes ?? { actors: [], resources: [], locations: [] }; const handleDateRangeChange = ( startDate: DateTimeValue, endDate: DateTimeValue ) => { setDateRange({ startDate, endDate }); setCurrentPage(0); updateUrlParamsForAllFilters({ start: startDate.date?.toISOString() || "", end: endDate.date?.toISOString() || "" }); }; const handlePageChange = (newPage: number) => { setCurrentPage(newPage); }; const handlePageSizeChange = (newPageSize: number) => { setPageSize(newPageSize); setCurrentPage(0); }; const handleFilterChange = ( filterType: keyof typeof filters, value: string | undefined ) => { const newFilters = { ...filters, [filterType]: value }; setFilters(newFilters); setCurrentPage(0); updateUrlParamsForAllFilters(newFilters); }; const updateUrlParamsForAllFilters = ( newFilters: | typeof filters | { start: string; end: string; } ) => { const params = new URLSearchParams(searchParams); Object.entries(newFilters).forEach(([key, value]) => { if (value) { params.set(key, value); } else { params.delete(key); } }); router.replace(`?${params.toString()}`, { scroll: false }); }; const exportData = async () => { try { const params: any = { timeStart: dateRange.startDate?.date ? new Date(dateRange.startDate.date).toISOString() : undefined, timeEnd: dateRange.endDate?.date ? new Date(dateRange.endDate.date).toISOString() : undefined, ...filters }; const response = await api.get(`/org/${orgId}/logs/access/export`, { responseType: "blob", params }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement("a"); link.href = url; const epoch = Math.floor(Date.now() / 1000); link.setAttribute( "download", `access-audit-logs-${orgId}-${epoch}.csv` ); document.body.appendChild(link); link.click(); link.parentNode?.removeChild(link); } catch (error) { let apiErrorMessage: string | null = null; if (axios.isAxiosError(error) && error.response) { const data = error.response.data; if (data instanceof Blob && data.type === "application/json") { const text = await data.text(); const errorData = JSON.parse(text); apiErrorMessage = errorData.message; } } toast({ title: t("error"), description: apiErrorMessage ?? t("exportError"), variant: "destructive" }); } }; const columns: ColumnDef[] = [ { accessorKey: "timestamp", header: () => { return t("timestamp"); }, cell: ({ row }) => { return (
{new Date( row.original.timestamp * 1000 ).toLocaleString()}
); } }, { accessorKey: "action", header: () => { return (
{t("action")} handleFilterChange("action", value) } searchPlaceholder="Search..." emptyMessage="None found" />
); }, cell: ({ row }) => { return ( {row.original.action ? <>Allowed : <>Denied} ); } }, { accessorKey: "ip", header: () => t("ip") }, { accessorKey: "location", header: () => { return (
{t("location")} ({ value: location, label: location }) )} selectedValue={filters.location} onValueChange={(value) => handleFilterChange("location", value) } searchPlaceholder="Search..." emptyMessage="None found" />
); }, cell: ({ row }) => { return ( {row.original.location ? ( {row.original.location} ) : ( - )} ); } }, { accessorKey: "resourceName", header: () => { return (
{t("resource")} ({ value: res.id.toString(), label: res.name || "Unnamed Resource" }))} selectedValue={filters.resourceId} onValueChange={(value) => handleFilterChange("resourceId", value) } searchPlaceholder="Search..." emptyMessage="None found" />
); }, cell: ({ row }) => { return ( ); } }, { accessorKey: "type", header: () => { return (
{t("type")} handleFilterChange("type", value) } searchPlaceholder="Search..." emptyMessage="None found" />
); }, cell: ({ row }) => { const typeLabel = row.original.type === "ssh" ? "SSH" : row.original.type.charAt(0).toUpperCase() + row.original.type.slice(1); return {typeLabel || "-"}; } }, { accessorKey: "actor", header: () => { return (
{t("actor")} ({ value: actor, label: actor }))} selectedValue={filters.actor} onValueChange={(value) => handleFilterChange("actor", value) } searchPlaceholder="Search..." emptyMessage="None found" />
); }, cell: ({ row }) => { return ( {row.original.actor ? ( <> {row.original.actorType == "user" ? ( ) : ( )} {row.original.actor} ) : ( <>- )} ); } }, { accessorKey: "actorId", header: () => t("actorId"), cell: ({ row }) => ( {row.original.actorId || "-"} ) } ]; const renderExpandedRow = (row: any) => { return (
{row.userAgent != "node" && (
User Agent:

{row.userAgent || "N/A"}

)}
Metadata:
                            {row.metadata
                                ? JSON.stringify(
                                      JSON.parse(row.metadata),
                                      null,
                                      2
                                  )
                                : "N/A"}
                        
); }; return ( <> refetch()} isRefreshing={isFetching} onExport={() => startTransition(exportData)} isExporting={isExporting} onDateRangeChange={handleDateRangeChange} dateRange={{ start: dateRange.startDate, end: dateRange.endDate }} defaultSort={{ id: "timestamp", desc: true }} totalCount={totalCount} currentPage={currentPage} pageSize={pageSize} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange} isLoading={isLoading} expandable={true} renderExpandedRow={renderExpandedRow} disabled={!isPaidUser(tierMatrix.accessLogs) || build === "oss"} /> ); } function generateSampleAccessLogs(): QueryAccessAuditLogResponse["log"] { const locations = ["US", "DE", "GB", "FR", "JP", "CA", "AU"]; const types = ["password", "pincode", "login", "whitelistedEmail", "ssh"]; const actors = [ "alice@example.com", "bob@example.com", "carol@example.com", null ]; const now = Math.floor(Date.now() / 1000); const sevenDaysAgo = now - 7 * 24 * 60 * 60; return Array.from({ length: 10 }, (_, i) => { const action = Math.random() > 0.3; const actor = actors[Math.floor(Math.random() * actors.length)]; return { timestamp: Math.floor( sevenDaysAgo + Math.random() * (now - sevenDaysAgo) ), action, orgId: "sample-org", actorType: actor ? "user" : null, actor, actorId: actor ? `user-${i}` : null, resourceId: Math.floor(Math.random() * 5) + 1, resourceNiceId: `resource-${(i % 3) + 1}`, resourceName: `Resource ${(i % 3) + 1}`, ip: `${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`, location: locations[Math.floor(Math.random() * locations.length)], userAgent: "Mozilla/5.0", metadata: null, type: types[Math.floor(Math.random() * types.length)] }; }); }