]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/audit-logger.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / audit-logger.ts
CommitLineData
59390818 1import { diff } from 'deep-object-diff'
d95d1559 2import * as express from 'express'
59390818 3import * as flatten from 'flat'
d95d1559
C
4import { chain } from 'lodash'
5import * as path from 'path'
59390818 6import * as winston from 'winston'
d95d1559 7import { AUDIT_LOG_FILENAME } from '@server/initializers/constants'
edbc9325 8import { AdminAbuse, User, VideoChannel, VideoDetails, VideoImport } from '../../shared'
80e36cd9 9import { CustomConfig } from '../../shared/models/server/custom-config.model'
d95d1559 10import { VideoComment } from '../../shared/models/videos/video-comment.model'
6dd9de95 11import { CONFIG } from '../initializers/config'
d95d1559 12import { jsonLoggerFormat, labelFormatter } from './logger'
993cef4b
C
13
14function getAuditIdFromRes (res: express.Response) {
2ba92871 15 return res.locals.oauth.token.User.username
993cef4b 16}
59390818
AB
17
18enum AUDIT_TYPE {
19 CREATE = 'create',
20 UPDATE = 'update',
21 DELETE = 'delete'
22}
23
24const colors = winston.config.npm.colors
25colors.audit = winston.config.npm.colors.info
26
27winston.addColors(colors)
28
29const auditLogger = winston.createLogger({
30 levels: { audit: 0 },
31 transports: [
32 new winston.transports.File({
566c125d 33 filename: path.join(CONFIG.STORAGE.LOG_DIR, AUDIT_LOG_FILENAME),
59390818
AB
34 level: 'audit',
35 maxsize: 5242880,
36 maxFiles: 5,
37 format: winston.format.combine(
38 winston.format.timestamp(),
1b05d82d 39 labelFormatter(),
59390818
AB
40 winston.format.splat(),
41 jsonLoggerFormat
42 )
43 })
44 ],
45 exitOnError: true
46})
47
48function auditLoggerWrapper (domain: string, user: string, action: AUDIT_TYPE, entity: EntityAuditView, oldEntity: EntityAuditView = null) {
49 let entityInfos: object
50 if (action === AUDIT_TYPE.UPDATE && oldEntity) {
51 const oldEntityKeys = oldEntity.toLogKeys()
52 const diffObject = diff(oldEntityKeys, entity.toLogKeys())
53 const diffKeys = Object.entries(diffObject).reduce((newKeys, entry) => {
54 newKeys[`new-${entry[0]}`] = entry[1]
55 return newKeys
56 }, {})
57 entityInfos = { ...oldEntityKeys, ...diffKeys }
58 } else {
59 entityInfos = { ...entity.toLogKeys() }
60 }
61 auditLogger.log('audit', JSON.stringify({
62 user,
63 domain,
64 action,
65 ...entityInfos
66 }))
67}
68
69function auditLoggerFactory (domain: string) {
70 return {
71 create (user: string, entity: EntityAuditView) {
72 auditLoggerWrapper(domain, user, AUDIT_TYPE.CREATE, entity)
73 },
74 update (user: string, entity: EntityAuditView, oldEntity: EntityAuditView) {
75 auditLoggerWrapper(domain, user, AUDIT_TYPE.UPDATE, entity, oldEntity)
76 },
77 delete (user: string, entity: EntityAuditView) {
78 auditLoggerWrapper(domain, user, AUDIT_TYPE.DELETE, entity)
79 }
80 }
81}
82
83abstract class EntityAuditView {
a1587156
C
84 constructor (private readonly keysToKeep: string[], private readonly prefix: string, private readonly entityInfos: object) { }
85
59390818
AB
86 toLogKeys (): object {
87 return chain(flatten(this.entityInfos, { delimiter: '-', safe: true }))
88 .pick(this.keysToKeep)
89 .mapKeys((value, key) => `${this.prefix}-${key}`)
90 .value()
91 }
92}
93
94const videoKeysToKeep = [
95 'tags',
96 'uuid',
97 'id',
98 'uuid',
99 'createdAt',
100 'updatedAt',
101 'publishedAt',
102 'category',
103 'licence',
104 'language',
105 'privacy',
106 'description',
107 'duration',
108 'isLocal',
109 'name',
110 'thumbnailPath',
111 'previewPath',
112 'nsfw',
113 'waitTranscoding',
114 'account-id',
115 'account-uuid',
116 'account-name',
117 'channel-id',
118 'channel-uuid',
119 'channel-name',
120 'support',
156c50af 121 'commentsEnabled',
7f2cfe3a 122 'downloadEnabled'
59390818 123]
80e36cd9 124class VideoAuditView extends EntityAuditView {
a1587156 125 constructor (private readonly video: VideoDetails) {
59390818
AB
126 super(videoKeysToKeep, 'video', video)
127 }
128}
129
7e5f9f00
C
130const videoImportKeysToKeep = [
131 'id',
132 'targetUrl',
133 'video-name'
134]
135class VideoImportAuditView extends EntityAuditView {
a1587156 136 constructor (private readonly videoImport: VideoImport) {
7e5f9f00
C
137 super(videoImportKeysToKeep, 'video-import', videoImport)
138 }
139}
140
80e36cd9
AB
141const commentKeysToKeep = [
142 'id',
143 'text',
144 'threadId',
145 'inReplyToCommentId',
146 'videoId',
147 'createdAt',
148 'updatedAt',
149 'totalReplies',
150 'account-id',
151 'account-uuid',
152 'account-name'
153]
154class CommentAuditView extends EntityAuditView {
a1587156 155 constructor (private readonly comment: VideoComment) {
80e36cd9
AB
156 super(commentKeysToKeep, 'comment', comment)
157 }
158}
159
160const userKeysToKeep = [
161 'id',
162 'username',
163 'email',
164 'nsfwPolicy',
165 'autoPlayVideo',
166 'role',
167 'videoQuota',
168 'createdAt',
169 'account-id',
170 'account-uuid',
171 'account-name',
172 'account-followingCount',
173 'account-followersCount',
174 'account-createdAt',
175 'account-updatedAt',
176 'account-avatar-path',
177 'account-avatar-createdAt',
178 'account-avatar-updatedAt',
179 'account-displayName',
180 'account-description',
181 'videoChannels'
182]
183class UserAuditView extends EntityAuditView {
a1587156 184 constructor (private readonly user: User) {
80e36cd9
AB
185 super(userKeysToKeep, 'user', user)
186 }
187}
188
189const channelKeysToKeep = [
190 'id',
191 'uuid',
192 'name',
193 'followingCount',
194 'followersCount',
195 'createdAt',
196 'updatedAt',
197 'avatar-path',
198 'avatar-createdAt',
199 'avatar-updatedAt',
200 'displayName',
201 'description',
202 'support',
203 'isLocal',
204 'ownerAccount-id',
205 'ownerAccount-uuid',
206 'ownerAccount-name',
207 'ownerAccount-displayedName'
208]
209class VideoChannelAuditView extends EntityAuditView {
a1587156 210 constructor (private readonly channel: VideoChannel) {
80e36cd9
AB
211 super(channelKeysToKeep, 'channel', channel)
212 }
213}
214
d95d1559 215const abuseKeysToKeep = [
80e36cd9
AB
216 'id',
217 'reason',
218 'reporterAccount',
80e36cd9
AB
219 'createdAt'
220]
d95d1559 221class AbuseAuditView extends EntityAuditView {
edbc9325 222 constructor (private readonly abuse: AdminAbuse) {
d95d1559 223 super(abuseKeysToKeep, 'abuse', abuse)
80e36cd9
AB
224 }
225}
226
227const customConfigKeysToKeep = [
228 'instance-name',
229 'instance-shortDescription',
230 'instance-description',
231 'instance-terms',
232 'instance-defaultClientRoute',
233 'instance-defaultNSFWPolicy',
234 'instance-customizations-javascript',
235 'instance-customizations-css',
236 'services-twitter-username',
237 'services-twitter-whitelisted',
238 'cache-previews-size',
239 'cache-captions-size',
240 'signup-enabled',
241 'signup-limit',
d9eaee39 242 'signup-requiresEmailVerification',
80e36cd9
AB
243 'admin-email',
244 'user-videoQuota',
245 'transcoding-enabled',
246 'transcoding-threads',
247 'transcoding-resolutions'
248]
249class CustomConfigAuditView extends EntityAuditView {
250 constructor (customConfig: CustomConfig) {
251 const infos: any = customConfig
252 const resolutionsDict = infos.transcoding.resolutions
253 const resolutionsArray = []
a1587156
C
254
255 Object.entries(resolutionsDict)
256 .forEach(([ resolution, isEnabled ]) => {
257 if (isEnabled) resolutionsArray.push(resolution)
258 })
259
5d08a6a7 260 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
80e36cd9
AB
261 super(customConfigKeysToKeep, 'config', infos)
262 }
263}
264
59390818 265export {
993cef4b
C
266 getAuditIdFromRes,
267
59390818 268 auditLoggerFactory,
7e5f9f00 269 VideoImportAuditView,
80e36cd9
AB
270 VideoChannelAuditView,
271 CommentAuditView,
272 UserAuditView,
273 VideoAuditView,
d95d1559 274 AbuseAuditView,
80e36cd9 275 CustomConfigAuditView
59390818 276}