2023-02-21 20:14:57 +01:00
|
|
|
import { AuditLogFn, Event, IdentityType, HostInfo } from "@budibase/types"
|
2022-05-10 11:33:59 +02:00
|
|
|
import { processors } from "./processors"
|
2023-01-11 10:37:37 +01:00
|
|
|
import identification from "./identification"
|
2023-02-15 13:37:32 +01:00
|
|
|
import { getAppId } from "../context"
|
2022-05-31 22:04:41 +02:00
|
|
|
import * as backfill from "./backfill"
|
2023-02-21 20:14:57 +01:00
|
|
|
import { createQueue, JobQueue } from "../queue"
|
|
|
|
import BullQueue from "bull"
|
2022-04-01 22:29:44 +02:00
|
|
|
|
2023-02-21 20:14:57 +01:00
|
|
|
type AuditLogEvent = {
|
|
|
|
event: Event
|
|
|
|
properties: any
|
|
|
|
opts: {
|
|
|
|
timestamp?: string | number
|
|
|
|
userId?: string
|
|
|
|
appId?: string
|
|
|
|
hostInfo?: HostInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let auditLogsEnabled = false
|
|
|
|
let auditLogQueue: BullQueue.Queue<AuditLogEvent>
|
2023-02-15 14:49:30 +01:00
|
|
|
|
2023-02-15 19:03:55 +01:00
|
|
|
export const configure = (fn: AuditLogFn) => {
|
2023-02-21 20:14:57 +01:00
|
|
|
auditLogsEnabled = true
|
|
|
|
const writeAuditLogs = fn
|
|
|
|
auditLogQueue = createQueue<AuditLogEvent>(JobQueue.AUDIT_LOG)
|
|
|
|
return auditLogQueue.process(async job => {
|
|
|
|
await writeAuditLogs(job.data.event, job.data.properties, {
|
|
|
|
userId: job.data.opts.userId,
|
|
|
|
timestamp: job.data.opts.timestamp,
|
|
|
|
appId: job.data.opts.appId,
|
|
|
|
hostInfo: job.data.opts.hostInfo,
|
|
|
|
})
|
|
|
|
})
|
2023-02-15 14:49:30 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:15:52 +02:00
|
|
|
export const publishEvent = async (
|
|
|
|
event: Event,
|
|
|
|
properties: any,
|
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
2022-05-23 23:14:44 +02:00
|
|
|
// in future this should use async events via a distributed queue.
|
2022-05-24 21:01:13 +02:00
|
|
|
const identity = await identification.getCurrentIdentity()
|
2022-05-31 22:04:41 +02:00
|
|
|
|
|
|
|
const backfilling = await backfill.isBackfillingEvent(event)
|
|
|
|
// no backfill - send the event and exit
|
|
|
|
if (!backfilling) {
|
2023-02-21 20:14:57 +01:00
|
|
|
await processors.processEvent(event, identity, properties, timestamp)
|
|
|
|
if (auditLogsEnabled) {
|
|
|
|
// only audit log actual events, don't include backfills
|
|
|
|
const userId =
|
|
|
|
identity.type === IdentityType.USER ? identity.id : undefined
|
|
|
|
// add to event queue, rather than just writing immediately
|
|
|
|
await auditLogQueue.add({
|
|
|
|
event,
|
|
|
|
properties,
|
|
|
|
opts: {
|
|
|
|
userId,
|
|
|
|
timestamp,
|
|
|
|
appId: getAppId(),
|
|
|
|
},
|
2023-02-15 14:49:30 +01:00
|
|
|
})
|
|
|
|
}
|
2022-05-31 22:04:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// backfill active - check if the event has been sent already
|
|
|
|
const alreadySent = await backfill.isAlreadySent(event, properties)
|
|
|
|
if (alreadySent) {
|
|
|
|
// do nothing
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// send and record the event
|
|
|
|
await processors.processEvent(event, identity, properties, timestamp)
|
|
|
|
await backfill.recordEvent(event, properties)
|
|
|
|
}
|
2022-04-01 22:29:44 +02:00
|
|
|
}
|