Skip to content

Commit

Permalink
PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AsafAklerPX committed Jan 25, 2024
1 parent 4978994 commit 52b3b36
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
53 changes: 27 additions & 26 deletions lib/pxclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions lib/pxenforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 6 additions & 3 deletions lib/pxexternallogparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}};
}
}

Expand Down
10 changes: 10 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 52b3b36

Please sign in to comment.