From 7f3ec3f1e48aabb6a192e60139935308f3cba785 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Tue, 15 Sep 2020 12:29:49 -0500 Subject: [PATCH] [Security Solution] [Detections] EQL Rule Creation (#76831) * Use existing predicate helper to avoid hardcoded strings * Render our field components with React.createElement Without this, we get some bad behaviors: * Cannot use React.memo'd components * Cannot switch between UseField components (causes a "change in the order of hooks" error from React) * WIP: EQL Rules can be created WIP because: they're probably not treated well in the UI, and they're certainly not going to execute properly, and there are no tests. * Add unit tests for changes to schema + helpers * Add unit tests for new EQL query input component It's mostly just a glorified textarea for now. * Add integration test for EQL Rule creation * Does not assert the query language, as that is not displayed on Rule Details * Does not exercise rule execution * Use predicate helper * Throw an error if an EQL Rule is executed This is to prevent undefined behavior until EQL execution is implemented. * Fix failing tests I changed the default value for the form field mock from an array to a string; this fixes the few tests that were relying on it being an array. * Audit our rule statements/switches I made a pass through our treatment of RuleType to verify that EQL rules would be treated appropriately. Since the default/fallthrough case is typically the Query rule, and since this rule has the same attributes/behavior as the new EQL rule, not much had to change here. I converted a few if statements to exhaustive switches where possible, and used predicate helpers in places where it was not. * Add tests around use of custom components with UseField There was an issue previously where memoized components would not work; these are primarily regression tests covering that use case. * Fix typo * Add keys to UseField to ensure unmount When swapping between the Custom Query and EQL rule types, we want to ensure that the corresponding input component coming from UseField fully unmounts and remounts with the new component. Co-authored-by: Elastic Machine --- .../components/use_field.test.tsx | 62 ++++++- .../hook_form_lib/components/use_field.tsx | 8 +- .../schemas/common/schemas.ts | 3 +- .../add_prepackaged_rules_type_dependents.ts | 12 +- .../request/create_rules_type_dependents.ts | 12 +- .../request/import_rules_type_dependents.ts | 12 +- .../request/patch_rules_type_dependents.ts | 8 +- .../request/update_rules_type_dependents.ts | 12 +- .../schemas/response/rules_schema.test.ts | 10 ++ .../schemas/response/rules_schema.ts | 9 +- .../common/detection_engine/utils.ts | 4 +- .../common/machine_learning/helpers.ts | 2 +- .../alerts_detection_rules_eql.spec.ts | 167 ++++++++++++++++++ .../security_solution/cypress/objects/rule.ts | 14 ++ .../cypress/screens/create_new_rule.ts | 4 + .../cypress/tasks/create_new_rule.ts | 14 ++ .../public/common/mock/test_providers.tsx | 4 +- .../rules/add_item_form/index.test.tsx | 2 +- .../rules/description_step/helpers.tsx | 8 + .../rules/description_step/translations.tsx | 7 + .../eql_query_bar/eql_query_bar.test.tsx | 47 +++++ .../rules/eql_query_bar/eql_query_bar.tsx | 59 +++++++ .../components/rules/eql_query_bar/index.ts | 7 + .../components/rules/mitre/index.test.tsx | 2 +- .../rules/select_rule_type/index.tsx | 28 ++- .../rules/select_rule_type/translations.ts | 15 ++ .../rules/step_define_rule/index.tsx | 78 ++++---- .../rules/all/batch_actions.tsx | 3 +- .../rules/create/helpers.test.ts | 26 +++ .../detection_engine/rules/create/helpers.ts | 30 ++-- .../detection_engine/rules/details/index.tsx | 2 +- .../pages/detection_engine/rules/helpers.tsx | 20 ++- .../routes/rules/create_rules_bulk_route.ts | 8 +- .../routes/rules/create_rules_route.ts | 8 +- .../routes/rules/import_rules_route.ts | 8 +- .../routes/rules/update_rules_bulk_route.ts | 8 +- .../routes/rules/update_rules_route.ts | 8 +- .../detection_engine/signals/get_filter.ts | 1 + .../signals/signal_rule_alert_type.ts | 5 +- 39 files changed, 618 insertions(+), 119 deletions(-) create mode 100644 x-pack/plugins/security_solution/cypress/integration/alerts_detection_rules_eql.spec.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.test.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/index.ts diff --git a/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/use_field.test.tsx b/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/use_field.test.tsx index c14471991ccd33..dbf53a9f0a359a 100644 --- a/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/use_field.test.tsx +++ b/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/use_field.test.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { useEffect } from 'react'; +import React, { useEffect, FunctionComponent } from 'react'; import { act } from 'react-dom/test-utils'; import { registerTestBed, TestBed } from '../shared_imports'; @@ -237,4 +237,64 @@ describe('', () => { expect(serializer).not.toBeCalled(); }); }); + + describe('custom components', () => { + interface MyForm { + name: string; + } + + let formHook: FormHook | null = null; + + beforeEach(() => { + formHook = null; + }); + + const onFormHook = (_form: FormHook) => { + formHook = _form; + }; + + const TestComp = ({ + component, + onForm, + }: { + component: FunctionComponent; + onForm: (form: FormHook) => void; + }) => { + const { form } = useForm(); + + useEffect(() => { + onForm(form); + }, [onForm, form]); + + return ( +
+ + + ); + }; + + it('allows function components', () => { + const Component = () =>