Skip to content

Commit

Permalink
fix: add proxy support when fetching patches
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored and Anton Drukh committed Jan 9, 2018
1 parent d47ab14 commit 3e409ca
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/protect/fetch-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ var Promise = require('es6-promise').Promise; // jshint ignore:line
var needle = require('needle');
var fs = require('then-fs');
var analytics = require('../analytics');
var debug = require('debug')('snyk:fetch-patch');
var getProxyForUrl = require('proxy-from-env').getProxyForUrl;

function getPatchFile(url, filename, attempts) {
return new Promise(function (resolve, reject) {
needle.get(url)
var options = {};
var proxy = getProxyForUrl(url);
if (proxy) {
debug('Using proxy: ', proxy);
options.proxy = proxy;
}

needle.get(url, options)
.on('response', function (response) {
if (response.statusCode >= 400) {
if (!attempts) {
Expand Down
104 changes: 104 additions & 0 deletions test/patch-fetch-proxy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var test = require('tap-only');
var proxyquire = require('proxyquire');
var sinon = require('sinon');
var spy = sinon.spy();
var shouldWork = true;
var timeout = false;
var switchAfterFailure = true;
var analyticsEvent;

var PROXY_HOST = 'my.proxy.dot.com';
var PROXY_PORT = 4242;
var PATCH_URL = 'https://s3.amazonaws.com/snyk-rules-pre-repository/' +
'snapshots/master/patches/npm/qs/20170213/603_604.patch';

var getPatchFile = proxyquire('../lib/protect/fetch-patch', {
'then-fs': {
createWriteStream: function () {},
},
needle: {
get: function () {
spy(...arguments);
return {
on: function (_, responseCb) {
responseCb({ statusCode: 200 });
return {
on: function (_, cb) {
cb();
return {
on: function (_, cb) {
cb({ message: 'foo', code: 'bar' });
return {
pipe: function () {},
};
},
};
},
};
},
};
},
},
'../analytics': {
add: function (type, data) {
analyticsEvent = {
type: type,
data: data,
};
},
},
});

test('Fetch gets patches with no proxy', t => {
t.plan(1);
return getPatchFile(PATCH_URL, 'unused')
.then(() => {
t.is(spy.getCall(0).args[1].proxy, undefined, 'no proxy url found');
})
.catch(err => t.fail(err.message));
});

/**
* Verify support for http(s) proxy from environments variables
* (https_proxy, HTTPS_PROXY, no_proxy)
* see https://www.gnu.org/software/wget/manual/html_node/Proxies.html
*/
test('proxy environment variables', function (t) {
t.plan(3);

t.test('https_proxy', function (t) {
var proxyUrl = 'http://' + PROXY_HOST + ':' + PROXY_PORT;
process.env.https_proxy = proxyUrl;
return getPatchFile(PATCH_URL, 'unused')
.then(() => {
t.is(spy.getCall(1).args[1].proxy, proxyUrl, 'proxy url found');
})
.catch(err => t.fail(err.message))
.then(() => delete process.env.https_proxy);
});

t.test('HTTPS_PROXY', function (t) {
var proxyUrl = 'http://' + PROXY_HOST + ':' + PROXY_PORT;
process.env.HTTPS_PROXY = proxyUrl;
return getPatchFile(PATCH_URL, 'unused')
.then(() => {
t.is(spy.getCall(2).args[1].proxy, proxyUrl, 'proxy url found');
})
.catch(err => t.fail(err.message))
.then(() => delete process.env.HTTPS_PROXY);
});

t.test('no_proxy', function (t) {
process.env.https_proxy = 'http://' + PROXY_HOST + ':' + PROXY_PORT;
process.env.no_proxy = '*';
return getPatchFile(PATCH_URL, 'unused')
.then(() => {
t.is(spy.getCall(3).args[1].proxy, undefined, 'no proxy url found');
})
.catch(err => t.fail(err.message))
.then(() => {
delete process.env.https_proxy;
delete process.env.no_proxy;
});
});
});

0 comments on commit 3e409ca

Please sign in to comment.