Skip to content

Commit

Permalink
license setup in the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
parkiino committed Nov 6, 2020
1 parent e61c76d commit 22feb08
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
53 changes: 53 additions & 0 deletions x-pack/plugins/security_solution/common/license/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, Subscription } from 'rxjs';
import { ILicense } from '../../../licensing/common/types';

// Generic license service class that works with the license observable
// Both server and client plugins instancates a singleton version of this class
export class LicenseService {
private observable: Observable<ILicense> | null = null;
private subscription: Subscription | null = null;
private licenseInformation: ILicense | null = null;

private updateInformation(licenseInformation: ILicense) {
this.licenseInformation = licenseInformation;
}

public start(license$: Observable<ILicense>) {
this.observable = license$;
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
}

public stop() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}

public getLicenseInformation() {
return this.licenseInformation;
}

public getLicenseInformation$() {
return this.observable;
}

public isGoldPlus() {
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('gold')
);
}
public isEnterprise() {
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('enterprise')
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { LicenseService } from '../../../common/license/license';

export const licenseService = new LicenseService();

export function useLicense() {
return licenseService;
}
5 changes: 2 additions & 3 deletions x-pack/plugins/security_solution/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
IndexFieldsStrategyResponse,
} from '../common/search_strategy/index_fields';
import { SecurityAppStore } from './common/store/store';
import { getCaseConnectorUI } from './common/lib/connectors';
import { licenseService } from './common/hooks/use_license';

export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, StartPlugins> {
private kibanaVersion: string;
Expand Down Expand Up @@ -313,8 +313,6 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
},
});

plugins.triggersActionsUi.actionTypeRegistry.register(getCaseConnectorUI());

return {
resolver: async () => {
/**
Expand All @@ -337,6 +335,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
ConfigureEndpointPackagePolicy
);
}
licenseService.start(plugins.licensing.license$);

return {};
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/security_solution/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Network } from './network';
import { Overview } from './overview';
import { Timelines } from './timelines';
import { Management } from './management';
import { LicensingPluginStart } from '../../licensing/public';

export interface SetupPlugins {
home?: HomePublicPluginSetup;
Expand All @@ -49,6 +50,7 @@ export interface StartPlugins {
inspector: InspectorStart;
ingestManager?: IngestManagerStart;
lists?: ListsPluginStart;
licensing: LicensingPluginStart;
newsfeed?: NewsfeedPublicPluginStart;
triggersActionsUi: TriggersActionsStart;
uiActions: UiActionsStart;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { LicenseService } from '../../../common/license/license';

export const licenseService = new LicenseService();
9 changes: 7 additions & 2 deletions x-pack/plugins/security_solution/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { MlPluginSetup as MlSetup } from '../../ml/server';
import { ListPluginSetup } from '../../lists/server';
import { EncryptedSavedObjectsPluginSetup as EncryptedSavedObjectsSetup } from '../../encrypted_saved_objects/server';
import { SpacesPluginSetup as SpacesSetup } from '../../spaces/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { ILicense, LicensingPluginStart } from '../../licensing/server';
import { IngestManagerStartContract, ExternalCallback } from '../../ingest_manager/server';
import { TaskManagerSetupContract, TaskManagerStartContract } from '../../task_manager/server';
import { initServer } from './init_server';
Expand Down Expand Up @@ -74,13 +74,13 @@ import {
TelemetryPluginStart,
TelemetryPluginSetup,
} from '../../../../src/plugins/telemetry/server';
import { licenseService } from './lib/license/license';

export interface SetupPlugins {
alerts: AlertingSetup;
data: DataPluginSetup;
encryptedSavedObjects?: EncryptedSavedObjectsSetup;
features: FeaturesSetup;
licensing: LicensingPluginSetup;
lists?: ListPluginSetup;
ml?: MlSetup;
security?: SecuritySetup;
Expand All @@ -94,6 +94,7 @@ export interface StartPlugins {
alerts: AlertPluginStartContract;
data: DataPluginStart;
ingestManager?: IngestManagerStartContract;
licensing: LicensingPluginStart;
taskManager?: TaskManagerStartContract;
telemetry?: TelemetryPluginStart;
}
Expand Down Expand Up @@ -125,6 +126,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
private readonly telemetryEventsSender: TelemetryEventsSender;

private lists: ListPluginSetup | undefined; // TODO: can we create ListPluginStart?
private licensing$!: Observable<ILicense>;

private manifestTask: ManifestTask | undefined;
private exceptionsCache: LRU<string, Buffer>;
Expand Down Expand Up @@ -364,6 +366,8 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
}

this.telemetryEventsSender.start(core, plugins.telemetry);
this.licensing$ = plugins.licensing.license$;
licenseService.start(this.licensing$);

return {};
}
Expand All @@ -372,5 +376,6 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
this.logger.debug('Stopping plugin');
this.telemetryEventsSender.stop();
this.endpointAppContextService.stop();
this.licenseService.stop();
}
}

0 comments on commit 22feb08

Please sign in to comment.