import { CoreService } from '../../types' ; import { CoreContext } from '../core_context' ; import { Logger } from '../logging' ; import { OpenSearchDashboardsRequest , LegacyRequest } from '../http' ; import { ensureRawRequest } from '../http/router' ; import { Auditor , AuditorFactory , AuditTrailSetup , AuditTrailStart } from './types' ; const defaultAuditorFactory = { asScoped ( ) { return { add ( ) { } , withAuditScope ( ) { } , } ; } , } ; export class AuditTrailService implements CoreService < AuditTrailSetup , AuditTrailStart > { private readonly log : Logger ; private auditor : AuditorFactory = defaultAuditorFactory ; private readonly auditors = new WeakMap < LegacyRequest , Auditor > ( ) ; constructor ( core ) { this . log = core . logger . get ( 'audit_trail' ) ; } setup ( ) { return { register : ( auditor ) => { if ( this . auditor !== defaultAuditorFactory ) { throw new Error ( 'An auditor factory has been already registered' ) ; } this . auditor = auditor ; this . log . debug ( 'An auditor factory has been registered' ) ; } , } ; } start ( ) { return { asScoped : ( request ) => { const key = ensureRawRequest ( request ) ; if ( ! this . auditors . has ( key ) ) { this . auditors . set ( key , this . auditor ! . asScoped ( request ) ) ; } return this . auditors . get ( key ) ! ; } , } ; } stop ( ) { } }