From 0ff1192078970625aa26c6e56fa444e07d73b841 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Tue, 16 Jan 2018 11:08:43 -0600 Subject: [PATCH] Convert Azure module to pull azure ip ranges from MS - remove old data file - update tests to allow for new Azure IPs - fix bug in Azure's transform function that limited it to only listing the first Region in the xml - extend the test timeout to allow for the download time Azure files need --- data/PublicIPs.xml | 2050 -------------------------------------------- lib/azure.js | 49 +- package.json | 2 +- test/azure.js | 2 +- test/cli.js | 2 +- test/which.js | 2 +- 6 files changed, 43 insertions(+), 2064 deletions(-) delete mode 100644 data/PublicIPs.xml diff --git a/data/PublicIPs.xml b/data/PublicIPs.xml deleted file mode 100644 index c36844a..0000000 --- a/data/PublicIPs.xml +++ /dev/null @@ -1,2050 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/lib/azure.js b/lib/azure.js index 4973e6f..0a69bb3 100644 --- a/lib/azure.js +++ b/lib/azure.js @@ -1,29 +1,58 @@ const assign = require('lodash.assign') -const fs = require('fs') -const path = require('path') +const request = require('request') const parse = require('xml2js').parseString function Azure (opts) { assign(this, { - rangesFile: path.resolve(__dirname, '../data/PublicIPs.xml'), + rangesURL: 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653', name: 'azure' }, opts) } Azure.prototype.list = function (cb) { - fs.readFile(this.rangesFile, 'utf-8', function (err, output) { - if (err) return cb(err) - parse(output, function (err, obj) { - if (err) return cb(err) - else return cb(null, transform(obj)) + getHrefURI(this.rangesURL) + .then(function (output) { + parse(output, function (err, obj) { + if (err) return cb(err) + else return cb(null, transform(obj)) + }) + }) + .catch(function (err) { + if (err) { throw err } + }) +} + +const getHrefURI = function (uri) { + return new Promise((resolve, reject) => { + request(uri, (err, body, resp) => { + if (err) { return reject(err) } + if (body.statusCode !== 200) { + return reject(new Error('Azure: Non-200 response from ' + uri)) + } + const fileRegex = /href="(.*?PublicIPs.*?xml)"/g + const uriMatches = fileRegex.exec(resp) + if (uriMatches.length < 2) { + return reject(new Error('Azure: No file download urls found at ' + uri)) + } + const rangeXMLUri = uriMatches[1] + request(rangeXMLUri, (err, body, resp) => { + if (err) { return reject(err) } + if (body.statusCode !== 200) { + return reject(new Error('Azure: Non-200 response from ' + rangeXMLUri)) + } + resolve(resp) + }) }) }) } function transform (obj) { - return obj.AzurePublicIpAddresses.Region[0].IpRange.map(function (range) { - return range.$.Subnet + const regions = obj.AzurePublicIpAddresses.Region.map(function (range) { + return range.IpRange.map(function (ipr) { + return ipr.$.Subnet + }) }) + return [].concat.apply([], regions) } module.exports = function (opts) { diff --git a/package.json b/package.json index 335fc11..eb3afe3 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "global": true, "scripts": { "pretest": "standard", - "test": "nyc mocha --timeout=2500 test/*.js", + "test": "nyc mocha --timeout=5000 test/*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", "release": "standard-version" }, diff --git a/test/azure.js b/test/azure.js index 71cd9c1..21894eb 100644 --- a/test/azure.js +++ b/test/azure.js @@ -9,7 +9,7 @@ describe('Azure', function () { const azure = Azure() azure.list(function (err, ranges) { if (err) return done(err) - ranges.should.include('40.112.124.0/24') + ranges.should.include('13.65.0.0/16') return done() }) }) diff --git a/test/cli.js b/test/cli.js index 5468d55..fdb4b50 100644 --- a/test/cli.js +++ b/test/cli.js @@ -15,7 +15,7 @@ describe('cli', function () { }) it('looks up an ip', function (done) { - exec('./bin/which-cloud.js 94.245.97.0', function (err, stdout, stderr) { + exec('./bin/which-cloud.js 13.65.128.128', function (err, stdout, stderr) { if (err) return done(err) stdout.should.match(/azure/) return done() diff --git a/test/which.js b/test/which.js index d57641c..fa55d16 100644 --- a/test/which.js +++ b/test/which.js @@ -6,7 +6,7 @@ const which = require('../') describe('which', function () { it('returns appropriate response for azure ip', function (done) { - which('94.245.97.0', function (err, name) { + which('13.65.128.128', function (err, name) { if (err) return done(err) name.should.equal('azure') return done()