Skip to content

Commit

Permalink
address 'event.' fields
Browse files Browse the repository at this point in the history
  • Loading branch information
peluja1012 committed Jul 14, 2020
1 parent 1a3ce47 commit e7ecadc
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 2 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/security_solution/common/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export {
entriesList,
namespaceType,
ExceptionListType,
EntryMatch,
} from '../../lists/common';
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
enrichExceptionItemsWithOS,
entryHasListType,
entryHasNonEcsType,
prepareExceptionItemsForBulkClose,
} from './helpers';
import { FormattedEntry, DescriptionListItem, EmptyEntry } from './types';
import {
Expand All @@ -44,6 +45,7 @@ import {
getEntryListMock,
getEntryMatchMock,
getEntryMatchAnyMock,
getEntryNestedMock,
getEntriesArrayMock,
} from '../../../../../lists/common/schemas/types/entries.mock';
import { getCommentsArrayMock } from '../../../../../lists/common/schemas/types/comments.mock';
Expand Down Expand Up @@ -683,4 +685,112 @@ describe('Exception helpers', () => {
expect(result).toEqual(true);
});
});

describe('#prepareExceptionItemsForBulkClose', () => {
test('it should return no exceptionw when passed in an empty array', () => {
const payload: ExceptionListItemSchema[] = [];
const result = prepareExceptionItemsForBulkClose(payload);
expect(result).toEqual([]);
});

test("should not make any updates when the exception entries don't contain 'event.'", () => {
const payload = [getExceptionListItemSchemaMock(), getExceptionListItemSchemaMock()];
const result = prepareExceptionItemsForBulkClose(payload);
expect(result).toEqual(payload);
});

test("should update entry fields when they start with 'event.'", () => {
const payload = [
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryMatchMock(),
field: 'event.kind',
},
getEntryMatchMock(),
],
},
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryMatchMock(),
field: 'event.module',
},
],
},
];
const expected = [
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryMatchMock(),
field: 'signal.original_event.kind',
},
getEntryMatchMock(),
],
},
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryMatchMock(),
field: 'signal.original_event.module',
},
],
},
];
const result = prepareExceptionItemsForBulkClose(payload);
expect(result).toEqual(expected);
});

test("should update nested entry fields when they start with 'event.'", () => {
const payload: ExceptionListItemSchema[] = [
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryNestedMock(),
field: 'event.kind',
},
{
...getEntryNestedMock(),
entries: [
{
...getEntryMatchMock(),
field: 'event.module',
},
getEntryMatchMock(),
],
},
],
},
];
const expected = [
{
...getExceptionListItemSchemaMock(),
entries: [
{
...getEntryNestedMock(),
field: 'signal.original_event.kind',
},
{
...getEntryNestedMock(),
entries: [
{
...getEntryMatchMock(),
field: 'signal.original_event.module',
},
getEntryMatchMock(),
],
},
],
},
];
const result = prepareExceptionItemsForBulkClose(payload);
expect(result).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
exceptionListItemSchema,
UpdateExceptionListItemSchema,
ExceptionListType,
EntriesArray,
EntryMatch,
} from '../../../lists_plugin_deps';
import { IFieldType, IIndexPattern } from '../../../../../../../src/plugins/data/common';
import { TimelineNonEcsData } from '../../../graphql/types';
Expand Down Expand Up @@ -380,6 +382,53 @@ export const formatExceptionItemForUpdate = (
};
};

/**
* Maps "event." fields to "signal.original_event.". This is because when a rule is created
* the "event" field is copied over to "original_event". When the user creates an exception,
* they expect it to match against the original_event's fields, not the signal event's.
* @param exceptionItems new or existing ExceptionItem[]
*/
export const prepareExceptionItemsForBulkClose = (
exceptionItems: Array<ExceptionListItemSchema | CreateExceptionListItemSchema>
): Array<ExceptionListItemSchema | CreateExceptionListItemSchema> => {
const replaceField = (fieldToReplace: string) => {
return fieldToReplace.startsWith('event.')
? fieldToReplace.replace(/^event./, 'signal.original_event.')
: fieldToReplace;
};

return exceptionItems.map((item: ExceptionListItemSchema | CreateExceptionListItemSchema) => {
if (item.entries !== undefined) {
const newEntries = item.entries.map((itemEntry: EntriesArray[0]) => {
if (itemEntry.type === 'nested') {
const newNestedEntries = itemEntry.entries.map((nestedEntry: EntryMatch) => {
return {
...nestedEntry,
field: replaceField(nestedEntry.field),
};
});
return {
...itemEntry,
field: replaceField(itemEntry.field),
entries: newNestedEntries,
};
} else {
return {
...itemEntry,
field: replaceField(itemEntry.field),
};
}
});
return {
...item,
entries: newEntries,
};
} else {
return item;
}
});
};

/**
* Adds new and existing comments to all new exceptionItems if not present already
* @param exceptionItems new or existing ExceptionItem[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getUpdateAlertsQuery } from '../../../detections/components/alerts_tabl
import { buildAlertStatusFilter } from '../../../detections/components/alerts_table/default_config';
import { getQueryFilter } from '../../../../common/detection_engine/get_query_filter';
import { Index } from '../../../../common/detection_engine/schemas/common/schemas';
import { formatExceptionItemForUpdate } from './helpers';
import { formatExceptionItemForUpdate, prepareExceptionItemsForBulkClose } from './helpers';

/**
* Adds exception items to the list. Also optionally closes alerts.
Expand Down Expand Up @@ -123,7 +123,7 @@ export const useAddOrUpdateException = ({
'kuery',
buildAlertStatusFilter('open'),
bulkCloseIndex,
exceptionItemsToAddOrUpdate,
prepareExceptionItemsForBulkClose(exceptionItemsToAddOrUpdate),
false
);
await updateAlertStatus({
Expand Down

0 comments on commit e7ecadc

Please sign in to comment.