Skip to content

Commit b95fa2b

Browse files
committed
[Security Solution][Alerts] Format alerts for per-alert action context variables (#155829)
## Summary Closes [#155812](#155812) In #155384, detection rules were switched to support per-alert actions. When passing the context variable, it was suggested that we should be calling formatAlert to format the alert for notifications, however doing that causes some test failures because formatAlert is fairly heavyweight and bunch of tests were timing out. Thanks to @marshallmain we have this much faster `expandDottedObject` that solves the issue with the very slow `formatAlert`. (cherry picked from commit 8f59720)
1 parent 02fefd6 commit b95fa2b

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

x-pack/plugins/rule_registry/server/utils/create_persistence_rule_type_wrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper
195195
start: Date.parse(alert[TIMESTAMP]),
196196
end: Date.parse(alert[TIMESTAMP]),
197197
}),
198-
alerts: [alert],
198+
alerts: [formatAlert?.(alert) ?? alert],
199199
})
200200
);
201201

@@ -387,7 +387,7 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper
387387
start: Date.parse(alert[TIMESTAMP]),
388388
end: Date.parse(alert[TIMESTAMP]),
389389
}),
390-
alerts: [alert],
390+
alerts: [formatAlert?.(alert) ?? alert],
391391
})
392392
);
393393

x-pack/plugins/security_solution/common/utils/expand_dotted.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ describe('Expand Dotted', () => {
7070
});
7171
});
7272

73+
it('overwrites earlier fields when later fields conflict', () => {
74+
const simpleDottedObj = {
75+
'kibana.test.1': 'the spice must flow',
76+
'kibana.test': 2,
77+
};
78+
expect(expandDottedObject(simpleDottedObj)).toEqual({
79+
kibana: {
80+
test: 2,
81+
},
82+
});
83+
});
84+
7385
it('expands non dotted field without changing it other than reference', () => {
7486
const simpleDottedObj = {
7587
test: { value: '123' },

x-pack/plugins/security_solution/common/utils/expand_dotted.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@
55
* 2.0.
66
*/
77

8-
import { merge } from '@kbn/std';
9-
10-
const expandDottedField = (dottedFieldName: string, val: unknown): object => {
11-
const parts = dottedFieldName.split('.');
12-
if (parts.length === 1) {
13-
return { [parts[0]]: val };
14-
} else {
15-
return { [parts[0]]: expandDottedField(parts.slice(1).join('.'), val) };
16-
}
17-
};
8+
import { setWith } from 'lodash';
189

1910
/*
2011
* Expands an object with "dotted" fields to a nested object with unflattened fields.
@@ -48,8 +39,9 @@ export const expandDottedObject = (dottedObj: object) => {
4839
if (Array.isArray(dottedObj)) {
4940
return dottedObj;
5041
}
51-
return Object.entries(dottedObj).reduce(
52-
(acc, [key, val]) => merge(acc, expandDottedField(key, val)),
53-
{}
54-
);
42+
const returnObj = {};
43+
Object.entries(dottedObj).forEach(([key, value]) => {
44+
setWith(returnObj, key, value, Object);
45+
});
46+
return returnObj;
5547
};

0 commit comments

Comments
 (0)