-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- **Commit 1:** Configurable headers for all elasticsearch requests A new server-side configuration, elasticsearch.customHeaders, allows people to configure any number of custom headers that will get sent along to all requests to Elasticsearch that are made via the proxy or exposed client. This allows for advanced architectures that do things such as dynamic routing based on install-specific headers. * Original sha: d00d177 * Authored by Court Ewing <court@epixa.com> on 2016-08-13T16:46:54Z
- Loading branch information
Showing
10 changed files
with
189 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import expect from 'expect.js'; | ||
import mapUri from '../map_uri'; | ||
import sinon from 'sinon'; | ||
|
||
describe('plugins/elasticsearch', function () { | ||
describe('lib/map_uri', function () { | ||
|
||
let request; | ||
|
||
beforeEach(function () { | ||
request = { | ||
path: '/elasticsearch/some/path', | ||
headers: { | ||
cookie: 'some_cookie_string', | ||
'accept-encoding': 'gzip, deflate', | ||
origin: 'https://localhost:5601', | ||
'content-type': 'application/json', | ||
'x-my-custom-header': '42', | ||
accept: 'application/json, text/plain, */*', | ||
authorization: '2343d322eda344390fdw42' | ||
} | ||
}; | ||
}); | ||
|
||
it('sends custom headers if set', function () { | ||
const get = sinon.stub(); | ||
get.withArgs('elasticsearch.customHeaders').returns({ foo: 'bar' }); | ||
const server = { config: () => ({ get }) }; | ||
|
||
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { | ||
expect(err).to.be(null); | ||
expect(upstreamHeaders).to.have.property('foo', 'bar'); | ||
}); | ||
}); | ||
|
||
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'}); | ||
const server = { config: () => ({ get }) }; | ||
|
||
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { | ||
expect(err).to.be(null); | ||
expect(upstreamHeaders).to.have.property('x-my-custom-header', 'asconfigured'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import expect from 'expect.js'; | ||
import sinon from 'sinon'; | ||
import setHeaders from '../set_headers'; | ||
|
||
describe('plugins/elasticsearch', function () { | ||
describe('lib/set_headers', function () { | ||
it('throws if not given an object as the first argument', function () { | ||
const fn = () => setHeaders(null, {}); | ||
expect(fn).to.throwError(); | ||
}); | ||
|
||
it('throws if not given an object as the second argument', function () { | ||
const fn = () => setHeaders({}, null); | ||
expect(fn).to.throwError(); | ||
}); | ||
|
||
it('returns a new object', function () { | ||
const originalHeaders = {}; | ||
const newHeaders = {}; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).not.to.be(originalHeaders); | ||
expect(returnedHeaders).not.to.be(newHeaders); | ||
}); | ||
|
||
it('returns object with newHeaders merged with originalHeaders', function () { | ||
const originalHeaders = { foo: 'bar' }; | ||
const newHeaders = { one: 'two' }; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).to.eql({ foo: 'bar', one: 'two' }); | ||
}); | ||
|
||
it('returns object where newHeaders takes precedence for any matching keys', function () { | ||
const originalHeaders = { foo: 'bar' }; | ||
const newHeaders = { one: 'two', foo: 'notbar' }; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).to.eql({ foo: 'notbar', one: 'two' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isPlainObject } from 'lodash'; | ||
|
||
export default function setHeaders(originalHeaders, newHeaders) { | ||
if (!isPlainObject(originalHeaders)) { | ||
throw new Error(`Expected originalHeaders to be an object, but ${typeof originalHeaders} given`); | ||
} | ||
if (!isPlainObject(newHeaders)) { | ||
throw new Error(`Expected newHeaders to be an object, but ${typeof newHeaders} given`); | ||
} | ||
|
||
return { | ||
...originalHeaders, | ||
...newHeaders | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import _ from 'lodash'; | ||
import toPath from 'lodash/internal/toPath'; | ||
|
||
module.exports = function unset(object, rawPath) { | ||
if (!object) return; | ||
const path = toPath(rawPath); | ||
|
||
switch (path.length) { | ||
case 0: | ||
return; | ||
|
||
case 1: | ||
delete object[rawPath]; | ||
break; | ||
|
||
default: | ||
const leaf = path.pop(); | ||
const parentPath = path.slice(); | ||
const parent = _.get(object, parentPath); | ||
unset(parent, leaf); | ||
if (!_.size(parent)) { | ||
unset(object, parentPath); | ||
} | ||
break; | ||
} | ||
}; |