From 474fb1adab5f77ac249280a1edbb5a55d50a3295 Mon Sep 17 00:00:00 2001 From: Tudor Golubenco Date: Tue, 24 Nov 2020 18:23:02 +0100 Subject: [PATCH] First pass at a Security Diagnostics task --- .../server/lib/telemetry/sender.ts | 25 ++++++- .../server/lib/telemetry/task.ts | 70 +++++++++++++++++++ .../security_solution/server/plugin.ts | 4 +- 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/security_solution/server/lib/telemetry/task.ts diff --git a/x-pack/plugins/security_solution/server/lib/telemetry/sender.ts b/x-pack/plugins/security_solution/server/lib/telemetry/sender.ts index 88ce963757f6..50638b28ae73 100644 --- a/x-pack/plugins/security_solution/server/lib/telemetry/sender.ts +++ b/x-pack/plugins/security_solution/server/lib/telemetry/sender.ts @@ -14,6 +14,11 @@ import { TelemetryPluginStart, TelemetryPluginSetup, } from '../../../../../../src/plugins/telemetry/server'; +import { + TaskManagerSetupContract, + TaskManagerStartContract, +} from '../../../../task_manager/server'; +import { TelemetryDiagTask } from './task'; export type SearchTypes = | string @@ -56,24 +61,38 @@ export class TelemetryEventsSender { private isSending = false; private queue: TelemetryEvent[] = []; private isOptedIn?: boolean = true; // Assume true until the first check + private diagTask?: TelemetryDiagTask; constructor(logger: Logger) { this.logger = logger.get('telemetry_events'); } - public setup(telemetrySetup?: TelemetryPluginSetup) { + public setup(telemetrySetup?: TelemetryPluginSetup, taskManager?: TaskManagerSetupContract) { this.telemetrySetup = telemetrySetup; + + if (taskManager) { + this.diagTask = new TelemetryDiagTask(this.logger, taskManager, this); + } } - public start(core?: CoreStart, telemetryStart?: TelemetryPluginStart) { + public start( + core?: CoreStart, + telemetryStart?: TelemetryPluginStart, + taskManager?: TaskManagerStartContract + ) { this.telemetryStart = telemetryStart; this.core = core; - this.logger.debug(`Starting task`); + this.logger.debug(`Starting local task`); setTimeout(() => { this.sendIfDue(); this.intervalId = setInterval(() => this.sendIfDue(), this.checkIntervalMs); }, this.initialCheckDelayMs); + + if (taskManager && this.diagTask) { + this.logger.debug(`Starting diag task`); + this.diagTask.start(taskManager); + } } public stop() { diff --git a/x-pack/plugins/security_solution/server/lib/telemetry/task.ts b/x-pack/plugins/security_solution/server/lib/telemetry/task.ts new file mode 100644 index 000000000000..65097fc58d06 --- /dev/null +++ b/x-pack/plugins/security_solution/server/lib/telemetry/task.ts @@ -0,0 +1,70 @@ +/* + * 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 { Logger } from 'src/core/server'; +import { + ConcreteTaskInstance, + TaskManagerSetupContract, + TaskManagerStartContract, +} from '../../../../task_manager/server'; +import { TelemetryEventsSender } from './sender'; + +export const TelemetryDiagTaskConstants = { + TIMEOUT: '1m', + TYPE: 'security:telemetry-diagnostics', + INTERVAL: '10s', // TODO: update to 5m + VERSION: '1.0.0', +}; + +export class TelemetryDiagTask { + private readonly logger: Logger; + private readonly sender: TelemetryEventsSender; + + constructor( + logger: Logger, + taskManager: TaskManagerSetupContract, + sender: TelemetryEventsSender + ) { + this.logger = logger; + this.sender = sender; + + taskManager.registerTaskDefinitions({ + [TelemetryDiagTaskConstants.TYPE]: { + title: 'Security Solution Telemetry Diagnostics task', + timeout: TelemetryDiagTaskConstants.TIMEOUT, + createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => { + return { + run: async () => { + this.logger.debug('Telemetry Diagnostic task is running'); + this.logger.debug(`Sender pointer: ${this.sender}`); + }, + cancel: async () => {}, + }; + }, + }, + }); + } + + public start = async (taskManager: TaskManagerStartContract) => { + try { + await taskManager.ensureScheduled({ + id: this.getTaskId(), + taskType: TelemetryDiagTaskConstants.TYPE, + scope: ['securitySolution'], + schedule: { + interval: TelemetryDiagTaskConstants.INTERVAL, + }, + state: {}, + params: { version: TelemetryDiagTaskConstants.VERSION }, + }); + } catch (e) { + this.logger.error(`Error scheduling task, received ${e.message}`); + } + }; + + private getTaskId = (): string => { + return `${TelemetryDiagTaskConstants.TYPE}:${TelemetryDiagTaskConstants.VERSION}`; + }; +} diff --git a/x-pack/plugins/security_solution/server/plugin.ts b/x-pack/plugins/security_solution/server/plugin.ts index d963b3b093d8..a5216ac68c49 100644 --- a/x-pack/plugins/security_solution/server/plugin.ts +++ b/x-pack/plugins/security_solution/server/plugin.ts @@ -314,7 +314,7 @@ export class Plugin implements IPlugin