Skip to content

Commit

Permalink
First pass at a Security Diagnostics task
Browse files Browse the repository at this point in the history
  • Loading branch information
tsg committed Nov 24, 2020
1 parent e4ff981 commit 474fb1a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
25 changes: 22 additions & 3 deletions x-pack/plugins/security_solution/server/lib/telemetry/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
70 changes: 70 additions & 0 deletions x-pack/plugins/security_solution/server/lib/telemetry/task.ts
Original file line number Diff line number Diff line change
@@ -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}`;
};
}
4 changes: 2 additions & 2 deletions x-pack/plugins/security_solution/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
);
});

this.telemetryEventsSender.setup(plugins.telemetry);
this.telemetryEventsSender.setup(plugins.telemetry, plugins.taskManager);

return {};
}
Expand Down Expand Up @@ -367,7 +367,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
this.logger.debug('User artifacts task not available.');
}

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

Expand Down

0 comments on commit 474fb1a

Please sign in to comment.