1 import * as path from 'path'
2 import { diff } from 'deep-object-diff'
3 import { chain } from 'lodash'
4 import * as flatten from 'flat'
5 import * as winston from 'winston'
6 import { CONFIG } from '../initializers'
7 import { jsonLoggerFormat, labelFormatter } from './logger'
8 import { VideoDetails, User, VideoChannel, VideoAbuse } from '../../shared'
9 import { VideoComment } from '../../shared/models/videos/video-comment.model'
10 import { CustomConfig } from '../../shared/models/server/custom-config.model'
18 const colors = winston.config.npm.colors
19 colors.audit = winston.config.npm.colors.info
21 winston.addColors(colors)
23 const auditLogger = winston.createLogger({
26 new winston.transports.File({
27 filename: path.join(CONFIG.STORAGE.LOG_DIR, 'peertube-audit.log'),
31 format: winston.format.combine(
32 winston.format.timestamp(),
34 winston.format.splat(),
42 function auditLoggerWrapper (domain: string, user: string, action: AUDIT_TYPE, entity: EntityAuditView, oldEntity: EntityAuditView = null) {
43 let entityInfos: object
44 if (action === AUDIT_TYPE.UPDATE && oldEntity) {
45 const oldEntityKeys = oldEntity.toLogKeys()
46 const diffObject = diff(oldEntityKeys, entity.toLogKeys())
47 const diffKeys = Object.entries(diffObject).reduce((newKeys, entry) => {
48 newKeys[`new-${entry[0]}`] = entry[1]
51 entityInfos = { ...oldEntityKeys, ...diffKeys }
53 entityInfos = { ...entity.toLogKeys() }
55 auditLogger.log('audit', JSON.stringify({
63 function auditLoggerFactory (domain: string) {
65 create (user: string, entity: EntityAuditView) {
66 auditLoggerWrapper(domain, user, AUDIT_TYPE.CREATE, entity)
68 update (user: string, entity: EntityAuditView, oldEntity: EntityAuditView) {
69 auditLoggerWrapper(domain, user, AUDIT_TYPE.UPDATE, entity, oldEntity)
71 delete (user: string, entity: EntityAuditView) {
72 auditLoggerWrapper(domain, user, AUDIT_TYPE.DELETE, entity)
77 abstract class EntityAuditView {
78 constructor (private keysToKeep: Array<string>, private prefix: string, private entityInfos: object) { }
79 toLogKeys (): object {
80 return chain(flatten(this.entityInfos, { delimiter: '-', safe: true }))
81 .pick(this.keysToKeep)
82 .mapKeys((value, key) => `${this.prefix}-${key}`)
87 const videoKeysToKeep = [
116 class VideoAuditView extends EntityAuditView {
117 constructor (private video: VideoDetails) {
118 super(videoKeysToKeep, 'video', video)
122 const commentKeysToKeep = [
126 'inReplyToCommentId',
135 class CommentAuditView extends EntityAuditView {
136 constructor (private comment: VideoComment) {
137 super(commentKeysToKeep, 'comment', comment)
141 const userKeysToKeep = [
153 'account-followingCount',
154 'account-followersCount',
157 'account-avatar-path',
158 'account-avatar-createdAt',
159 'account-avatar-updatedAt',
160 'account-displayName',
161 'account-description',
164 class UserAuditView extends EntityAuditView {
165 constructor (private user: User) {
166 super(userKeysToKeep, 'user', user)
170 const channelKeysToKeep = [
188 'ownerAccount-displayedName'
190 class VideoChannelAuditView extends EntityAuditView {
191 constructor (private channel: VideoChannel) {
192 super(channelKeysToKeep, 'channel', channel)
196 const videoAbuseKeysToKeep = [
205 class VideoAbuseAuditView extends EntityAuditView {
206 constructor (private videoAbuse: VideoAbuse) {
207 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
211 const customConfigKeysToKeep = [
213 'instance-shortDescription',
214 'instance-description',
216 'instance-defaultClientRoute',
217 'instance-defaultNSFWPolicy',
218 'instance-customizations-javascript',
219 'instance-customizations-css',
220 'services-twitter-username',
221 'services-twitter-whitelisted',
222 'cache-previews-size',
223 'cache-captions-size',
228 'transcoding-enabled',
229 'transcoding-threads',
230 'transcoding-resolutions'
232 class CustomConfigAuditView extends EntityAuditView {
233 constructor (customConfig: CustomConfig) {
234 const infos: any = customConfig
235 const resolutionsDict = infos.transcoding.resolutions
236 const resolutionsArray = []
237 Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => {
239 resolutionsArray.push(resolution)
242 infos.transcoding.resolutions = resolutionsArray
243 super(customConfigKeysToKeep, 'config', infos)
249 VideoChannelAuditView,
254 CustomConfigAuditView