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

View File

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

View File

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

View File

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

View File

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