mirror of
				https://github.com/nttgin/BGPalerter.git
				synced 2024-05-19 06:50:08 +00:00 
			
		
		
		
	hotfix vrp file reloading (#298)
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -11,3 +11,4 @@ alertdata/
 | 
			
		||||
.npmrc
 | 
			
		||||
.cache/
 | 
			
		||||
volumetests/
 | 
			
		||||
tests/rpki_tests/vrp.json
 | 
			
		||||
@@ -11,3 +11,4 @@ alertdata/
 | 
			
		||||
.npmrc
 | 
			
		||||
.cache/
 | 
			
		||||
volumetests/
 | 
			
		||||
tests/rpki_tests/vrp.json
 | 
			
		||||
 
 | 
			
		||||
@@ -19,12 +19,45 @@ export default class MonitorRPKI extends Monitor {
 | 
			
		||||
        this.thresholdMinPeers = (params && params.thresholdMinPeers != null) ? params.thresholdMinPeers : 0;
 | 
			
		||||
        this.validationQueue = [];
 | 
			
		||||
 | 
			
		||||
        this.loadRpkiValidator(env);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    loadRpkiValidator = (env) => {
 | 
			
		||||
        if (!!this.params.vrpFile) {
 | 
			
		||||
            const vrpFile = env.config.volume + this.params.vrpFile;
 | 
			
		||||
            this._loadRpkiValidatorFromVrpFile(env, vrpFile);
 | 
			
		||||
            this._watchVrpFile(env, vrpFile);
 | 
			
		||||
        } else {
 | 
			
		||||
            this._loadRpkiValidatorFromVrpProvider(env);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _watchVrpFile = (env, vrpFile) => {
 | 
			
		||||
        const reload = () => { // Watch the external file to refresh the list
 | 
			
		||||
            if (this.watchFileTimer) {
 | 
			
		||||
                clearTimeout(this.watchFileTimer);
 | 
			
		||||
            }
 | 
			
		||||
            this.watchFileTimer = setTimeout(() => {
 | 
			
		||||
                this.logger.log({
 | 
			
		||||
                    level: 'info',
 | 
			
		||||
                    message: "VRPs reloaded due to file change."
 | 
			
		||||
                });
 | 
			
		||||
                this._loadRpkiValidatorFromVrpFile(env, vrpFile);
 | 
			
		||||
            }, 500);
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        fs.watchFile(vrpFile, reload);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _loadRpkiValidatorFromVrpProvider = (env) => {
 | 
			
		||||
 | 
			
		||||
        if (!this.rpki) {
 | 
			
		||||
            const rpkiValidatorOptions = {
 | 
			
		||||
                connector: this.providers[0],
 | 
			
		||||
                clientId: env.clientId
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
        if (this.params.vrpProvider) {
 | 
			
		||||
            if (this.params.vrpProvider) { // Use vrp provider
 | 
			
		||||
                if (this.providers.includes(this.params.vrpProvider)) {
 | 
			
		||||
                    rpkiValidatorOptions.connector = this.params.vrpProvider;
 | 
			
		||||
                } else {
 | 
			
		||||
@@ -35,10 +68,6 @@ export default class MonitorRPKI extends Monitor {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        if (this.params.vrpFile) {
 | 
			
		||||
            this._readExternalVrpsFile(env.config.volume + this.params.vrpFile, rpkiValidatorOptions);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            if (!this.params.noProxy && env.agent) {
 | 
			
		||||
                rpkiValidatorOptions.httpsAgent = env.agent;
 | 
			
		||||
            }
 | 
			
		||||
@@ -46,40 +75,60 @@ export default class MonitorRPKI extends Monitor {
 | 
			
		||||
            this.rpki = new rpki(rpkiValidatorOptions);
 | 
			
		||||
 | 
			
		||||
            if (!!this.preCacheROAs) {
 | 
			
		||||
            this.rpki.preCache(Math.max(this.refreshVrpListMinutes, 15))
 | 
			
		||||
                this.rpki
 | 
			
		||||
                    .preCache(Math.max(this.refreshVrpListMinutes, 15))
 | 
			
		||||
                    .then(() => {
 | 
			
		||||
                    setInterval(this.validateBatch, 100);
 | 
			
		||||
                        this.validationTimer = setInterval(this.validateBatch, 100); // If already cached, we can validate more often
 | 
			
		||||
                    })
 | 
			
		||||
                    .catch(() => {
 | 
			
		||||
                        this.logger.log({
 | 
			
		||||
                            level: 'error',
 | 
			
		||||
                        message: "One of the VRPs lists cannot be downloaded. The RPKI monitoring should be able to work anyway."
 | 
			
		||||
                            message: "One of the VRPs lists cannot be downloaded. The RPKI monitoring should be working anyway with one of the on-line providers."
 | 
			
		||||
                        });
 | 
			
		||||
                    });
 | 
			
		||||
            } else {
 | 
			
		||||
            setInterval(this.validateBatch, 400);
 | 
			
		||||
                this.validationTimer = setInterval(this.validateBatch, 400); // Don't overload on-line validation
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _readExternalVrpsFile = (file, rpkiValidatorOptions) => {
 | 
			
		||||
    _loadRpkiValidatorFromVrpFile = (env, vrpFile) => {
 | 
			
		||||
 | 
			
		||||
        if (!!this.params.vrpProvider && this.params.vrpProvider !== "external") {
 | 
			
		||||
            rpkiValidatorOptions.connector = this.providers[0];
 | 
			
		||||
            delete rpkiValidatorOptions.vrps;
 | 
			
		||||
            this.logger.log({
 | 
			
		||||
                level: 'error',
 | 
			
		||||
                message: "You cannot specify a vrpProvider different from 'external' if you want to use a vrps file. Using default vrpProvider."
 | 
			
		||||
            });
 | 
			
		||||
        } else {
 | 
			
		||||
            if (fs.existsSync(file)) {
 | 
			
		||||
            if (fs.existsSync(vrpFile)) {
 | 
			
		||||
                try {
 | 
			
		||||
                    const vrps = JSON.parse(fs.readFileSync(file));
 | 
			
		||||
                    const vrps = JSON.parse(fs.readFileSync(vrpFile));
 | 
			
		||||
 | 
			
		||||
                    if (vrps.length > 0) {
 | 
			
		||||
                        rpkiValidatorOptions.connector = "external";
 | 
			
		||||
                        rpkiValidatorOptions.vrps = vrps;
 | 
			
		||||
 | 
			
		||||
                        if (this.validationTimer) {
 | 
			
		||||
                            clearInterval(this.validationTimer); // Stop validation cycle
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        this.rpki = new rpki({
 | 
			
		||||
                            connector: "external",
 | 
			
		||||
                            clientId: env.clientId,
 | 
			
		||||
                            vrps
 | 
			
		||||
                        });
 | 
			
		||||
 | 
			
		||||
                        this.rpki
 | 
			
		||||
                            .preCache()
 | 
			
		||||
                            .then(() => {
 | 
			
		||||
                                this.validationTimer = setInterval(this.validateBatch, 100); // If already cached, we can validate more often
 | 
			
		||||
                            })
 | 
			
		||||
                            .catch(() => {
 | 
			
		||||
                                this.logger.log({
 | 
			
		||||
                                    level: 'error',
 | 
			
		||||
                                    message: "It was not possible to load correctly the VRPs file. Possibly there is an error in the format. The RPKI monitoring should be working anyway with one of the on-line providers."
 | 
			
		||||
                                });
 | 
			
		||||
                            });
 | 
			
		||||
 | 
			
		||||
                    } else {
 | 
			
		||||
                        rpkiValidatorOptions.connector = this.providers[0];
 | 
			
		||||
                        delete rpkiValidatorOptions.vrps;
 | 
			
		||||
                        this.logger.log({
 | 
			
		||||
                            level: 'error',
 | 
			
		||||
                            message: "The provided VRPs file is empty. Using default vrpProvider."
 | 
			
		||||
@@ -87,23 +136,20 @@ export default class MonitorRPKI extends Monitor {
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    rpkiValidatorOptions.connector = this.providers[0];
 | 
			
		||||
                    delete rpkiValidatorOptions.vrps;
 | 
			
		||||
                    this.logger.log({
 | 
			
		||||
                        level: 'error',
 | 
			
		||||
                        message: "The provided VRPs file cannot be parsed. Using default vrpProvider."
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                rpkiValidatorOptions.connector = this.providers[0];
 | 
			
		||||
                delete rpkiValidatorOptions.vrps;
 | 
			
		||||
 | 
			
		||||
                this.logger.log({
 | 
			
		||||
                    level: 'error',
 | 
			
		||||
                    message: "The provided VRPs file cannot be found. Using default vrpProvider."
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return this._loadRpkiValidatorFromVrpProvider(env);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    updateMonitoredResources = () => {
 | 
			
		||||
 
 | 
			
		||||
@@ -32,16 +32,23 @@
 | 
			
		||||
 | 
			
		||||
const chai = require("chai");
 | 
			
		||||
const chaiSubset = require('chai-subset');
 | 
			
		||||
const fs = require('fs');
 | 
			
		||||
const expect = chai.expect;
 | 
			
		||||
const asyncTimeout = 200000;
 | 
			
		||||
chai.use(chaiSubset);
 | 
			
		||||
 | 
			
		||||
global.EXTERNAL_CONFIG_FILE = "tests/rpki_tests/config.rpki.test.external.yml";
 | 
			
		||||
 | 
			
		||||
fs.copyFileSync("tests/rpki_tests/vrp.wrong.json", "tests/rpki_tests/vrp.json");
 | 
			
		||||
 | 
			
		||||
const worker = require("../../index");
 | 
			
		||||
const pubSub = worker.pubSub;
 | 
			
		||||
 | 
			
		||||
describe("RPKI monitoring 2", function() {
 | 
			
		||||
 | 
			
		||||
    //Test rpki watch file reloading
 | 
			
		||||
    fs.copyFileSync("tests/rpki_tests/vrp.correct.json", "tests/rpki_tests/vrp.json");
 | 
			
		||||
 | 
			
		||||
    it("external connector", function (done) {
 | 
			
		||||
 | 
			
		||||
        const expectedData = {
 | 
			
		||||
@@ -77,14 +84,16 @@ describe("RPKI monitoring 2", function() {
 | 
			
		||||
                if (Object.keys(expectedData).length === 0) {
 | 
			
		||||
                    setTimeout(() => {
 | 
			
		||||
                        rpkiTestCompletedExternal = true;
 | 
			
		||||
                        fs.unlinkSync("tests/rpki_tests/vrp.json");
 | 
			
		||||
                        done();
 | 
			
		||||
                    }, 5000);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        setTimeout(() => { // Wait that the watcher realizes the file changed
 | 
			
		||||
            pubSub.publish("test-type", "rpki");
 | 
			
		||||
 | 
			
		||||
        }, 5000);
 | 
			
		||||
 | 
			
		||||
    }).timeout(asyncTimeout);
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										17
									
								
								tests/rpki_tests/vrp.correct.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								tests/rpki_tests/vrp.correct.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
[
 | 
			
		||||
  {
 | 
			
		||||
    "asn": "13335",
 | 
			
		||||
    "prefix": "103.21.244.0/24",
 | 
			
		||||
    "maxLength": 24
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    "asn": "AS2914",
 | 
			
		||||
    "prefix": "8.8.8.0/22",
 | 
			
		||||
    "maxLength": 24
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    "asn": 1234,
 | 
			
		||||
    "prefix": "82.112.100.0/24",
 | 
			
		||||
    "maxLength": 24
 | 
			
		||||
  }
 | 
			
		||||
]
 | 
			
		||||
							
								
								
									
										12
									
								
								tests/rpki_tests/vrp.wrong.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tests/rpki_tests/vrp.wrong.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
[
 | 
			
		||||
  {
 | 
			
		||||
    "asn": "13335",
 | 
			
		||||
    "prefix": "103.21.244.0/24",
 | 
			
		||||
    "maxLength": 24
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    "asn": "2914",
 | 
			
		||||
    "prefix": "123.4.5.0/14",
 | 
			
		||||
    "maxLength": 24
 | 
			
		||||
  }
 | 
			
		||||
]
 | 
			
		||||
		Reference in New Issue
	
	Block a user