From 4262c5971c6d90bbdcc562bad13cd9a9fedc6c7f Mon Sep 17 00:00:00 2001 From: Davis Plumlee Date: Thu, 9 Jul 2020 14:34:27 -0400 Subject: [PATCH] comments --- .../exceptions/add_exception_modal/index.tsx | 24 +++++++++++-------- .../components/exceptions/helpers.test.tsx | 24 +++++++++++++++++++ .../common/components/exceptions/helpers.tsx | 12 +++++----- .../alerts_table/default_config.tsx | 2 +- .../components/alerts_table/index.tsx | 2 +- 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx index 69a8840c63e78..10d510c5f56c3 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/add_exception_modal/index.tsx @@ -66,7 +66,7 @@ interface AddExceptionModalProps { nonEcsData: TimelineNonEcsData[]; }; onCancel: () => void; - onConfirm: (didCloseAlert?: boolean) => void; + onConfirm: (didCloseAlert: boolean) => void; } const Modal = styled(EuiModal)` @@ -222,14 +222,18 @@ export const AddExceptionModal = memo(function AddExceptionModal({ ); const retrieveAlertOsTypes = useCallback(() => { - const osTypes = getMappedNonEcsValue({ - data: alertData.nonEcsData, - fieldName: 'host.os.family', - }); - if (osTypes.length === 0) { - return ['windows', 'macos', 'linux']; + const osDefaults = ['windows', 'macos', 'linux']; + if (alertData) { + const osTypes = getMappedNonEcsValue({ + data: alertData.nonEcsData, + fieldName: 'host.os.family', + }); + if (osTypes.length === 0) { + return osDefaults; + } + return osTypes; } - return osTypes; + return osDefaults; }, [alertData]); const enrichExceptionItems = useCallback(() => { @@ -239,11 +243,11 @@ export const AddExceptionModal = memo(function AddExceptionModal({ ? enrichExceptionItemsWithComments(exceptionItemsToAdd, [{ comment }]) : exceptionItemsToAdd; if (exceptionListType === 'endpoint') { - const osTypes = alertData ? retrieveAlertOsTypes() : ['windows', 'macos', 'linux']; + const osTypes = retrieveAlertOsTypes(); enriched = enrichExceptionItemsWithOS(enriched, osTypes); } return enriched; - }, [comment, exceptionItemsToAdd, exceptionListType, alertData, retrieveAlertOsTypes]); + }, [comment, exceptionItemsToAdd, exceptionListType, retrieveAlertOsTypes]); const onAddExceptionConfirm = useCallback(() => { if (addOrUpdateExceptionItems !== null) { diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.test.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.test.tsx index d362fe1f9aaeb..36d230904ea63 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.test.tsx @@ -317,6 +317,30 @@ describe('Exception helpers', () => { }); describe('#getOperatingSystems', () => { + test('it returns null if no operating system tag specified', () => { + const result = getOperatingSystems(['some tag', 'some other tag']); + + expect(result).toEqual(''); + }); + + test('it returns null if operating system tag malformed', () => { + const result = getOperatingSystems(['some tag', 'jibberos:mac,windows', 'some other tag']); + + expect(result).toEqual(''); + }); + + test('it returns operating systems if space included in os tag', () => { + const result = getOperatingSystems(['some tag', 'os: macos', 'some other tag']); + expect(result).toEqual(['macos']); + }); + + test('it returns operating systems if multiple os tags specified', () => { + const result = getOperatingSystems(['some tag', 'os: macos', 'some other tag', 'os:windows']); + expect(result).toEqual(['macos', 'windows']); + }); + }); + + describe('#formatOperatingSystems', () => { test('it returns null if no operating system tag specified', () => { const result = formatOperatingSystems(getOperatingSystems(['some tag', 'some other tag'])); diff --git a/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.tsx b/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.tsx index 93ac173dc2e7c..8492a6f02b1f5 100644 --- a/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.tsx +++ b/x-pack/plugins/security_solution/public/common/components/exceptions/helpers.tsx @@ -453,6 +453,10 @@ export const entryHasNonEcsType = ( exceptionItems: Array, indexPatterns: IIndexPattern ): boolean => { + const doesFieldNameExist = (exceptionEntry: Entry): boolean => { + return indexPatterns.fields.some(({ name }) => name === exceptionEntry.field); + }; + if (exceptionItems.length === 0) { return false; } @@ -460,20 +464,16 @@ export const entryHasNonEcsType = ( for (const exceptionEntry of entries ?? []) { if (exceptionEntry.type === 'nested') { for (const nestedExceptionEntry of exceptionEntry.entries) { - if (doesFieldNameExist(nestedExceptionEntry) === true) { + if (doesFieldNameExist(nestedExceptionEntry) === false) { return true; } } - } else if (doesFieldNameExist(exceptionEntry) === true) { + } else if (doesFieldNameExist(exceptionEntry) === false) { return true; } } } return false; - - function doesFieldNameExist(exceptionEntry: Entry) { - return indexPatterns.fields.find(({ name }) => name === exceptionEntry.field) === undefined; - } }; /** diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx index cfc0bd2e684ad..e95ea4531d9ad 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/default_config.tsx @@ -338,7 +338,7 @@ export const getAlertActions = ({ } }, id: 'addEndpointException', - isActionDisabled: () => !canUserCRUD || !hasIndexWrite || isEndpointAlert() === false, + isActionDisabled: () => !canUserCRUD || !hasIndexWrite || !isEndpointAlert(), dataTestSubj: 'add-endpoint-exception-menu-item', ariaLabel: 'Add Endpoint Exception', content: {i18n.ACTION_ADD_ENDPOINT_EXCEPTION}, diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx index 07d30c41e47d9..b9b963a84e966 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/index.tsx @@ -405,7 +405,7 @@ export const AlertsTableComponent: React.FC = ({ }, [closeAddExceptionModal]); const onAddExceptionConfirm = useCallback( - (didCloseAlert?: boolean) => { + (didCloseAlert: boolean) => { closeAddExceptionModal(); }, [closeAddExceptionModal]