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