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, VideoImport } 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 videoImportKeysToKeep = [
127 class VideoImportAuditView extends EntityAuditView {
128 constructor (private videoImport: VideoImport) {
129 super(videoImportKeysToKeep, 'video-import', videoImport)
133 const commentKeysToKeep = [
137 'inReplyToCommentId',
146 class CommentAuditView extends EntityAuditView {
147 constructor (private comment: VideoComment) {
148 super(commentKeysToKeep, 'comment', comment)
152 const userKeysToKeep = [
164 'account-followingCount',
165 'account-followersCount',
168 'account-avatar-path',
169 'account-avatar-createdAt',
170 'account-avatar-updatedAt',
171 'account-displayName',
172 'account-description',
175 class UserAuditView extends EntityAuditView {
176 constructor (private user: User) {
177 super(userKeysToKeep, 'user', user)
181 const channelKeysToKeep = [
199 'ownerAccount-displayedName'
201 class VideoChannelAuditView extends EntityAuditView {
202 constructor (private channel: VideoChannel) {
203 super(channelKeysToKeep, 'channel', channel)
207 const videoAbuseKeysToKeep = [
216 class VideoAbuseAuditView extends EntityAuditView {
217 constructor (private videoAbuse: VideoAbuse) {
218 super(videoAbuseKeysToKeep, 'abuse', videoAbuse)
222 const customConfigKeysToKeep = [
224 'instance-shortDescription',
225 'instance-description',
227 'instance-defaultClientRoute',
228 'instance-defaultNSFWPolicy',
229 'instance-customizations-javascript',
230 'instance-customizations-css',
231 'services-twitter-username',
232 'services-twitter-whitelisted',
233 'cache-previews-size',
234 'cache-captions-size',
239 'transcoding-enabled',
240 'transcoding-threads',
241 'transcoding-resolutions'
243 class CustomConfigAuditView extends EntityAuditView {
244 constructor (customConfig: CustomConfig) {
245 const infos: any = customConfig
246 const resolutionsDict = infos.transcoding.resolutions
247 const resolutionsArray = []
248 Object.entries(resolutionsDict).forEach(([resolution, isEnabled]) => {
249 if (isEnabled) resolutionsArray.push(resolution)
251 Object.assign({}, infos, { transcoding: { resolutions: resolutionsArray } })
252 super(customConfigKeysToKeep, 'config', infos)
258 VideoImportAuditView,
259 VideoChannelAuditView,
264 CustomConfigAuditView