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 = [
122 class VideoAuditView extends EntityAuditView {
123 constructor (private video: VideoDetails) {
124 super(videoKeysToKeep, 'video', video)
128 const videoImportKeysToKeep = [
133 class VideoImportAuditView extends EntityAuditView {
134 constructor (private videoImport: VideoImport) {
135 super(videoImportKeysToKeep, 'video-import', videoImport)
139 const commentKeysToKeep = [
143 'inReplyToCommentId',
152 class CommentAuditView extends EntityAuditView {
153 constructor (private 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 user: User) {
183 super(userKeysToKeep, 'user', user)
187 const channelKeysToKeep = [
205 'ownerAccount-displayedName'
207 class VideoChannelAuditView extends EntityAuditView {
208 constructor (private channel: VideoChannel) {
209 super(channelKeysToKeep, 'channel', channel)
213 const videoAbuseKeysToKeep = [
222 class VideoAbuseAuditView extends EntityAuditView {
223 constructor (private videoAbuse: VideoAbuse) {
224 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
228 const customConfigKeysToKeep = [
230 'instance-shortDescription',
231 'instance-description',
233 'instance-defaultClientRoute',
234 'instance-defaultNSFWPolicy',
235 'instance-customizations-javascript',
236 'instance-customizations-css',
237 'services-twitter-username',
238 'services-twitter-whitelisted',
239 'cache-previews-size',
240 'cache-captions-size',
243 'signup-requiresEmailVerification',
246 'transcoding-enabled',
247 'transcoding-threads',
248 'transcoding-resolutions'
250 class CustomConfigAuditView extends EntityAuditView {
251 constructor (customConfig: CustomConfig) {
252 const infos: any = customConfig
253 const resolutionsDict = infos.transcoding.resolutions
254 const resolutionsArray = []
255 Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => {
256 if (isEnabled) resolutionsArray.push(resolution)
258 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
259 super(customConfigKeysToKeep, 'config', infos)
267 VideoImportAuditView,
268 VideoChannelAuditView,
273 CustomConfigAuditView