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

Add Internal Alert/Signal ID to Endpoint Alert telemetry #126216

Merged
merged 12 commits into from
Mar 3, 2022
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 @@ -176,7 +176,13 @@ export const searchAfterAndBulkCreate = async ({
buildRuleMessage(`enrichedEvents.hits.hits: ${enrichedEvents.hits.hits.length}`)
);

sendAlertTelemetryEvents(logger, eventsTelemetry, enrichedEvents, buildRuleMessage);
sendAlertTelemetryEvents(
logger,
eventsTelemetry,
enrichedEvents,
createdItems,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are the response objects generated after the signals are created.

buildRuleMessage
);
}

if (!hasSortId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { selectEvents } from './send_telemetry_events';
import { selectEvents, enrichEndpointAlertsSignalID } from './send_telemetry_events';

describe('sendAlertTelemetry', () => {
it('selectEvents', () => {
Expand Down Expand Up @@ -33,6 +33,9 @@ describe('sendAlertTelemetry', () => {
data_stream: {
dataset: 'endpoint.events',
},
event: {
id: 'foo',
},
},
},
{
Expand All @@ -47,6 +50,9 @@ describe('sendAlertTelemetry', () => {
dataset: 'endpoint.alerts',
other: 'x',
},
event: {
id: 'bar',
},
},
},
{
Expand All @@ -58,13 +64,52 @@ describe('sendAlertTelemetry', () => {
'@timestamp': 'x',
key3: 'hello',
data_stream: {},
event: {
id: 'baz',
},
donaherc marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
_index: 'y',
_type: 'y',
_id: 'y',
_score: 0,
_source: {
'@timestamp': 'y',
key3: 'hello',
data_stream: {
dataset: 'endpoint.alerts',
other: 'y',
},
event: {
id: 'not-in-map',
},
},
},
{
_index: 'z',
_type: 'z',
_id: 'z',
_score: 0,
_source: {
'@timestamp': 'z',
key3: 'no-event-id',
data_stream: {
dataset: 'endpoint.alerts',
other: 'z',
},
},
},
],
},
};

const sources = selectEvents(filteredEvents);
const joinMap = new Map<string, string>([
['foo', '1234'],
['bar', 'abcd'],
['baz', '4567'],
]);
const subsetEvents = selectEvents(filteredEvents);
const sources = enrichEndpointAlertsSignalID(subsetEvents, joinMap);
expect(sources).toStrictEqual([
{
'@timestamp': 'x',
Expand All @@ -73,6 +118,31 @@ describe('sendAlertTelemetry', () => {
dataset: 'endpoint.alerts',
other: 'x',
},
event: {
id: 'bar',
},
signal_id: 'abcd',
},
{
'@timestamp': 'y',
key3: 'hello',
data_stream: {
dataset: 'endpoint.alerts',
other: 'y',
},
event: {
id: 'not-in-map',
},
signal_id: undefined,
},
{
'@timestamp': 'z',
key3: 'no-event-id',
data_stream: {
dataset: 'endpoint.alerts',
other: 'z',
},
signal_id: undefined,
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import { BuildRuleMessage } from './rule_messages';
import { SignalSearchResponse, SignalSource } from './types';
import { Logger } from '../../../../../../../src/core/server';

export interface SearchResultWithSource {
interface SearchResultSource {
_source: SignalSource;
}

type CreatedSignalId = string;
type AlertId = string;

export function selectEvents(filteredEvents: SignalSearchResponse): TelemetryEvent[] {
// @ts-expect-error @elastic/elasticsearch _source is optional
const sources: TelemetryEvent[] = filteredEvents.hits.hits.map(function (
obj: SearchResultWithSource
obj: SearchResultSource
): TelemetryEvent {
return obj._source;
});
Expand All @@ -27,20 +30,49 @@ export function selectEvents(filteredEvents: SignalSearchResponse): TelemetryEve
return sources.filter((obj: TelemetryEvent) => obj.data_stream?.dataset === 'endpoint.alerts');
}

export function enrichEndpointAlertsSignalID(
events: TelemetryEvent[],
signalIdMap: Map<string, string>
): TelemetryEvent[] {
return events.map(function (obj: TelemetryEvent): TelemetryEvent {
obj.signal_id = undefined;
if (obj?.event?.id !== undefined) {
obj.signal_id = signalIdMap.get(obj.event.id);
}
return obj;
});
}

export function sendAlertTelemetryEvents(
logger: Logger,
eventsTelemetry: ITelemetryEventsSender | undefined,
filteredEvents: SignalSearchResponse,
createdEvents: SignalSource[],
buildRuleMessage: BuildRuleMessage
) {
if (eventsTelemetry === undefined) {
return;
}

const sources = selectEvents(filteredEvents);
let selectedEvents = selectEvents(filteredEvents);
if (selectedEvents.length > 0) {
// Create map of ancenstor_id -> alert_id
let signalIdMap = new Map<CreatedSignalId, AlertId>();
/* eslint-disable no-param-reassign */
signalIdMap = createdEvents.reduce((signalMap, obj) => {
const ancestorId = obj['kibana.alert.original_event.id']?.toString();
const alertId = obj._id?.toString();
if (ancestorId !== null && ancestorId !== undefined && alertId !== undefined) {
signalMap = signalIdMap.set(ancestorId, alertId);
}

return signalMap;
}, new Map<CreatedSignalId, AlertId>());

selectedEvents = enrichEndpointAlertsSignalID(selectedEvents, signalIdMap);
}
try {
eventsTelemetry.queueTelemetryEvents(sources);
eventsTelemetry.queueTelemetryEvents(selectedEvents);
} catch (exc) {
logger.error(buildRuleMessage(`[-] queing telemetry events failed ${exc}`));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const allowlistBaseEventFields: AllowlistFields = {
export const allowlistEventFields: AllowlistFields = {
_id: true,
'@timestamp': true,
signal_id: true,
agent: true,
Endpoint: true,
/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('TelemetryEventsSender', () => {
{
event: {
kind: 'alert',
id: 'test',
},
dns: {
question: {
Expand Down Expand Up @@ -108,6 +109,7 @@ describe('TelemetryEventsSender', () => {
{
event: {
kind: 'alert',
id: 'test',
},
dns: {
question: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface TelemetryEvent {
};
};
license?: ESLicense;
event?: {
id?: string;
kind?: string;
};
}

// EP Policy Response
Expand Down