diff --git a/src/plugins/elasticsearch/lib/__tests__/map_uri.js b/src/plugins/elasticsearch/lib/__tests__/map_uri.js index 9531149910680..36acf2f84774e 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 c7c61d3226570..feee7af3dcd0a 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); }; };