Skip to content

Commit c048093

Browse files
committed
update tests
1 parent bdc2414 commit c048093

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ describe('reason_formatter', () => {
160160
);
161161
});
162162
});
163+
describe('when rule and mergedDoc are provided without any fields of interest', () => {
164+
it('should return the full reason message', () => {
165+
const updatedMergedDoc = {
166+
fields: {
167+
'event.category': ['test'],
168+
'user.name': ['test-user'],
169+
'@timestamp': '2021-08-11T02:28:59.101Z',
170+
},
171+
};
172+
expect(buildReasonMessageUtil({ rule, mergedDoc: updatedMergedDoc })).toMatchInlineSnapshot(
173+
`"test event by test-user created medium alert my-rule."`
174+
);
175+
});
176+
});
163177
describe('when only rule is provided', () => {
164178
it('should return the reason message without host name or user name', () => {
165179
expect(buildReasonMessageUtil({ rule })).toMatchInlineSnapshot(`""`);

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ export const buildReasonMessageUtil = ({ rule, mergedDoc }: BuildReasonMessageUt
5656

5757
const fieldPresenceTracker = { hasFieldOfInterest: false };
5858

59-
const getFieldTemplateValue = (field: string | string[] | undefined | null): string => {
60-
if (!field || !field.length || (field.length === 1 && field[0] === '-')) return 'null';
61-
if (!fieldPresenceTracker.hasFieldOfInterest) fieldPresenceTracker.hasFieldOfInterest = true;
59+
const getFieldTemplateValue = (
60+
field: string | string[] | undefined | null,
61+
isFieldOfInterest?: boolean
62+
): string | null => {
63+
if (!field || !field.length || (field.length === 1 && field[0] === '-')) return null;
64+
if (isFieldOfInterest && !fieldPresenceTracker.hasFieldOfInterest)
65+
fieldPresenceTracker.hasFieldOfInterest = true;
6266
return Array.isArray(field) ? field.join(', ') : field;
6367
};
6468

6569
return i18n.translate('xpack.securitySolution.detectionEngine.signals.alertReasonDescription', {
6670
defaultMessage: `{eventCategory, select, null {} other {{eventCategory}{whitespace}}}event\
67-
{hasFieldOfInterest, select, null {} other {{whitespace}with}}\
71+
{hasFieldOfInterest, select, false {} other {{whitespace}with}}\
6872
{processName, select, null {} other {{whitespace}process {processName},} }\
6973
{processParentName, select, null {} other {{whitespace}parent process {processParentName},} }\
7074
{fileName, select, null {} other {{whitespace}file {fileName},} }\
@@ -76,15 +80,15 @@ created {alertSeverity} alert {alertName}.`,
7680
values: {
7781
alertName: rule.name,
7882
alertSeverity: rule.severity,
79-
destinationAddress: getFieldTemplateValue(destinationAddress),
80-
destinationPort: getFieldTemplateValue(destinationPort),
83+
destinationAddress: getFieldTemplateValue(destinationAddress, true),
84+
destinationPort: getFieldTemplateValue(destinationPort, true),
8185
eventCategory: getFieldTemplateValue(eventCategory),
82-
fileName: getFieldTemplateValue(fileName),
86+
fileName: getFieldTemplateValue(fileName, true),
8387
hostName: getFieldTemplateValue(hostName),
84-
processName: getFieldTemplateValue(processName),
85-
processParentName: getFieldTemplateValue(processParentName),
86-
sourceAddress: getFieldTemplateValue(sourceAddress),
87-
sourcePort: getFieldTemplateValue(sourcePort),
88+
processName: getFieldTemplateValue(processName, true),
89+
processParentName: getFieldTemplateValue(processParentName, true),
90+
sourceAddress: getFieldTemplateValue(sourceAddress, true),
91+
sourcePort: getFieldTemplateValue(sourcePort, true),
8892
userName: getFieldTemplateValue(userName),
8993
hasFieldOfInterest: fieldPresenceTracker.hasFieldOfInterest, // Tracking if we have any fields to show the 'with' word
9094
whitespace: ' ', // there isn't support for the unicode /u0020 for whitespace, and leading spaces are deleted, so to prevent double-whitespace explicitly passing the space in.

0 commit comments

Comments
 (0)