Skip to content

Commit

Permalink
Respect configured elasticsearch host in proxy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epixa committed Aug 26, 2016
1 parent a89947e commit 60e2553
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/plugins/elasticsearch/lib/__tests__/map_uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/elasticsearch/lib/map_uri.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
};
};

0 comments on commit 60e2553

Please sign in to comment.