From 4eb105a437f5cc9bb8899cd18b1170afbe45da0c Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Thu, 23 Jul 2020 20:56:25 +0200 Subject: [PATCH] hotfix reporthttp - content of undefined on showPaths (#302) --- src/env.js | 2 +- src/monitors/monitorRPKI.js | 4 +- src/reports/reportHTTP.js | 14 ++-- src/reports/reportTelegram.js | 2 +- tests/2_alerting.js | 1 - tests/reports_tests/config.reports.test.yml | 16 ++++ .../{tests.js => testReportSyslog.js} | 4 +- tests/reports_tests/testsReportHttp.js | 75 +++++++++++++++++++ tests/rpki_tests/tests.default.js | 4 +- tests/rpki_tests/tests.external.js | 2 +- 10 files changed, 105 insertions(+), 19 deletions(-) rename tests/reports_tests/{tests.js => testReportSyslog.js} (96%) create mode 100644 tests/reports_tests/testsReportHttp.js diff --git a/src/env.js b/src/env.js index ebaadecb..2db10856 100644 --- a/src/env.js +++ b/src/env.js @@ -57,7 +57,7 @@ let config = { name: "ris", params: { carefulSubscription: true, - url: "wss://ris-live.ripe.net/v1/ws/", + url: "ws://ris-live.ripe.net/v1/ws/", perMessageDeflate: true, subscription: { moreSpecific: true, diff --git a/src/monitors/monitorRPKI.js b/src/monitors/monitorRPKI.js index 9053ec09..789c8a9f 100644 --- a/src/monitors/monitorRPKI.js +++ b/src/monitors/monitorRPKI.js @@ -182,9 +182,9 @@ export default class MonitorRPKI extends Monitor { const coveringString = (covering) ? `Valid ROAs: ${covering}`: ''; if (extra.valid === null && this.params.checkUncovered) { - return `The route ${message.prefix} announced by ${message.originAS} is not covered by a ROA. Accepted with AS path: ${message.path}`; + return `The route ${message.prefix} announced by ${message.originAS} is not covered by a ROA`; } else { - return `The route ${message.prefix} announced by ${message.originAS} is not RPKI valid. Accepted with AS path: ${message.path}. ${coveringString}`; + return `The route ${message.prefix} announced by ${message.originAS} is not RPKI valid. ${coveringString}`; } } }; diff --git a/src/reports/reportHTTP.js b/src/reports/reportHTTP.js index d0351db0..2da882e4 100644 --- a/src/reports/reportHTTP.js +++ b/src/reports/reportHTTP.js @@ -60,18 +60,14 @@ export default class ReportHTTP extends Report { } } - - _getMessage = (channel, content) => { - return this.parseTemplate(this.params.templates[channel] || this.params.templates["default"], this.getContext(channel, content)); - }; - _sendHTTPMessage = (url, channel, content) => { - content = JSON.parse(JSON.stringify(content)); - if (this.params.showPaths > 0) { - content.message += `${content.message}. Top ${context.pathNumber} most used AS paths: \n ${context.paths}`; + const context = this.getContext(channel, content); + + if (this.params.showPaths > 0 && context.pathNumber > 0) { + context.summary = `${context.summary}. Top ${context.pathNumber} most used AS paths: ${context.paths}`; } - const blob = this._getMessage(channel, content); + const blob = this.parseTemplate(this.params.templates[channel] || this.params.templates["default"], context); this.axios({ url: url, diff --git a/src/reports/reportTelegram.js b/src/reports/reportTelegram.js index 08b00764..84d54efe 100644 --- a/src/reports/reportTelegram.js +++ b/src/reports/reportTelegram.js @@ -43,7 +43,7 @@ export default class reportTelegram extends ReportHTTP { "chat_id": chatId, "text": "${summary}", "parse_mode": 'HTML', - "disable_web_page_preview": true, + "disable_web_page_preview": true }); }; diff --git a/tests/2_alerting.js b/tests/2_alerting.js index b11ec4a9..e2d59a02 100644 --- a/tests/2_alerting.js +++ b/tests/2_alerting.js @@ -33,7 +33,6 @@ const chai = require("chai"); const fs = require("fs"); const chaiSubset = require('chai-subset'); -const Syslogd = require("syslogd"); chai.use(chaiSubset); const expect = chai.expect; const volume = "volumetests/"; diff --git a/tests/reports_tests/config.reports.test.yml b/tests/reports_tests/config.reports.test.yml index 09dbcaa1..0331fc15 100644 --- a/tests/reports_tests/config.reports.test.yml +++ b/tests/reports_tests/config.reports.test.yml @@ -68,6 +68,22 @@ reports: visibility: "++BGPalerter-5-${type}: ${summary}|${prefix}|${description}|${asn}|${earliest}|${latest}|${peers}" misconfiguration: "++BGPalerter-3-${type}: ${summary}|${asn}|${prefix}|${earliest}|${latest}" + - file: reportHTTP + channels: + - hijack + - newprefix + - visibility + - path + - misconfiguration + - rpki + params: + templates: # See here how to write a template https://github.com/nttgin/BGPalerter/blob/master/docs/context.md + default: '{"text": "${summary}"}' + headers: + isTemplateJSON: true + showPaths: 5 # Amount of AS_PATHs to report in the alert + hooks: + default: http://localhost:8090/test # The file containing the monitored prefixes. Please see monitored_prefixes_test.yml for an example # This is an array (use new lines and dashes!) diff --git a/tests/reports_tests/tests.js b/tests/reports_tests/testReportSyslog.js similarity index 96% rename from tests/reports_tests/tests.js rename to tests/reports_tests/testReportSyslog.js index 2bd8b04b..858c3919 100644 --- a/tests/reports_tests/tests.js +++ b/tests/reports_tests/testReportSyslog.js @@ -40,7 +40,7 @@ chai.use(chaiSubset); global.EXTERNAL_VERSION_FOR_TEST = "0.0.1"; global.EXTERNAL_CONFIG_FILE = "tests/reports_tests/config.reports.test.yml"; -describe("Reports", function() { +describe("Reports 1", function() { const worker = require("../../index"); const pubSub = worker.pubSub; @@ -62,6 +62,6 @@ describe("Reports", function() { }); pubSub.publish("test-type", "visibility"); - }).timeout(asyncTimeout); + }); \ No newline at end of file diff --git a/tests/reports_tests/testsReportHttp.js b/tests/reports_tests/testsReportHttp.js new file mode 100644 index 00000000..79035ae2 --- /dev/null +++ b/tests/reports_tests/testsReportHttp.js @@ -0,0 +1,75 @@ +/* + * BSD 3-Clause License + * + * Copyright (c) 2019, NTT Ltd. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +const chai = require("chai"); +const chaiSubset = require('chai-subset'); +const Syslogd = require("syslogd"); +const restify = require("restify"); +const expect = chai.expect; +const asyncTimeout = 20000; +chai.use(chaiSubset); +const assert = chai.assert + +global.EXTERNAL_VERSION_FOR_TEST = "0.0.1"; +global.EXTERNAL_CONFIG_FILE = "tests/reports_tests/config.reports.test.yml"; + +describe("Reports 2", function() { + const worker = require("../../index"); + const pubSub = worker.pubSub; + + it("reportHTTP", function (done) { + const server = restify.createServer(); + server.pre(restify.pre.sanitizePath()); + server.use(restify.plugins.bodyParser({ mapParams: true })); + let expectedData = [ + "The prefix 2a00:5884::/32 (alarig fix test) is announced by AS15563 instead of AS204092, and AS45. Top 1 most used AS paths: [2,3,15563]", + "A new prefix 2a00:5884:ffff::/48 is announced by AS208585. It should be instead 2a00:5884::/32 (alarig fix test) announced by AS204092, and AS45. Top 1 most used AS paths: [2,3,208585]", + ]; + + pubSub.publish("test-type", "hijack"); + + server.post('/test', function (req, res, next) { + const text = req.body.text; + if (expectedData.includes(text)) { + expectedData = expectedData.filter(i => i !== text); + } else { + assert.fail(text, "none", "The message is not expected"); + } + + if (expectedData.length === 0) { + done(); + } + }); + server.listen(8090); + + }).timeout(asyncTimeout); +}); \ No newline at end of file diff --git a/tests/rpki_tests/tests.default.js b/tests/rpki_tests/tests.default.js index c434b428..1166b3a6 100644 --- a/tests/rpki_tests/tests.default.js +++ b/tests/rpki_tests/tests.default.js @@ -50,14 +50,14 @@ describe("RPKI monitoring 1", function() { id: "a103_21_244_0_24-13335-false", origin: 'rpki-monitor', affected: '103.21.244.0/24', - message: 'The route 103.21.244.0/24 announced by AS13335 is not RPKI valid. Accepted with AS path: [1,2,3,4321,13335]. Valid ROAs: 103.21.244.0/23|AS0|maxLength:23', + message: 'The route 103.21.244.0/24 announced by AS13335 is not RPKI valid. Valid ROAs: 103.21.244.0/23|AS0|maxLength:23', }, "a8_8_8_8_22-2914-": { id: "a8_8_8_8_22-2914-", origin: 'rpki-monitor', affected: '8.8.8.8/22', - message: 'The route 8.8.8.8/22 announced by AS2914 is not covered by a ROA. Accepted with AS path: [1,2,3,4321,5060,2914]', + message: 'The route 8.8.8.8/22 announced by AS2914 is not covered by a ROA', } }; diff --git a/tests/rpki_tests/tests.external.js b/tests/rpki_tests/tests.external.js index 54ef154f..009cf53f 100644 --- a/tests/rpki_tests/tests.external.js +++ b/tests/rpki_tests/tests.external.js @@ -57,7 +57,7 @@ describe("RPKI monitoring 2", function() { id: 'a82_112_100_0_24-2914-false', origin: 'rpki-monitor', affected: '82.112.100.0/24', - message: 'The route 82.112.100.0/24 announced by AS2914 is not RPKI valid. Accepted with AS path: [1,2,3,4321,2914]. Valid ROAs: 82.112.100.0/24|AS1234|maxLength:24' + message: 'The route 82.112.100.0/24 announced by AS2914 is not RPKI valid. Valid ROAs: 82.112.100.0/24|AS1234|maxLength:24' } };