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

[server/rewriteBasePath] Support rewriting basePath requests #16724

Merged
merged 10 commits into from
Feb 19, 2018

Conversation

spalger
Copy link
Contributor

@spalger spalger commented Feb 13, 2018

Fixes #9522

For some reason server.basePath expects the proxy sending the request to rewrite the request and strip the basePath. Unfortunately, users constantly encounter this expectation with surprise and many waste a good amount of time wondering why Kibana isn't responding properly.

The new server.rewriteBasePath setting enables request rewriting in the Kibana server so that Kibana will respond as expected when it receives requests starting with the server.baseRoute. For BWC server.rewriteBasePath is false by default, and setting server.basePath without setting server.rewriteBasePath will cause a deprecation warning to be logged.

In 7.0 server.rewriteBasePath will default to true and the deprecation warning will go away (a PR will follow this one to enable this behavior for 7.0).

Release Note: Adds support for rewriting the server.basePath within in the Kibana server so you no longer need to rewrite urls in your reverse proxy. To control this behavior in Kibana 6.3+ add server.rewriteBasePath: true|false to your Kibana config file. If you are using server.basePath and don't enable or disable the server.rewriteBasePath setting in your config file then a deprecation warning will be logged at startup because this setting will default to true in Kibana 7.0.

@spalger spalger added release_note:enhancement review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.0.0 v6.3.0 labels Feb 13, 2018
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@spalger spalger changed the title [server/baseRoute] Enhanced version of basePath, also rewrites requests [server/rewriteBasePath] Support rewriting basePath requests Feb 14, 2018
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0
Copy link
Member

Choose a reason for hiding this comment

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

nit: full stop after Kibana 7.0?

the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests
to Kibana. This setting cannot end in a slash (`/`).
`server.basePath:`:: Enables you to specify a path to mount Kibana at if you are running behind a proxy. Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath from requests it receives, and to prevent a deprecation warning at startup. This setting cannot end in a slash (`/`).
`server.rewriteBasePath:`:: *Default: false* Specifies whether Kibana should rewrite requests that are prefixed with `server.basePath` or require that they are rewritten by your reverse proxy. This setting was effectively always `false` before Kibana 6.3 and will default to `true` starting in Kibana 7.0
Copy link
Member

Choose a reason for hiding this comment

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

nit: full stop after Kibana 7.0?

@@ -53,6 +53,11 @@ export default () => Joi.object({
autoListen: Joi.boolean().default(true),
defaultRoute: Joi.string().default('/app/kibana').regex(/^\//, `start with a slash`),
basePath: Joi.string().default('').allow('').regex(/(^$|^\/.*[^\/]$)/, `start with a slash, don't end with one`),
rewriteBasePath: Joi.boolean().when('basePath', {
is: Joi.valid(''),
Copy link
Member

Choose a reason for hiding this comment

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

nit: can it be just is: ''?

@@ -25,6 +25,18 @@ const savedObjectsIndexCheckTimeout = (settings, log) => {
}
};

const rewriteBasePath = (settings, log) => {
if (_.has(settings, 'server.basePath') && !_.has(settings, 'server.rewriteBasePath')) {
log(
Copy link
Member

@azasypkin azasypkin Feb 15, 2018

Choose a reason for hiding this comment

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

question: is there any magic step (apart from defining deprecated keys in config obviously) I should make to see this warning? I don't see any deprecation warnings defined in this file for some reason :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, well, looks like the server isn't logging deprecation messages... #16795. I think you'll just have to verify the code until @epixa gets to #16795

Copy link
Member

Choose a reason for hiding this comment

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

Haha, yeah code lgtm

@@ -0,0 +1,188 @@
import { Server } from 'hapi';
Copy link
Member

Choose a reason for hiding this comment

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

nit: not sure what is our convention here, but shouldn't this test file have the same name as the file it tests?


describe('no base path', () => {
it('/bar => 404', async () => {
const server = createServer({ basePath: '', rewriteBasePath: false });
Copy link
Member

Choose a reason for hiding this comment

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

nit: maybe move const server = createServer({ basePath: '', rewriteBasePath: false }); to beforeEach (for this suite and suites below)?

expect(resp.payload).to.be('resp:/');
});

it('/bar/ => 404', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

nit: wrong description

expect(resp.payload).to.be('resp:/');
});

it('/bar/foo => 404', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

nit: wrong description

Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

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

LGTM

@spalger spalger requested review from jbudz and rhoboat and removed request for tylersmalley February 16, 2018 20:19
log(
'Setting server.basePath without server.rewriteBasePath is deprecated until Kibana 7.0, ' +
'when the default behavior of server.basePath will change and Kibana will start expecting ' +
'that all requests start with the server.basePath rather expecting you to rewrite the ' +
Copy link
Member

Choose a reason for hiding this comment

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

nit, rather than expecting

Copy link

@rhoboat rhoboat Feb 16, 2018

Choose a reason for hiding this comment

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

Also maybe a nit, I found it hard to follow this sentence. Would it be easier to read if it said,

You should set server.basePath along with server.rewriteBasePath. From Kibana 7.0 onward, server.rewriteBasePath defaults to true, even if not set; Kibana will start expecting that all requests start with the server.basePath rather than expecting you to rewrite the requests in your reverse proxy. Explicitly set server.rewriteBasePath to false to preserve the current behavior and silence this warning.

I would 👍 as it is, leaving it up to you, of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @archanid, I like your phasing in certain parts a lot more. I'll merge the two.

the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests
to Kibana. This setting cannot end in a slash (`/`).
`server.basePath:`:: Enables you to specify a path to mount Kibana at if you are running behind a proxy. Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath from requests it receives, and to prevent a deprecation warning at startup. This setting cannot end in a slash (`/`).
`server.rewriteBasePath:`:: *Default: false* Specifies whether Kibana should rewrite requests that are prefixed with `server.basePath` or require that they are rewritten by your reverse proxy. This setting was effectively always `false` before Kibana 6.3 and will default to `true` starting in Kibana 7.0.
Copy link

Choose a reason for hiding this comment

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

++

@@ -22,12 +22,14 @@ export default class ClusterManager {
this.basePathProxy = new BasePathProxy(this, settings);

optimizerArgv.push(
`--server.basePath=${this.basePathProxy.basePath}`
`--server.basePath=${this.basePathProxy.basePath}`,
'--server.rewriteBasePath=true',
Copy link

Choose a reason for hiding this comment

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

thanks for trailing commas

const { error } = validate({ server: { rewriteBasePath: 'foo' } });
expect(error).to.have.property('details');
expect(error.details[0]).to.have.property('path', 'server.rewriteBasePath');
});
Copy link

Choose a reason for hiding this comment

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

should there be a test case rejecting everything but strings for basePath?

rewriteBasePath: Joi.boolean().when('basePath', {
is: '',
then: Joi.default(false).valid(false),
otherwise: Joi.default(false)
Copy link

Choose a reason for hiding this comment

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

nit: trailing comma?

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@rhoboat
Copy link

rhoboat commented Feb 16, 2018

Looks like you have two LGTMs, so I'm leaving mine off--I haven't run the code yet. But code LGTM!

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@spalger spalger merged commit 1516e07 into elastic:master Feb 19, 2018
spalger added a commit to spalger/kibana that referenced this pull request Feb 19, 2018
…#16724)

* [server/rewriteBasePath] add option to enable basePath rewriting

* [server/rewriteBasePath/docs] end sentences with periods

* [server/rewriteBasePath] simplify Joi schema a smidge

* [server/rewriteBasePath] rename test file to match source

* [server/rewriteBasePath] initialize server in before/after hooks

* [server/rewriteBasePath] rephrase deprecation warning

* [server/config/schema] verify that non-strings are not accepted for basePath

* [server/config/schema] toss a trailing comma in there
spalger added a commit that referenced this pull request Feb 19, 2018
…#16814)

* [server/rewriteBasePath] add option to enable basePath rewriting

* [server/rewriteBasePath/docs] end sentences with periods

* [server/rewriteBasePath] simplify Joi schema a smidge

* [server/rewriteBasePath] rename test file to match source

* [server/rewriteBasePath] initialize server in before/after hooks

* [server/rewriteBasePath] rephrase deprecation warning

* [server/config/schema] verify that non-strings are not accepted for basePath

* [server/config/schema] toss a trailing comma in there
@spalger
Copy link
Contributor Author

spalger commented Feb 19, 2018

6.3/6.x: ae0358a

@spalger spalger deleted the implement/server-base-route branch February 19, 2018 21:43
w33ble added a commit to w33ble/kibana that referenced this pull request Sep 13, 2018
kibana/elastic#16724 added a basepath rewrite to the kibana server, and any route that doesn't go through hapi needs to deal with it explicitly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:enhancement review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v6.3.0 v7.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants