-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution][Detections] EQL Validation #77493
Merged
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
30b9a94
WIP: Adding new route for EQL Validation
rylnd 01fc159
Cherry-pick Marshall's EQL types
rylnd f81ed6c
Implements actual EQL validation
rylnd c45ca39
Adds validation calls to the EQL form input
rylnd 587a60a
Do not call the validation API if query is not present
rylnd cfaa05c
Remove EQL Help Text
rylnd d68ff61
Flesh out and use our popover for displaying validation errors
rylnd 15f955e
Include verification_exception errors as validation errors
rylnd 4cf2d55
Generalize our validation helpers
rylnd 051db75
Move error popover and EQL reference link to footer
rylnd d3a6c75
Fix jest tests following additional prop
rylnd 9ee7397
Add icon for EQL Rule card
rylnd 2662d77
Fixes existing EqlQueryBar tests
rylnd 490d2e3
Add unit tests around error rendering on EQL Query Bar
rylnd c47f2c0
Add tests for ErrorPopover
rylnd fc27ca8
Merge branch 'master' into eql_validation
rylnd 7a6c8dc
Remove unused schema type
rylnd 2a97944
Remove duplicated header
rylnd cd20b8c
Merge branch 'master' into eql_validation
rylnd 6655ce1
Use ignore parameter to prevent EQL validations from logging errors
rylnd 9c27650
Include mapping_exceptions during EQL query validation
rylnd 4ef8069
Display toast messages for non-validation messages
rylnd 6c0693c
Merge branch 'master' into eql_validation
rylnd 34f43c8
fix type errors
rylnd fb2da66
Merge branch 'master' into eql_validation
rylnd c4e8519
Merge branch 'master' into eql_validation
rylnd 9684414
Do not request data in our validation request
rylnd a6f36ac
Merge branch 'master' into eql_validation
rylnd 80ed453
Move EQL validation to an async form validator
rylnd 8fa5fda
Invalidate our query field when index patterns change
rylnd 429c763
Merge branch 'master' into eql_validation_async
rylnd b865866
Set a min-height on our EQL textarea
rylnd 7865eaf
Remove unused prop from EqlQueryBar
rylnd aa3f9b0
Update EQL overview link to point to elasticsearch docs
rylnd 32f292f
Remove unused prop from stale tests
rylnd 075ff96
Merge branch 'master' into eql_validation
rylnd bbc1469
Update docLinks documentation with new EQL link
rylnd 03060f3
Fix bug where saved query rules had no type selected on Edit
rylnd 5a5e9bc
Wait for kibana requests to complete before moving between rule tabs
rylnd 1d2b5a8
Merge branch 'master' into eql_validation
rylnd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
x-pack/plugins/security_solution/public/common/hooks/eql/use_eql_validation.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ins/security_solution/public/detections/components/rules/eql_query_bar/validators.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* 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 { ValidationError } from '../../../../shared_imports'; | ||
import { ERROR_CODES } from './validators'; | ||
|
||
export const getEqlResponseError = (): ValidationError => ({ | ||
code: ERROR_CODES.FAILED_REQUEST, | ||
message: 'something went wrong', | ||
}); | ||
|
||
export const getEqlValidationError = (): ValidationError => ({ | ||
code: ERROR_CODES.INVALID_EQL, | ||
messages: ['line 1: WRONG\nline 2: ALSO WRONG'], | ||
message: '', | ||
}); |
52 changes: 52 additions & 0 deletions
52
...ins/security_solution/public/detections/components/rules/eql_query_bar/validators.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 { debounceAsync } from './validators'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('debounceAsync', () => { | ||
let fn: jest.Mock; | ||
|
||
beforeEach(() => { | ||
fn = jest.fn().mockResolvedValueOnce('first'); | ||
}); | ||
|
||
it('resolves with the underlying invocation result', async () => { | ||
const debounced = debounceAsync(fn, 0); | ||
const promise = debounced(); | ||
jest.runOnlyPendingTimers(); | ||
|
||
expect(await promise).toEqual('first'); | ||
}); | ||
|
||
it('resolves intermediate calls when the next invocation resolves', async () => { | ||
const debounced = debounceAsync(fn, 200); | ||
fn.mockResolvedValueOnce('second'); | ||
|
||
const promise = debounced(); | ||
jest.runOnlyPendingTimers(); | ||
expect(await promise).toEqual('first'); | ||
|
||
const promises = [debounced(), debounced()]; | ||
jest.runOnlyPendingTimers(); | ||
|
||
expect(await Promise.all(promises)).toEqual(['second', 'second']); | ||
}); | ||
|
||
it('debounces the function', async () => { | ||
const debounced = debounceAsync(fn, 200); | ||
|
||
debounced(); | ||
jest.runOnlyPendingTimers(); | ||
|
||
debounced(); | ||
debounced(); | ||
jest.runOnlyPendingTimers(); | ||
|
||
expect(fn).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
105 changes: 105 additions & 0 deletions
105
.../plugins/security_solution/public/detections/components/rules/eql_query_bar/validators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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 { isEmpty } from 'lodash'; | ||
|
||
import { FieldHook, ValidationError, ValidationFunc } from '../../../../shared_imports'; | ||
import { isEqlRule } from '../../../../../common/detection_engine/utils'; | ||
import { KibanaServices } from '../../../../common/lib/kibana'; | ||
import { DefineStepRule } from '../../../pages/detection_engine/rules/types'; | ||
import { validateEql } from '../../../../common/hooks/eql/api'; | ||
import { FieldValueQueryBar } from '../query_bar'; | ||
import * as i18n from './translations'; | ||
|
||
export enum ERROR_CODES { | ||
FAILED_REQUEST = 'ERR_FAILED_REQUEST', | ||
INVALID_EQL = 'ERR_INVALID_EQL', | ||
} | ||
|
||
/** | ||
* Unlike lodash's debounce, which resolves intermediate calls with the most | ||
* recent value, this implementation waits to resolve intermediate calls until | ||
* the next invocation resolves. | ||
* | ||
* @param fn an async function | ||
* | ||
* @returns A debounced async function that resolves on the next invocation | ||
*/ | ||
export const debounceAsync = <Args extends unknown[], Result extends Promise<unknown>>( | ||
fn: (...args: Args) => Result, | ||
interval: number | ||
): ((...args: Args) => Result) => { | ||
let handle: ReturnType<typeof setTimeout> | undefined; | ||
let resolves: Array<(value?: Result) => void> = []; | ||
|
||
return (...args: Args): Result => { | ||
if (handle) { | ||
clearTimeout(handle); | ||
} | ||
|
||
handle = setTimeout(() => { | ||
const result = fn(...args); | ||
resolves.forEach((resolve) => resolve(result)); | ||
resolves = []; | ||
}, interval); | ||
|
||
return new Promise((resolve) => resolves.push(resolve)) as Result; | ||
}; | ||
}; | ||
|
||
export const eqlValidator = async ( | ||
...args: Parameters<ValidationFunc> | ||
): Promise<ValidationError<ERROR_CODES> | void | undefined> => { | ||
const [{ value, formData }] = args; | ||
const { query: queryValue } = value as FieldValueQueryBar; | ||
const query = queryValue.query as string; | ||
const { index, ruleType } = formData as DefineStepRule; | ||
|
||
const needsValidation = isEqlRule(ruleType) && !isEmpty(query); | ||
if (!needsValidation) { | ||
return; | ||
} | ||
|
||
try { | ||
const { http } = KibanaServices.get(); | ||
const signal = new AbortController().signal; | ||
const response = await validateEql({ query, http, signal, index }); | ||
|
||
if (response?.valid === false) { | ||
return { | ||
code: ERROR_CODES.INVALID_EQL, | ||
message: '', | ||
messages: response.errors, | ||
}; | ||
} | ||
} catch (error) { | ||
return { | ||
code: ERROR_CODES.FAILED_REQUEST, | ||
message: i18n.EQL_VALIDATION_REQUEST_ERROR, | ||
error, | ||
}; | ||
} | ||
}; | ||
|
||
export const getValidationResults = <T = unknown>( | ||
field: FieldHook<T> | ||
): { isValid: boolean; message: string; messages?: string[]; error?: Error } => { | ||
const hasErrors = field.errors.length > 0; | ||
const isValid = !field.isChangingValue && !hasErrors; | ||
|
||
if (hasErrors) { | ||
const [error] = field.errors; | ||
const message = error.message; | ||
|
||
if (error.code === ERROR_CODES.INVALID_EQL) { | ||
return { isValid, message, messages: error.messages }; | ||
} else { | ||
return { isValid, message, error: error.error }; | ||
} | ||
} else { | ||
return { isValid, message: '' }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 👍