Skip to content

Commit 31a48a6

Browse files
authored
[7.x] [SECURITY SOLUTION] [Detections] Increase lookback when gap is detected (#68339) (#70371)
* add POC logic to modify the 'from' param in the search * fixes formatting for appending gap diff to from * computes new max signals based on how many intervals of rule runs were missed when gap in consecutive rule runs is detected * adds logging, fixes bug where we could end up with negative values for diff, adds calculatedFrom to the search after query * remove console.log and for some reason two eslint disables were added so i removed one of them * rename variables, add test based on log message - need to figure out a better way to test this * remove unused import * fully re-worked the algorithm for searching discrete time periods, still need search_after because a user could submit a rule with a custom maxSignals so that would still serve a purpose. This needs heavy refactoring though, and tests. * updated loop to include maxSignals per time interval tuple, this way we guarantee maxSignals per full rule interval. Needs some refactoring though. * move logic into utils function, utils function still needs refactoring * adds unit tests and cleans up new util function for determining time intervals for searching to occur * more code cleanup * remove more logging statements * fix type errors * updates unit tests and fixes bug where search result would return 0 hits but we were accessing property on non-existent hit item * fix rebase conflict * fixes a bug where a negative gap could exist if a rule ran before the lookback time, also fixes a bug where the search and bulk loop would return false when successful. * gap is a duration, not a number. * remove logging variable * remove logging function from test * fix type import from rebase with master * updates missed test when rebased with master, removes unused import * modify log statements to include meta information for logged rule events, adds tests * remove unnecessary ts-ignores * indentation on stringify * adds a test to ensure we are parsing the elapsed time correctly
1 parent 9fd0530 commit 31a48a6

File tree

8 files changed

+563
-114
lines changed

8 files changed

+563
-114
lines changed

x-pack/plugins/security_solution/server/lib/detection_engine/signals/filter_events_with_list.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
import uuid from 'uuid';
88
import { filterEventsAgainstList } from './filter_events_with_list';
9+
import { buildRuleMessageFactory } from './rule_messages';
910
import { mockLogger, repeatedSearchResultsWithSortId } from './__mocks__/es_results';
1011

1112
import { getExceptionListItemSchemaMock } from '../../../../../lists/common/schemas/response/exception_list_item_schema.mock';
1213
import { getListItemResponseMock } from '../../../../../lists/common/schemas/response/list_item_schema.mock';
1314
import { listMock } from '../../../../../lists/server/mocks';
1415

1516
const someGuids = Array.from({ length: 13 }).map((x) => uuid.v4());
16-
17+
const buildRuleMessage = buildRuleMessageFactory({
18+
id: 'fake id',
19+
ruleId: 'fake rule id',
20+
index: 'fakeindex',
21+
name: 'fake name',
22+
});
1723
describe('filterEventsAgainstList', () => {
1824
let listClient = listMock.getListClient();
1925
beforeEach(() => {
@@ -33,6 +39,7 @@ describe('filterEventsAgainstList', () => {
3339
'3.3.3.3',
3440
'7.7.7.7',
3541
]),
42+
buildRuleMessage,
3643
});
3744
expect(res.hits.hits.length).toEqual(4);
3845
});
@@ -57,6 +64,7 @@ describe('filterEventsAgainstList', () => {
5764
listClient,
5865
exceptionsList: [exceptionItem],
5966
eventSearchResult: repeatedSearchResultsWithSortId(4, 4, someGuids.slice(0, 3)),
67+
buildRuleMessage,
6068
});
6169
expect(res.hits.hits.length).toEqual(4);
6270
});
@@ -91,6 +99,7 @@ describe('filterEventsAgainstList', () => {
9199
'3.3.3.3',
92100
'7.7.7.7',
93101
]),
102+
buildRuleMessage,
94103
});
95104
expect((listClient.getListItemByValues as jest.Mock).mock.calls[0][0].type).toEqual('ip');
96105
expect((listClient.getListItemByValues as jest.Mock).mock.calls[0][0].listId).toEqual(
@@ -118,6 +127,7 @@ describe('filterEventsAgainstList', () => {
118127
listClient,
119128
exceptionsList: [exceptionItem],
120129
eventSearchResult: repeatedSearchResultsWithSortId(4, 4, someGuids.slice(0, 3)),
130+
buildRuleMessage,
121131
});
122132
expect(res.hits.hits.length).toEqual(0);
123133
});
@@ -152,6 +162,7 @@ describe('filterEventsAgainstList', () => {
152162
'3.3.3.3',
153163
'7.7.7.7',
154164
]),
165+
buildRuleMessage,
155166
});
156167
expect((listClient.getListItemByValues as jest.Mock).mock.calls[0][0].type).toEqual('ip');
157168
expect((listClient.getListItemByValues as jest.Mock).mock.calls[0][0].listId).toEqual(

x-pack/plugins/security_solution/server/lib/detection_engine/signals/filter_events_with_list.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Logger } from 'src/core/server';
88

99
import { ListClient } from '../../../../../lists/server';
1010
import { SignalSearchResponse, SearchTypes } from './types';
11+
import { BuildRuleMessage } from './rule_messages';
1112
import {
1213
entriesList,
1314
EntryList,
@@ -19,16 +20,20 @@ interface FilterEventsAgainstList {
1920
exceptionsList: ExceptionListItemSchema[];
2021
logger: Logger;
2122
eventSearchResult: SignalSearchResponse;
23+
buildRuleMessage: BuildRuleMessage;
2224
}
2325

2426
export const filterEventsAgainstList = async ({
2527
listClient,
2628
exceptionsList,
2729
logger,
2830
eventSearchResult,
31+
buildRuleMessage,
2932
}: FilterEventsAgainstList): Promise<SignalSearchResponse> => {
3033
try {
34+
logger.debug(buildRuleMessage(`exceptionsList: ${JSON.stringify(exceptionsList, null, 2)}`));
3135
if (exceptionsList == null || exceptionsList.length === 0) {
36+
logger.debug(buildRuleMessage('about to return original search result'));
3237
return eventSearchResult;
3338
}
3439

@@ -86,7 +91,7 @@ export const filterEventsAgainstList = async ({
8691
return false;
8792
});
8893
const diff = eventSearchResult.hits.hits.length - filteredEvents.length;
89-
logger.debug(`Lists filtered out ${diff} events`);
94+
logger.debug(buildRuleMessage(`Lists filtered out ${diff} events`));
9095
return filteredEvents;
9196
});
9297

0 commit comments

Comments
 (0)