Skip to content

Commit

Permalink
[Security Solution][Alerts] Format alerts for per-alert action contex…
Browse files Browse the repository at this point in the history
…t 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`.
  • Loading branch information
e40pud authored Apr 26, 2023
1 parent 53daa33 commit 8f59720
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper
start: Date.parse(alert[TIMESTAMP]),
end: Date.parse(alert[TIMESTAMP]),
}),
alerts: [alert],
alerts: [formatAlert?.(alert) ?? alert],
})
);

Expand Down Expand Up @@ -387,7 +387,7 @@ export const createPersistenceRuleTypeWrapper: CreatePersistenceRuleTypeWrapper
start: Date.parse(alert[TIMESTAMP]),
end: Date.parse(alert[TIMESTAMP]),
}),
alerts: [alert],
alerts: [formatAlert?.(alert) ?? alert],
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ describe('Expand Dotted', () => {
});
});

it('overwrites earlier fields when later fields conflict', () => {
const simpleDottedObj = {
'kibana.test.1': 'the spice must flow',
'kibana.test': 2,
};
expect(expandDottedObject(simpleDottedObj)).toEqual({
kibana: {
test: 2,
},
});
});

it('expands non dotted field without changing it other than reference', () => {
const simpleDottedObj = {
test: { value: '123' },
Expand Down
20 changes: 6 additions & 14 deletions x-pack/plugins/security_solution/common/utils/expand_dotted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@
* 2.0.
*/

import { merge } from '@kbn/std';

const expandDottedField = (dottedFieldName: string, val: unknown): object => {
const parts = dottedFieldName.split('.');
if (parts.length === 1) {
return { [parts[0]]: val };
} else {
return { [parts[0]]: expandDottedField(parts.slice(1).join('.'), val) };
}
};
import { setWith } from 'lodash';

/*
* Expands an object with "dotted" fields to a nested object with unflattened fields.
Expand Down Expand Up @@ -48,8 +39,9 @@ export const expandDottedObject = (dottedObj: object) => {
if (Array.isArray(dottedObj)) {
return dottedObj;
}
return Object.entries(dottedObj).reduce(
(acc, [key, val]) => merge(acc, expandDottedField(key, val)),
{}
);
const returnObj = {};
Object.entries(dottedObj).forEach(([key, value]) => {
setWith(returnObj, key, value, Object);
});
return returnObj;
};

0 comments on commit 8f59720

Please sign in to comment.