From 687d30197ba778f01dd6c8fc82766f5c272e7a46 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Tue, 28 Apr 2020 14:12:09 -0500 Subject: [PATCH 1/6] Remove static src dependencies from kibana.json We are only importing static code from these plugins, and not consuming their plugin contracts. For this reason, we can safely remove them from kibana.json as that imported code will always be included in our own bundle. --- x-pack/plugins/siem/kibana.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/siem/kibana.json b/x-pack/plugins/siem/kibana.json index e0ff82eb10eb2..577d76dc70297 100644 --- a/x-pack/plugins/siem/kibana.json +++ b/x-pack/plugins/siem/kibana.json @@ -8,11 +8,9 @@ "alerting", "data", "embeddable", - "esUiShared", "features", "home", "inspector", - "kibanaUtils", "licensing", "maps", "triggers_actions_ui", From 809382acfa42aec71eed5d9c2fd96484b1e472e9 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 29 Apr 2020 12:32:05 -0500 Subject: [PATCH 2/6] Make usageCollection an optional dependency If it's not enabled, we simply use a noop for our tracker call. --- x-pack/plugins/siem/kibana.json | 10 ++++++++-- x-pack/plugins/siem/public/lib/telemetry/index.ts | 6 ++++-- x-pack/plugins/siem/public/plugin.tsx | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/siem/kibana.json b/x-pack/plugins/siem/kibana.json index 577d76dc70297..39e50838c1c97 100644 --- a/x-pack/plugins/siem/kibana.json +++ b/x-pack/plugins/siem/kibana.json @@ -14,10 +14,16 @@ "licensing", "maps", "triggers_actions_ui", - "uiActions", + "uiActions" + ], + "optionalPlugins": [ + "encryptedSavedObjects", + "ml", + "newsfeed", + "security", + "spaces", "usageCollection" ], - "optionalPlugins": ["encryptedSavedObjects", "ml", "newsfeed", "security", "spaces"], "server": true, "ui": true } diff --git a/x-pack/plugins/siem/public/lib/telemetry/index.ts b/x-pack/plugins/siem/public/lib/telemetry/index.ts index 856b7783a5367..7f806159191ac 100644 --- a/x-pack/plugins/siem/public/lib/telemetry/index.ts +++ b/x-pack/plugins/siem/public/lib/telemetry/index.ts @@ -13,6 +13,8 @@ export { METRIC_TYPE }; type TrackFn = (type: UiStatsMetricType, event: string | string[], count?: number) => void; +const noop = () => {}; + let _track: TrackFn; export const track: TrackFn = (type, event, count) => { @@ -25,9 +27,9 @@ export const track: TrackFn = (type, event, count) => { export const initTelemetry = (usageCollection: SetupPlugins['usageCollection'], appId: string) => { try { - _track = usageCollection.reportUiStats.bind(null, appId); + _track = usageCollection?.reportUiStats.bind(null, appId) ?? noop; } catch (error) { - // ignore failed setup here, as we'll just have an inert tracker + _track = noop; } }; diff --git a/x-pack/plugins/siem/public/plugin.tsx b/x-pack/plugins/siem/public/plugin.tsx index e54c96364ba6b..7d17eb5ad1d58 100644 --- a/x-pack/plugins/siem/public/plugin.tsx +++ b/x-pack/plugins/siem/public/plugin.tsx @@ -37,7 +37,7 @@ export interface SetupPlugins { home: HomePublicPluginSetup; security: SecurityPluginSetup; triggers_actions_ui: TriggersActionsSetup; - usageCollection: UsageCollectionSetup; + usageCollection?: UsageCollectionSetup; } export interface StartPlugins { From b0da9f9fc4c95b1d865ed4752b1632c9d57a4efe Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 29 Apr 2020 15:48:58 -0500 Subject: [PATCH 3/6] Remove unused plugin contracts These are only needed when we're actually using them in our codebase. For request handler contexts, we only need our kibana.json declaration. --- x-pack/plugins/siem/server/plugin.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/siem/server/plugin.ts b/x-pack/plugins/siem/server/plugin.ts index dc484b75ab531..5bfaa7a64b5d8 100644 --- a/x-pack/plugins/siem/server/plugin.ts +++ b/x-pack/plugins/siem/server/plugin.ts @@ -15,16 +15,12 @@ import { PluginInitializerContext, Logger, } from '../../../../src/core/server'; -import { - PluginStartContract as AlertingStart, - PluginSetupContract as AlertingSetup, -} from '../../alerting/server'; +import { PluginSetupContract as AlertingSetup } from '../../alerting/server'; import { SecurityPluginSetup as SecuritySetup } from '../../security/server'; import { PluginSetupContract as FeaturesSetup } from '../../features/server'; import { MlPluginSetup as MlSetup } from '../../ml/server'; import { EncryptedSavedObjectsPluginSetup as EncryptedSavedObjectsSetup } from '../../encrypted_saved_objects/server'; import { SpacesPluginSetup as SpacesSetup } from '../../spaces/server'; -import { PluginStartContract as ActionsStart } from '../../actions/server'; import { LicensingPluginSetup } from '../../licensing/server'; import { initServer } from './init_server'; import { compose } from './lib/compose/kibana'; @@ -52,10 +48,8 @@ export interface SetupPlugins { ml?: MlSetup; } -export interface StartPlugins { - actions: ActionsStart; - alerting: AlertingStart; -} +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface StartPlugins {} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface PluginSetup {} From 11f06626cb80ecbb75f5c382b0ea1103859e72c5 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 30 Apr 2020 12:02:11 -0500 Subject: [PATCH 4/6] Remove unnecessary try/catch With the addition of the null coalescing, the only chance for an error is in usageCollection. However, if the usageCollection contract changes, we should get a type error long before we see a runtime error. --- x-pack/plugins/siem/public/lib/telemetry/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x-pack/plugins/siem/public/lib/telemetry/index.ts b/x-pack/plugins/siem/public/lib/telemetry/index.ts index 7f806159191ac..37d181e9b8ad7 100644 --- a/x-pack/plugins/siem/public/lib/telemetry/index.ts +++ b/x-pack/plugins/siem/public/lib/telemetry/index.ts @@ -26,11 +26,7 @@ export const track: TrackFn = (type, event, count) => { }; export const initTelemetry = (usageCollection: SetupPlugins['usageCollection'], appId: string) => { - try { - _track = usageCollection?.reportUiStats.bind(null, appId) ?? noop; - } catch (error) { - _track = noop; - } + _track = usageCollection?.reportUiStats?.bind(null, appId) ?? noop; }; export enum TELEMETRY_EVENT { From 808b1b47aaf46fe464e38b92b7452b8bea5538b6 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 30 Apr 2020 12:18:34 -0500 Subject: [PATCH 5/6] Improve typings of our Plugin classes * passes missing generic arguments to public plugin interface * passes missing generic arguments to both plugins' CoreSetup types --- x-pack/plugins/siem/public/plugin.tsx | 4 ++-- x-pack/plugins/siem/server/plugin.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/siem/public/plugin.tsx b/x-pack/plugins/siem/public/plugin.tsx index 7d17eb5ad1d58..f9c44bd341fac 100644 --- a/x-pack/plugins/siem/public/plugin.tsx +++ b/x-pack/plugins/siem/public/plugin.tsx @@ -59,14 +59,14 @@ export interface PluginSetup {} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface PluginStart {} -export class Plugin implements IPlugin { +export class Plugin implements IPlugin { private kibanaVersion: string; constructor(initializerContext: PluginInitializerContext) { this.kibanaVersion = initializerContext.env.packageInfo.version; } - public setup(core: CoreSetup, plugins: SetupPlugins) { + public setup(core: CoreSetup, plugins: SetupPlugins) { initTelemetry(plugins.usageCollection, APP_ID); plugins.home.featureCatalogue.register({ diff --git a/x-pack/plugins/siem/server/plugin.ts b/x-pack/plugins/siem/server/plugin.ts index 5bfaa7a64b5d8..b186b40b48ded 100644 --- a/x-pack/plugins/siem/server/plugin.ts +++ b/x-pack/plugins/siem/server/plugin.ts @@ -71,7 +71,7 @@ export class Plugin implements IPlugin, plugins: SetupPlugins) { this.logger.debug('plugin setup'); if (hasListsFeature()) { From 98e2cc3602786fdddeda2551b68cae2a4e8989b0 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 30 Apr 2020 12:30:12 -0500 Subject: [PATCH 6/6] Don't re-export core types Instead, import them from core as needed. --- x-pack/plugins/siem/server/lib/compose/kibana.ts | 3 ++- .../siem/server/lib/framework/kibana_framework_adapter.ts | 3 ++- x-pack/plugins/siem/server/plugin.ts | 2 -- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/siem/server/lib/compose/kibana.ts b/x-pack/plugins/siem/server/lib/compose/kibana.ts index 4a595032e43eb..8bc90bed25168 100644 --- a/x-pack/plugins/siem/server/lib/compose/kibana.ts +++ b/x-pack/plugins/siem/server/lib/compose/kibana.ts @@ -4,7 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreSetup, SetupPlugins } from '../../plugin'; +import { CoreSetup } from '../../../../../../src/core/server'; +import { SetupPlugins } from '../../plugin'; import { Authentications } from '../authentications'; import { ElasticsearchAuthenticationAdapter } from '../authentications/elasticsearch_adapter'; diff --git a/x-pack/plugins/siem/server/lib/framework/kibana_framework_adapter.ts b/x-pack/plugins/siem/server/lib/framework/kibana_framework_adapter.ts index 762416149c0fb..1e99c40ef5727 100644 --- a/x-pack/plugins/siem/server/lib/framework/kibana_framework_adapter.ts +++ b/x-pack/plugins/siem/server/lib/framework/kibana_framework_adapter.ts @@ -9,6 +9,7 @@ import { GraphQLSchema } from 'graphql'; import { runHttpQuery } from 'apollo-server-core'; import { schema as configSchema } from '@kbn/config-schema'; import { + CoreSetup, IRouter, KibanaResponseFactory, RequestHandlerContext, @@ -16,7 +17,7 @@ import { } from '../../../../../../src/core/server'; import { IndexPatternsFetcher } from '../../../../../../src/plugins/data/server'; import { AuthenticatedUser } from '../../../../security/common/model'; -import { CoreSetup, SetupPlugins } from '../../plugin'; +import { SetupPlugins } from '../../plugin'; import { FrameworkAdapter, diff --git a/x-pack/plugins/siem/server/plugin.ts b/x-pack/plugins/siem/server/plugin.ts index b186b40b48ded..3ef4b39bd0979 100644 --- a/x-pack/plugins/siem/server/plugin.ts +++ b/x-pack/plugins/siem/server/plugin.ts @@ -36,8 +36,6 @@ import { createConfig$, ConfigType } from './config'; import { initUiSettings } from './ui_settings'; import { APP_ID, APP_ICON } from '../common/constants'; -export { CoreSetup, CoreStart }; - export interface SetupPlugins { alerting: AlertingSetup; encryptedSavedObjects?: EncryptedSavedObjectsSetup;