Skip to content

Commit

Permalink
start/stop subscription mgmt
Browse files Browse the repository at this point in the history
little cleanups
  • Loading branch information
pzl committed Nov 25, 2020
1 parent a8376be commit 30a0516
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Subscription } from 'rxjs';

import {
KibanaRequest,
Logger,
Expand All @@ -21,7 +24,8 @@ import { licenseService } from '../../../lib/license/license';
export class PolicyWatcher {
private logger: Logger;
private soClient: SavedObjectsClientContract;
private policyService?: PackagePolicyServiceInterface;
private policyService: PackagePolicyServiceInterface;
private subscription: Subscription | undefined;
constructor(
policyService: PackagePolicyServiceInterface,
soStart: SavedObjectsServiceStart,
Expand All @@ -44,6 +48,16 @@ export class PolicyWatcher {
return soStart.getScopedClient(fakeRequest, { excludedWrappers: ['security'] });
}

public start() {
this.subscription = licenseService.getLicenseInformation$()?.subscribe(this.watch.bind(this));
}

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

public async watch(license: ILicense) {
let packagePolicies: PackagePolicy[];
if (!this.policyService) {
Expand All @@ -53,6 +67,7 @@ export class PolicyWatcher {
return;
}

// @todo: actually page and fetch them ALL
try {
packagePolicies = (
await this.policyService.list(this.soClient, {
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/security_solution/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S

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

private manifestTask: ManifestTask | undefined;
private exceptionsCache: LRU<string, Buffer>;
Expand Down Expand Up @@ -371,16 +372,20 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
this.telemetryEventsSender.start(core, plugins.telemetry);
this.licensing$ = plugins.licensing.license$;
licenseService.start(this.licensing$);
const policyWatcher = new PolicyWatcher(plugins.fleet?.packagePolicyService);
licenseService.getLicenseInformation$()?.subscribe(policyWatcher.watch);

this.policyWatcher = new PolicyWatcher(
plugins.fleet!.packagePolicyService,
core.savedObjects,
this.logger
);
this.policyWatcher.start();
return {};
}

public stop() {
this.logger.debug('Stopping plugin');
this.telemetryEventsSender.stop();
this.endpointAppContextService.stop();
this.policyWatcher?.stop();
licenseService.stop();
}
}

0 comments on commit 30a0516

Please sign in to comment.