]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/logs.ts
Add ability for client to create server logs
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / logs.ts
1 import express from 'express'
2 import { body, query } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { isStringArray } from '@server/helpers/custom-validators/search'
5 import { CONFIG } from '@server/initializers/config'
6 import { HttpStatusCode } from '@shared/models'
7 import {
8 isValidClientLogLevel,
9 isValidClientLogMessage,
10 isValidClientLogMeta,
11 isValidClientLogStackTrace,
12 isValidClientLogUserAgent,
13 isValidLogLevel
14 } from '../../helpers/custom-validators/logs'
15 import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
16 import { logger } from '../../helpers/logger'
17 import { areValidationErrors } from './shared'
18
19 const createClientLogValidator = [
20 body('message')
21 .custom(isValidClientLogMessage).withMessage('Should have a valid log message'),
22
23 body('url')
24 .custom(isUrlValid).withMessage('Should have a valid log url'),
25
26 body('level')
27 .custom(isValidClientLogLevel).withMessage('Should have a valid log message'),
28
29 body('stackTrace')
30 .optional()
31 .custom(isValidClientLogStackTrace).withMessage('Should have a valid log stack trace'),
32
33 body('meta')
34 .optional()
35 .custom(isValidClientLogMeta).withMessage('Should have a valid log meta'),
36
37 body('userAgent')
38 .optional()
39 .custom(isValidClientLogUserAgent).withMessage('Should have a valid log user agent'),
40
41 (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking createClientLogValidator parameters.', { parameters: req.query })
43
44 if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) {
45 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
46 }
47
48 if (areValidationErrors(req, res)) return
49
50 return next()
51 }
52 ]
53
54 const getLogsValidator = [
55 query('startDate')
56 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
57 query('level')
58 .optional()
59 .custom(isValidLogLevel).withMessage('Should have a valid level'),
60 query('tagsOneOf')
61 .optional()
62 .customSanitizer(toArray)
63 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
64 query('endDate')
65 .optional()
66 .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
67
68 (req: express.Request, res: express.Response, next: express.NextFunction) => {
69 logger.debug('Checking getLogsValidator parameters.', { parameters: req.query })
70
71 if (areValidationErrors(req, res)) return
72
73 return next()
74 }
75 ]
76
77 const getAuditLogsValidator = [
78 query('startDate')
79 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
80 query('endDate')
81 .optional()
82 .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
83
84 (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking getAuditLogsValidator parameters.', { parameters: req.query })
86
87 if (areValidationErrors(req, res)) return
88
89 return next()
90 }
91 ]
92
93 // ---------------------------------------------------------------------------
94
95 export {
96 getLogsValidator,
97 getAuditLogsValidator,
98 createClientLogValidator
99 }