From 501a4e56aed30c9667a818dff9461a9223b95fa1 Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Fri, 20 Nov 2020 04:41:33 +0100 Subject: [PATCH] getContext must return a basic alert and never crash (#402) --- src/reports/report.js | 197 ++++++++++++++++++++++-------------------- 1 file changed, 105 insertions(+), 92 deletions(-) diff --git a/src/reports/report.js b/src/reports/report.js index 6a1d7684..eab1706c 100644 --- a/src/reports/report.js +++ b/src/reports/report.js @@ -74,100 +74,113 @@ export default class Report { }; getContext = (channel, content) => { - let context = { - summary: content.message, - earliest: moment(content.earliest).utc().format("YYYY-MM-DD HH:mm:ss"), - latest: moment(content.latest).utc().format("YYYY-MM-DD HH:mm:ss"), - channel, - type: content.origin, - }; - - let matched = null; - let pathsCount = {}; - let sortedPathIndex; - - if (this.params.showPaths > 0) { - content.data - .filter(i => !!i.matchedMessage && !!i.matchedMessage.path) - .map(i => JSON.stringify(i.matchedMessage.path.getValues().slice(1))) - .forEach(path => { - if (!pathsCount[path]) { - pathsCount[path] = 0; - } - pathsCount[path]++; - }); - - sortedPathIndex = Object.keys(pathsCount) - .map(key => [key, pathsCount[key]]); - - sortedPathIndex.sort((first, second) => second[1] - first[1]); - context.pathNumber = Math.min(this.params.showPaths, sortedPathIndex.length); - context.paths = sortedPathIndex - .slice(0, this.params.showPaths) - .map(i => i[0]).join(","); - } else { - context.pathNumber = ""; - context.paths = "Disabled"; - } + try { + let context = { + summary: content.message, + earliest: moment(content.earliest).utc().format("YYYY-MM-DD HH:mm:ss"), + latest: moment(content.latest).utc().format("YYYY-MM-DD HH:mm:ss"), + channel, + type: content.origin, + }; + + let matched = null; + let pathsCount = {}; + let sortedPathIndex; + + if (this.params.showPaths > 0) { + content.data + .filter(i => !!i.matchedMessage && !!i.matchedMessage.path) + .map(i => JSON.stringify(i.matchedMessage.path.getValues().slice(1))) + .forEach(path => { + if (!pathsCount[path]) { + pathsCount[path] = 0; + } + pathsCount[path]++; + }); + + + sortedPathIndex = Object.keys(pathsCount) + .map(key => [key, pathsCount[key]]); + + sortedPathIndex.sort((first, second) => second[1] - first[1]); + context.pathNumber = Math.min(this.params.showPaths, sortedPathIndex.length); + context.paths = sortedPathIndex + .slice(0, this.params.showPaths) + .map(i => i[0]).join(","); + } else { + context.pathNumber = ""; + context.paths = "Disabled"; + } - switch(channel){ - case "hijack": - matched = content.data[0].matchedRule; - context.prefix = matched.prefix; - context.description = matched.description; - context.asn = matched.asn.toString(); - context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; - context.neworigin = content.data[0].matchedMessage.originAS; - context.newprefix = content.data[0].matchedMessage.prefix; - context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); - - break; - - case "visibility": - matched = content.data[0].matchedRule; - context.prefix = matched.prefix; - context.description = matched.description; - context.asn = matched.asn.toString(); - context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; - context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); - break; - - case "newprefix": - matched = content.data[0].matchedRule; - context.prefix = matched.prefix; - context.description = matched.description; - context.asn = matched.asn.toString(); - context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; - context.neworigin = content.data[0].matchedMessage.originAS; - context.newprefix = content.data[0].matchedMessage.prefix; - context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); - break; - - case "software-update": - break; - - case "path": - break; - - case "misconfiguration": - context.asn = content.data[0].matchedRule.asn.toString(); - break; - - case "rpki": - matched = content.data[0].matchedRule; - context.asn = matched.asn.toString(); - context.prefix = matched.prefix || content.data[0].matchedMessage.prefix; - context.description = matched.description; - break; - - default: - matched = content.data[0].matchedRule; - context.prefix = matched.prefix; - context.description = matched.description; - context.asn = matched.asn.toString(); - } + switch(channel){ + case "hijack": + matched = content.data[0].matchedRule; + context.prefix = matched.prefix; + context.description = matched.description; + context.asn = matched.asn.toString(); + context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; + context.neworigin = content.data[0].matchedMessage.originAS; + context.newprefix = content.data[0].matchedMessage.prefix; + context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); + + break; + + case "visibility": + matched = content.data[0].matchedRule; + context.prefix = matched.prefix; + context.description = matched.description; + context.asn = matched.asn.toString(); + context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; + context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); + break; + + case "newprefix": + matched = content.data[0].matchedRule; + context.prefix = matched.prefix; + context.description = matched.description; + context.asn = matched.asn.toString(); + context.peers = [...new Set(content.data.map(alert => alert.matchedMessage.peer))].length; + context.neworigin = content.data[0].matchedMessage.originAS; + context.newprefix = content.data[0].matchedMessage.prefix; + context.bgplay = this.getBGPlayLink(matched.prefix, content.earliest, content.latest); + break; + + case "software-update": + break; + + case "path": + break; + + case "misconfiguration": + context.asn = content.data[0].matchedRule.asn.toString(); + break; + + case "rpki": + matched = content.data[0].matchedRule; + context.asn = matched.asn.toString(); + context.prefix = matched.prefix || content.data[0].matchedMessage.prefix; + context.description = matched.description; + break; + + default: + matched = content.data[0].matchedRule; + context.prefix = matched.prefix; + context.description = matched.description; + context.asn = matched.asn.toString(); + } + + return context; - return context; + } catch (error) { // This MUST never happen. But if it happens we need do send a basic alert anyway and don't crash + this.logger.log({ + level: 'error', + message: `It was not possible to generate a context: ${error}` + }); + + return { + summary: content.message + } + } }; parseTemplate = (template, context) => {