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

[Code] Migrate routers/elasticsearch clients/savedobjects clients to NP #47643

Merged
merged 15 commits into from
Oct 18, 2019
Merged

[Code] Migrate routers/elasticsearch clients/savedobjects clients to NP #47643

merged 15 commits into from
Oct 18, 2019

Conversation

mw-ding
Copy link
Contributor

@mw-ding mw-ding commented Oct 8, 2019

Summary

https://github.com/elastic/code/issues/1596

Finish the migrations of:

  • Routers
  • Elasticsearch clients
  • SavedObjectClient

Checklist

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

For maintainers

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

Pinging @elastic/code (Team:Code)

@elasticmachine
Copy link
Contributor

💔 Build Failed

@mw-ding mw-ding changed the title Migrate the elasticsearch admin client [Code] Migrate routers and elasticsearch clients to NP Oct 9, 2019
@mw-ding mw-ding added v7.6.0 v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.5.0 labels Oct 9, 2019
@@ -1,39 +0,0 @@
/*
Copy link
Contributor Author

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.

Copy link
Contributor

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();
Copy link
Contributor Author

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?

Copy link
Contributor

@mshustov mshustov Oct 10, 2019

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();

@elasticmachine
Copy link
Contributor

💔 Build Failed

@mw-ding mw-ding marked this pull request as ready for review October 9, 2019 22:32
@mw-ding mw-ding requested a review from a team as a code owner October 9, 2019 22:32
@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 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);
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

@mw-ding mw-ding Oct 10, 2019

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:

  1. 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.
  2. 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.
  3. 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?

Copy link
Contributor

@spacedragon spacedragon Oct 11, 2019

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.

Copy link
Contributor Author

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.

x-pack/legacy/plugins/code/server/security.ts Outdated Show resolved Hide resolved
req: KibanaRequest,
res: KibanaResponseFactory
) => {
const { context, params } = req.body as RequestPayload;
Copy link
Contributor

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,
  ...

Copy link
Contributor Author

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);
Copy link
Contributor

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

x-pack/legacy/plugins/code/server/git_operations.ts Outdated Show resolved Hide resolved
constructor(private readonly initContext: PluginSetupContract) {
this.log = {} as Logger;
this.serverOptions = {} as ServerOptions;
}

public setup(core: CoreSetup) {
public async setup(core: CoreSetup, npHttp: any) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@mshustov mshustov Oct 14, 2019

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)

) => {
expect(baseUrl).toBe(codeNodeUrl);
const response = await hapiServer.inject({
method: 'POST',
url: path,
headers: originRequest.headers,
headers: originRequest.headers as any,
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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

x-pack/legacy/plugins/code/server/plugin.ts Show resolved Hide resolved
Copy link
Contributor Author

@mw-ding mw-ding left a 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({
Copy link
Contributor Author

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.

Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

💔 Build Failed

@joshdover joshdover removed their request for review October 14, 2019 19:51
@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

securityPlugin &&
securityPlugin.authorization &&
// @ts-ignore
securityPlugin.authorization.mode.useRbacForRequest(req);
Copy link
Contributor Author

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.

@mw-ding mw-ding changed the title [Code] Migrate routers and elasticsearch clients to NP [Code] Migrate routers/elasticsearch clients/savedobjects clients to NP Oct 17, 2019
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@mw-ding mw-ding merged commit 55349d7 into elastic:master Oct 18, 2019
mw-ding added a commit that referenced this pull request Oct 18, 2019
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes v7.5.0 v7.6.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants