Skip to content

Commit

Permalink
Merge branch 'master' into task/new_trust_approach
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Sep 26, 2023
2 parents 424ea21 + 9181b45 commit 0ff6183
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 66 deletions.
5 changes: 4 additions & 1 deletion CHANGES_NEXT_RELEASE
@@ -1,8 +1,11 @@
- Add: new approach to handle trust auth (urbo-deployer#868)
- Add: apply expandVar with JSON.parse to all fields of all actions (sms, smpp, email, post, update) (#746)
- Fix: check domain before access domain
- Fix: expandVar return a 'null' instead of null (#746)
- Fix: expandVar for single string values with null, boolean, number (#746)
- Fix: smtp and smpp logs (#738)
- Add: allow access entities using NGSIv2 API for non_signal rules (new setting nonSignalByAPI / PERSEO_CHECK_NON_SIGNAL_BY_API) (#549)
- Remove support for ngsv1 notifications (#714)
- Remove ngsiv1 support for updateAction (#714)
- Fix: check timer string in lower case in TimedRule checker

- Fix: dep of chai get-func-name to 2.0.0 to avoid installation errors
16 changes: 8 additions & 8 deletions lib/models/emailAction.js
Expand Up @@ -37,10 +37,10 @@ function buildTransporterOptions(action, event) {
options.smtp = {};
options.smtp = action.parameters.smtp;
if (action.parameters.smtp.host) {
options.smtp.host = myutils.expandVar(action.parameters.smtp.host, event);
options.smtp.host = myutils.expandVar(action.parameters.smtp.host, event, true);
}
if (action.parameters.smtp.port) {
options.smtp.port = myutils.expandVar(action.parameters.smtp.port, event);
options.smtp.port = myutils.expandVar(action.parameters.smtp.port, event, true);
}
}
return options;
Expand All @@ -49,16 +49,16 @@ function buildTransporterOptions(action, event) {
function buildMailOptions(action, event) {
var options = {};
options = {
from: myutils.expandVar(action.parameters.from, event),
to: myutils.expandVar(action.parameters.to, event),
subject: myutils.expandVar(action.parameters.subject || '', event),
text: myutils.expandVar(action.template, event)
from: myutils.expandVar(action.parameters.from, event, true),
to: myutils.expandVar(action.parameters.to, event, true),
subject: myutils.expandVar(action.parameters.subject || '', event, true),
text: myutils.expandVar(action.template, event, true)
};
if (action.parameters.cc) {
options.cc = myutils.expandVar(action.parameters.cc, event);
options.cc = myutils.expandVar(action.parameters.cc, event, true);
}
if (action.parameters.bcc) {
options.bcc = myutils.expandVar(action.parameters.bcc, event);
options.bcc = myutils.expandVar(action.parameters.bcc, event, true);
}
return options;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/models/smppAction.js
Expand Up @@ -31,25 +31,25 @@ var logger = require('logops'),

function buildSMSOptions(action, event) {
var options = {
text: myutils.expandVar(action.template, event),
to: myutils.expandVar(action.parameters.to, event)
text: myutils.expandVar(action.template, event, true),
to: myutils.expandVar(action.parameters.to, event, false) // it is expected be string number
};
if (action.parameters.smpp) {
options.smpp = {};
if (action.parameters.smpp.from) {
options.smpp.from = myutils.expandVar(action.parameters.smpp.from, event);
options.smpp.from = myutils.expandVar(action.parameters.smpp.from, event, true);
}
if (action.parameters.smpp.host) {
options.smpp.host = myutils.expandVar(action.parameters.smpp.host, event);
options.smpp.host = myutils.expandVar(action.parameters.smpp.host, event, true);
}
if (action.parameters.smpp.port) {
options.smpp.port = myutils.expandVar(action.parameters.smpp.port, event);
options.smpp.port = myutils.expandVar(action.parameters.smpp.port, event, true);
}
if (action.parameters.smpp.systemid) {
options.smpp.systemid = myutils.expandVar(action.parameters.smpp.systemid, event);
options.smpp.systemid = myutils.expandVar(action.parameters.smpp.systemid, event, true);
}
if (action.parameters.smpp.password) {
options.smpp.password = myutils.expandVar(action.parameters.smpp.password, event);
options.smpp.password = myutils.expandVar(action.parameters.smpp.password, event, true);
}
}
return options;
Expand Down
12 changes: 6 additions & 6 deletions lib/models/smsAction.js
Expand Up @@ -31,22 +31,22 @@ var util = require('util'),

function buildSMSOptions(action, event) {
var options = {
text: myutils.expandVar(action.template, event),
to: myutils.expandVar(action.parameters.to, event)
text: myutils.expandVar(action.template, event, true),
to: myutils.expandVar(action.parameters.to, event, false) // it is expected be string number
};
if (action.parameters.sms) {
options.sms = {};
if (action.parameters.sms.URL) {
options.sms.URL = myutils.expandVar(action.parameters.sms.URL, event);
options.sms.URL = myutils.expandVar(action.parameters.sms.URL, event, true);
}
if (action.parameters.sms.API_KEY) {
options.sms.API_KEY = myutils.expandVar(action.parameters.sms.API_KEY, event);
options.sms.API_KEY = myutils.expandVar(action.parameters.sms.API_KEY, event, true);
}
if (action.parameters.sms.API_SECRET) {
options.sms.API_SECRET = myutils.expandVar(action.parameters.sms.API_SECRET, event);
options.sms.API_SECRET = myutils.expandVar(action.parameters.sms.API_SECRET, event, true);
}
if (action.parameters.sms.from) {
options.sms.from = myutils.expandVar(action.parameters.sms.from, event);
options.sms.from = myutils.expandVar(action.parameters.sms.from, event, true);
}
}
return options;
Expand Down
2 changes: 1 addition & 1 deletion lib/models/twitterAction.js
Expand Up @@ -29,7 +29,7 @@ var myutils = require('../myutils'),
metrics = require('./metrics');

function buildTwitterOptions(action, event) {
return { text: myutils.expandVar(action.template, event) };
return { text: myutils.expandVar(action.template, event, true) };
}

function doIt(action, event, callback) {
Expand Down
24 changes: 12 additions & 12 deletions lib/models/updateAction.js
Expand Up @@ -76,7 +76,7 @@ function buildQueryOptionsParams(action, event) {
var filter = {};

Object.keys(action.parameters.filter).forEach(function(key) {
filter[key] = myutils.expandVar(action.parameters.filter[key], event);
filter[key] = myutils.expandVar(action.parameters.filter[key], event, true);
});

// Translate filter with geojsonpolygon -> georel, geometry, coords
Expand Down Expand Up @@ -126,7 +126,7 @@ function processOptionParams(action, event) {
action.parameters.attributes.forEach(function(attr) {
// Direct value for Text, DateTime, and others. Apply expandVar for strings
let theValue = myutils.expandVar(attr.value, event, true);
let theType = myutils.expandVar(attr.type, event);
let theType = myutils.expandVar(attr.type, event, true);
// Metadata should be null or object
let theMeta = attr.metadata;

Expand Down Expand Up @@ -160,7 +160,7 @@ function processOptionParams(action, event) {
//Nothing to do
}
}
var key = myutils.expandVar(attr.name, event);
var key = myutils.expandVar(attr.name, event, true);
changes[key] = {
value: theValue,
type: theType
Expand All @@ -169,8 +169,8 @@ function processOptionParams(action, event) {
changes[key].metadata = theMeta;
}
});
changes.id = myutils.expandVar(action.parameters.id, event);
changes.type = myutils.expandVar(action.parameters.type, event);
changes.id = myutils.expandVar(action.parameters.id, event, true);
changes.type = myutils.expandVar(action.parameters.type, event, true);
logger.debug('processOptionParams changes: %j', changes);
return changes;
}
Expand All @@ -189,7 +189,7 @@ function processUpdateOptionParams(action, entity, event) {
var options = {};
action.parameters.attributes.forEach(function(attr) {
let theValue = myutils.expandVar(attr.value, event, true);
let theType = myutils.expandVar(attr.type, event);
let theType = myutils.expandVar(attr.type, event, true);

// Metadata should be null or object
let theMeta = attr.metadata;
Expand Down Expand Up @@ -222,7 +222,7 @@ function processUpdateOptionParams(action, entity, event) {
break;
}
}
var key = myutils.expandVar(attr.name, event);
var key = myutils.expandVar(attr.name, event, true);
options[key] = {
value: theValue,
type: theType
Expand Down Expand Up @@ -254,11 +254,11 @@ function getUpdateConnection(connection, action, event, token) {
};
var pep = false;
if (action.parameters.service !== undefined) {
options.service = myutils.expandVar(action.parameters.service, event);
options.service = myutils.expandVar(action.parameters.service, event, true);
pep = true;
}
if (action.parameters.subservice !== undefined) {
options.servicepath = myutils.expandVar(action.parameters.subservice, event);
options.servicepath = myutils.expandVar(action.parameters.subservice, event, true);
pep = true;
}
// token headers
Expand Down Expand Up @@ -318,13 +318,13 @@ function doUpdateActionWithFilter(queryOptions, action, event, token, connection
};

if (action.parameters.actionType !== undefined) {
changes.actionType = myutils.expandVar(action.parameters.actionType, event);
changes.actionType = myutils.expandVar(action.parameters.actionType, event, true);
}

if (response && response.results) {
// Process response (array of entities)
response.results.forEach(function(entity) {
var updateOptions = processUpdateOptionParams(action, entity, event);
var updateOptions = processUpdateOptionParams(action, entity, event, true);
changes.entities.push(updateOptions);
}); // For each
}
Expand Down Expand Up @@ -404,7 +404,7 @@ function doUpdateAction(action, event, token, connection, callback) {
entities: [processOptionParams(action, event)]
};
if (action.parameters.actionType !== undefined) {
changes.actionType = myutils.expandVar(action.parameters.actionType, event);
changes.actionType = myutils.expandVar(action.parameters.actionType, event, true);
}
metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate);
logger.debug('entity to update: %j', changes);
Expand Down
16 changes: 10 additions & 6 deletions lib/models/visualRules.js
Expand Up @@ -256,12 +256,16 @@ function vr2rule(cardRule) {
r.nosignal.idRegexp = idRegexp;
r.nosignal.type = type;
} else {
r.text = myutils.expandVar(eplTemplate, {
name: r.name,
conditions: conditions.join(' AND '),
service: r.service,
subservice: r.subservice
});
r.text = myutils.expandVar(
eplTemplate,
{
name: r.name,
conditions: conditions.join(' AND '),
service: r.service,
subservice: r.subservice
},
true
);
}
} catch (ex) {
// SHOULD BE ex instanceof TypeError. Do not do anything else inside try
Expand Down
29 changes: 19 additions & 10 deletions lib/myutils.js
Expand Up @@ -55,9 +55,13 @@ function expandVar(val, mapping, trycast) {
if (typeof val === 'string') {
var expanded = false;
var valObj = null;
var uniqueString = false;
Object.keys(mapping).forEach(function(p) {
if (typeof mapping[p] === 'string' && Object.keys(mapping).length === 1) {
uniqueString = true;
}
val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]);
// javascript replace always return a string: we need to keep object value
// javascript replace always return a string: we need to keep object value (#692)
if (!expanded && val === '[object Object]') {
valObj = mapping[p]; // for cases where mapping[p] is an object like { type: 'Point', coordinates: [] }
expanded = true;
Expand All @@ -66,9 +70,9 @@ function expandVar(val, mapping, trycast) {
if (valObj) {
val = valObj;
}
if (trycast) {
if (trycast && !uniqueString) {
try {
// Check if "${num}" and expand it as real value, non string
// Check if "${num}" and expand it as real value, non string (#362, #369)
var n = JSON.parse(val);
switch (typeof n) {
case 'number':
Expand All @@ -77,15 +81,20 @@ function expandVar(val, mapping, trycast) {
val = n;
expanded = true;
break;
// default: keep val value
}
} catch (e) {
// keep val value
}
}
if (!expanded) {
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
val = null;
if (val !== 'null' || !uniqueString) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
}
}
}
}
Expand Down Expand Up @@ -143,7 +152,7 @@ function requestHelperAux(method, options, withMetrics, callback) {
}
}
options.headers = headers;
if (withMetrics && options.json && domain.context) {
if (withMetrics && options.json && domain && domain.context) {
try {
metrics.IncMetrics(
domain.context.srv,
Expand All @@ -157,7 +166,7 @@ function requestHelperAux(method, options, withMetrics, callback) {
}
request[method](options, function cbRequest2core(err, response, body) {
var bodySz = 0;
if (withMetrics && domain.context) {
if (withMetrics && domain && domain.context) {
if (body) {
if (typeof body === 'string') {
bodySz = Buffer.byteLength(body);
Expand All @@ -178,7 +187,7 @@ function requestHelperAux(method, options, withMetrics, callback) {
}
if (err) {
logErrorIf(err, util.format('error %s to %s', method, options.url));
if (withMetrics && domain.context) {
if (withMetrics && domain && domain.context) {
metrics.IncMetrics(domain.context.srv, domain.context.subsrv, metrics.outgoingTransactionsErrors);
}
return callback(err, null);
Expand All @@ -196,7 +205,7 @@ function requestHelperAux(method, options, withMetrics, callback) {
);
localError.httpCode = respObj.code < 500 && respObj.code >= 400 ? respObj.code : 500;
logErrorIf(localError, null, domain && domain.context);
if (withMetrics && domain.context) {
if (withMetrics && domain && domain.context) {
metrics.IncMetrics(domain.context.srv, domain.context.subsrv, metrics.outgoingTransactionsErrors);
}
return callback(localError, respObj);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"chai": "~4.1.2",
"get-func-name": "2.0.0",
"coveralls": "~3.0.2",
"husky": "~1.1.0",
"nyc": "~15.1.0",
Expand Down

0 comments on commit 0ff6183

Please sign in to comment.