Some work to make sure the user agent and ip address are always passed correctly to audit logs.

This commit is contained in:
mike12345567 2023-02-22 16:10:54 +00:00
parent 6681853acf
commit c6dadaa4ff
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 {