]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/audit-logger.ts
Add downloadingEnabled property to video model
[github/Chocobozzz/PeerTube.git] / server / helpers / audit-logger.ts
1 import * as path from 'path'
2 import * as express from 'express'
3 import { diff } from 'deep-object-diff'
4 import { chain } from 'lodash'
5 import * as flatten from 'flat'
6 import * as winston from 'winston'
7 import { CONFIG } from '../initializers'
8 import { jsonLoggerFormat, labelFormatter } from './logger'
9 import { VideoDetails, User, VideoChannel, VideoAbuse, VideoImport } from '../../shared'
10 import { VideoComment } from '../../shared/models/videos/video-comment.model'
11 import { CustomConfig } from '../../shared/models/server/custom-config.model'
12 import { UserModel } from '../models/account/user'
13
14 function getAuditIdFromRes (res: express.Response) {
15 return (res.locals.oauth.token.User as UserModel).username
16 }
17
18 enum AUDIT_TYPE {
19 CREATE = 'create',
20 UPDATE = 'update',
21 DELETE = 'delete'
22 }
23
24 const colors = winston.config.npm.colors
25 colors.audit = winston.config.npm.colors.info
26
27 winston.addColors(colors)
28
29 const auditLogger = winston.createLogger({
30 levels: { audit: 0 },
31 transports: [
32 new winston.transports.File({
33 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube-audit.log'),
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
48 function 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
69 function 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
83 abstract 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
93 const 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',
120 'commentsEnabled',
121 'downloadingEnabled'
122 ]
123 class VideoAuditView extends EntityAuditView {
124 constructor (private video: VideoDetails) {
125 super(videoKeysToKeep, 'video', video)
126 }
127 }
128
129 const videoImportKeysToKeep = [
130 'id',
131 'targetUrl',
132 'video-name'
133 ]
134 class VideoImportAuditView extends EntityAuditView {
135 constructor (private videoImport: VideoImport) {
136 super(videoImportKeysToKeep, 'video-import', videoImport)
137 }
138 }
139
140 const 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 ]
153 class CommentAuditView extends EntityAuditView {
154 constructor (private comment: VideoComment) {
155 super(commentKeysToKeep, 'comment', comment)
156 }
157 }
158
159 const 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 ]
182 class UserAuditView extends EntityAuditView {
183 constructor (private user: User) {
184 super(userKeysToKeep, 'user', user)
185 }
186 }
187
188 const 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 ]
208 class VideoChannelAuditView extends EntityAuditView {
209 constructor (private channel: VideoChannel) {
210 super(channelKeysToKeep, 'channel', channel)
211 }
212 }
213
214 const videoAbuseKeysToKeep = [
215 'id',
216 'reason',
217 'reporterAccount',
218 'video-id',
219 'video-name',
220 'video-uuid',
221 'createdAt'
222 ]
223 class VideoAbuseAuditView extends EntityAuditView {
224 constructor (private videoAbuse: VideoAbuse) {
225 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
226 }
227 }
228
229 const 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',
244 'signup-requiresEmailVerification',
245 'admin-email',
246 'user-videoQuota',
247 'transcoding-enabled',
248 'transcoding-threads',
249 'transcoding-resolutions'
250 ]
251 class 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]) => {
257 if (isEnabled) resolutionsArray.push(resolution)
258 })
259 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
260 super(customConfigKeysToKeep, 'config', infos)
261 }
262 }
263
264 export {
265 getAuditIdFromRes,
266
267 auditLoggerFactory,
268 VideoImportAuditView,
269 VideoChannelAuditView,
270 CommentAuditView,
271 UserAuditView,
272 VideoAuditView,
273 VideoAbuseAuditView,
274 CustomConfigAuditView
275 }