]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/audit-logger.ts
Serve audit logs to client
[github/Chocobozzz/PeerTube.git] / server / helpers / audit-logger.ts
CommitLineData
59390818 1import * as path from 'path'
993cef4b 2import * as express from 'express'
59390818
AB
3import { diff } from 'deep-object-diff'
4import { chain } from 'lodash'
5import * as flatten from 'flat'
6import * as winston from 'winston'
59390818 7import { jsonLoggerFormat, labelFormatter } from './logger'
2ba92871 8import { User, VideoAbuse, VideoChannel, VideoDetails, VideoImport } from '../../shared'
80e36cd9
AB
9import { VideoComment } from '../../shared/models/videos/video-comment.model'
10import { CustomConfig } from '../../shared/models/server/custom-config.model'
6dd9de95 11import { CONFIG } from '../initializers/config'
566c125d 12import { AUDIT_LOG_FILENAME } from '@server/initializers/constants'
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(),
39 labelFormatter,
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 {
84 constructor (private keysToKeep: Array<string>, private prefix: string, private entityInfos: object) { }
85 toLogKeys (): object {
86 return chain(flatten(this.entityInfos, { delimiter: '-', safe: true }))
87 .pick(this.keysToKeep)
88 .mapKeys((value, key) => `${this.prefix}-${key}`)
89 .value()
90 }
91}
92
93const videoKeysToKeep = [
94 'tags',
95 'uuid',
96 'id',
97 'uuid',
98 'createdAt',
99 'updatedAt',
100 'publishedAt',
101 'category',
102 'licence',
103 'language',
104 'privacy',
105 'description',
106 'duration',
107 'isLocal',
108 'name',
109 'thumbnailPath',
110 'previewPath',
111 'nsfw',
112 'waitTranscoding',
113 'account-id',
114 'account-uuid',
115 'account-name',
116 'channel-id',
117 'channel-uuid',
118 'channel-name',
119 'support',
156c50af 120 'commentsEnabled',
7f2cfe3a 121 'downloadEnabled'
59390818 122]
80e36cd9 123class VideoAuditView extends EntityAuditView {
59390818
AB
124 constructor (private video: VideoDetails) {
125 super(videoKeysToKeep, 'video', video)
126 }
127}
128
7e5f9f00
C
129const videoImportKeysToKeep = [
130 'id',
131 'targetUrl',
132 'video-name'
133]
134class VideoImportAuditView extends EntityAuditView {
135 constructor (private videoImport: VideoImport) {
136 super(videoImportKeysToKeep, 'video-import', videoImport)
137 }
138}
139
80e36cd9
AB
140const commentKeysToKeep = [
141 'id',
142 'text',
143 'threadId',
144 'inReplyToCommentId',
145 'videoId',
146 'createdAt',
147 'updatedAt',
148 'totalReplies',
149 'account-id',
150 'account-uuid',
151 'account-name'
152]
153class CommentAuditView extends EntityAuditView {
154 constructor (private comment: VideoComment) {
155 super(commentKeysToKeep, 'comment', comment)
156 }
157}
158
159const userKeysToKeep = [
160 'id',
161 'username',
162 'email',
163 'nsfwPolicy',
164 'autoPlayVideo',
165 'role',
166 'videoQuota',
167 'createdAt',
168 'account-id',
169 'account-uuid',
170 'account-name',
171 'account-followingCount',
172 'account-followersCount',
173 'account-createdAt',
174 'account-updatedAt',
175 'account-avatar-path',
176 'account-avatar-createdAt',
177 'account-avatar-updatedAt',
178 'account-displayName',
179 'account-description',
180 'videoChannels'
181]
182class UserAuditView extends EntityAuditView {
183 constructor (private user: User) {
184 super(userKeysToKeep, 'user', user)
185 }
186}
187
188const channelKeysToKeep = [
189 'id',
190 'uuid',
191 'name',
192 'followingCount',
193 'followersCount',
194 'createdAt',
195 'updatedAt',
196 'avatar-path',
197 'avatar-createdAt',
198 'avatar-updatedAt',
199 'displayName',
200 'description',
201 'support',
202 'isLocal',
203 'ownerAccount-id',
204 'ownerAccount-uuid',
205 'ownerAccount-name',
206 'ownerAccount-displayedName'
207]
208class VideoChannelAuditView extends EntityAuditView {
209 constructor (private channel: VideoChannel) {
210 super(channelKeysToKeep, 'channel', channel)
211 }
212}
213
214const videoAbuseKeysToKeep = [
215 'id',
216 'reason',
217 'reporterAccount',
218 'video-id',
219 'video-name',
220 'video-uuid',
221 'createdAt'
222]
223class VideoAbuseAuditView extends EntityAuditView {
224 constructor (private videoAbuse: VideoAbuse) {
225 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
226 }
227}
228
229const customConfigKeysToKeep = [
230 'instance-name',
231 'instance-shortDescription',
232 'instance-description',
233 'instance-terms',
234 'instance-defaultClientRoute',
235 'instance-defaultNSFWPolicy',
236 'instance-customizations-javascript',
237 'instance-customizations-css',
238 'services-twitter-username',
239 'services-twitter-whitelisted',
240 'cache-previews-size',
241 'cache-captions-size',
242 'signup-enabled',
243 'signup-limit',
d9eaee39 244 'signup-requiresEmailVerification',
80e36cd9
AB
245 'admin-email',
246 'user-videoQuota',
247 'transcoding-enabled',
248 'transcoding-threads',
249 'transcoding-resolutions'
250]
251class CustomConfigAuditView extends EntityAuditView {
252 constructor (customConfig: CustomConfig) {
253 const infos: any = customConfig
254 const resolutionsDict = infos.transcoding.resolutions
255 const resolutionsArray = []
256 Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => {
5d08a6a7 257 if (isEnabled) resolutionsArray.push(resolution)
80e36cd9 258 })
5d08a6a7 259 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
80e36cd9
AB
260 super(customConfigKeysToKeep, 'config', infos)
261 }
262}
263
59390818 264export {
993cef4b
C
265 getAuditIdFromRes,
266
59390818 267 auditLoggerFactory,
7e5f9f00 268 VideoImportAuditView,
80e36cd9
AB
269 VideoChannelAuditView,
270 CommentAuditView,
271 UserAuditView,
272 VideoAuditView,
273 VideoAbuseAuditView,
274 CustomConfigAuditView
59390818 275}