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, User, VideoChannel, VideoDetails, VideoImport } from '../../shared'
9 import { CustomConfig } from '../../shared/models/server/custom-config.model'
10 import { VideoComment } from '../../shared/models/videos/comment/video-comment.model'
11 import { CONFIG } from '../initializers/config'
12 import { jsonLoggerFormat, labelFormatter } from './logger'
14 function getAuditIdFromRes (res: express.Response) {
15 return res.locals.oauth.token.User.username
24 const colors = config.npm.colors
25 colors.audit = config.npm.colors.info
29 const auditLogger = createLogger({
33 filename: join(CONFIG.STORAGE.LOG_DIR, AUDIT_LOG_FILENAME),
37 format: format.combine(
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]
57 entityInfos = { ...oldEntityKeys, ...diffKeys }
59 entityInfos = { ...entity.toLogKeys() }
61 auditLogger.log('audit', JSON.stringify({
69 function auditLoggerFactory (domain: string) {
71 create (user: string, entity: EntityAuditView) {
72 auditLoggerWrapper(domain, user, AUDIT_TYPE.CREATE, entity)
74 update (user: string, entity: EntityAuditView, oldEntity: EntityAuditView) {
75 auditLoggerWrapper(domain, user, AUDIT_TYPE.UPDATE, entity, oldEntity)
77 delete (user: string, entity: EntityAuditView) {
78 auditLoggerWrapper(domain, user, AUDIT_TYPE.DELETE, entity)
83 abstract class EntityAuditView {
84 constructor (private readonly keysToKeep: string[], private readonly prefix: string, private readonly entityInfos: object) { }
86 toLogKeys (): object {
87 return chain(flatten<object, any>(this.entityInfos, { delimiter: '-', safe: true }))
88 .pick(this.keysToKeep)
89 .mapKeys((_value, key) => `${this.prefix}-${key}`)
94 const videoKeysToKeep = [
124 class VideoAuditView extends EntityAuditView {
125 constructor (private readonly video: VideoDetails) {
126 super(videoKeysToKeep, 'video', video)
130 const videoImportKeysToKeep = [
135 class VideoImportAuditView extends EntityAuditView {
136 constructor (private readonly videoImport: VideoImport) {
137 super(videoImportKeysToKeep, 'video-import', videoImport)
141 const commentKeysToKeep = [
145 'inReplyToCommentId',
154 class CommentAuditView extends EntityAuditView {
155 constructor (private readonly comment: VideoComment) {
156 super(commentKeysToKeep, 'comment', comment)
160 const userKeysToKeep = [
172 'account-followingCount',
173 'account-followersCount',
176 'account-avatar-path',
177 'account-avatar-createdAt',
178 'account-avatar-updatedAt',
179 'account-displayName',
180 'account-description',
183 class UserAuditView extends EntityAuditView {
184 constructor (private readonly user: User) {
185 super(userKeysToKeep, 'user', user)
189 const channelKeysToKeep = [
207 'ownerAccount-displayedName'
209 class VideoChannelAuditView extends EntityAuditView {
210 constructor (private readonly channel: VideoChannel) {
211 super(channelKeysToKeep, 'channel', channel)
215 const abuseKeysToKeep = [
221 class AbuseAuditView extends EntityAuditView {
222 constructor (private readonly abuse: AdminAbuse) {
223 super(abuseKeysToKeep, 'abuse', abuse)
227 const customConfigKeysToKeep = [
229 'instance-shortDescription',
230 'instance-description',
232 'instance-defaultClientRoute',
233 'instance-defaultNSFWPolicy',
234 'instance-customizations-javascript',
235 'instance-customizations-css',
236 'services-twitter-username',
237 'services-twitter-whitelisted',
238 'cache-previews-size',
239 'cache-captions-size',
242 'signup-requiresEmailVerification',
245 'transcoding-enabled',
246 'transcoding-threads',
247 'transcoding-resolutions'
249 class CustomConfigAuditView extends EntityAuditView {
250 constructor (customConfig: CustomConfig) {
251 const infos: any = customConfig
252 const resolutionsDict = infos.transcoding.resolutions
253 const resolutionsArray = []
255 Object.entries(resolutionsDict)
256 .forEach(([ resolution, isEnabled ]) => {
257 if (isEnabled) resolutionsArray.push(resolution)
260 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
261 super(customConfigKeysToKeep, 'config', infos)
269 VideoImportAuditView,
270 VideoChannelAuditView,
275 CustomConfigAuditView