From 52b3b361aadcabd52a9551fee15efe0a7559e6b6 Mon Sep 17 00:00:00 2001 From: AsafAklerPX <82310719+AsafAklerPX@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:24:15 +0200 Subject: [PATCH] PR Fixes --- lib/pxclient.js | 53 +++++++++++++++++++------------------- lib/pxenforcer.js | 3 +-- lib/pxexternallogparser.js | 9 ++++--- lib/request.js | 10 +++++++ 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/lib/pxclient.js b/lib/pxclient.js index beca3aa..23445f4 100644 --- a/lib/pxclient.js +++ b/lib/pxclient.js @@ -4,7 +4,7 @@ const pxUtil = require('./pxutil'); const pxHttpc = require('./pxhttpc'); const { ActivityType } = require('./enums/ActivityType'); const { CIVersion } = require('./enums/CIVersion'); -const p = require('agent-phin'); +const { makeAsyncRequest } = require('./request'); const { LoggerSeverity } = require('./enums/LoggerSeverity'); const { PxExternalLogsParser } = require('./pxexternallogparser'); const PxLogger = require('./pxlogger'); @@ -24,20 +24,14 @@ const { ErrorType } = require('./enums/ErrorType'); class PxClient { constructor() { this.activitiesBuffer = []; - this._isRemoteConfigOutdated = false; - this.remoteConfigLatestVersion = INVALID_VERSION_NUMBER; + this._remoteConfigLatestVersion = INVALID_VERSION_NUMBER; } init() { //stub for overriding } - - set isRemoteConfigOutdated(value) { - this._isRemoteConfigOutdated = value; - } - - get isRemoteConfigOutdated() { - return this._isRemoteConfigOutdated; + get remoteConfigLatestVersion() { + return this._remoteConfigLatestVersion; } set remoteConfigLatestVersion(value) { @@ -81,19 +75,8 @@ class PxClient { const maxRetries = 5; for (let i = 0; i < maxRetries; i++) { try { - const callData = { - url: `https://sapi-${config.px_app_id}.perimeterx.net/config/`, - headers: { 'Authorization': `Bearer ${config.px_remote_config_secret}`, 'Accept-Encoding': '' }, - timeout: 20000, - }; - const res = await p({ url: callData.url, headers: callData.headers, timeout: callData.timeout, method: 'GET' }); - const remoteConfigObject = JSON.parse(res.body); - if (remoteConfigObject.id !== config.px_remote_config_id) { - throw new Error(`Remote configuration id mismatch. Expected: ${config.px_remote_config_id}, Actual: ${remoteConfigObject.id}`); - } - if (this._remoteConfigLatestVersion !== INVALID_VERSION_NUMBER && remoteConfigObject.version !== this._remoteConfigLatestVersion) { - throw new Error(`Remote configuration version mismatch. Expected: ${this._remoteConfigLatestVersion}, Actual: ${remoteConfigObject.version}`); - } + + const remoteConfigObject = await this.getRemoteConfigObject(config); return { px_remote_config_id: remoteConfigObject.id, px_remote_config_version: remoteConfigObject.version, @@ -111,6 +94,23 @@ class PxClient { } } + async getRemoteConfigObject(config) { + const callData = { + url: `https://sapi-${config.px_app_id}.perimeterx.net/config/`, + headers: { 'Authorization': `Bearer ${config.px_remote_config_secret}`, 'Accept-Encoding': '' }, + timeout: 20000, + }; + const res = await makeAsyncRequest({ url: callData.url, headers: callData.headers, timeout: callData.timeout, method: 'GET' }, config); + const remoteConfigObject = JSON.parse(res.body); + if (remoteConfigObject.id !== config.px_remote_config_id) { + throw new Error(`Remote configuration id mismatch. Expected: ${config.px_remote_config_id}, Actual: ${remoteConfigObject.id}`); + } + if (this._remoteConfigLatestVersion !== INVALID_VERSION_NUMBER && remoteConfigObject.version !== this._remoteConfigLatestVersion) { + throw new Error(`Remote configuration version mismatch. Expected: ${this._remoteConfigLatestVersion}, Actual: ${remoteConfigObject.version}`); + } + return remoteConfigObject; + } + addAdditionalFieldsToActivity(details, ctx, config) { if (ctx.additionalFields && ctx.additionalFields.loginCredentials) { const { loginCredentials } = ctx.additionalFields; @@ -243,16 +243,17 @@ class PxClient { } sendRemoteLog(message, severity, errorType, config) { - const enforcerConfig = new PxConfig(config, new PxLogger(config)); + const pxLogger = config.logger ? config.logger : new PxLogger(config); + const enforcerConfig = new PxConfig(config, pxLogger); const reqHeaders = { 'Authorization': 'Bearer ' + enforcerConfig.config.LOGGER_AUTH_TOKEN, 'Content-Type': 'application/json', }; const logParser = new PxExternalLogsParser( { appId: config.PX_APP_ID, remoteConfigId: config.REMOTE_CONFIG_ID, remoteConfigVersion: config.REMOTE_CONFIG_VERSION }); const logs = [{ message, severity, errorType }]; - logParser.enrichLogs(logs); + const enrichedLogs = logParser.enrichLogs(logs); pxHttpc.callServer( - logs, + enrichedLogs, reqHeaders, EXTERNAL_LOGGER_SERVICE_PATH, 'remote-log', diff --git a/lib/pxenforcer.js b/lib/pxenforcer.js index 05c177d..a6d7e2e 100644 --- a/lib/pxenforcer.js +++ b/lib/pxenforcer.js @@ -261,8 +261,7 @@ class PxEnforcer { pxApi.evalByServerCall(ctx, this._config, (action) => { ctx.riskRtt = Date.now() - startRiskRtt; - if (this.config.config.REMOTE_CONFIG_ENABLED && ctx.remoteConfigLatestVersion > this._config.REMOTE_CONFIG_VERSION) { - this.pxClient.isRemoteConfigOutdated = true; + if (this.config.config.REMOTE_CONFIG_ENABLED) { this.pxClient.remoteConfigLatestVersion = ctx.remoteConfigLatestVersion; } diff --git a/lib/pxexternallogparser.js b/lib/pxexternallogparser.js index c41a625..a3715eb 100644 --- a/lib/pxexternallogparser.js +++ b/lib/pxexternallogparser.js @@ -5,17 +5,20 @@ class PxExternalLogsParser { this.remoteConfigVersion = remoteConfigVersion; } enrichLogs(logs) { - return logs.map((log) => this.enrichLogRecord(log)); + const enrichedLogs = logs.map((log) => { + return this.enrichLogRecord(log); + }); + return enrichedLogs; } enrichLogRecord(log) { - Object.assign(log, { + return {...log, ...{ messageTimestamp: new Date().toISOString(), appID: this.appId, container: 'enforcer', configID: this.remoteConfigId, configVersion: this.remoteConfigVersion - }); + }}; } } diff --git a/lib/request.js b/lib/request.js index 0a77076..c537e2a 100644 --- a/lib/request.js +++ b/lib/request.js @@ -16,6 +16,16 @@ exports.post = (options, config, cb) => { return makeRequest(options, config, cb); }; +exports.makeAsyncRequest = (options, config) => { + return new Promise((resolve, reject) => { + makeRequest(options, config, (err, res) => { + if (err) { + return reject(err); + } + return resolve(res); + }); + }); +}; function makeRequest(options, config, cb) { if (options.url && options.url.startsWith('https://')) { options.agent = config.agent || httpsKeepAliveAgent;