Skip to content

Commit

Permalink
getContext must return a basic alert and never crash (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Nov 20, 2020
1 parent 6da0b5e commit 501a4e5
Showing 1 changed file with 105 additions and 92 deletions.
197 changes: 105 additions & 92 deletions src/reports/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 501a4e5

Please sign in to comment.