-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Code] Migrate routers/elasticsearch clients/savedobjects clients to NP #47643
Conversation
💔 Build Failed
|
Pinging @elastic/code (Team:Code) |
💔 Build Failed
|
@@ -1,39 +0,0 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spacedragon this file does not seem to be used anywhere, so remove now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, thanks.
@@ -17,12 +17,12 @@ import { Logger } from '../log'; | |||
import { ConsoleLoggerFactory } from '../utils/console_logger_factory'; | |||
|
|||
const log: Logger = new ConsoleLoggerFactory().getLogger(['test']); | |||
let hapiServer: Server = createTestHapiServer(); | |||
let hapiServer = createTestHapiServer(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@restrry any util functions to create the router for unit tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/elastic/kibana/blob/master/src/core/MIGRATION.md#mock-new-platform-services-in-tests
you can create a mocked instance of the router
import { httpServiceMock } from 'src/core/server/mocks';
const routerMock = httpServiceMock.createRouter();
💔 Build Failed
|
💔 Build Failed
|
💔 Build Failed
|
const body: Boom.Payload = await Wreck.read(res, { json: true }); | ||
throw new Boom(body.message, { statusCode: res.statusCode || 500, data: body.error }); | ||
const body = await Wreck.read(res, { json: true }); | ||
throw new Error(body.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure removing statusCode
is right here, some status code is important, such as 404
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it important to proxy this error code to the client? we already logged statusCode in https://github.com/elastic/kibana/pull/47643/files#diff-d170e5fce495c0bd5b8d0e8e28a0a33dR111
you still can use Boom error in the plugin, just add a top level wrapper that handles those errors. example from Security team https://github.com/restrry/kibana/blob/40ef4b76d82806b1b72724843d1b1ba7e1dde484/x-pack/plugins/security/server/authentication/index.ts#L126-L130
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree simply switching from Boom to Error will change some status code, but I anyway do it for the following reasons:
- we nested the Boom HTTP status code deeply into our internal service is not a good idea. This will make the internal service coupled with the http server layer very tightly. This is will make some future changes (like this PR) quite difficult to proceed.
- Ideally, we should only append status code in the http server layer, i.e. the route definitions, while define our own Error types (e.g.
LangServerNotInitializedError()
so that the route definition could assemble the right status code appropriately. - I think our frontend does not rely on the status code too much that a 404 handling will be the same as 500 (@spacedragon need your confirmation). So I anyway make this change.
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least, 404 is used for that File not found
page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spacedragon revert back the changes of removing Boom. let's figure this out later. Also, skippe the 2 unit tests here: https://github.com/elastic/kibana/blob/5015a368c3fd581e115248cf85b32941c0f87047/x-pack/legacy/plugins/code/server/distributed/code_services.test.ts#L65. Let's chat offline and fix it later.
req: KibanaRequest, | ||
res: KibanaResponseFactory | ||
) => { | ||
const { context, params } = req.body as RequestPayload; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend starting declaring validation from the very beginning. This validation
field was made required for safety reasons. If I'm not mistaken, we can declare schemas almost for all the request parameters.
// security.ts
case 'post': {
this.router.post({
path: route.path,
validate: {
body: route.validate.body || schema.object({}, { allowUnknowns: true }),
...
}
// in this file
const requestContextSchema = schema.object({
path: schema.string(),
resource: schema.string(),
});
const validate = {
body: schema.object({
context: requestContextSchema,
params: schema.object({}, { allowUnknowns: true }),
}),
};
this.router.route({
validate,
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned before in the channel, I will assign this to each stakeholders which have better context of the schema in the comment.
const body: Boom.Payload = await Wreck.read(res, { json: true }); | ||
throw new Boom(body.message, { statusCode: res.statusCode || 500, data: body.error }); | ||
const body = await Wreck.read(res, { json: true }); | ||
throw new Error(body.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it important to proxy this error code to the client? we already logged statusCode in https://github.com/elastic/kibana/pull/47643/files#diff-d170e5fce495c0bd5b8d0e8e28a0a33dR111
you still can use Boom error in the plugin, just add a top level wrapper that handles those errors. example from Security team https://github.com/restrry/kibana/blob/40ef4b76d82806b1b72724843d1b1ba7e1dde484/x-pack/plugins/security/server/authentication/index.ts#L126-L130
constructor(private readonly initContext: PluginSetupContract) { | ||
this.log = {} as Logger; | ||
this.serverOptions = {} as ServerOptions; | ||
} | ||
|
||
public setup(core: CoreSetup) { | ||
public async setup(core: CoreSetup, npHttp: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to pass a real CoreSetup as the first argument and legacy HTTP server separately? https://github.com/restrry/kibana/blob/40ef4b76d82806b1b72724843d1b1ba7e1dde484/x-pack/legacy/plugins/code/index.ts#L66-L72
We are losing all TypeScript benefits right now. And it will complicate refactoring in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is the server
instance is still depended, and we need to pass it in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but then you have a real typed CoreSetup as the first argument, not a hack
https://github.com/restrry/kibana/blob/ce9ea0bbb34869356030ffd2eaebe42d97b5fc82/x-pack/legacy/plugins/code/server/plugin.ts#L85
you can switch to setup(core: CoreSetup, _hapiHttp: Server)
x-pack/legacy/plugins/code/server/distributed/code_services.test.ts
Outdated
Show resolved
Hide resolved
) => { | ||
expect(baseUrl).toBe(codeNodeUrl); | ||
const response = await hapiServer.inject({ | ||
method: 'POST', | ||
url: path, | ||
headers: originRequest.headers, | ||
headers: originRequest.headers as any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need all the headers from KibanaRequest
object? We're going to restrict access to authorization
header for all plugins except for Security
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do let us know when the change happens. There is much more context here of why we need to forward the headers. Let me add a comment here just in case, but this is not something we should handle in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, this is for the test only.
import { RepositoryIndexInitializerFactory } from './indexer'; | ||
import { RepositoryConfigController } from './repository_config_controller'; | ||
import { EsClientWithInternalRequest } from './utils/esclient_with_internal_request'; | ||
import { EsClient } from './lib/esqueue'; | ||
import { Logger } from './log'; | ||
|
||
export async function initEs(server: Server, log: Logger) { | ||
// wait until elasticsearch is ready | ||
await server.plugins.elasticsearch.waitUntilReady(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need this logic in the legacy platform anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be a question for you. After we use the context.elasticsearch.xxx
, I think this is not required. Can you confirm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, NP doesn't support it yet #43456
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the significant changes in this PR, I won't merge this one until the 7.5
branch has been cut.
@@ -80,13 +82,13 @@ test('non-code-node could send request to code-node', async () => { | |||
baseUrl: string, | |||
path: string, | |||
payload: RequestPayload, | |||
originRequest: Request | |||
originRequest: KibanaRequest | |||
) => { | |||
expect(baseUrl).toBe(codeNodeUrl); | |||
const response = await hapiServer.inject({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@restrry we will need the mock for this one to replace the hapiServer here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already mocked router in the test. If you want to add an integration test instead, you can use kbnTestServer
. There is an example how to do it https://github.com/elastic/kibana/blob/master/src/core/server/legacy/integration_tests/legacy_service.test.ts
💔 Build Failed
|
💔 Build Failed
|
💔 Build Failed
|
💔 Build Failed
|
💔 Build Failed
|
💚 Build Succeeded
|
This reverts commit 7eda19ee6e5b001f46ce86e93bd9694d6a146976.
securityPlugin && | ||
securityPlugin.authorization && | ||
// @ts-ignore | ||
securityPlugin.authorization.mode.useRbacForRequest(req); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@azasypkin still use the old one but apply the new KibanaRequest
here.
💚 Build Succeeded |
…NP (#47643) (#48598) * Migrate the elasticsearch admin client * initial router migration * update the code service request * more routes migration * remove Boom * remove redirect route * test * more route * addressing comments * remove the notFound * Revert "remove Boom" This reverts commit 7eda19ee6e5b001f46ce86e93bd9694d6a146976. * handle isBoom * more fixes * minor fix * shim security useRbacForRequest
Summary
https://github.com/elastic/code/issues/1596
Finish the migrations of:
Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.For maintainers