1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00

hijack detection test

This commit is contained in:
Massimo Candela
2019-07-09 04:29:36 +02:00
parent 4319fbfea6
commit 4ba008786d
4 changed files with 111 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ export default class Connector {
constructor(name, params, env){
this.config = env.config;
this.logger = env.logger;
this.pubSub = env.pubSub;
this.params = params;
this.name = name;
this.messageCallback = null;

View File

@@ -7,6 +7,11 @@ export default class ConnectorTest extends Connector{
constructor(name, params, env) {
super(name, params, env);
console.log("Test connector running");
this.pubSub.subscribe("test-type", (type, message) => {
clearInterval(this.timer);
this.subscribe({type: message});
console.log("switching to", message);
});
}
connect = () =>
@@ -14,35 +19,58 @@ export default class ConnectorTest extends Connector{
resolve(true);
});
subscribe = () =>
subscribe = (params) =>
new Promise((resolve, reject) => {
resolve(true);
const update = (this.params.testType === 'withdrawal') ?
{
data: {
withdrawals: ["124.40.52.128/26"],
peer: "124.0.0.2"
},
type: "ris_message"
} :
{
data: {
announcements: [{
prefixes: ["124.40.52.0/22"],
next_hop: "124.0.0.2"
}],
peer: "124.0.0.2",
path: "1,2,3,2914".split(",")
},
type: "ris_message"
};
const type = params.type || this.params.testType;
setInterval(() => {
let update;
switch (type) {
case "hijack":
update = {
data: {
announcements: [{
prefixes: ["180.50.120.0/22"],
next_hop: "124.0.0.2"
}],
peer: "124.0.0.2",
path: "1,2,3,4".split(",")
},
type: "ris_message"
};
break;
case "newprefix":
update = {
data: {
announcements: [{
prefixes: ["124.40.52.0/22"],
next_hop: "124.0.0.2"
}],
peer: "124.0.0.2",
path: "1,2,3,2914".split(",")
},
type: "ris_message"
};
break;
default:
update = {
data: {
withdrawals: ["124.40.52.128/26"],
peer: "124.0.0.2"
},
type: "ris_message"
};
}
this.timer = setInterval(() => {
this.message(JSON.stringify(update));
let peer = update.data.peer.split('.');
peer[3] = Math.min(parseInt(peer[3]) + 1, 254);
update.data.peer = peer.join(".");
if (type === 'withdrawal') {
let peer = update.data.peer.split('.');
peer[3] = Math.min(parseInt(peer[3]) + 1, 254);
update.data.peer = peer.join(".");
}
}, 1000);
});

View File

@@ -21,8 +21,8 @@ export default class MonitorVisibility extends Monitor {
if (peers >= this.threshold) {
return (peers === 1) ?
`The prefix ${alerts[0].matchedMessage.prefix} it's no longer visible (withdrawn) from the peer ${alerts[0].matchedMessage.peer}.` :
`The prefix ${alerts[0].matchedMessage.prefix} has been withdrawn. It is no longer visible from ${peers} peers.`;
`The prefix ${alerts[0].matchedMessage.prefix} (${alerts[0].matchedRule.description}) it's no longer visible (withdrawn) from the peer ${alerts[0].matchedMessage.peer}.` :
`The prefix ${alerts[0].matchedMessage.prefix} (${alerts[0].matchedRule.description}) has been withdrawn. It is no longer visible from ${peers} peers.`;
} else {
return false;
}

View File

@@ -176,7 +176,7 @@ describe("Alerting", function() {
var pubSub = require("../index");
var env = require("../env");
it("Data analyzed in time (from boot)", function(done) {
it("Alert reporting", function(done) {
pubSub.subscribe("visibility", function (type, message) {
@@ -185,7 +185,59 @@ describe("Alerting", function() {
id: '124.40.52.128/26',
origin: 'withdrawal-detection',
affected: 50601,
message: 'The prefix 124.40.52.128/26 has been withdrawn. It is no longer visible from 4 peers.'
message: 'The prefix 124.40.52.128/26 (Solid Trading / Crossivity) has been withdrawn. It is no longer visible from 4 peers.'
});
expect(message).to.contain
.keys([
"latest",
"earliest",
"data"
]);
done();
});
}).timeout(5000);
it("Hijack reporting", function(done) {
pubSub.publish("test-type", "hijack");
pubSub.subscribe("hijack", function (type, message) {
expect(message).to
.containSubset({
"affected": 4713,
"data": [
{
"extra": {},
"matchedMessage": {
"nextHop": "124.0.0.2",
"originAs": "4",
"path": [
"1",
"2",
"3",
"4",
],
"peer": "124.0.0.2",
"prefix": "180.50.120.0/22",
"type": "announcement",
},
"matchedRule": {
"asn": 4713,
"description": "OCN prefix",
"ignoreMorespecifics": false,
"prefix": "180.50.120.0/21",
"user": "default"
},
}
],
"id": "4-180.50.120.0/22",
"message": "A new prefix 180.50.120.0/22 is announced by AS4. It should be instead 180.50.120.0/21 (OCN prefix) announced by AS4713",
"origin": "basic-hijack-detection",
});
expect(message).to.contain
@@ -199,7 +251,6 @@ describe("Alerting", function() {
process.exit()
});
}).timeout(5000);
});
}).timeout(10000);
});