Skip to content
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 40 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
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 Sep 14, 2020
01fc159
Cherry-pick Marshall's EQL types
rylnd Sep 15, 2020
f81ed6c
Implements actual EQL validation
rylnd Sep 15, 2020
c45ca39
Adds validation calls to the EQL form input
rylnd Sep 16, 2020
587a60a
Do not call the validation API if query is not present
rylnd Sep 16, 2020
cfaa05c
Remove EQL Help Text
rylnd Sep 16, 2020
d68ff61
Flesh out and use our popover for displaying validation errors
rylnd Sep 16, 2020
15f955e
Include verification_exception errors as validation errors
rylnd Sep 17, 2020
4cf2d55
Generalize our validation helpers
rylnd Sep 17, 2020
051db75
Move error popover and EQL reference link to footer
rylnd Sep 17, 2020
d3a6c75
Fix jest tests following additional prop
rylnd Sep 17, 2020
9ee7397
Add icon for EQL Rule card
rylnd Sep 18, 2020
2662d77
Fixes existing EqlQueryBar tests
rylnd Sep 18, 2020
490d2e3
Add unit tests around error rendering on EQL Query Bar
rylnd Sep 18, 2020
c47f2c0
Add tests for ErrorPopover
rylnd Sep 18, 2020
fc27ca8
Merge branch 'master' into eql_validation
rylnd Sep 18, 2020
7a6c8dc
Remove unused schema type
rylnd Sep 22, 2020
2a97944
Remove duplicated header
rylnd Sep 22, 2020
cd20b8c
Merge branch 'master' into eql_validation
rylnd Sep 22, 2020
6655ce1
Use ignore parameter to prevent EQL validations from logging errors
rylnd Sep 22, 2020
9c27650
Include mapping_exceptions during EQL query validation
rylnd Sep 22, 2020
4ef8069
Display toast messages for non-validation messages
rylnd Sep 24, 2020
6c0693c
Merge branch 'master' into eql_validation
rylnd Sep 24, 2020
34f43c8
fix type errors
rylnd Sep 24, 2020
fb2da66
Merge branch 'master' into eql_validation
rylnd Sep 27, 2020
c4e8519
Merge branch 'master' into eql_validation
rylnd Sep 28, 2020
9684414
Do not request data in our validation request
rylnd Sep 28, 2020
a6f36ac
Merge branch 'master' into eql_validation
rylnd Sep 29, 2020
80ed453
Move EQL validation to an async form validator
rylnd Oct 1, 2020
8fa5fda
Invalidate our query field when index patterns change
rylnd Oct 1, 2020
429c763
Merge branch 'master' into eql_validation_async
rylnd Oct 1, 2020
b865866
Set a min-height on our EQL textarea
rylnd Oct 1, 2020
7865eaf
Remove unused prop from EqlQueryBar
rylnd Oct 1, 2020
aa3f9b0
Update EQL overview link to point to elasticsearch docs
rylnd Oct 1, 2020
32f292f
Remove unused prop from stale tests
rylnd Oct 1, 2020
075ff96
Merge branch 'master' into eql_validation
rylnd Oct 1, 2020
bbc1469
Update docLinks documentation with new EQL link
rylnd Oct 1, 2020
03060f3
Fix bug where saved query rules had no type selected on Edit
rylnd Oct 1, 2020
5a5e9bc
Wait for kibana requests to complete before moving between rule tabs
rylnd Oct 2, 2020
1d2b5a8
Merge branch 'master' into eql_validation
rylnd Oct 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@ import React from 'react';
import { shallow, mount } from 'enzyme';

import { TestProviders, useFormFieldMock } from '../../../../common/mock';
import { useEqlValidation } from '../../../../common/hooks/eql/use_eql_validation';
import {
getEqlValidationResponseMock,
getValidEqlValidationResponseMock,
} from '../../../../../common/detection_engine/schemas/response/eql_validation_schema.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');
jest.mock('../../../../common/hooks/eql/use_eql_validation');

describe('EqlQueryBar', () => {
let mockField: EqlQueryBarProps['field'];
let mockIndex: string[];

beforeEach(() => {
(useEqlValidation as jest.Mock).mockReturnValue({
result: getValidEqlValidationResponseMock(),
});
mockIndex = ['index-123*'];
mockField = useFormFieldMock({
value: mockQueryBar,
Expand Down Expand Up @@ -77,13 +69,13 @@ describe('EqlQueryBar', () => {
});

it('renders errors for an invalid query', () => {
(useEqlValidation as jest.Mock).mockReturnValue({
result: getEqlValidationResponseMock(),
const invalidMockField = useFormFieldMock({
value: mockQueryBar,
errors: [getEqlValidationError()],
});

const wrapper = mount(
<TestProviders>
<EqlQueryBar index={mockIndex} dataTestSubj="myQueryBar" field={mockField} />
<EqlQueryBar index={mockIndex} dataTestSubj="myQueryBar" field={invalidMockField} />
</TestProviders>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ 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 { useEqlValidation } from '../../../../common/hooks/eql/use_eql_validation';
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 { useKibana } from '../../../../common/lib/kibana';
import { EqlQueryBarFooter } from './footer';
import { getValidationResults } from './validators';

const TextArea = styled(EuiTextArea)`
display: block;
Expand All @@ -31,32 +30,23 @@ export interface EqlQueryBarProps {
}

export const EqlQueryBar: FC<EqlQueryBarProps> = ({ dataTestSubj, field, idAria, index }) => {
const { http } = useKibana().services;
const { addError } = useAppToasts();
const [errorMessages, setErrorMessages] = useState<string[]>([]);
const { error, start, result } = useEqlValidation();
const { setErrors, setValue } = field;
const { isInvalid, errorMessage } = getFieldValidityAndErrorMessage(field);
const { setValue } = field;
const { isValid, message, messages, error } = getValidationResults(field);
const fieldValue = field.value.query.query as string;

useEffect(() => {
if (error) {
addError(error, { title: i18n.EQL_VALIDATION_REQUEST_ERROR });
if (messages && messages.length > 0) {
setErrorMessages(messages);
}
}, [error, addError]);
}, [messages]);

useEffect(() => {
if (result != null && result.valid === false && result.errors.length > 0) {
setErrors([{ message: '' }]);
setErrorMessages(result.errors);
}
}, [result, setErrors]);

const handleValidation = useCallback(() => {
if (fieldValue) {
start({ http, index, query: fieldValue });
if (error) {
addError(error, { title: i18n.EQL_VALIDATION_REQUEST_ERROR });
}
}, [fieldValue, http, index, start]);
}, [error, addError]);

const handleChange = useCallback(
(e: ChangeEvent<HTMLTextAreaElement>) => {
Expand All @@ -79,8 +69,8 @@ export const EqlQueryBar: FC<EqlQueryBarProps> = ({ 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}
Expand All @@ -89,9 +79,8 @@ export const EqlQueryBar: FC<EqlQueryBarProps> = ({ dataTestSubj, field, idAria,
<TextArea
data-test-subj="eqlQueryBarTextInput"
fullWidth
isInvalid={isInvalid}
isInvalid={!isValid}
value={fieldValue}
onBlur={handleValidation}
onChange={handleChange}
/>
<EqlQueryBarFooter errors={errorMessages} />
Expand Down
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: '',
});
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);
});
});
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>>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 👍

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: '' };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEmpty } from 'lodash';
import { i18n } from '@kbn/i18n';
import { EuiText } from '@elastic/eui';
import { isEmpty } from 'lodash/fp';
import React from 'react';

import { isMlRule } from '../../../../../common/machine_learning/helpers';
Expand All @@ -21,6 +21,7 @@ import {
} from '../../../../shared_imports';
import { DefineStepRule } from '../../../pages/detection_engine/rules/types';
import { CUSTOM_QUERY_REQUIRED, INVALID_CUSTOM_QUERY, INDEX_HELPER_TEXT } from './translations';
import { debounceAsync, eqlValidator } from '../eql_query_bar/validators';

export const schema: FormSchema<DefineStepRule> = {
index: {
Expand Down Expand Up @@ -102,6 +103,9 @@ export const schema: FormSchema<DefineStepRule> = {
}
},
},
{
validator: debounceAsync(eqlValidator, 300),
},
],
},
ruleType: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/security_solution/public/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
useForm,
useFormContext,
useFormData,
ValidationError,
ValidationFunc,
VALIDATION_TYPES,
} from '../../../../src/plugins/es_ui_shared/static/forms/hook_form_lib';
Expand Down