aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/audit-logger.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/helpers/audit-logger.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/helpers/audit-logger.ts')
-rw-r--r--server/helpers/audit-logger.ts287
1 files changed, 0 insertions, 287 deletions
diff --git a/server/helpers/audit-logger.ts b/server/helpers/audit-logger.ts
deleted file mode 100644
index 7e8a03e8f..000000000
--- a/server/helpers/audit-logger.ts
+++ /dev/null
@@ -1,287 +0,0 @@
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}