1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13:00

Merge branch 'feature/audit-logs' of github.com:Budibase/budibase into feature/audit-logs

This commit is contained in:
Peter Clement 2023-02-22 17:32:37 +00:00
commit 34f03a5129
5 changed files with 23 additions and 17 deletions

View file

@ -1,4 +1,10 @@
import { AuditLogFn, Event, IdentityType, HostInfo } from "@budibase/types"
import {
AuditLogFn,
Event,
IdentityType,
AuditedEventFriendlyName,
AuditLogQueueEvent,
} from "@budibase/types"
import { processors } from "./processors"
import identification from "./identification"
import { getAppId } from "../context"
@ -6,24 +12,17 @@ import * as backfill from "./backfill"
import { createQueue, JobQueue } from "../queue"
import BullQueue from "bull"
type AuditLogEvent = {
event: Event
properties: any
opts: {
timestamp?: string | number
userId?: string
appId?: string
hostInfo?: HostInfo
}
export function isAudited(event: Event) {
return !!AuditedEventFriendlyName[event]
}
let auditLogsEnabled = false
let auditLogQueue: BullQueue.Queue<AuditLogEvent>
let auditLogQueue: BullQueue.Queue<AuditLogQueueEvent>
export const configure = (fn: AuditLogFn) => {
auditLogsEnabled = true
const writeAuditLogs = fn
auditLogQueue = createQueue<AuditLogEvent>(JobQueue.AUDIT_LOG)
auditLogQueue = createQueue<AuditLogQueueEvent>(JobQueue.AUDIT_LOG)
return auditLogQueue.process(async job => {
await writeAuditLogs(job.data.event, job.data.properties, {
userId: job.data.opts.userId,
@ -46,11 +45,11 @@ export const publishEvent = async (
// no backfill - send the event and exit
if (!backfilling) {
await processors.processEvent(event, identity, properties, timestamp)
if (auditLogsEnabled) {
if (auditLogsEnabled && isAudited(event)) {
// 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
// add to the event queue, rather than just writing immediately
await auditLogQueue.add({
event,
properties,
@ -58,6 +57,7 @@ export const publishEvent = async (
userId,
timestamp,
appId: getAppId(),
hostInfo: identity.hostInfo,
},
})
}

View file

@ -89,7 +89,7 @@ const getCurrentIdentity = async (): Promise<Identity> => {
installationId,
tenantId,
environment,
hostInfo: userContext.host,
hostInfo: userContext.hostInfo,
}
} else {
throw new Error("Unknown identity type")

View file

@ -3,7 +3,7 @@ export * as processors from "./processors"
export * as analytics from "./analytics"
export { default as identification } from "./identification"
export * as backfillCache from "./backfill"
export { configure } from "./events"
export { configure, isAudited } from "./events"
import { processors } from "./processors"

View file

@ -12,3 +12,9 @@ export type AuditLogFn = (
metadata: any,
opts: AuditWriteOpts
) => Promise<any>
export type AuditLogQueueEvent = {
event: Event
properties: any
opts: AuditWriteOpts
}

View file

@ -46,7 +46,7 @@ export interface Identity {
environment: string
installationId?: string
tenantId?: string
hostInfo: HostInfo
hostInfo?: HostInfo
}
export interface UserIdentity extends Identity {