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 { jsonLoggerFormat, labelFormatter } from './logger'
8 import { User, VideoAbuse, VideoChannel, VideoDetails, VideoImport } from '../../shared'
9 import { VideoComment } from '../../shared/models/videos/video-comment.model'
10 import { CustomConfig } from '../../shared/models/server/custom-config.model'
11 import { CONFIG } from '../initializers/config'
12 import { AUDIT_LOG_FILENAME } from '@server/initializers/constants'
14 function getAuditIdFromRes (res: express.Response) {
15 return res.locals.oauth.token.User.username
24 const colors = winston.config.npm.colors
25 colors.audit = winston.config.npm.colors.info
27 winston.addColors(colors)
29 const auditLogger = winston.createLogger({
32 new winston.transports.File({
33 filename: path.join(CONFIG.STORAGE.LOG_DIR, AUDIT_LOG_FILENAME),
37 format: winston.format.combine(
38 winston.format.timestamp(),
40 winston.format.splat(),
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(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 videoAbuseKeysToKeep = [
224 class VideoAbuseAuditView extends EntityAuditView {
225 constructor (private readonly videoAbuse: VideoAbuse) {
226 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
230 const customConfigKeysToKeep = [
232 'instance-shortDescription',
233 'instance-description',
235 'instance-defaultClientRoute',
236 'instance-defaultNSFWPolicy',
237 'instance-customizations-javascript',
238 'instance-customizations-css',
239 'services-twitter-username',
240 'services-twitter-whitelisted',
241 'cache-previews-size',
242 'cache-captions-size',
245 'signup-requiresEmailVerification',
248 'transcoding-enabled',
249 'transcoding-threads',
250 'transcoding-resolutions'
252 class CustomConfigAuditView extends EntityAuditView {
253 constructor (customConfig: CustomConfig) {
254 const infos: any = customConfig
255 const resolutionsDict = infos.transcoding.resolutions
256 const resolutionsArray = []
258 Object.entries(resolutionsDict)
259 .forEach(([ resolution, isEnabled ]) => {
260 if (isEnabled) resolutionsArray.push(resolution)
263 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
264 super(customConfigKeysToKeep, 'config', infos)
272 VideoImportAuditView,
273 VideoChannelAuditView,
278 CustomConfigAuditView