From 19fa96752eaa96cff592da0ae8a98d3e34007045 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Fri, 2 Oct 2020 13:19:02 -0500 Subject: [PATCH] [Security Solution][Detections] EQL Validation (#77493) * WIP: Adding new route for EQL Validation This is mostly boilerplate with some rough parameter definitions; the actual implementation of the validation is going to live in our validateEql function. A few tests are failing as the mocks haven't yet been implemented, I need to see the shape of the responses first. * Cherry-pick Marshall's EQL types * Implements actual EQL validation * Performs an EQL search * filters out non-parsing errors, and returns what remains in the response * Adds mocks for empty EQL responses (we don't yet have a need for mocked data, but we will when we unit-test validateEql) * Adds validation calls to the EQL form input * Adds EQL Validation response schema,mocks,tests * Adds frontend function to call our validation endpoint * Adds hook, useEqlValidation, to call the above function and return state * Adds labels/help text for EQL Query bar * EqlQueryBar consumes useEqlValidation and marks the field as invalid, but does not yet report errors. * Do not call the validation API if query is not present This causes a broader error that results in a 400 response; we can (and do) handle the case of a blank query in the form itself. * Remove EQL Help Text It doesn't add any information for the user, and it currently looks bad when combined with validation errors. * Flesh out and use our popover for displaying validation errors * Fixes issue where old errors were persisted after the user had made modifications * Include verification_exception errors as validation errors These include errors related to index fields and mappings. * Generalize our validation helpers We're concerned with validation errors; the source of those errors is an implementation detail of these functions. * Move error popover and EQL reference link to footer This more closely resembles the new Eui Markdown editor, which places errors and doc links in a footer. * Fix jest tests following additional prop * Add icon for EQL Rule card * Fixes existing EqlQueryBar tests These were broken by our use of useAppToasts and the EUI theme. * Add unit tests around error rendering on EQL Query Bar * Add tests for ErrorPopover * Remove unused schema type Decode doesn't do any additional processing, so we can use t.TypeOf here (the default for buildRouteValidation). * Remove duplicated header * Use ignore parameter to prevent EQL validations from logging errors Without `ignore: [400]` the ES client will log errors and then throw them. We can catch the error, but the log is undesirable. This updates the query to use the ignore parameter, along with updating the validation logic to work with the updated response. Adds some mocks and tests around these responses and helpers, since these will exist independent of the validation implementation. * Include mapping_exceptions during EQL query validation These include errors for inaccessible indexes, which should be useful to the rule writer in writing their EQL query. * Display toast messages for non-validation messages * fix type errors This type was renamed. * Do not request data in our validation request By not having the cluster retrieve/send any data, this should saves us a few CPU cycles. * Move EQL validation to an async form validator Rather than invoking a custom validation hook (useEqlValidation) at custom times (onBlur) in our EqlQueryBar component, we can instead move this functionality to a form validation function and have it be invoked automatically by our form when values change. However, because we still need to handle the validation messages slightly differently (place them in a popover as opposed to an EuiFormRow), we also need custom error retrieval in the form of getValidationResults. After much pain, it was determined that the default behavior of _.debounce does not work with async validator functions, as a debounced call will not "wait" for the eventual invocation but will instead return the most recently resolved value. This leads to stale validation results and terrible UX, so I wrote a custom function (debounceAsync) that behaves like we want/need; see tests for details. * Invalidate our query field when index patterns change Since EQL rules actually validate against the relevant indexes, changing said indexes should invalidate/revalidate the query. With the form lib, this is beautifully simple :) * Set a min-height on our EQL textarea * Remove unused prop from EqlQueryBar Index corresponds to the value from the index field; now that our EQL validation is performed by the form we have no need for it here. * Update EQL overview link to point to elasticsearch docs Adds an entry in our doclinks service, and uses that. * Remove unused prop from stale tests * Update docLinks documentation with new EQL link * Fix bug where saved query rules had no type selected on Edit * Wait for kibana requests to complete before moving between rule tabs With our new async validation, a user can quickly navigate away from the Definition tab before the validation has completed, resulting in the form being invalidated. Any subsequent user actions cause the form to correct itself, but until I can find a better solution here this really just gives the validation time to complete and sidesteps the issue. --- ...-plugin-core-public.doclinksstart.links.md | 1 + ...kibana-plugin-core-public.doclinksstart.md | 2 +- .../public/doc_links/doc_links_service.ts | 2 + src/core/public/public.api.md | 1 + .../security_solution/common/constants.ts | 1 + .../request/eql_validation_schema.mock.ts | 12 ++ .../request/eql_validation_schema.test.ts | 59 ++++++++++ .../schemas/request/eql_validation_schema.ts | 18 +++ .../response/eql_validation_schema.mock.ts | 17 +++ .../response/eql_validation_schema.test.ts | 59 ++++++++++ .../schemas/response/eql_validation_schema.ts | 16 +++ .../common/detection_engine/utils.ts | 3 +- .../alerts_detection_rules_custom.spec.ts | 3 +- .../cypress/screens/edit_rule.ts | 2 + .../cypress/tasks/edit_rule.ts | 6 +- .../public/common/hooks/eql/api.ts | 31 ++++++ .../rules/eql_query_bar/eql_overview_link.tsx | 26 +++++ .../eql_query_bar/eql_query_bar.test.tsx | 37 +++++- .../rules/eql_query_bar/eql_query_bar.tsx | 53 +++++++-- .../eql_query_bar/errors_popover.test.tsx | 50 +++++++++ .../rules/eql_query_bar/errors_popover.tsx | 55 +++++++++ .../components/rules/eql_query_bar/footer.tsx | 42 +++++++ .../rules/eql_query_bar/translations.ts | 42 +++++++ .../rules/eql_query_bar/validators.mock.ts | 19 ++++ .../rules/eql_query_bar/validators.test.ts | 52 +++++++++ .../rules/eql_query_bar/validators.ts | 105 ++++++++++++++++++ .../select_rule_type/eql_search_icon.svg | 6 + .../rules/select_rule_type/index.tsx | 5 +- .../rules/step_define_rule/index.tsx | 5 + .../rules/step_define_rule/schema.tsx | 13 +-- .../rules/step_define_rule/translations.tsx | 14 +++ .../public/shared_imports.ts | 1 + .../routes/__mocks__/request_context.ts | 5 +- .../routes/__mocks__/request_responses.ts | 27 ++++- .../routes/eql/helpers.mock.ts | 69 ++++++++++++ .../routes/eql/helpers.test.ts | 58 ++++++++++ .../detection_engine/routes/eql/helpers.ts | 35 ++++++ .../routes/eql/validate_eql.ts | 46 ++++++++ .../routes/eql/validation_route.test.ts | 53 +++++++++ .../routes/eql/validation_route.ts | 49 ++++++++ .../security_solution/server/routes/index.ts | 4 + 41 files changed, 1075 insertions(+), 29 deletions(-) create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.mock.ts create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.test.ts create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.ts create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.mock.ts create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.test.ts create mode 100644 x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.ts create mode 100644 x-pack/plugins/security_solution/public/common/hooks/eql/api.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_overview_link.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/errors_popover.test.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/errors_popover.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/footer.tsx create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/translations.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/validators.mock.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/validators.test.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/validators.ts create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/select_rule_type/eql_search_icon.svg create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/helpers.mock.ts create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/helpers.test.ts create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/helpers.ts create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/validate_eql.ts create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/validation_route.test.ts create mode 100644 x-pack/plugins/security_solution/server/lib/detection_engine/routes/eql/validation_route.ts diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md index f7b55b0650d8b..3afd5eaa6f1f7 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md @@ -91,6 +91,7 @@ readonly links: { readonly gettingStarted: string; }; readonly query: { + readonly eql: string; readonly luceneQuerySyntax: string; readonly queryDsl: string; readonly kueryQuerySyntax: string; diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md index 3f58cf08ee6b6..5249381969b98 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md @@ -17,5 +17,5 @@ export interface DocLinksStart | --- | --- | --- | | [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinksstart.doc_link_version.md) | string | | | [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinksstart.elastic_website_url.md) | string | | -| [links](./kibana-plugin-core-public.doclinksstart.links.md) | {
readonly dashboard: {
readonly drilldowns: string;
readonly drilldownsTriggerPicker: string;
readonly urlDrilldownTemplateSyntax: string;
readonly urlDrilldownVariables: string;
};
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly addData: string;
readonly kibana: string;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
readonly visualize: Record<string, string>;
} | | +| [links](./kibana-plugin-core-public.doclinksstart.links.md) | {
readonly dashboard: {
readonly drilldowns: string;
readonly drilldownsTriggerPicker: string;
readonly urlDrilldownTemplateSyntax: string;
readonly urlDrilldownVariables: string;
};
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly addData: string;
readonly kibana: string;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly eql: string;
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
readonly visualize: Record<string, string>;
} | | diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index f813a9326d741..63018c226c557 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -119,6 +119,7 @@ export class DocLinksService { gettingStarted: `${ELASTIC_WEBSITE_URL}guide/en/security/${DOC_LINK_VERSION}/index.html`, }, query: { + eql: `${ELASTICSEARCH_DOCS}eql.html`, luceneQuerySyntax: `${ELASTICSEARCH_DOCS}query-dsl-query-string-query.html#query-string-syntax`, queryDsl: `${ELASTICSEARCH_DOCS}query-dsl.html`, kueryQuerySyntax: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/kuery-query.html`, @@ -228,6 +229,7 @@ export interface DocLinksStart { readonly gettingStarted: string; }; readonly query: { + readonly eql: string; readonly luceneQuerySyntax: string; readonly queryDsl: string; readonly kueryQuerySyntax: string; diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 5970c9a8571c4..08491dc76cd27 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -539,6 +539,7 @@ export interface DocLinksStart { readonly gettingStarted: string; }; readonly query: { + readonly eql: string; readonly luceneQuerySyntax: string; readonly queryDsl: string; readonly kueryQuerySyntax: string; diff --git a/x-pack/plugins/security_solution/common/constants.ts b/x-pack/plugins/security_solution/common/constants.ts index 2910f02a187f4..e46bd9e28d8c4 100644 --- a/x-pack/plugins/security_solution/common/constants.ts +++ b/x-pack/plugins/security_solution/common/constants.ts @@ -117,6 +117,7 @@ export const DETECTION_ENGINE_PREPACKAGED_URL = `${DETECTION_ENGINE_RULES_URL}/p export const DETECTION_ENGINE_PRIVILEGES_URL = `${DETECTION_ENGINE_URL}/privileges`; export const DETECTION_ENGINE_INDEX_URL = `${DETECTION_ENGINE_URL}/index`; export const DETECTION_ENGINE_TAGS_URL = `${DETECTION_ENGINE_URL}/tags`; +export const DETECTION_ENGINE_EQL_VALIDATION_URL = `${DETECTION_ENGINE_URL}/validate_eql`; export const DETECTION_ENGINE_RULES_STATUS_URL = `${DETECTION_ENGINE_RULES_URL}/_find_statuses`; export const DETECTION_ENGINE_PREPACKAGED_RULES_STATUS_URL = `${DETECTION_ENGINE_RULES_URL}/prepackaged/_status`; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.mock.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.mock.ts new file mode 100644 index 0000000000000..96afc0c85df44 --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.mock.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { EqlValidationSchema } from './eql_validation_schema'; + +export const getEqlValidationSchemaMock = (): EqlValidationSchema => ({ + index: ['index-123'], + query: 'process where process.name == "regsvr32.exe"', +}); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.test.ts new file mode 100644 index 0000000000000..84bb8e067bf75 --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.test.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { pipe } from 'fp-ts/lib/pipeable'; +import { left } from 'fp-ts/lib/Either'; + +import { exactCheck } from '../../../exact_check'; +import { foldLeftRight, getPaths } from '../../../test_utils'; +import { eqlValidationSchema, EqlValidationSchema } from './eql_validation_schema'; +import { getEqlValidationSchemaMock } from './eql_validation_schema.mock'; + +describe('EQL validation schema', () => { + it('requires a value for index', () => { + const payload = { + ...getEqlValidationSchemaMock(), + index: undefined, + }; + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "index"', + ]); + expect(message.schema).toEqual({}); + }); + + it('requires a value for query', () => { + const payload = { + ...getEqlValidationSchemaMock(), + query: undefined, + }; + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "query"', + ]); + expect(message.schema).toEqual({}); + }); + + it('validates a payload with index and query', () => { + const payload = getEqlValidationSchemaMock(); + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + const expected: EqlValidationSchema = { + index: ['index-123'], + query: 'process where process.name == "regsvr32.exe"', + }; + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(expected); + }); +}); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.ts new file mode 100644 index 0000000000000..abbbe33a32258 --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/eql_validation_schema.ts @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as t from 'io-ts'; + +import { index, query } from '../common/schemas'; + +export const eqlValidationSchema = t.exact( + t.type({ + index, + query, + }) +); + +export type EqlValidationSchema = t.TypeOf; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.mock.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.mock.ts new file mode 100644 index 0000000000000..98e5db47253fb --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.mock.ts @@ -0,0 +1,17 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { EqlValidationSchema } from './eql_validation_schema'; + +export const getEqlValidationResponseMock = (): EqlValidationSchema => ({ + valid: false, + errors: ['line 3:52: token recognition error at: '], +}); + +export const getValidEqlValidationResponseMock = (): EqlValidationSchema => ({ + valid: true, + errors: [], +}); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.test.ts new file mode 100644 index 0000000000000..939238e340cff --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.test.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { pipe } from 'fp-ts/lib/pipeable'; +import { left } from 'fp-ts/lib/Either'; + +import { exactCheck } from '../../../exact_check'; +import { foldLeftRight, getPaths } from '../../../test_utils'; +import { getEqlValidationResponseMock } from './eql_validation_schema.mock'; +import { eqlValidationSchema } from './eql_validation_schema'; + +describe('EQL validation response schema', () => { + it('validates a typical response', () => { + const payload = getEqlValidationResponseMock(); + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([]); + expect(message.schema).toEqual(getEqlValidationResponseMock()); + }); + + it('invalidates a response with extra properties', () => { + const payload = { ...getEqlValidationResponseMock(), extra: 'nope' }; + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual(['invalid keys "extra"']); + expect(message.schema).toEqual({}); + }); + + it('invalidates a response with missing properties', () => { + const payload = { ...getEqlValidationResponseMock(), valid: undefined }; + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "undefined" supplied to "valid"', + ]); + expect(message.schema).toEqual({}); + }); + + it('invalidates a response with properties of the wrong type', () => { + const payload = { ...getEqlValidationResponseMock(), errors: 'should be an array' }; + const decoded = eqlValidationSchema.decode(payload); + const checked = exactCheck(payload, decoded); + const message = pipe(checked, foldLeftRight); + + expect(getPaths(left(message.errors))).toEqual([ + 'Invalid value "should be an array" supplied to "errors"', + ]); + expect(message.schema).toEqual({}); + }); +}); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.ts new file mode 100644 index 0000000000000..e999e1dd273f8 --- /dev/null +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/eql_validation_schema.ts @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as t from 'io-ts'; + +export const eqlValidationSchema = t.exact( + t.type({ + valid: t.boolean, + errors: t.array(t.string), + }) +); + +export type EqlValidationSchema = t.TypeOf; diff --git a/x-pack/plugins/security_solution/common/detection_engine/utils.ts b/x-pack/plugins/security_solution/common/detection_engine/utils.ts index f76417099bb17..d7b23755699f5 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/utils.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/utils.ts @@ -19,5 +19,6 @@ export const hasNestedEntry = (entries: EntriesArray): boolean => { export const isEqlRule = (ruleType: Type | undefined): boolean => ruleType === 'eql'; export const isThresholdRule = (ruleType: Type | undefined): boolean => ruleType === 'threshold'; -export const isQueryRule = (ruleType: Type | undefined): boolean => ruleType === 'query'; +export const isQueryRule = (ruleType: Type | undefined): boolean => + ruleType === 'query' || ruleType === 'saved_query'; export const isThreatMatchRule = (ruleType: Type): boolean => ruleType === 'threat_match'; diff --git a/x-pack/plugins/security_solution/cypress/integration/alerts_detection_rules_custom.spec.ts b/x-pack/plugins/security_solution/cypress/integration/alerts_detection_rules_custom.spec.ts index f999c5cecc392..d8832dc4ee600 100644 --- a/x-pack/plugins/security_solution/cypress/integration/alerts_detection_rules_custom.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/alerts_detection_rules_custom.spec.ts @@ -93,7 +93,7 @@ import { goToScheduleStepTab, waitForTheRuleToBeExecuted, } from '../tasks/create_new_rule'; -import { saveEditedRule } from '../tasks/edit_rule'; +import { saveEditedRule, waitForKibana } from '../tasks/edit_rule'; import { esArchiverLoad, esArchiverUnload } from '../tasks/es_archiver'; import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; import { refreshPage } from '../tasks/security_header'; @@ -290,6 +290,7 @@ describe('Custom detection rules deletion and edition', () => { context('Edition', () => { it('Allows a rule to be edited', () => { editFirstRule(); + waitForKibana(); // expect define step to populate cy.get(CUSTOM_QUERY_INPUT).should('have.text', existingRule.customQuery); diff --git a/x-pack/plugins/security_solution/cypress/screens/edit_rule.ts b/x-pack/plugins/security_solution/cypress/screens/edit_rule.ts index 1bf0ff34ebd94..e25eb7453c63c 100644 --- a/x-pack/plugins/security_solution/cypress/screens/edit_rule.ts +++ b/x-pack/plugins/security_solution/cypress/screens/edit_rule.ts @@ -5,3 +5,5 @@ */ export const EDIT_SUBMIT_BUTTON = '[data-test-subj="ruleEditSubmitButton"]'; +export const KIBANA_LOADING_INDICATOR = '[data-test-subj="globalLoadingIndicator"]'; +export const KIBANA_LOADING_COMPLETE_INDICATOR = '[data-test-subj="globalLoadingIndicator-hidden"]'; diff --git a/x-pack/plugins/security_solution/cypress/tasks/edit_rule.ts b/x-pack/plugins/security_solution/cypress/tasks/edit_rule.ts index 690a36058ec33..2dc1318ccb81d 100644 --- a/x-pack/plugins/security_solution/cypress/tasks/edit_rule.ts +++ b/x-pack/plugins/security_solution/cypress/tasks/edit_rule.ts @@ -4,9 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ -import { EDIT_SUBMIT_BUTTON } from '../screens/edit_rule'; +import { EDIT_SUBMIT_BUTTON, KIBANA_LOADING_COMPLETE_INDICATOR } from '../screens/edit_rule'; export const saveEditedRule = () => { cy.get(EDIT_SUBMIT_BUTTON).should('exist').click({ force: true }); cy.get(EDIT_SUBMIT_BUTTON).should('not.exist'); }; + +export const waitForKibana = () => { + cy.get(KIBANA_LOADING_COMPLETE_INDICATOR).should('exist'); +}; diff --git a/x-pack/plugins/security_solution/public/common/hooks/eql/api.ts b/x-pack/plugins/security_solution/public/common/hooks/eql/api.ts new file mode 100644 index 0000000000000..11fe79910bc87 --- /dev/null +++ b/x-pack/plugins/security_solution/public/common/hooks/eql/api.ts @@ -0,0 +1,31 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { HttpStart } from '../../../../../../../src/core/public'; +import { DETECTION_ENGINE_EQL_VALIDATION_URL } from '../../../../common/constants'; +import { EqlValidationSchema as EqlValidationRequest } from '../../../../common/detection_engine/schemas/request/eql_validation_schema'; +import { EqlValidationSchema as EqlValidationResponse } from '../../../../common/detection_engine/schemas/response/eql_validation_schema'; + +interface ApiParams { + http: HttpStart; + signal: AbortSignal; +} + +export const validateEql = async ({ + http, + query, + index, + signal, +}: ApiParams & EqlValidationRequest) => { + return http.fetch(DETECTION_ENGINE_EQL_VALIDATION_URL, { + method: 'POST', + body: JSON.stringify({ + query, + index, + }), + signal, + }); +}; diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_overview_link.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_overview_link.tsx new file mode 100644 index 0000000000000..e9891fc066ec2 --- /dev/null +++ b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_overview_link.tsx @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import React from 'react'; +import styled from 'styled-components'; +import { EuiLink, EuiText } from '@elastic/eui'; + +import { useKibana } from '../../../../common/lib/kibana'; +import { EQL_OVERVIEW_LINK_TEXT } from './translations'; + +const InlineText = styled(EuiText)` + display: inline-block; +`; + +export const EqlOverviewLink = () => { + const overviewUrl = useKibana().services.docLinks.links.query.eql; + + return ( + + {EQL_OVERVIEW_LINK_TEXT} + + ); +}; diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.test.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.test.tsx index 331c0ba4c4491..5539e5eb2c294 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.test.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.test.tsx @@ -7,9 +7,12 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; -import { useFormFieldMock } from '../../../../common/mock'; +import { TestProviders, useFormFieldMock } from '../../../../common/mock'; import { mockQueryBar } from '../../../pages/detection_engine/rules/all/__mocks__/mock'; import { EqlQueryBar, EqlQueryBarProps } from './eql_query_bar'; +import { getEqlValidationError } from './validators.mock'; + +jest.mock('../../../../common/lib/kibana'); describe('EqlQueryBar', () => { let mockField: EqlQueryBarProps['field']; @@ -27,7 +30,11 @@ describe('EqlQueryBar', () => { }); it('sets the field value on input change', () => { - const wrapper = mount(); + const wrapper = mount( + + + + ); wrapper .find('[data-test-subj="eqlQueryBarTextInput"]') @@ -44,4 +51,30 @@ describe('EqlQueryBar', () => { expect(mockField.setValue).toHaveBeenCalledWith(expected); }); + + it('does not render errors for a valid query', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('[data-test-subj="eql-validation-errors-popover"]').exists()).toEqual( + false + ); + }); + + it('renders errors for an invalid query', () => { + const invalidMockField = useFormFieldMock({ + value: mockQueryBar, + errors: [getEqlValidationError()], + }); + const wrapper = mount( + + + + ); + + expect(wrapper.find('[data-test-subj="eql-validation-errors-popover"]').exists()).toEqual(true); + }); }); diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.tsx index e3f33ea9b9b87..f7ee5be18154c 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/eql_query_bar/eql_query_bar.tsx @@ -4,11 +4,24 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC, useCallback, ChangeEvent } from 'react'; +import React, { FC, useCallback, ChangeEvent, useEffect, useState } from 'react'; +import styled from 'styled-components'; import { EuiFormRow, EuiTextArea } from '@elastic/eui'; -import { FieldHook, getFieldValidityAndErrorMessage } from '../../../../shared_imports'; +import { FieldHook } from '../../../../shared_imports'; +import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; import { DefineStepRule } from '../../../pages/detection_engine/rules/types'; +import * as i18n from './translations'; +import { EqlQueryBarFooter } from './footer'; +import { getValidationResults } from './validators'; + +const TextArea = styled(EuiTextArea)` + display: block; + border: ${({ theme }) => theme.eui.euiBorderThin}; + border-bottom: 0; + box-shadow: none; + min-height: ${({ theme }) => theme.eui.euiFormControlHeight}; +`; export interface EqlQueryBarProps { dataTestSubj: string; @@ -17,14 +30,27 @@ export interface EqlQueryBarProps { } export const EqlQueryBar: FC = ({ dataTestSubj, field, idAria }) => { + const { addError } = useAppToasts(); + const [errorMessages, setErrorMessages] = useState([]); const { setValue } = field; - const { isInvalid, errorMessage } = getFieldValidityAndErrorMessage(field); + const { isValid, message, messages, error } = getValidationResults(field); const fieldValue = field.value.query.query as string; + useEffect(() => { + setErrorMessages(messages ?? []); + }, [messages]); + + useEffect(() => { + if (error) { + addError(error, { title: i18n.EQL_VALIDATION_REQUEST_ERROR }); + } + }, [error, addError]); + const handleChange = useCallback( (e: ChangeEvent) => { const newQuery = e.target.value; + setErrorMessages([]); setValue({ filters: [], query: { @@ -41,19 +67,22 @@ export const EqlQueryBar: FC = ({ dataTestSubj, field, idAria label={field.label} labelAppend={field.labelAppend} helpText={field.helpText} - error={errorMessage} - isInvalid={isInvalid} + error={message} + isInvalid={!isValid} fullWidth data-test-subj={dataTestSubj} describedByIds={idAria ? [idAria] : undefined} > - + <> +