From 60e25534e2a108827199d00420d64a626db9f1cd Mon Sep 17 00:00:00 2001 From: Court Ewing Date: Fri, 26 Aug 2016 14:16:25 -0400 Subject: [PATCH] Respect configured elasticsearch host in proxy The proxy should not send along the host header from the client and instead should pass a host header derived from the configured elasticsearch url, otherwise Kibana cannot run on a different host than elasticsearch. --- .../elasticsearch/lib/__tests__/map_uri.js | 15 +++++++++++++++ src/plugins/elasticsearch/lib/map_uri.js | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/plugins/elasticsearch/lib/__tests__/map_uri.js b/src/plugins/elasticsearch/lib/__tests__/map_uri.js index 953114991068..36acf2f84774 100644 --- a/src/plugins/elasticsearch/lib/__tests__/map_uri.js +++ b/src/plugins/elasticsearch/lib/__tests__/map_uri.js @@ -11,6 +11,7 @@ describe('plugins/elasticsearch', function () { request = { path: '/elasticsearch/some/path', headers: { + host: 'localhost:5601', cookie: 'some_cookie_string', 'accept-encoding': 'gzip, deflate', origin: 'https://localhost:5601', @@ -22,9 +23,22 @@ describe('plugins/elasticsearch', function () { }; }); + it('sends the host header based on the elasticsearch.url rather than the Kibana client', function () { + const get = sinon.stub(); + get.withArgs('elasticsearch.customHeaders').returns({}); + get.withArgs('elasticsearch.url').returns('http://example.com:1234'); + const server = { config: () => ({ get }) }; + + mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { + expect(err).to.be(null); + expect(upstreamHeaders).to.have.property('host', 'example.com:1234'); + }); + }); + it('sends custom headers if set', function () { const get = sinon.stub(); get.withArgs('elasticsearch.customHeaders').returns({ foo: 'bar' }); + get.withArgs('elasticsearch.url').returns('http://example.com:1234'); const server = { config: () => ({ get }) }; mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { @@ -36,6 +50,7 @@ describe('plugins/elasticsearch', function () { it('sends configured custom headers even if the same named header exists in request', function () { const get = sinon.stub(); get.withArgs('elasticsearch.customHeaders').returns({'x-my-custom-header': 'asconfigured'}); + get.withArgs('elasticsearch.url').returns('http://example.com:1234'); const server = { config: () => ({ get }) }; mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { diff --git a/src/plugins/elasticsearch/lib/map_uri.js b/src/plugins/elasticsearch/lib/map_uri.js index c7c61d322657..feee7af3dcd0 100644 --- a/src/plugins/elasticsearch/lib/map_uri.js +++ b/src/plugins/elasticsearch/lib/map_uri.js @@ -1,5 +1,5 @@ import querystring from 'querystring'; -import { resolve } from 'url'; +import { parse as parseUrl } from 'url'; import setHeaders from './set_headers'; export default function mapUri(server, prefix) { @@ -8,13 +8,19 @@ export default function mapUri(server, prefix) { return function (request, done) { const path = request.path.replace('/elasticsearch', ''); let url = config.get('elasticsearch.url'); + const { host } = parseUrl(url); if (path) { if (/\/$/.test(url)) url = url.substring(0, url.length - 1); url += path; } const query = querystring.stringify(request.query); if (query) url += '?' + query; - const customHeaders = setHeaders(request.headers, config.get('elasticsearch.customHeaders')); + // We want the host of elasticsearch rather than of Kibana + const headers = { + ...request.headers, + host + }; + const customHeaders = setHeaders(headers, config.get('elasticsearch.customHeaders')); done(null, url, customHeaders); }; };