From 566c125d6eee3bd907404523d94e1e0b5e403a46 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 11 Dec 2019 14:14:01 +0100 Subject: Serve audit logs to client --- server/controllers/api/server/logs.ts | 57 +++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/server/logs.ts b/server/controllers/api/server/logs.ts index e9d1f2efd..a0ca21cd5 100644 --- a/server/controllers/api/server/logs.ts +++ b/server/controllers/api/server/logs.ts @@ -3,11 +3,12 @@ import { UserRight } from '../../../../shared/models/users' import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' import { mtimeSortFilesDesc } from '../../../../shared/core-utils/logs/logs' import { readdir, readFile } from 'fs-extra' -import { MAX_LOGS_OUTPUT_CHARACTERS } from '../../../initializers/constants' +import { AUDIT_LOG_FILENAME, MAX_LOGS_OUTPUT_CHARACTERS, LOG_FILENAME } from '../../../initializers/constants' import { join } from 'path' -import { getLogsValidator } from '../../../middlewares/validators/logs' +import { getAuditLogsValidator, getLogsValidator } from '../../../middlewares/validators/logs' import { LogLevel } from '../../../../shared/models/server/log-level.type' import { CONFIG } from '../../../initializers/config' +import { logger } from '@server/helpers/logger' const logsRouter = express.Router() @@ -18,6 +19,13 @@ logsRouter.get('/logs', asyncMiddleware(getLogs) ) +logsRouter.get('/audit-logs', + authenticate, + ensureUserHasRight(UserRight.MANAGE_LOGS), + getAuditLogsValidator, + asyncMiddleware(getAuditLogs) +) + // --------------------------------------------------------------------------- export { @@ -26,18 +34,50 @@ export { // --------------------------------------------------------------------------- +const auditLogNameFilter = generateLogNameFilter(AUDIT_LOG_FILENAME) +async function getAuditLogs (req: express.Request, res: express.Response) { + const output = await generateOutput({ + startDateQuery: req.query.startDate, + endDateQuery: req.query.endDate, + level: 'audit', + nameFilter: auditLogNameFilter + }) + + return res.json(output).end() +} + +const logNameFilter = generateLogNameFilter(LOG_FILENAME) async function getLogs (req: express.Request, res: express.Response) { + const output = await generateOutput({ + startDateQuery: req.query.startDate, + endDateQuery: req.query.endDate, + level: req.query.level || 'info', + nameFilter: logNameFilter + }) + + return res.json(output).end() +} + +async function generateOutput (options: { + startDateQuery: string, + endDateQuery?: string, + level: LogLevel, + nameFilter: RegExp +}) { + const { startDateQuery, level, nameFilter } = options + const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) const sortedLogFiles = await mtimeSortFilesDesc(logFiles, CONFIG.STORAGE.LOG_DIR) let currentSize = 0 - const startDate = new Date(req.query.startDate) - const endDate = req.query.endDate ? new Date(req.query.endDate) : new Date() - const level: LogLevel = req.query.level || 'info' + const startDate = new Date(startDateQuery) + const endDate = options.endDateQuery ? new Date(options.endDateQuery) : new Date() let output: string[] = [] for (const meta of sortedLogFiles) { + if (nameFilter.exec(meta.file) === null) continue + const path = join(CONFIG.STORAGE.LOG_DIR, meta.file) const result = await getOutputFromFile(path, startDate, endDate, level, currentSize) @@ -49,7 +89,7 @@ async function getLogs (req: express.Request, res: express.Response) { if (currentSize > MAX_LOGS_OUTPUT_CHARACTERS || (result.logTime && result.logTime < startDate.getTime())) break } - return res.json(output).end() + return output } async function getOutputFromFile (path: string, startDate: Date, endDate: Date, level: LogLevel, currentSize: number) { @@ -58,6 +98,7 @@ async function getOutputFromFile (path: string, startDate: Date, endDate: Date, let logTime: number const logsLevel: { [ id in LogLevel ]: number } = { + audit: -1, debug: 0, info: 1, warn: 2, @@ -93,3 +134,7 @@ async function getOutputFromFile (path: string, startDate: Date, endDate: Date, return { currentSize, output: output.reverse(), logTime } } + +function generateLogNameFilter (baseName: string) { + return new RegExp('^' + baseName.replace(/\.log$/, '') + '\d*.log$') +} -- cgit v1.2.3