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

Configurable headers for all elasticsearch requests #7996

Merged
merged 1 commit into from
Aug 19, 2016

Conversation

epixa
Copy link
Contributor

@epixa epixa commented Aug 13, 2016

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.

Closes #7964

@ycombinator
Copy link
Contributor

Please add a section in config/kibana.yml similar to https://github.com/elastic/kibana/blob/master/config/kibana.yml#L64-L67.

@ycombinator
Copy link
Contributor

ycombinator commented Aug 13, 2016

How do you feel about combining customHeaders with requestHeadersWhitelist into a single setting, given that a header in the former will take precedence over the same header in the latter.

Such a configuration setting would take an array of strings or objects. I think it would make it a tad more obvious to the person making the configuration what's going on, particularly if they would've otherwise inadvertently defined the same header in both settings at different points in time.

@epixa
Copy link
Contributor Author

epixa commented Aug 13, 2016

They are really two different things though: one is the name of headers that should be proxied through from the clieny, the other is essentially hardcoded header names and values and is not exposed to the client at all.

On Aug 13, 2016, at 1:19 PM, Shaunak Kashyap notifications@github.com wrote:

How do you feel about combining customHeaders with requestHeadersWhitelist, given that a header in the former will take precedence over the same header in the latter. The configuration setting would take an array of strings or objects. I think it would make it a tad more obvious to the person making the configuration what's going on, particularly if they would've otherwise inadvertently defined the same header in both settings at different points in time.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@ycombinator
Copy link
Contributor

Alright. I think then we should make it clear in the inline config
documentation for the new setting that it's header values take precedence
over the whitelist header values or client header values.
On Sat, Aug 13, 2016 at 12:42 PM Court Ewing notifications@github.com
wrote:

They are really two different things though: one is the name of headers
that should be proxied through from the clieny, the other is essentially
hardcoded header names and values and is not exposed to the client at all.

On Aug 13, 2016, at 1:19 PM, Shaunak Kashyap notifications@github.com
wrote:

How do you feel about combining customHeaders with
requestHeadersWhitelist, given that a header in the former will take
precedence over the same header in the latter. The configuration setting
would take an array of strings or objects. I think it would make it a tad
more obvious to the person making the configuration what's going on,
particularly if they would've otherwise inadvertently defined the same
header in both settings at different points in time.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.


You are receiving this because you were assigned.

Reply to this email directly, view it on GitHub
#7996 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADHdZV3jcoEWi9fETiyX3u5T3giRoOrks5qfgHrgaJpZM4JjtrY
.

@epixa
Copy link
Contributor Author

epixa commented Aug 14, 2016

@ycombinator Good call. I updated the kibana.yml as well as docs.

@@ -65,6 +65,10 @@
# headers, set this value to [] (an empty list).
# elasticsearch.requestHeadersWhitelist: [ authorization ]

# Header names and values that are sent to Elasticsearch. Any custom headers cannot be
# overwritten by client-side headers, regardless of requestHeadersWhitelist configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: change requestHeadersWhitelist to elasticsearch.requestHeadersWhitelist for consistency.

@epixa
Copy link
Contributor Author

epixa commented Aug 15, 2016

@ycombinator Updated

import { isObject } from 'lodash';

export default function setHeaders(originalHeaders, newHeaders) {
if (!isObject(originalHeaders)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using isPlainObject as it is a stricter check than isObject.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but spread should work just fine with any enumerable object, so I couldn't think of a reason to only allow plain objects.

Copy link
Contributor

@ycombinator ycombinator Aug 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more that you probably don't want either of these to be arrays or functions, for example, right? I know they could be and the spread operator would work fine but, from a validation perspective for what this function does, you only want headers to be plain objects, right?

Copy link
Contributor

@spalger spalger Aug 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @ycombinator here. If the intention is to verify that the arguments are compatible with the spread operator then so be it, but if the intention is to make sure that setHeaders() is being used as intended, a more strict check is probably appropriate.

Someone sending an instance of RegExp, or an array, is probably a mistake that would not be caught by these helpful checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sold.

@ycombinator
Copy link
Contributor

Left a couple of comments in the code. Functionality LGTM, though:

With only elasticsearch.customHeaders set:

screen shot 2016-08-16 at 7 49 15 am

screen shot 2016-08-16 at 7 49 12 am

With elasticsearch.customHeaders and elasticsearch.requestHeaderWhitelist set to same header

screen shot 2016-08-16 at 7 58 45 am

![screen shot 2016-08-16 at 7 58 35 am](https://cloud.githubusercontent.com/assets/51061/17698257/fe33bad6-6387-11e6-9313-920af248d911.png)

screen shot 2016-08-16 at 7 58 40 am

@kimchy
Copy link
Member

kimchy commented Aug 17, 2016

can we see if this gets merged in time to backporting to the upcoming 4.x release as well?

@epixa
Copy link
Contributor Author

epixa commented Aug 17, 2016

@kimchy The backporting is likely to take longer than the remaining time to get this merged, but that's the goal.

@epixa epixa added the v4.6.0 label Aug 17, 2016
const host = {
host: uri.hostname,
port: uri.port,
protocol: uri.protocol.replace(':', ''),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replacing the ":" isn't necessary, and protocol will be null if the parsed uri is just //localhost or something... just a thought

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the unnecessary : a tested feature of the elasticsearch client?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome.

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.
@epixa
Copy link
Contributor Author

epixa commented Aug 18, 2016

@spalger @ycombinator All outstanding feedback should be addressed now.

@spalger
Copy link
Contributor

spalger commented Aug 19, 2016

LGTM

@spalger spalger removed their assignment Aug 19, 2016
@BigFunger
Copy link
Contributor

LGTM

@BigFunger BigFunger removed their assignment Aug 19, 2016
@epixa epixa merged commit bca4732 into elastic:master Aug 19, 2016
@epixa epixa deleted the 7964-esclientheaders branch August 19, 2016 17:14
epixa added a commit that referenced this pull request Aug 19, 2016
---------

**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
epixa added a commit that referenced this pull request Aug 22, 2016
[backport] PR #7996 to 4.x - Configurable headers for all elasticsearch requests
airow pushed a commit to airow/kibana that referenced this pull request Feb 16, 2017
Configurable headers for all elasticsearch requests

Former-commit-id: bca4732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants