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