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

[Reporting] Define shims of legacy dependencies #54082

Merged
merged 33 commits into from
Jan 16, 2020

Conversation

tsullivan
Copy link
Member

@tsullivan tsullivan commented Jan 7, 2020

Summary

Part of #53898

This PR defines serverFacade and requestFacade objects that use bare-bones types of the legacy dependencies

Checklist

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

For maintainers

@tsullivan tsullivan added review (Deprecated) Feature:Reporting Use Reporting:Screenshot, Reporting:CSV, or Reporting:Framework instead v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Feature:NP Migration v7.6.0 Team:Reporting Services labels Jan 7, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-reporting-services (Team:Reporting Services)

@tsullivan tsullivan changed the title Reporting/shim plugin [Reporting] Define shims of legacy dependencies Jan 7, 2020
@tsullivan tsullivan mentioned this pull request Jan 7, 2020
19 tasks
@tsullivan tsullivan requested a review from joelgriffith January 7, 2020 00:33
@tsullivan tsullivan force-pushed the reporting/shim-plugin branch from a5cdf86 to c931081 Compare January 7, 2020 00:34
headers: Legacy.Request['headers'];
params: Legacy.Request['params'];
payload: JobParamPostPayload | GenerateExportTypePayload;
query: ListQuery & GenerateQuery;
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure why this isn't ListQuery | GenerateQuery

@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

import { ServerFacade } from '../../types';

export function getUserFactory(server: ServerFacade) {
return async (request: Legacy.Request) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This function takes a Legacy request because it is called from middleware. That means we get into this code before we get into any request handlers which is where we pick the properties for RequestFacade.

Copy link
Member Author

Choose a reason for hiding this comment

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

@pgayvallet @restrry this means that I don't actually need to call request.getRawRequest - doing so made all the code break since this isn't an actual RequestFacade :)

Copy link
Member Author

Choose a reason for hiding this comment

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

ddf8fac added that method back, but for a different usage

Copy link
Contributor

Choose a reason for hiding this comment

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

Just thinking loudly: why not call this function from a route handler? NP routing doesn't provide a pre-middleware mechanism at the moment.

Copy link
Member Author

Choose a reason for hiding this comment

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

NP routing doesn't provide a pre-middleware mechanism at the moment.

Hm, if that is the case it will be a major blocker for Reporting migration.

@rudolf can you confirm whether we will be able to register routing middleware in the new platform?

Copy link
Contributor

@pgayvallet pgayvallet Jan 21, 2020

Choose a reason for hiding this comment

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

@rudolf can you confirm whether we will be able to register routing middleware in the new platform?

ATM this is not something we are planing to provide.

Hm, if that is the case it will be a major blocker for Reporting migration.

Correct me if I'm wrong, but the whole pre-routing mechanism can easily be replaced by some wrapper around the actual route handlers. The usage would be really close to what you currently have and should not be that big of a change

Looking at your getRouteConfigFactoryReportingPre method and usages in x-pack/legacy/plugins/reporting/server/routes/generate_from_jobparams.ts, the pre-routing can be moved to an higher level. This would be something similar as what we are going in src/core/server/http/router/error_wrapper.ts

  const getRouteConfig = () => {
    const getOriginalRouteConfig: GetRouteConfigFactoryFn = getRouteConfigFactoryReportingPre(
      server
    );
    const routeConfigFactory: RouteConfigFactory = getOriginalRouteConfig(
      ({ params: { exportType } }) => exportType
    );

    return {
      ...routeConfigFactory,
      validate: {
        params: Joi.object({
         [...]
      },
    };
  };

  // generate report
  server.route({
    path: `${BASE_GENERATE}/{exportType}`,
    method: 'POST',
    options: getRouteConfig(),
    handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
      [...]
      return response;
    },
  });

would become something like (also adapted to NP router format)

  const getRouteConfig = () => {
    const getOriginalRouteConfig: GetRouteConfigFactoryFn = getRouteConfigFactoryReportingPre(
      server
    );
    const routeConfigFactory: RouteConfigFactory = getOriginalRouteConfig(
      ({ params: { exportType } }) => exportType
    );

    return {
      ...routeConfigFactory,
      validate: {
        params: schema.object({
         [...]
      },
    };
  };


const routeConfig = getRouteConfig()
router.post(
      {
        path: `${BASE_GENERATE}/{exportType}`,
        options: routeConfig.options
        validate: routeConfig.validate,
      },
      routeConfig.performPreAuth(async (ctx, req, res, preAuthResult) => {
        const request = makeRequestFacade(legacyRequest, preAuthResults);
        ....
      })
    );

The performPreAuth generated from getRouteConfigFactoryReportingPre would need some adaption from the existing code to execute all pre-handlers and either returns a response error if one throws or execute the underlying handler with an additional parameter containing the resolved objects from the pre handlers (basically what HAPI was doing itself before), but that doesn't sound like big changes, the actual pre-hook handlers would remains unchanged.

Please ping me if you want me to go a little more in-details with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm ++ on any solution that will allow us to move further, and I don't see any problems with your proposals. I think there are a lot of ways to achieve the goal that the code is doing and pre-routing is just one of them.

@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

@tsullivan tsullivan force-pushed the reporting/shim-plugin branch from aee133f to ddf8fac Compare January 15, 2020 22:05
@tsullivan
Copy link
Member Author

@elasticmachine merge upstream

management: {
jobTypes: any;
};
user: any;
Copy link
Contributor

@mshustov mshustov Jan 16, 2020

Choose a reason for hiding this comment

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

btw Security exports AuthenticatedUser type

export { AuthenticatedUser } from '../common/model';

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 approve of this message

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, when I tried that, I get an ESLint error:
@kbn/eslint/no-restricted-paths: Unexpected path "../../../plugins/security/server" imported in restricted zone. Server modules cannot be imported into client modules or shared modules.

if (!server.plugins.security) {
return null;
}

try {
return await server.plugins.security.getUser(request);
} catch (err) {
server.log(['reporting', 'getUser', 'debug'], err);
server.log(['reporting', 'getUser', 'error'], err);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch here.

@@ -27,10 +27,6 @@ export function oncePerServer(fn: ServerFn) {
throw new TypeError('This function expects to be called with a single argument');
}

if (!server || typeof server.expose !== 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yesssss

@@ -54,7 +56,8 @@ export function registerGenerateFromJobParams(
path: `${BASE_GENERATE}/{exportType}`,
method: 'POST',
options: getRouteConfig(),
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
handler: async (originalRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(originalRequest);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to keep with the legacy naming convention here? originalRequest to legacyRequest?

Copy link
Contributor

@joelgriffith joelgriffith left a comment

Choose a reason for hiding this comment

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

LGTM! Saw the note below on Type's we can use for the user, which I'm a +1 on.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@tsullivan tsullivan merged commit cfbd095 into elastic:master Jan 16, 2020
@tsullivan tsullivan deleted the reporting/shim-plugin branch January 16, 2020 22:15
tsullivan added a commit to tsullivan/kibana that referenced this pull request Jan 16, 2020
* simplify serverfacade definition

* simplify requestfacade definition

* use the shim

* makeRequestFacade

* requestFacade

* import sorting

* originalServer

* reduce loc change

* remove consolelog

* hacks to fix tests

* ServerFacade in index

* Cosmetic

* remove field from serverfacade

* add raw to the request

* fix types

* add fieldFormatServiceFactory to legacy

* Pass the complete request object to sec plugin

* Fix test

* fix test 2

* getUser takes a legacy request

* add unit test for new lib

* add getRawRequest to pass to saved objects method

* update test snapshot

* leave a TODO comment for type import

* variable rename for legacy id

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
jloleysens added a commit to jloleysens/kibana that referenced this pull request Jan 17, 2020
…t-of-legacy

* 'master' of github.com:elastic/kibana: (142 commits)
  [Vis] Move Timelion Vis to vis_type_timelion (elastic#52069)
  Deprecate `chrome.navlinks.update` and add documentation (elastic#54893)
  [ML] Single Metric Viewer: Fix time bounds with custom strings. (elastic#55045)
  [Vis: Default editor] EUIficate and Reactify the sidebar (elastic#49864)
  [Mappings editor] Fix cannot set boolean value for "null_value" param (elastic#55015)
  [SIEM] Adds support for apm-* to the network map (elastic#54876)
  [Reporting] Define shims of legacy dependencies (elastic#54082)
  Resolver is overflow: hidden to prevent obscured elements from showing up (elastic#55076)
  Upgraded EUI to 18.2.1 (elastic#55090)
  [Maps] Support styles on agg fields with _of_ in name (elastic#54965)
  Remove xpack_main requirement, it's no longer in use (elastic#55060)
  Fix Snapshots Policies Alignment Issue in IE11 (elastic#54866)
  first rule cuts (elastic#54990)
  [DOCS] Adds geocentroid note to coordinate map (elastic#54389)
  [Canvas] Fixes the Copy Post Url link (elastic#54831)
  Fixes bugs with full screen filters (elastic#54792)
  [ML] Fix decoding in the URL state  (elastic#54915)
  Remove redundant `x-pack/typings`. (elastic#55042)
  [SIEM][Detection Engine] Adds critical missing status route to prepackaged rules
  Generate legacy vars when rendering all applications (elastic#54768)
  ...

# Conflicts:
#	x-pack/plugins/translations/translations/ja-JP.json
#	x-pack/plugins/translations/translations/zh-CN.json
jkelastic pushed a commit to jkelastic/kibana that referenced this pull request Jan 17, 2020
* simplify serverfacade definition

* simplify requestfacade definition

* use the shim

* makeRequestFacade

* requestFacade

* import sorting

* originalServer

* reduce loc change

* remove consolelog

* hacks to fix tests

* ServerFacade in index

* Cosmetic

* remove field from serverfacade

* add raw to the request

* fix types

* add fieldFormatServiceFactory to legacy

* Pass the complete request object to sec plugin

* Fix test

* fix test 2

* getUser takes a legacy request

* add unit test for new lib

* add getRawRequest to pass to saved objects method

* update test snapshot

* leave a TODO comment for type import

* variable rename for legacy id

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jan 20, 2020
* upstream/master: (83 commits)
  [Reporting] Fix map tiles not loading by using Chrome's Remote Protocol (elastic#55137)
  [Data Plugin] combine autocomplete provider and suggestions provider (elastic#54451)
  resolves elastic#53038 - remove references to specific license levels (elastic#53858)
  highlighting rules should still know about url parts when in sql state (elastic#55200)
  [Metric] convert mocha tests to jest (elastic#54054)
  [skip-ci] Update vector styling docs for 7.6 UI changes and new features (elastic#55087)
  Fix enable API to schedule task after alert is updated (elastic#55095)
  chore(NA): add 7.6 branch to the list of backport branches (elastic#54998)
  Convert tests to jest in vis_type_timeseries/public & common folders (elastic#55023)
  [ML] Accessibility fix for structural markup on table rows (elastic#55075)
  [Mappings editor] include/exclude fields only support custom options (elastic#54949)
  [Vis] Move Timelion Vis to vis_type_timelion (elastic#52069)
  Deprecate `chrome.navlinks.update` and add documentation (elastic#54893)
  [ML] Single Metric Viewer: Fix time bounds with custom strings. (elastic#55045)
  [Vis: Default editor] EUIficate and Reactify the sidebar (elastic#49864)
  [Mappings editor] Fix cannot set boolean value for "null_value" param (elastic#55015)
  [SIEM] Adds support for apm-* to the network map (elastic#54876)
  [Reporting] Define shims of legacy dependencies (elastic#54082)
  Resolver is overflow: hidden to prevent obscured elements from showing up (elastic#55076)
  Upgraded EUI to 18.2.1 (elastic#55090)
  ...
tsullivan added a commit that referenced this pull request Jan 21, 2020
* [Reporting] Define shims of legacy dependencies (#54082)

* simplify serverfacade definition

* simplify requestfacade definition

* use the shim

* makeRequestFacade

* requestFacade

* import sorting

* originalServer

* reduce loc change

* remove consolelog

* hacks to fix tests

* ServerFacade in index

* Cosmetic

* remove field from serverfacade

* add raw to the request

* fix types

* add fieldFormatServiceFactory to legacy

* Pass the complete request object to sec plugin

* Fix test

* fix test 2

* getUser takes a legacy request

* add unit test for new lib

* add getRawRequest to pass to saved objects method

* update test snapshot

* leave a TODO comment for type import

* variable rename for legacy id

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* update legacy to fix ts errors

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
(Deprecated) Feature:Reporting Use Reporting:Screenshot, Reporting:CSV, or Reporting:Framework instead Feature:NP Migration release_note:skip Skip the PR/issue when compiling release notes review v7.6.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants