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 { CONFIG } from '../initializers'
8 import { jsonLoggerFormat, labelFormatter } from './logger'
9 import { VideoDetails, User, VideoChannel, VideoAbuse, VideoImport } from '../../shared'
10 import { VideoComment } from '../../shared/models/videos/video-comment.model'
11 import { CustomConfig } from '../../shared/models/server/custom-config.model'
12 import { UserModel } from '../models/account/user'
14 function getAuditIdFromRes (res: express.Response) {
15 return (res.locals.oauth.token.User as UserModel).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, 'peertube-audit.log'),
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 keysToKeep: Array<string>, private prefix: string, private entityInfos: object) { }
85 toLogKeys (): object {
86 return chain(flatten(this.entityInfos, { delimiter: '-', safe: true }))
87 .pick(this.keysToKeep)
88 .mapKeys((value, key) => `${this.prefix}-${key}`)
93 const videoKeysToKeep = [
123 class VideoAuditView extends EntityAuditView {
124 constructor (private video: VideoDetails) {
125 super(videoKeysToKeep, 'video', video)
129 const videoImportKeysToKeep = [
134 class VideoImportAuditView extends EntityAuditView {
135 constructor (private videoImport: VideoImport) {
136 super(videoImportKeysToKeep, 'video-import', videoImport)
140 const commentKeysToKeep = [
144 'inReplyToCommentId',
153 class CommentAuditView extends EntityAuditView {
154 constructor (private comment: VideoComment) {
155 super(commentKeysToKeep, 'comment', comment)
159 const userKeysToKeep = [
171 'account-followingCount',
172 'account-followersCount',
175 'account-avatar-path',
176 'account-avatar-createdAt',
177 'account-avatar-updatedAt',
178 'account-displayName',
179 'account-description',
182 class UserAuditView extends EntityAuditView {
183 constructor (private user: User) {
184 super(userKeysToKeep, 'user', user)
188 const channelKeysToKeep = [
206 'ownerAccount-displayedName'
208 class VideoChannelAuditView extends EntityAuditView {
209 constructor (private channel: VideoChannel) {
210 super(channelKeysToKeep, 'channel', channel)
214 const videoAbuseKeysToKeep = [
223 class VideoAbuseAuditView extends EntityAuditView {
224 constructor (private videoAbuse: VideoAbuse) {
225 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
229 const customConfigKeysToKeep = [
231 'instance-shortDescription',
232 'instance-description',
234 'instance-defaultClientRoute',
235 'instance-defaultNSFWPolicy',
236 'instance-customizations-javascript',
237 'instance-customizations-css',
238 'services-twitter-username',
239 'services-twitter-whitelisted',
240 'cache-previews-size',
241 'cache-captions-size',
244 'signup-requiresEmailVerification',
247 'transcoding-enabled',
248 'transcoding-threads',
249 'transcoding-resolutions'
251 class CustomConfigAuditView extends EntityAuditView {
252 constructor (customConfig: CustomConfig) {
253 const infos: any = customConfig
254 const resolutionsDict = infos.transcoding.resolutions
255 const resolutionsArray = []
256 Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => {
257 if (isEnabled) resolutionsArray.push(resolution)
259 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
260 super(customConfigKeysToKeep, 'config', infos)
268 VideoImportAuditView,
269 VideoChannelAuditView,
274 CustomConfigAuditView