-
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.
Browse files
Browse the repository at this point in the history
Only proxy whitelisted request headers to ES server upstream
- Loading branch information
Showing
10 changed files
with
127 additions
and
10 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
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,76 @@ | ||
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('only sends the whitelisted request headers', function () { | ||
|
||
const get = sinon.stub() | ||
.withArgs('elasticsearch.url').returns('http://foobar:9200') | ||
.withArgs('elasticsearch.requestHeadersWhitelist').returns(['x-my-custom-HEADER', 'Authorization']); | ||
const config = function () { return { get: get }; }; | ||
const server = { | ||
config: config | ||
}; | ||
|
||
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { | ||
expect(err).to.be(null); | ||
expect(upstreamHeaders).to.have.property('authorization'); | ||
expect(upstreamHeaders).to.have.property('x-my-custom-header'); | ||
expect(Object.keys(upstreamHeaders).length).to.be(2); | ||
}); | ||
}); | ||
|
||
it('sends no headers if whitelist is set to []', function () { | ||
|
||
const get = sinon.stub() | ||
.withArgs('elasticsearch.url').returns('http://foobar:9200') | ||
.withArgs('elasticsearch.requestHeadersWhitelist').returns([]); | ||
const config = function () { return { get: get }; }; | ||
const server = { | ||
config: config | ||
}; | ||
|
||
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { | ||
expect(err).to.be(null); | ||
expect(Object.keys(upstreamHeaders).length).to.be(0); | ||
}); | ||
}); | ||
|
||
it('sends no headers if whitelist is set to no value', function () { | ||
|
||
const get = sinon.stub() | ||
.withArgs('elasticsearch.url').returns('http://foobar:9200') | ||
.withArgs('elasticsearch.requestHeadersWhitelist').returns([ null ]); // This is how Joi returns it | ||
const config = function () { return { get: get }; }; | ||
const server = { | ||
config: config | ||
}; | ||
|
||
mapUri(server)(request, function (err, upstreamUri, upstreamHeaders) { | ||
expect(err).to.be(null); | ||
expect(Object.keys(upstreamHeaders).length).to.be(0); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
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,22 @@ | ||
import _ from 'lodash'; | ||
|
||
export default function (originalHeaders, headersToKeep) { | ||
|
||
const normalizeHeader = function (header) { | ||
if (!header) { | ||
return ''; | ||
} | ||
header = header.toString(); | ||
return header.trim().toLowerCase(); | ||
}; | ||
|
||
// Normalize list of headers we want to allow in upstream request | ||
const headersToKeepNormalized = headersToKeep.map(normalizeHeader); | ||
|
||
// Normalize original headers in request | ||
const originalHeadersNormalized = _.mapKeys(originalHeaders, function (headerValue, headerName) { | ||
return normalizeHeader(headerName); | ||
}); | ||
|
||
return _.pick(originalHeaders, headersToKeepNormalized); | ||
} |
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