Skip to content

Commit

Permalink
[SIEM] NP Plugin dependency cleanup (elastic#64842)
Browse files Browse the repository at this point in the history
* 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.

* Make usageCollection an optional dependency

If it's not enabled, we simply use a noop for our tracker call.

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

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

* Improve typings of our Plugin classes

* passes missing generic arguments to public plugin interface
* passes missing generic arguments to both plugins' CoreSetup types

* Don't re-export core types

Instead, import them from core as needed.
  • Loading branch information
rylnd committed Apr 30, 2020
1 parent 01d40bd commit 6fb4157
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 26 deletions.
12 changes: 8 additions & 4 deletions x-pack/plugins/siem/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
"alerting",
"data",
"embeddable",
"esUiShared",
"features",
"home",
"inspector",
"kibanaUtils",
"licensing",
"maps",
"triggers_actions_ui",
"uiActions",
"uiActions"
],
"optionalPlugins": [
"encryptedSavedObjects",
"ml",
"newsfeed",
"security",
"spaces",
"usageCollection"
],
"optionalPlugins": ["encryptedSavedObjects", "ml", "newsfeed", "security", "spaces"],
"server": true,
"ui": true
}
8 changes: 3 additions & 5 deletions x-pack/plugins/siem/public/lib/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -24,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);
} catch (error) {
// ignore failed setup here, as we'll just have an inert tracker
}
_track = usageCollection?.reportUiStats?.bind(null, appId) ?? noop;
};

export enum TELEMETRY_EVENT {
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/siem/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface SetupPlugins {
home: HomePublicPluginSetup;
security: SecurityPluginSetup;
triggers_actions_ui: TriggersActionsSetup;
usageCollection: UsageCollectionSetup;
usageCollection?: UsageCollectionSetup;
}

export interface StartPlugins {
Expand All @@ -59,14 +59,14 @@ export interface PluginSetup {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface PluginStart {}

export class Plugin implements IPlugin<PluginSetup, PluginStart> {
export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, StartPlugins> {
private kibanaVersion: string;

constructor(initializerContext: PluginInitializerContext) {
this.kibanaVersion = initializerContext.env.packageInfo.version;
}

public setup(core: CoreSetup, plugins: SetupPlugins) {
public setup(core: CoreSetup<StartPlugins, PluginStart>, plugins: SetupPlugins) {
initTelemetry(plugins.usageCollection, APP_ID);

plugins.home.featureCatalogue.register({
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/siem/server/lib/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { GraphQLSchema } from 'graphql';
import { runHttpQuery } from 'apollo-server-core';
import { schema as configSchema } from '@kbn/config-schema';
import {
CoreSetup,
IRouter,
KibanaResponseFactory,
RequestHandlerContext,
KibanaRequest,
} 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,
Expand Down
16 changes: 4 additions & 12 deletions x-pack/plugins/siem/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -40,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;
Expand All @@ -52,10 +46,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 {}
Expand All @@ -77,7 +69,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
this.logger.debug('plugin initialized');
}

public async setup(core: CoreSetup, plugins: SetupPlugins) {
public async setup(core: CoreSetup<StartPlugins, PluginStart>, plugins: SetupPlugins) {
this.logger.debug('plugin setup');

if (hasListsFeature()) {
Expand Down

0 comments on commit 6fb4157

Please sign in to comment.