Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution][Detections] Implement hybrid approach to writing rule execution event logs #114852

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import { sum } from 'lodash';
import { SavedObjectsClientContract } from '../../../../../../../../src/core/server';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can be imported as src/core/server or kibana/server. I think kibana/server is stricter in terms of what can be imported for some reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually don't write import statements manually. Instead, VS Code adds them automatically. In this case it thinks that '../../../../../../../../src/core/server' is the best place to import SavedObjectsClientContract from, so who am I to argue 🙂

import { IEventLogService } from '../../../../../../event_log/server';
import { SavedObjectsAdapter } from '../saved_objects_adapter/saved_objects_adapter';
import {
FindBulkExecutionLogArgs,
FindExecutionLogArgs,
Expand All @@ -18,21 +21,32 @@ import { EventLogClient } from './event_log_client';

export class EventLogAdapter implements IRuleExecutionLogClient {
private eventLogClient: EventLogClient;
/**
* @deprecated Saved objects adapter is used during the transition period while the event log doesn't support all features needed to implement the execution log.
* We use savedObjectsAdapter to write/read the latest rule execution status and eventLogClient to read/write historical execution data.
* We can remove savedObjectsAdapter as soon as the event log supports all methods that we need (find, findBulk).
*/
private savedObjectsAdapter: IRuleExecutionLogClient;

constructor(eventLogService: IEventLogService) {
constructor(eventLogService: IEventLogService, savedObjectsClient: SavedObjectsClientContract) {
this.eventLogClient = new EventLogClient(eventLogService);
this.savedObjectsAdapter = new SavedObjectsAdapter(savedObjectsClient);
}

public async find({ ruleId, logsCount = 1, spaceId }: FindExecutionLogArgs) {
return []; // TODO Implement
public async find(args: FindExecutionLogArgs) {
return this.savedObjectsAdapter.find(args);
}

public async findBulk({ ruleIds, logsCount = 1, spaceId }: FindBulkExecutionLogArgs) {
return {}; // TODO Implement
public async findBulk(args: FindBulkExecutionLogArgs) {
return this.savedObjectsAdapter.findBulk(args);
}

public async update({ attributes, spaceId, ruleName, ruleType }: UpdateExecutionLogArgs) {
// execution events are immutable, so we just log a status change istead of updating previous
public async update(args: UpdateExecutionLogArgs) {
const { attributes, spaceId, ruleName, ruleType } = args;

await this.savedObjectsAdapter.update(args);

// EventLog execution events are immutable, so we just log a status change istead of updating previous
if (attributes.status) {
this.eventLogClient.logStatusChange({
ruleName,
Expand All @@ -45,33 +59,35 @@ export class EventLogAdapter implements IRuleExecutionLogClient {
}

public async delete(id: string) {
// execution events are immutable, nothing to do here
await this.savedObjectsAdapter.delete(id);

// EventLog execution events are immutable, nothing to do here
}

public async logExecutionMetrics({
ruleId,
spaceId,
ruleType,
ruleName,
metrics,
}: LogExecutionMetricsArgs) {
public async logExecutionMetrics(args: LogExecutionMetricsArgs) {
const { ruleId, spaceId, ruleType, ruleName, metrics } = args;
await this.savedObjectsAdapter.logExecutionMetrics(args);

this.eventLogClient.logExecutionMetrics({
ruleId,
ruleName,
ruleType,
spaceId,
metrics: {
executionGapDuration: metrics.executionGap?.asSeconds(),
totalIndexingDuration: metrics.indexingDurations?.reduce(
(acc, cur) => acc + Number(cur),
0
),
totalSearchDuration: metrics.searchDurations?.reduce((acc, cur) => acc + Number(cur), 0),
totalIndexingDuration: metrics.indexingDurations
? sum(metrics.indexingDurations.map(Number))
: undefined,
totalSearchDuration: metrics.searchDurations
? sum(metrics.searchDurations.map(Number))
: undefined,
},
});
}

public async logStatusChange(args: LogStatusChangeArgs) {
await this.savedObjectsAdapter.logStatusChange(args);

if (args.metrics) {
this.logExecutionMetrics({
ruleId: args.ruleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class RuleExecutionLogClient implements IRuleExecutionLogClient {
this.client = new SavedObjectsAdapter(savedObjectsClient);
break;
case UnderlyingLogClient.eventLog:
this.client = new EventLogAdapter(eventLogService);
this.client = new EventLogAdapter(eventLogService, savedObjectsClient);
break;
}
}
Expand Down