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

Respect configured elasticsearch host in proxy #8105

Merged
merged 1 commit into from
Aug 26, 2016
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
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);
};
};