]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/audit-logger.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / helpers / audit-logger.ts
CommitLineData
59390818 1import { diff } from 'deep-object-diff'
41fb13c3
C
2import express from 'express'
3import flatten from 'flat'
d95d1559 4import { chain } from 'lodash'
41fb13c3
C
5import { join } from 'path'
6import { addColors, config, createLogger, format, transports } from 'winston'
d95d1559 7import { AUDIT_LOG_FILENAME } from '@server/initializers/constants'
2a491182 8import { AdminAbuse, CustomConfig, User, VideoChannel, VideoChannelSync, VideoComment, VideoDetails, VideoImport } from '@shared/models'
6dd9de95 9import { CONFIG } from '../initializers/config'
d95d1559 10import { jsonLoggerFormat, labelFormatter } from './logger'
993cef4b
C
11
12function getAuditIdFromRes (res: express.Response) {
2ba92871 13 return res.locals.oauth.token.User.username
993cef4b 14}
59390818
AB
15
16enum AUDIT_TYPE {
17 CREATE = 'create',
18 UPDATE = 'update',
19 DELETE = 'delete'
20}
21
41fb13c3
C
22const colors = config.npm.colors
23colors.audit = config.npm.colors.info
59390818 24
41fb13c3 25addColors(colors)
59390818 26
41fb13c3 27const auditLogger = createLogger({
59390818
AB
28 levels: { audit: 0 },
29 transports: [
41fb13c3
C
30 new transports.File({
31 filename: join(CONFIG.STORAGE.LOG_DIR, AUDIT_LOG_FILENAME),
59390818
AB
32 level: 'audit',
33 maxsize: 5242880,
34 maxFiles: 5,
41fb13c3
C
35 format: format.combine(
36 format.timestamp(),
1b05d82d 37 labelFormatter(),
41fb13c3 38 format.splat(),
59390818
AB
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 {
a1587156
C
82 constructor (private readonly keysToKeep: string[], private readonly prefix: string, private readonly entityInfos: object) { }
83
59390818 84 toLogKeys (): object {
41fb13c3 85 return chain(flatten<object, any>(this.entityInfos, { delimiter: '-', safe: true }))
59390818 86 .pick(this.keysToKeep)
41fb13c3 87 .mapKeys((_value, key) => `${this.prefix}-${key}`)
59390818
AB
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',
156c50af 119 'commentsEnabled',
7f2cfe3a 120 'downloadEnabled'
59390818 121]
80e36cd9 122class VideoAuditView extends EntityAuditView {
38f57175 123 constructor (video: VideoDetails) {
59390818
AB
124 super(videoKeysToKeep, 'video', video)
125 }
126}
127
7e5f9f00
C
128const videoImportKeysToKeep = [
129 'id',
130 'targetUrl',
131 'video-name'
132]
133class VideoImportAuditView extends EntityAuditView {
38f57175 134 constructor (videoImport: VideoImport) {
7e5f9f00
C
135 super(videoImportKeysToKeep, 'video-import', videoImport)
136 }
137}
138
80e36cd9
AB
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 {
38f57175 153 constructor (comment: VideoComment) {
80e36cd9
AB
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 {
38f57175 182 constructor (user: User) {
80e36cd9
AB
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 {
38f57175 208 constructor (channel: VideoChannel) {
80e36cd9
AB
209 super(channelKeysToKeep, 'channel', channel)
210 }
211}
212
d95d1559 213const abuseKeysToKeep = [
80e36cd9
AB
214 'id',
215 'reason',
216 'reporterAccount',
80e36cd9
AB
217 'createdAt'
218]
d95d1559 219class AbuseAuditView extends EntityAuditView {
38f57175 220 constructor (abuse: AdminAbuse) {
d95d1559 221 super(abuseKeysToKeep, 'abuse', abuse)
80e36cd9
AB
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',
d9eaee39 240 'signup-requiresEmailVerification',
80e36cd9
AB
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 = []
a1587156
C
252
253 Object.entries(resolutionsDict)
254 .forEach(([ resolution, isEnabled ]) => {
255 if (isEnabled) resolutionsArray.push(resolution)
256 })
257
5d08a6a7 258 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
80e36cd9
AB
259 super(customConfigKeysToKeep, 'config', infos)
260 }
261}
262
2a491182
F
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
59390818 275export {
993cef4b
C
276 getAuditIdFromRes,
277
59390818 278 auditLoggerFactory,
7e5f9f00 279 VideoImportAuditView,
80e36cd9
AB
280 VideoChannelAuditView,
281 CommentAuditView,
282 UserAuditView,
283 VideoAuditView,
d95d1559 284 AbuseAuditView,
2a491182
F
285 CustomConfigAuditView,
286 VideoChannelSyncAuditView
59390818 287}