Skip to content

Commit 7dc1e97

Browse files
committed
Finish tests
1 parent ad495ab commit 7dc1e97

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import uuidv5 from 'uuid/v5';
87
import { loggingSystemMock } from '../../../../../../../src/core/server/mocks';
98
import { sampleDocNoSortId, sampleDocSearchResultsNoSortId } from './__mocks__/es_results';
10-
import { NAMESPACE_ID, transformThresholdResultsToEcs } from './bulk_create_threshold_signals';
9+
import { transformThresholdResultsToEcs } from './bulk_create_threshold_signals';
10+
import { calculateThresholdSignalUuid } from './utils';
1111

12-
describe('', () => {
12+
describe('transformThresholdResultsToEcs', () => {
1313
it('should return transformed threshold results', () => {
1414
const threshold = {
1515
field: 'source.ip',
@@ -40,10 +40,10 @@ describe('', () => {
4040
undefined,
4141
loggingSystemMock.createLogger(),
4242
threshold,
43-
'abcd',
43+
'1234',
4444
undefined
4545
);
46-
const _id = uuidv5(`abcd${startedAt}source.ip127.0.0.1`, NAMESPACE_ID);
46+
const _id = calculateThresholdSignalUuid('1234', startedAt, 'source.ip', '127.0.0.1');
4747
expect(transformedResults).toEqual({
4848
took: 10,
4949
timed_out: false,

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import uuidv5 from 'uuid/v5';
87
import { get, isEmpty } from 'lodash/fp';
98
import set from 'set-value';
109

@@ -18,11 +17,9 @@ import { RuleAlertAction } from '../../../../common/detection_engine/types';
1817
import { RuleTypeParams, RefreshTypes } from '../types';
1918
import { singleBulkCreate, SingleBulkCreateResponse } from './single_bulk_create';
2019
import { SignalSearchResponse, ThresholdAggregationBucket } from './types';
20+
import { calculateThresholdSignalUuid } from './utils';
2121
import { BuildRuleMessage } from './rule_messages';
2222

23-
// used to generate constant Threshold Signals ID when run with the same params
24-
export const NAMESPACE_ID = '0684ec03-7201-4ee0-8ee0-3a3f6b2479b2';
25-
2623
interface BulkCreateThresholdSignalsParams {
2724
actions: RuleAlertAction[];
2825
someResult: SignalSearchResponse;
@@ -83,7 +80,7 @@ const getTransformedHits = (
8380
return [
8481
{
8582
_index: inputIndex,
86-
_id: uuidv5(`${ruleId}${startedAt}${threshold.field}`, NAMESPACE_ID),
83+
_id: calculateThresholdSignalUuid(ruleId, startedAt, threshold.field),
8784
_source: source,
8885
},
8986
];
@@ -111,7 +108,7 @@ const getTransformedHits = (
111108

112109
return {
113110
_index: inputIndex,
114-
_id: uuidv5(`${ruleId}${startedAt}${threshold.field}${key}`, NAMESPACE_ID),
111+
_id: calculateThresholdSignalUuid(ruleId, startedAt, threshold.field, key),
115112
_source: source,
116113
};
117114
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
mergeReturns,
3737
createTotalHitsFromSearchResult,
3838
lastValidDate,
39+
calculateThresholdSignalUuid,
3940
} from './utils';
4041
import { BulkResponseErrorAggregation, SearchAfterAndBulkCreateReturnType } from './types';
4142
import {
@@ -1303,4 +1304,18 @@ describe('utils', () => {
13031304
expect(result).toEqual(4);
13041305
});
13051306
});
1307+
1308+
describe('calculateThresholdSignalUuid', () => {
1309+
it('should generate a uuid without key', () => {
1310+
const startedAt = new Date('2020-12-17T16:27:00Z');
1311+
const signalUuid = calculateThresholdSignalUuid('abcd', startedAt, 'agent.name');
1312+
expect(signalUuid).toEqual('c0cbe4b7-48de-5734-ae81-d8de3e79839d');
1313+
});
1314+
1315+
it('should generate a uuid with key', () => {
1316+
const startedAt = new Date('2019-11-18T13:32:00Z');
1317+
const signalUuid = calculateThresholdSignalUuid('abcd', startedAt, 'host.ip', '1.2.3.4');
1318+
expect(signalUuid).toEqual('f568509e-b570-5d3c-a7ed-7c73fd29ddaf');
1319+
});
1320+
});
13061321
});

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { createHash } from 'crypto';
77
import moment from 'moment';
8+
import uuidv5 from 'uuid/v5';
89
import dateMath from '@elastic/datemath';
910

1011
import { TimestampOverrideOrUndefined } from '../../../../common/detection_engine/schemas/common/schemas';
@@ -661,3 +662,20 @@ export const createTotalHitsFromSearchResult = ({
661662
: searchResult.hits.total.value;
662663
return totalHits;
663664
};
665+
666+
export const calculateThresholdSignalUuid = (
667+
ruleId: string,
668+
startedAt: Date,
669+
thresholdField: string,
670+
key?: string
671+
): string => {
672+
// used to generate constant Threshold Signals ID when run with the same params
673+
const NAMESPACE_ID = '0684ec03-7201-4ee0-8ee0-3a3f6b2479b2';
674+
675+
let baseString = `${ruleId}${startedAt}${thresholdField}`;
676+
if (key != null) {
677+
baseString = `${baseString}${key}`;
678+
}
679+
680+
return uuidv5(baseString, NAMESPACE_ID);
681+
};

0 commit comments

Comments
 (0)