-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[backport] PR #7996 to 4.x - Configurable headers for all elasticsearch requests #8032
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen to the host header here?