From 2eac1e82f33aa7d6f9c6c54cf1c64501c5c2df71 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Tue, 27 Aug 2024 16:36:47 +0100 Subject: [PATCH] add audit logging from the launcher --- lib/launcher.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/launcher.js b/lib/launcher.js index a1c70a0..3427dcc 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -5,6 +5,7 @@ const path = require('path') const { log, info, debug, warn, NRlog } = require('./logging/log') const { copyDir, hasProperty } = require('./utils') const { States } = require('./states') +const { default: got } = require('got') const MIN_RESTART_TIME = 10000 // 10 seconds const MAX_RESTART_COUNT = 5 @@ -385,6 +386,31 @@ class Launcher { } } + async logAuditEvent (event, body) { + const data = { + timestamp: Date.now(), + event + } + if (body && typeof body === 'object') { + if (body.error) { + data.error = { + code: body.error.code || 'unexpected_error', + error: body.error.error || body.error.message || 'Unexpected error' + } + } else { + Object.assign(data, body) + } + } + return got.post(this.auditLogURL, { + json: data, + headers: { + authorization: 'Bearer ' + this.config.token + } + }).catch(_err => { + console.error('Failed to log audit event', _err, event) + }) + } + async start () { if (this.deferredStop) { await this.deferredStop