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

[New platform] Support configuring request specific basePath #35951

Merged
merged 11 commits into from
May 8, 2019

Conversation

mshustov
Copy link
Contributor

@mshustov mshustov commented May 2, 2019

Summary

With this PR http service in NP provides ability:

  • to read / write request specific base path in any downstream plugin/core service
  • re-write request Url in onRequest interceptor

Both features are required to replace current Spaces functionality in legacy platform.
#35429 (comment)

@legrego

Actually, a more complex need is the ability to rewrite the incoming request before it is processed by the rest of the handler chain. We currently rely on the HAPI api and request shape to enable this today.

We re-write requests to drop the space identifier from the URL:

Browser: GET /s/marketing/app/kibana
Spaces interceptor:

strip off /s/marketing, store in request.setBasePath()
craft new url, omitting /s/marketing from the request.
send the updated request through the handler chain for subsequent processing by Kibana.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

It's applied to identify incoming requests across New and Legacy platforms. We can rely on the value if want to attach additional information to incoming requests without mutating them.
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💔 Build Failed

}

// should work only for KibanaRequest as soon as spaces migrate to NP
private setBasePathFor(request: KibanaRequest | Request, basePath: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We store basePath in WeakMap, where request.raw.req: IncomingMessage is used as a key. Hapi re-uses request.raw.req when we proxy request to LP, so it still can be used as a key to retrieve basePath from NP api

Copy link
Contributor

Choose a reason for hiding this comment

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

Can I ask why we opt for a WeakMap instead of a Map for this cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use ordinary Map it will lead to memory leaks, because Map keeps a reference to incoming request and it cannot be removed by GC. Using WeakMap frees us from the obligation to control the allocated memory manually.

__dirname,
'./__fixtures__/plugins/dummy_on_request'
);
const plugin = path.resolve(__dirname, './__fixtures__/plugins/dummy_on_request');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO for myself: create an issue to cleanup tests. after #35297 we can use setup/ start contracts in tests and don't need to declare a plugin for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created issue #36256

@mshustov
Copy link
Contributor Author

mshustov commented May 3, 2019

retest

@elastic elastic deleted a comment from elasticmachine May 3, 2019
@elastic elastic deleted a comment from elasticmachine May 3, 2019
@mshustov mshustov changed the title Expose base path getters [New platform] Support configuring request specific basePath May 3, 2019
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

}

public getFilteredHeaders(headersToKeep: string[]) {
return filterHeaders(this.headers, headersToKeep);
}

// eslint-disable-next-line @typescript-eslint/camelcase
public unstable_getIncomingMessage() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure we want to expose it on a long distance, so I added prefix unstable_

@mshustov mshustov added Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:Security/Spaces Platform Security - Spaces feature v7.2.0 labels May 3, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform

@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security

@mshustov mshustov marked this pull request as ready for review May 3, 2019 12:06
@mshustov mshustov requested a review from a team as a code owner May 3, 2019 12:06
@mshustov mshustov added the release_note:skip Skip the PR/issue when compiling release notes label May 3, 2019
Copy link
Contributor

@eliperelman eliperelman left a comment

Choose a reason for hiding this comment

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

Have some syntax notes and question.

src/core/server/http/http_server.test.ts Outdated Show resolved Hide resolved
src/core/server/http/http_server.test.ts Outdated Show resolved Hide resolved
registerRouter(router);

await server.start(config);
await supertest(innerServer.listener)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be re-written to take advantage of the async function and removing the then?

const response = await supertest(innerServer.listener).get('/').expect(200);

expect(response.body).toEqual({ key: path });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but in this case, the test suit is inconsistent from other tests in this file.

}

// should work only for KibanaRequest as soon as spaces migrate to NP
private setBasePathFor(request: KibanaRequest | Request, basePath: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can I ask why we opt for a WeakMap instead of a Map for this cache?

@mshustov mshustov requested a review from eliperelman May 5, 2019 11:31
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@mshustov
Copy link
Contributor Author

mshustov commented May 6, 2019

@eliperelman addressed

Copy link
Contributor

@eliperelman eliperelman left a comment

Choose a reason for hiding this comment

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

LGTM with TODO addressed.

@mshustov mshustov merged commit 452f529 into elastic:master May 8, 2019
@mshustov mshustov deleted the expose-base-path-getters branch May 8, 2019 07:28
mshustov added a commit to mshustov/kibana that referenced this pull request May 8, 2019
…#35951)

* expose IncomingMessage from KibanaRequest

It's applied to identify incoming requests across New and Legacy platforms. We can rely on the value if want to attach additional information to incoming requests without mutating them.

* support attaching basePath information to incoming requests

*  Support Url changing for incoming requests

* add tests

* use NP API in the legacy platform

* relax KibanaRequest typings

* check basePath cannot be set twice

* address @eli comments

* generate docs
mshustov added a commit that referenced this pull request May 8, 2019
…#36257)

* expose IncomingMessage from KibanaRequest

It's applied to identify incoming requests across New and Legacy platforms. We can rely on the value if want to attach additional information to incoming requests without mutating them.

* support attaching basePath information to incoming requests

*  Support Url changing for incoming requests

* add tests

* use NP API in the legacy platform

* relax KibanaRequest typings

* check basePath cannot be set twice

* address @eli comments

* generate docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:New Platform Feature:Security/Spaces Platform Security - Spaces feature release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants