mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Added logic to fix the org variable if its been changed
This commit is contained in:
parent
edae03e20e
commit
d2a045a237
@ -1,4 +1,5 @@
|
||||
const clone = require('clone');
|
||||
const _ = require('lodash');
|
||||
|
||||
const variablesToCheck = [
|
||||
'logger.metadata.organization',
|
||||
@ -41,40 +42,70 @@ module.exports = class PayloadValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value at the given location in the given object
|
||||
*
|
||||
* @param {*} object
|
||||
* @param {*} location
|
||||
* @returns
|
||||
*/
|
||||
getValue(object, location) {
|
||||
return location.split('.').reduce((p, c) => (p && p[c]) || null, object);
|
||||
return _.get(object, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value at the given location in the given object to the given value
|
||||
*
|
||||
* @param {*} object
|
||||
* @param {*} location
|
||||
* @param {*} value
|
||||
* @returns
|
||||
*/
|
||||
setValue(object, location, value) {
|
||||
return _.set(object, location, value);
|
||||
}
|
||||
|
||||
check(_after) {
|
||||
let after = _after;
|
||||
try {
|
||||
variablesToCheck.forEach((location) => {
|
||||
const beforeValue = this.getValue(this.before, location);
|
||||
const afterValue = this.getValue(after, location);
|
||||
if (beforeValue !== afterValue) {
|
||||
const details = {
|
||||
message: `msg.${location} changed from "${beforeValue}" to "${afterValue}" for bot "${this.bot}"`,
|
||||
nodeId: this.nodeId,
|
||||
workerId: this.workerId
|
||||
};
|
||||
this.logger.error(details.message);
|
||||
this.logger.app.platform.organization({
|
||||
srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`,
|
||||
action: 'exception',
|
||||
actionType: 'invalid-payload-modification',
|
||||
details,
|
||||
conversationId: this.conversationId
|
||||
});
|
||||
after = this.setValue(after, location, beforeValue);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
console.log('Error while trying to verify variable changes');
|
||||
console.log(e);
|
||||
}
|
||||
return after;
|
||||
}
|
||||
|
||||
verify(_after) {
|
||||
const after = _after;
|
||||
if (this.isValidBefore) {
|
||||
try {
|
||||
let after = _after;
|
||||
if (Array.isArray(after)) {
|
||||
after = after.find((msg) => !!msg);
|
||||
}
|
||||
variablesToCheck.forEach((location) => {
|
||||
if (this.getValue(this.before, location) !== this.getValue(after, location)) {
|
||||
const details = {
|
||||
message: `msg.${location} changed from "${this.getValue(this.before, location)}" to "${this.getValue(after, location)}" for bot "${this.bot}"`,
|
||||
nodeId: this.nodeId,
|
||||
workerId: this.workerId
|
||||
};
|
||||
this.logger.error(details.message);
|
||||
this.logger.app.platform.organization({
|
||||
srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`,
|
||||
action: 'exception',
|
||||
actionType: 'invalid-payload-modification',
|
||||
details,
|
||||
conversationId: this.conversationId
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
console.log('Error while trying to verify variable changes');
|
||||
console.log(e);
|
||||
if (Array.isArray(after)) {
|
||||
const afterIndex = after.findIndex((msg) => !!msg);
|
||||
after[afterIndex] = this.check(after[afterIndex]);
|
||||
return after;
|
||||
}
|
||||
} else {
|
||||
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
|
||||
return this.check(after);
|
||||
}
|
||||
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
|
||||
return after;
|
||||
}
|
||||
};
|
||||
|
3
package-lock.json
generated
3
package-lock.json
generated
@ -5104,8 +5104,7 @@
|
||||
"lodash": {
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
|
||||
},
|
||||
"lodash.assign": {
|
||||
"version": "4.2.0",
|
||||
|
@ -11,9 +11,8 @@
|
||||
"main": "red/red.js",
|
||||
"scripts": {
|
||||
"start": "node red.js",
|
||||
"test": "grunt test-nodes",
|
||||
"build": "grunt build",
|
||||
"test:unit": "nyc node_modules/.bin/mocha --recursive test/unit/*"
|
||||
"test": "nyc node_modules/.bin/mocha --recursive test/unit/*"
|
||||
},
|
||||
"bin": {
|
||||
"node-red": "./red.js",
|
||||
@ -54,6 +53,7 @@
|
||||
"js-yaml": "3.11.0",
|
||||
"json-stringify-safe": "5.0.1",
|
||||
"jsonata": "1.5.4",
|
||||
"lodash": "^4.17.21",
|
||||
"media-typer": "0.3.0",
|
||||
"memorystore": "1.6.0",
|
||||
"mqtt": "2.18.0",
|
||||
|
@ -1,142 +1,192 @@
|
||||
const PayloadValidator = require('../../nodes/PayloadValidator')
|
||||
const orgEvent = require('./fixtures/data/orgEvent')
|
||||
const sinon = require('sinon');
|
||||
const assert = require('assert');
|
||||
const PayloadValidator = require('../../nodes/PayloadValidator');
|
||||
const orgEvent = require('./fixtures/data/orgEvent');
|
||||
|
||||
describe.only('Unit: PayloadValidator', () => {
|
||||
describe('Unit: PayloadValidator', () => {
|
||||
it('Should not log when no changes', () => {
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
payloadValidator.verify(beforeEvent);
|
||||
});
|
||||
|
||||
it('Should warn when org is overwritten', () => {
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
it('Should warn when org is overwritten and fix it', () => {
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
const errorLogStub = sinon.stub();
|
||||
const appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
beforeEvent.logger.app = {
|
||||
platform:{
|
||||
platform: {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
const modifiedEvent = orgEvent('after');
|
||||
|
||||
payloadValidator.verify(modifiedEvent);
|
||||
assert(errorLogStub.callCount === 4)
|
||||
assert(appLogStub.callCount === 4)
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
assert(log2.details.message.includes('payload.system.organization'))
|
||||
assert.strictEqual(log2.details.nodeId, nodeId)
|
||||
assert.strictEqual(log2.details.workerId, workerId)
|
||||
assert(log3.details.message.includes('event.event.organization'))
|
||||
assert.strictEqual(log3.details.nodeId, nodeId)
|
||||
assert.strictEqual(log3.details.workerId, workerId)
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'))
|
||||
assert.strictEqual(log4.details.nodeId, nodeId)
|
||||
assert.strictEqual(log4.details.workerId, workerId)
|
||||
const result = payloadValidator.verify(modifiedEvent);
|
||||
|
||||
assert(errorLogStub.callCount === 4);
|
||||
assert(appLogStub.callCount === 4);
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args;
|
||||
assert(log1.details.message.includes('logger.metadata.organization'));
|
||||
assert.strictEqual(log1.details.nodeId, nodeId);
|
||||
assert.strictEqual(log1.details.workerId, workerId);
|
||||
assert(log2.details.message.includes('payload.system.organization'));
|
||||
assert.strictEqual(log2.details.nodeId, nodeId);
|
||||
assert.strictEqual(log2.details.workerId, workerId);
|
||||
assert(log3.details.message.includes('event.event.organization'));
|
||||
assert.strictEqual(log3.details.nodeId, nodeId);
|
||||
assert.strictEqual(log3.details.workerId, workerId);
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'));
|
||||
assert.strictEqual(log4.details.nodeId, nodeId);
|
||||
assert.strictEqual(log4.details.workerId, workerId);
|
||||
// deleting due to sinon funness
|
||||
delete result.logger.error;
|
||||
delete result.logger.app;
|
||||
delete beforeEvent.logger.error;
|
||||
delete beforeEvent.logger.app;
|
||||
assert.deepStrictEqual(result, beforeEvent);
|
||||
});
|
||||
|
||||
|
||||
it('Should warn when org is deleted', () => {
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
it('Should warn when org is deleted and fix it', () => {
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
const errorLogStub = sinon.stub();
|
||||
const appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
beforeEvent.logger.app = {
|
||||
platform:{
|
||||
platform: {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
|
||||
delete beforeEvent.logger.metadata.organization;
|
||||
delete beforeEvent.payload.system.organization;
|
||||
delete beforeEvent.event.event.organization;
|
||||
delete beforeEvent.event.event.token.contents.organization;
|
||||
payloadValidator.verify(beforeEvent);
|
||||
assert(errorLogStub.callCount === 4)
|
||||
assert(appLogStub.callCount === 4)
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
assert(log2.details.message.includes('payload.system.organization'))
|
||||
assert.strictEqual(log2.details.nodeId, nodeId)
|
||||
assert.strictEqual(log2.details.workerId, workerId)
|
||||
assert(log3.details.message.includes('event.event.organization'))
|
||||
assert.strictEqual(log3.details.nodeId, nodeId)
|
||||
assert.strictEqual(log3.details.workerId, workerId)
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'))
|
||||
assert.strictEqual(log4.details.nodeId, nodeId)
|
||||
assert.strictEqual(log4.details.workerId, workerId)
|
||||
const result = payloadValidator.verify(beforeEvent);
|
||||
assert(errorLogStub.callCount === 4);
|
||||
assert(appLogStub.callCount === 4);
|
||||
const [[log1], [log2], [log3], [log4]] = appLogStub.args;
|
||||
assert(log1.details.message.includes('logger.metadata.organization'));
|
||||
assert.strictEqual(log1.details.nodeId, nodeId);
|
||||
assert.strictEqual(log1.details.workerId, workerId);
|
||||
assert(log2.details.message.includes('payload.system.organization'));
|
||||
assert.strictEqual(log2.details.nodeId, nodeId);
|
||||
assert.strictEqual(log2.details.workerId, workerId);
|
||||
assert(log3.details.message.includes('event.event.organization'));
|
||||
assert.strictEqual(log3.details.nodeId, nodeId);
|
||||
assert.strictEqual(log3.details.workerId, workerId);
|
||||
assert(log4.details.message.includes('event.event.token.contents.organization'));
|
||||
assert.strictEqual(log4.details.nodeId, nodeId);
|
||||
assert.strictEqual(log4.details.workerId, workerId);
|
||||
|
||||
// deleting due to sinon funness
|
||||
delete result.logger.error;
|
||||
delete result.logger.app;
|
||||
delete beforeEvent.logger.error;
|
||||
delete beforeEvent.logger.app;
|
||||
assert.deepStrictEqual(result, beforeEvent);
|
||||
});
|
||||
it('Should handle when after is an array', () =>{
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
|
||||
it('Should handle when after is an array and not find any issues', () => {
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
const errorLogStub = sinon.stub();
|
||||
const appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
beforeEvent.logger.app = {
|
||||
platform:{
|
||||
platform: {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
const modifiedEvent = orgEvent('before');
|
||||
modifiedEvent.logger.metadata.organization = 'after'
|
||||
payloadValidator.verify([modifiedEvent]);
|
||||
assert(errorLogStub.callCount === 1)
|
||||
assert(appLogStub.callCount === 1)
|
||||
const [[log1]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
})
|
||||
const after = orgEvent('before');
|
||||
const [result] = payloadValidator.verify([after]);
|
||||
// deleting due to sinon funness
|
||||
delete result.logger.error;
|
||||
delete result.logger.app;
|
||||
delete beforeEvent.logger.error;
|
||||
delete beforeEvent.logger.app;
|
||||
assert.deepStrictEqual(result, beforeEvent);
|
||||
});
|
||||
|
||||
it('Should handle when after is an array in something other than first position', () =>{
|
||||
const nodeId = 'abc-id'
|
||||
const workerId = 'worker-id'
|
||||
it('Should handle when after is an array', () => {
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
errorLogStub = sinon.stub();
|
||||
appLogStub = sinon.stub();
|
||||
const errorLogStub = sinon.stub();
|
||||
const appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
beforeEvent.logger.app = {
|
||||
platform:{
|
||||
platform: {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
const modifiedEvent = orgEvent('before');
|
||||
modifiedEvent.logger.metadata.organization = 'after'
|
||||
payloadValidator.verify([null, null, modifiedEvent, null]);
|
||||
assert(errorLogStub.callCount === 1)
|
||||
assert(appLogStub.callCount === 1)
|
||||
const [[log1]] = appLogStub.args
|
||||
assert(log1.details.message.includes('logger.metadata.organization'))
|
||||
assert.strictEqual(log1.details.nodeId, nodeId)
|
||||
assert.strictEqual(log1.details.workerId, workerId)
|
||||
})
|
||||
|
||||
modifiedEvent.logger.metadata.organization = 'after';
|
||||
const [result] = payloadValidator.verify([modifiedEvent]);
|
||||
assert(errorLogStub.callCount === 1);
|
||||
assert(appLogStub.callCount === 1);
|
||||
const [[log1]] = appLogStub.args;
|
||||
assert(log1.details.message.includes('logger.metadata.organization'));
|
||||
assert.strictEqual(log1.details.nodeId, nodeId);
|
||||
assert.strictEqual(log1.details.workerId, workerId);
|
||||
// deleting due to sinon funness
|
||||
delete result.logger.error;
|
||||
delete result.logger.app;
|
||||
delete beforeEvent.logger.error;
|
||||
delete beforeEvent.logger.app;
|
||||
assert.deepStrictEqual(result, beforeEvent);
|
||||
});
|
||||
|
||||
it('Should handle when after is an array in something other than first position', () => {
|
||||
const nodeId = 'abc-id';
|
||||
const workerId = 'worker-id';
|
||||
const beforeEvent = orgEvent('before', workerId);
|
||||
const errorLogStub = sinon.stub();
|
||||
const appLogStub = sinon.stub();
|
||||
beforeEvent.logger.error = errorLogStub;
|
||||
beforeEvent.logger.app = {
|
||||
platform: {
|
||||
organization: appLogStub
|
||||
}
|
||||
};
|
||||
|
||||
const payloadValidator = new PayloadValidator(beforeEvent, `before-${workerId}-${nodeId}`);
|
||||
|
||||
const modifiedEvent = orgEvent('before');
|
||||
modifiedEvent.logger.metadata.organization = 'after';
|
||||
const [,, result] = payloadValidator.verify([null, null, modifiedEvent, null]);
|
||||
assert(errorLogStub.callCount === 1);
|
||||
assert(appLogStub.callCount === 1);
|
||||
const [[log1]] = appLogStub.args;
|
||||
assert(log1.details.message.includes('logger.metadata.organization'));
|
||||
assert.strictEqual(log1.details.nodeId, nodeId);
|
||||
assert.strictEqual(log1.details.workerId, workerId);
|
||||
// deleting due to sinon funness
|
||||
delete result.logger.error;
|
||||
delete result.logger.app;
|
||||
delete beforeEvent.logger.error;
|
||||
delete beforeEvent.logger.app;
|
||||
assert.deepStrictEqual(result, beforeEvent);
|
||||
});
|
||||
|
||||
it('Should not die when error', () => {
|
||||
const beforeEvent = orgEvent('before');
|
||||
const payloadValidator = new PayloadValidator(beforeEvent);
|
||||
@ -155,5 +205,58 @@ describe.only('Unit: PayloadValidator', () => {
|
||||
payloadValidator.verify({});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
describe('Get Value', () => {
|
||||
let payloadValidator;
|
||||
before(() => {
|
||||
const event = orgEvent('event');
|
||||
payloadValidator = new PayloadValidator(event);
|
||||
});
|
||||
it('Should set a root level variable', () => {
|
||||
const location = 'hello';
|
||||
const value = 'world';
|
||||
const object = {
|
||||
[location]: value
|
||||
};
|
||||
const result = payloadValidator.getValue(object, location);
|
||||
assert.strictEqual(result, value);
|
||||
});
|
||||
|
||||
it('Should set a nested variable', () => {
|
||||
const value = 'world';
|
||||
const object = {
|
||||
should: {
|
||||
have: {
|
||||
hello: value
|
||||
}
|
||||
}
|
||||
};
|
||||
const location = 'should.have.hello';
|
||||
|
||||
const result = payloadValidator.getValue(object, location);
|
||||
assert.strictEqual(result, value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Set Value', () => {
|
||||
let payloadValidator;
|
||||
before(() => {
|
||||
const event = orgEvent('event');
|
||||
payloadValidator = new PayloadValidator(event);
|
||||
});
|
||||
it('Should set a root level variable', () => {
|
||||
const object = {};
|
||||
const location = 'hello';
|
||||
const value = 'world';
|
||||
const result = payloadValidator.setValue(object, location, value);
|
||||
assert.strictEqual(result[location], value);
|
||||
});
|
||||
|
||||
it('Should set a nested variable', () => {
|
||||
const object = {};
|
||||
const location = 'should.set.hello';
|
||||
const value = 'world';
|
||||
const result = payloadValidator.setValue(object, location, value);
|
||||
assert.strictEqual(result.should.set.hello, value);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user