|
| 1 | +import { QueryCommand } from "@aws-sdk/client-dynamodb"; |
| 2 | +import { unmarshall } from "@aws-sdk/util-dynamodb"; |
| 3 | +import { createAuditLogEntry } from "api/functions/auditLog.js"; |
| 4 | +import rateLimiter from "api/plugins/rateLimiter.js"; |
| 5 | +import { genericConfig } from "common/config.js"; |
| 6 | +import { |
| 7 | + BaseError, |
| 8 | + DatabaseFetchError, |
| 9 | + ValidationError, |
| 10 | +} from "common/errors/index.js"; |
| 11 | +import { Modules } from "common/modules.js"; |
| 12 | +import { AppRoles } from "common/roles.js"; |
| 13 | +import fastify, { FastifyPluginAsync } from "fastify"; |
| 14 | +import { request } from "http"; |
| 15 | + |
| 16 | +type GetLogsRequest = { |
| 17 | + Params: { module: string }; |
| 18 | + Querystring: { start: number; end: number }; |
| 19 | + Body: undefined; |
| 20 | +}; |
| 21 | + |
| 22 | +const logsPlugin: FastifyPluginAsync = async (fastify, _options) => { |
| 23 | + fastify.register(rateLimiter, { |
| 24 | + limit: 10, |
| 25 | + duration: 30, |
| 26 | + rateLimitIdentifier: "logs", |
| 27 | + }); |
| 28 | + fastify.get<GetLogsRequest>( |
| 29 | + "/:module", |
| 30 | + { |
| 31 | + schema: { |
| 32 | + querystring: { |
| 33 | + type: "object", |
| 34 | + required: ["start", "end"], |
| 35 | + properties: { |
| 36 | + start: { type: "number" }, |
| 37 | + end: { type: "number" }, |
| 38 | + }, |
| 39 | + additionalProperties: false, |
| 40 | + }, |
| 41 | + }, |
| 42 | + onRequest: async (request, reply) => { |
| 43 | + await fastify.authorize(request, reply, [AppRoles.AUDIT_LOG_VIEWER]); |
| 44 | + }, |
| 45 | + preValidation: async (request, reply) => { |
| 46 | + const { module } = request.params; |
| 47 | + const { start, end } = request.query; |
| 48 | + |
| 49 | + if (!Object.values(Modules).includes(module as Modules)) { |
| 50 | + throw new ValidationError({ message: `Invalid module "${module}".` }); |
| 51 | + } |
| 52 | + if (end <= start) { |
| 53 | + throw new ValidationError({ |
| 54 | + message: `End must be greater than start.`, |
| 55 | + }); |
| 56 | + } |
| 57 | + }, |
| 58 | + }, |
| 59 | + async (request, reply) => { |
| 60 | + const { module } = request.params; |
| 61 | + const { start, end } = request.query; |
| 62 | + const logPromise = createAuditLogEntry({ |
| 63 | + dynamoClient: fastify.dynamoClient, |
| 64 | + entry: { |
| 65 | + module: Modules.AUDIT_LOG, |
| 66 | + actor: request.username!, |
| 67 | + target: module, |
| 68 | + message: `Viewed audit log from ${start} to ${end}.`, |
| 69 | + }, |
| 70 | + }); |
| 71 | + const queryCommand = new QueryCommand({ |
| 72 | + TableName: genericConfig.AuditLogTable, |
| 73 | + KeyConditionExpression: "#pk = :module AND #sk BETWEEN :start AND :end", |
| 74 | + ExpressionAttributeNames: { |
| 75 | + "#pk": "module", |
| 76 | + "#sk": "createdAt", |
| 77 | + }, |
| 78 | + ExpressionAttributeValues: { |
| 79 | + ":module": { S: module }, |
| 80 | + ":start": { N: start.toString() }, |
| 81 | + ":end": { N: end.toString() }, |
| 82 | + }, |
| 83 | + ScanIndexForward: false, |
| 84 | + }); |
| 85 | + let response; |
| 86 | + try { |
| 87 | + response = await fastify.dynamoClient.send(queryCommand); |
| 88 | + if (!response.Items) { |
| 89 | + throw new DatabaseFetchError({ |
| 90 | + message: "Error occurred fetching audit log.", |
| 91 | + }); |
| 92 | + } |
| 93 | + } catch (e) { |
| 94 | + if (e instanceof BaseError) { |
| 95 | + throw e; |
| 96 | + } |
| 97 | + fastify.log.error(e); |
| 98 | + throw new DatabaseFetchError({ |
| 99 | + message: "Error occurred fetching audit log.", |
| 100 | + }); |
| 101 | + } |
| 102 | + await logPromise; |
| 103 | + reply.send(response.Items.map((x) => unmarshall(x))); |
| 104 | + }, |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +export default logsPlugin; |
0 commit comments