Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin installer proxy for HTTPS #15588

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"handlebars": "4.0.5",
"hapi": "14.2.0",
"http-proxy-agent": "1.0.0",
"https-proxy-agent": "2.1.1",
"imports-loader": "0.7.1",
"inert": "4.0.2",
"jade": "1.11.0",
Expand Down
25 changes: 24 additions & 1 deletion src/cli_plugin/install/__tests__/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ describe('kibana cli', function () {
const proxyUrl = `http://localhost:${proxyPort}`;

let proxyHit = false;
let proxyConnectHit = false;

const proxy = http.createServer(function (req, res) {
proxyHit = true;
Expand All @@ -273,12 +274,26 @@ describe('kibana cli', function () {
res.end();
});

proxy.on('connect', (req, socket) => {
// When the proxy is hit with a HTTPS request instead of a HTTP request,
// the above call handler will never be triggered. Instead the client
// sends a CONNECT request to the proxy, so that the proxy can setup
// a HTTPS connection between the client and the upstream server.
// We just intercept this CONNECT call here, write it an empty response
// and close the socket, which will fail the actual request, but we know
// that it tried to use the proxy.
proxyConnectHit = true;
socket.write('\r\n\r\n');
socket.end();
});

function expectProxyHit() {
expect(proxyHit).to.be(true);
}

function expectNoProxyHit() {
expect(proxyHit).to.be(false);
expect(proxyConnectHit).to.be(false);
}

function nockPluginForUrl(url) {
Expand All @@ -293,6 +308,7 @@ describe('kibana cli', function () {

beforeEach(function () {
proxyHit = false;
proxyConnectHit = false;
});

afterEach(function () {
Expand All @@ -314,7 +330,14 @@ describe('kibana cli', function () {
settings.urls = ['https://example.com/plugin.zip'];

return download(settings, logger)
.then(expectProxyHit);
.then(() => {
// If the proxy is hit, the request should fail, since our test proxy
// doesn't actually forward HTTPS requests.
expect().fail('Should not succeed a HTTPS proxy request.');
}, () => {
// Check if the proxy was actually hit before the failure.
expect(proxyConnectHit).to.be(true);
});
});

it('should not use http_proxy for HTTPS urls', function () {
Expand Down
8 changes: 7 additions & 1 deletion src/cli_plugin/install/downloaders/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Progress from '../progress';
import { fromNode as fn } from 'bluebird';
import { createWriteStream } from 'fs';
import HttpProxyAgent from 'http-proxy-agent';
import HttpsProxyAgent from 'https-proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';

function getProxyAgent(sourceUrl, logger) {
Expand All @@ -13,7 +14,12 @@ function getProxyAgent(sourceUrl, logger) {
}

logger.log(`Picked up proxy ${proxy} from environment variable.`);
return new HttpProxyAgent(proxy);

if (/^https/.test(sourceUrl)) {
return new HttpsProxyAgent(proxy);
} else {
return new HttpProxyAgent(proxy);
}
}

function sendRequest({ sourceUrl, timeout }, logger) {
Expand Down