1 import { diff } from 'deep-object-diff'
2 import express from 'express'
3 import flatten from 'flat'
4 import { chain } from 'lodash'
5 import { join } from 'path'
6 import { addColors, config, createLogger, format, transports } from 'winston'
7 import { AUDIT_LOG_FILENAME } from '@server/initializers/constants'
8 import { AdminAbuse, CustomConfig, User, VideoChannel, VideoComment, VideoDetails, VideoImport } from '@shared/models'
9 import { CONFIG } from '../initializers/config'
10 import { jsonLoggerFormat, labelFormatter } from './logger'
12 function getAuditIdFromRes (res: express.Response) {
13 return res.locals.oauth.token.User.username
22 const colors = config.npm.colors
23 colors.audit = config.npm.colors.info
27 const auditLogger = createLogger({
31 filename: join(CONFIG.STORAGE.LOG_DIR, AUDIT_LOG_FILENAME),
35 format: format.combine(
46 function 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]
55 entityInfos = { ...oldEntityKeys, ...diffKeys }
57 entityInfos = { ...entity.toLogKeys() }
59 auditLogger.log('audit', JSON.stringify({
67 function auditLoggerFactory (domain: string) {
69 create (user: string, entity: EntityAuditView) {
70 auditLoggerWrapper(domain, user, AUDIT_TYPE.CREATE, entity)
72 update (user: string, entity: EntityAuditView, oldEntity: EntityAuditView) {
73 auditLoggerWrapper(domain, user, AUDIT_TYPE.UPDATE, entity, oldEntity)
75 delete (user: string, entity: EntityAuditView) {
76 auditLoggerWrapper(domain, user, AUDIT_TYPE.DELETE, entity)
81 abstract class EntityAuditView {
82 constructor (private readonly keysToKeep: string[], private readonly prefix: string, private readonly entityInfos: object) { }
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}`)
92 const videoKeysToKeep = [
122 class VideoAuditView extends EntityAuditView {
123 constructor (private readonly video: VideoDetails) {
124 super(videoKeysToKeep, 'video', video)
128 const videoImportKeysToKeep = [
133 class VideoImportAuditView extends EntityAuditView {
134 constructor (private readonly videoImport: VideoImport) {
135 super(videoImportKeysToKeep, 'video-import', videoImport)
139 const commentKeysToKeep = [
143 'inReplyToCommentId',
152 class CommentAuditView extends EntityAuditView {
153 constructor (private readonly comment: VideoComment) {
154 super(commentKeysToKeep, 'comment', comment)
158 const userKeysToKeep = [
170 'account-followingCount',
171 'account-followersCount',
174 'account-avatar-path',
175 'account-avatar-createdAt',
176 'account-avatar-updatedAt',
177 'account-displayName',
178 'account-description',
181 class UserAuditView extends EntityAuditView {
182 constructor (private readonly user: User) {
183 super(userKeysToKeep, 'user', user)
187 const channelKeysToKeep = [
205 'ownerAccount-displayedName'
207 class VideoChannelAuditView extends EntityAuditView {
208 constructor (private readonly channel: VideoChannel) {
209 super(channelKeysToKeep, 'channel', channel)
213 const abuseKeysToKeep = [
219 class AbuseAuditView extends EntityAuditView {
220 constructor (private readonly abuse: AdminAbuse) {
221 super(abuseKeysToKeep, 'abuse', abuse)
225 const customConfigKeysToKeep = [
227 'instance-shortDescription',
228 'instance-description',
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',
240 'signup-requiresEmailVerification',
243 'transcoding-enabled',
244 'transcoding-threads',
245 'transcoding-resolutions'
247 class CustomConfigAuditView extends EntityAuditView {
248 constructor (customConfig: CustomConfig) {
249 const infos: any = customConfig
250 const resolutionsDict = infos.transcoding.resolutions
251 const resolutionsArray = []
253 Object.entries(resolutionsDict)
254 .forEach(([ resolution, isEnabled ]) => {
255 if (isEnabled) resolutionsArray.push(resolution)
258 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
259 super(customConfigKeysToKeep, 'config', infos)
267 VideoImportAuditView,
268 VideoChannelAuditView,
273 CustomConfigAuditView