[7.3] [basePathProxy] add __UNSAFE_bypassBasePath route (#4120… (#42451)

This commit is contained in:
Spencer 2019-08-02 13:41:40 -07:00 committed by GitHub
parent b9a767e1a7
commit 54024807b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View file

@ -77,6 +77,15 @@ To accomplish this the `serve` task does a few things:
- redirects from `/{any}/app/{appName}` to `/{randomBasePath}/app/{appName}` so that refreshes should work
- proxies all requests starting with `/{randomBasePath}/` to the Kibana server
If you're writing scripts that interact with the Kibana API, the base path proxy will likely
make this difficult. To bypass the base path proxy for a single request, prefix urls with
`__UNSAFE_bypassBasePath` and the request will be routed to the development Kibana server.
["source","shell"]
-----------
curl "http://elastic:changeme@localhost:5601/__UNSAFE_bypassBasePath/api/status"
-----------
This proxy can sometimes have unintended side effects in development, so when
needed you can opt out by passing the `--no-base-path` flag to the `serve` task
or `yarn start`.

View file

@ -18,7 +18,8 @@
*/
import { ByteSizeValue } from '@kbn/config-schema';
import { Server } from 'hapi';
import { Server, Request } from 'hapi';
import Url from 'url';
import { Agent as HttpsAgent, ServerOptions as TlsOptions } from 'https';
import { sample } from 'lodash';
import { DevConfig } from '../dev';
@ -145,6 +146,38 @@ export class BasePathProxyServer {
path: `${this.httpConfig.basePath}/{kbnPath*}`,
});
this.server.route({
handler: {
proxy: {
agent: this.httpsAgent,
passThrough: true,
xforward: true,
mapUri: (request: Request) => ({
uri: Url.format({
hostname: request.server.info.host,
port: this.devConfig.basePathProxyTargetPort,
protocol: request.server.info.protocol,
pathname: `${this.httpConfig.basePath}/${request.params.kbnPath}`,
query: request.query,
}),
headers: request.headers,
}),
},
},
method: '*',
options: {
pre: [
// Before we proxy request to a target port we may want to wait until some
// condition is met (e.g. until target listener is ready).
async (request, responseToolkit) => {
await blockUntil();
return responseToolkit.continue;
},
],
},
path: `/__UNSAFE_bypassBasePath/{kbnPath*}`,
});
// It may happen that basepath has changed, but user still uses the old one,
// so we can try to check if that's the case and just redirect user to the
// same URL, but with valid basepath.