mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-17 18:36:37 +00:00
Add logActionAudit and query endpoint
This commit is contained in:
0
server/private/routers/auditLogs/index.ts
Normal file
0
server/private/routers/auditLogs/index.ts
Normal file
147
server/private/routers/auditLogs/queryActionAuditLog.ts
Normal file
147
server/private/routers/auditLogs/queryActionAuditLog.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import { actionAuditLog, db } from "@server/db";
|
||||
import { registry } from "@server/openApi";
|
||||
import { NextFunction } from "express";
|
||||
import { Request, Response } from "express";
|
||||
import { eq, gt, lt, and, count } from "drizzle-orm";
|
||||
import { OpenAPITags } from "@server/openApi";
|
||||
import { z } from "zod";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { QueryActionAuditLogResponse } from "@server/routers/auditLogs/types";
|
||||
import response from "@server/lib/response";
|
||||
import logger from "@server/logger";
|
||||
|
||||
export const queryAccessAuditLogsQuery = z.object({
|
||||
// iso string just validate its a parseable date
|
||||
timeStart: z
|
||||
.string()
|
||||
.refine((val) => !isNaN(Date.parse(val)), {
|
||||
message: "timeStart must be a valid ISO date string"
|
||||
})
|
||||
.transform((val) => Math.floor(new Date(val).getTime() / 1000)),
|
||||
timeEnd: z
|
||||
.string()
|
||||
.refine((val) => !isNaN(Date.parse(val)), {
|
||||
message: "timeEnd must be a valid ISO date string"
|
||||
})
|
||||
.transform((val) => Math.floor(new Date(val).getTime() / 1000))
|
||||
.optional()
|
||||
.default(new Date().toISOString()),
|
||||
limit: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("1000")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive()),
|
||||
offset: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("0")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().nonnegative())
|
||||
});
|
||||
|
||||
export const queryAccessAuditLogsParams = z.object({
|
||||
orgId: z.string()
|
||||
});
|
||||
|
||||
function querySites(timeStart: number, timeEnd: number, orgId: string) {
|
||||
return db
|
||||
.select({
|
||||
orgId: actionAuditLog.orgId,
|
||||
action: actionAuditLog.action,
|
||||
actorType: actionAuditLog.actorType,
|
||||
timestamp: actionAuditLog.timestamp,
|
||||
actor: actionAuditLog.actor
|
||||
})
|
||||
.from(actionAuditLog)
|
||||
.where(
|
||||
and(
|
||||
gt(actionAuditLog.timestamp, timeStart),
|
||||
lt(actionAuditLog.timestamp, timeEnd),
|
||||
eq(actionAuditLog.orgId, orgId)
|
||||
)
|
||||
)
|
||||
.orderBy(actionAuditLog.timestamp);
|
||||
}
|
||||
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/org/{orgId}/logs/action",
|
||||
description: "Query the action audit log for an organization",
|
||||
tags: [OpenAPITags.Org],
|
||||
request: {
|
||||
query: queryAccessAuditLogsQuery,
|
||||
params: queryAccessAuditLogsParams
|
||||
},
|
||||
responses: {}
|
||||
});
|
||||
|
||||
export async function queryAccessAuditLogs(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = queryAccessAuditLogsQuery.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedQuery.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
const { timeStart, timeEnd, limit, offset } = parsedQuery.data;
|
||||
|
||||
const parsedParams = queryAccessAuditLogsParams.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
const { orgId } = parsedParams.data;
|
||||
|
||||
const baseQuery = querySites(timeStart, timeEnd, orgId);
|
||||
|
||||
const log = await baseQuery.limit(limit).offset(offset);
|
||||
|
||||
const countQuery = db
|
||||
.select({ count: count() })
|
||||
.from(actionAuditLog)
|
||||
.where(
|
||||
and(
|
||||
gt(actionAuditLog.timestamp, timeStart),
|
||||
lt(actionAuditLog.timestamp, timeEnd),
|
||||
eq(actionAuditLog.orgId, orgId)
|
||||
)
|
||||
);
|
||||
|
||||
const totalCountResult = await countQuery;
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return response<QueryActionAuditLogResponse>(res, {
|
||||
data: {
|
||||
log: log,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset
|
||||
}
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Action audit logs retrieved successfully",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from "@server/middlewares";
|
||||
import { ActionsEnum } from "@server/auth/actions";
|
||||
import {
|
||||
logActionAudit,
|
||||
verifyCertificateAccess,
|
||||
verifyIdpAccess,
|
||||
verifyLoginPageAccess,
|
||||
@@ -72,7 +73,8 @@ authenticated.put(
|
||||
verifyValidLicense,
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.createIdp),
|
||||
orgIdp.createOrgOidcIdp
|
||||
orgIdp.createOrgOidcIdp,
|
||||
logActionAudit(ActionsEnum.createIdp)
|
||||
);
|
||||
|
||||
authenticated.post(
|
||||
@@ -81,7 +83,8 @@ authenticated.post(
|
||||
verifyOrgAccess,
|
||||
verifyIdpAccess,
|
||||
verifyUserHasAction(ActionsEnum.updateIdp),
|
||||
orgIdp.updateOrgOidcIdp
|
||||
orgIdp.updateOrgOidcIdp,
|
||||
logActionAudit(ActionsEnum.updateIdp)
|
||||
);
|
||||
|
||||
authenticated.delete(
|
||||
@@ -90,7 +93,8 @@ authenticated.delete(
|
||||
verifyOrgAccess,
|
||||
verifyIdpAccess,
|
||||
verifyUserHasAction(ActionsEnum.deleteIdp),
|
||||
orgIdp.deleteOrgIdp
|
||||
orgIdp.deleteOrgIdp,
|
||||
logActionAudit(ActionsEnum.deleteIdp)
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
@@ -127,7 +131,8 @@ authenticated.post(
|
||||
verifyOrgAccess,
|
||||
verifyCertificateAccess,
|
||||
verifyUserHasAction(ActionsEnum.restartCertificate),
|
||||
certificates.restartCertificate
|
||||
certificates.restartCertificate,
|
||||
logActionAudit(ActionsEnum.restartCertificate)
|
||||
);
|
||||
|
||||
if (build === "saas") {
|
||||
@@ -152,14 +157,16 @@ if (build === "saas") {
|
||||
"/org/:orgId/billing/create-checkout-session",
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.billing),
|
||||
billing.createCheckoutSession
|
||||
billing.createCheckoutSession,
|
||||
logActionAudit(ActionsEnum.billing)
|
||||
);
|
||||
|
||||
authenticated.post(
|
||||
"/org/:orgId/billing/create-portal-session",
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.billing),
|
||||
billing.createPortalSession
|
||||
billing.createPortalSession,
|
||||
logActionAudit(ActionsEnum.billing)
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
@@ -206,7 +213,8 @@ authenticated.put(
|
||||
verifyValidLicense,
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.createRemoteExitNode),
|
||||
remoteExitNode.createRemoteExitNode
|
||||
remoteExitNode.createRemoteExitNode,
|
||||
logActionAudit(ActionsEnum.createRemoteExitNode)
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
@@ -240,7 +248,8 @@ authenticated.delete(
|
||||
verifyOrgAccess,
|
||||
verifyRemoteExitNodeAccess,
|
||||
verifyUserHasAction(ActionsEnum.deleteRemoteExitNode),
|
||||
remoteExitNode.deleteRemoteExitNode
|
||||
remoteExitNode.deleteRemoteExitNode,
|
||||
logActionAudit(ActionsEnum.deleteRemoteExitNode)
|
||||
);
|
||||
|
||||
authenticated.put(
|
||||
@@ -248,7 +257,8 @@ authenticated.put(
|
||||
verifyValidLicense,
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.createLoginPage),
|
||||
loginPage.createLoginPage
|
||||
loginPage.createLoginPage,
|
||||
logActionAudit(ActionsEnum.createLoginPage)
|
||||
);
|
||||
|
||||
authenticated.post(
|
||||
@@ -257,7 +267,8 @@ authenticated.post(
|
||||
verifyOrgAccess,
|
||||
verifyLoginPageAccess,
|
||||
verifyUserHasAction(ActionsEnum.updateLoginPage),
|
||||
loginPage.updateLoginPage
|
||||
loginPage.updateLoginPage,
|
||||
logActionAudit(ActionsEnum.updateLoginPage)
|
||||
);
|
||||
|
||||
authenticated.delete(
|
||||
@@ -266,7 +277,8 @@ authenticated.delete(
|
||||
verifyOrgAccess,
|
||||
verifyLoginPageAccess,
|
||||
verifyUserHasAction(ActionsEnum.deleteLoginPage),
|
||||
loginPage.deleteLoginPage
|
||||
loginPage.deleteLoginPage,
|
||||
logActionAudit(ActionsEnum.deleteLoginPage)
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
import { ActionsEnum } from "@server/auth/actions";
|
||||
|
||||
import { unauthenticated as ua, authenticated as a } from "@server/routers/integration";
|
||||
import { logActionAudit } from "#private/middlewares";
|
||||
|
||||
export const unauthenticated = ua;
|
||||
export const authenticated = a;
|
||||
@@ -31,12 +32,14 @@ authenticated.post(
|
||||
`/org/:orgId/send-usage-notification`,
|
||||
verifyApiKeyIsRoot, // We are the only ones who can use root key so its fine
|
||||
verifyApiKeyHasAction(ActionsEnum.sendUsageNotification),
|
||||
org.sendUsageNotification
|
||||
org.sendUsageNotification,
|
||||
logActionAudit(ActionsEnum.sendUsageNotification)
|
||||
);
|
||||
|
||||
authenticated.delete(
|
||||
"/idp/:idpId",
|
||||
verifyApiKeyIsRoot,
|
||||
verifyApiKeyHasAction(ActionsEnum.deleteIdp),
|
||||
orgIdp.deleteOrgIdp
|
||||
orgIdp.deleteOrgIdp,
|
||||
logActionAudit(ActionsEnum.deleteIdp)
|
||||
);
|
||||
Reference in New Issue
Block a user