]>
Commit | Line | Data |
---|---|---|
59390818 AB |
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' | |
7e5f9f00 | 8 | import { VideoDetails, User, VideoChannel, VideoAbuse, VideoImport } from '../../shared' |
80e36cd9 AB |
9 | import { VideoComment } from '../../shared/models/videos/video-comment.model' |
10 | import { CustomConfig } from '../../shared/models/server/custom-config.model' | |
59390818 AB |
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 | ] | |
80e36cd9 | 116 | class VideoAuditView extends EntityAuditView { |
59390818 AB |
117 | constructor (private video: VideoDetails) { |
118 | super(videoKeysToKeep, 'video', video) | |
119 | } | |
120 | } | |
121 | ||
7e5f9f00 C |
122 | const videoImportKeysToKeep = [ |
123 | 'id', | |
124 | 'targetUrl', | |
125 | 'video-name' | |
126 | ] | |
127 | class VideoImportAuditView extends EntityAuditView { | |
128 | constructor (private videoImport: VideoImport) { | |
129 | super(videoImportKeysToKeep, 'video-import', videoImport) | |
130 | } | |
131 | } | |
132 | ||
80e36cd9 AB |
133 | const commentKeysToKeep = [ |
134 | 'id', | |
135 | 'text', | |
136 | 'threadId', | |
137 | 'inReplyToCommentId', | |
138 | 'videoId', | |
139 | 'createdAt', | |
140 | 'updatedAt', | |
141 | 'totalReplies', | |
142 | 'account-id', | |
143 | 'account-uuid', | |
144 | 'account-name' | |
145 | ] | |
146 | class CommentAuditView extends EntityAuditView { | |
147 | constructor (private comment: VideoComment) { | |
148 | super(commentKeysToKeep, 'comment', comment) | |
149 | } | |
150 | } | |
151 | ||
152 | const userKeysToKeep = [ | |
153 | 'id', | |
154 | 'username', | |
155 | 'email', | |
156 | 'nsfwPolicy', | |
157 | 'autoPlayVideo', | |
158 | 'role', | |
159 | 'videoQuota', | |
160 | 'createdAt', | |
161 | 'account-id', | |
162 | 'account-uuid', | |
163 | 'account-name', | |
164 | 'account-followingCount', | |
165 | 'account-followersCount', | |
166 | 'account-createdAt', | |
167 | 'account-updatedAt', | |
168 | 'account-avatar-path', | |
169 | 'account-avatar-createdAt', | |
170 | 'account-avatar-updatedAt', | |
171 | 'account-displayName', | |
172 | 'account-description', | |
173 | 'videoChannels' | |
174 | ] | |
175 | class UserAuditView extends EntityAuditView { | |
176 | constructor (private user: User) { | |
177 | super(userKeysToKeep, 'user', user) | |
178 | } | |
179 | } | |
180 | ||
181 | const channelKeysToKeep = [ | |
182 | 'id', | |
183 | 'uuid', | |
184 | 'name', | |
185 | 'followingCount', | |
186 | 'followersCount', | |
187 | 'createdAt', | |
188 | 'updatedAt', | |
189 | 'avatar-path', | |
190 | 'avatar-createdAt', | |
191 | 'avatar-updatedAt', | |
192 | 'displayName', | |
193 | 'description', | |
194 | 'support', | |
195 | 'isLocal', | |
196 | 'ownerAccount-id', | |
197 | 'ownerAccount-uuid', | |
198 | 'ownerAccount-name', | |
199 | 'ownerAccount-displayedName' | |
200 | ] | |
201 | class VideoChannelAuditView extends EntityAuditView { | |
202 | constructor (private channel: VideoChannel) { | |
203 | super(channelKeysToKeep, 'channel', channel) | |
204 | } | |
205 | } | |
206 | ||
207 | const videoAbuseKeysToKeep = [ | |
208 | 'id', | |
209 | 'reason', | |
210 | 'reporterAccount', | |
211 | 'video-id', | |
212 | 'video-name', | |
213 | 'video-uuid', | |
214 | 'createdAt' | |
215 | ] | |
216 | class VideoAbuseAuditView extends EntityAuditView { | |
217 | constructor (private videoAbuse: VideoAbuse) { | |
218 | super(videoAbuseKeysToKeep, 'abuse', videoAbuse) | |
219 | } | |
220 | } | |
221 | ||
222 | const customConfigKeysToKeep = [ | |
223 | 'instance-name', | |
224 | 'instance-shortDescription', | |
225 | 'instance-description', | |
226 | 'instance-terms', | |
227 | 'instance-defaultClientRoute', | |
228 | 'instance-defaultNSFWPolicy', | |
229 | 'instance-customizations-javascript', | |
230 | 'instance-customizations-css', | |
231 | 'services-twitter-username', | |
232 | 'services-twitter-whitelisted', | |
233 | 'cache-previews-size', | |
234 | 'cache-captions-size', | |
235 | 'signup-enabled', | |
236 | 'signup-limit', | |
237 | 'admin-email', | |
238 | 'user-videoQuota', | |
239 | 'transcoding-enabled', | |
240 | 'transcoding-threads', | |
241 | 'transcoding-resolutions' | |
242 | ] | |
243 | class CustomConfigAuditView extends EntityAuditView { | |
244 | constructor (customConfig: CustomConfig) { | |
245 | const infos: any = customConfig | |
246 | const resolutionsDict = infos.transcoding.resolutions | |
247 | const resolutionsArray = [] | |
248 | Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => { | |
5d08a6a7 | 249 | if (isEnabled) resolutionsArray.push(resolution) |
80e36cd9 | 250 | }) |
5d08a6a7 | 251 | Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } }) |
80e36cd9 AB |
252 | super(customConfigKeysToKeep, 'config', infos) |
253 | } | |
254 | } | |
255 | ||
59390818 AB |
256 | export { |
257 | auditLoggerFactory, | |
7e5f9f00 | 258 | VideoImportAuditView, |
80e36cd9 AB |
259 | VideoChannelAuditView, |
260 | CommentAuditView, | |
261 | UserAuditView, | |
262 | VideoAuditView, | |
263 | VideoAbuseAuditView, | |
264 | CustomConfigAuditView | |
59390818 | 265 | } |