-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution][Detection Engine] improves ES|QL investigation fi…
…elds for detection rules (#177746) ## Summary - addresses investigation fields from elastic/security-team#7944 - addresses elastic/security-team#8771 - allows to select custom created fields in ES|QL query as investigation(highlighted) fields - shows only ES|QL fields for aggregating queries - shows ES|QL fields + index fields for non-aggregating queries. Since results are enriched with source documents in that case https://github.com/elastic/kibana/assets/92328789/34bc22fc-ffc6-44d6-ba6d-818ab9cbb5e5 ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5365
- Loading branch information
Showing
10 changed files
with
316 additions
and
7 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/security_solution/public/common/__mocks__/query_wrapper.tsx
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,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
|
||
export const createQueryWrapperMock = (): { | ||
queryClient: QueryClient; | ||
wrapper: React.FC; | ||
} => { | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
logger: { | ||
error: () => undefined, | ||
log: () => undefined, | ||
warn: () => undefined, | ||
}, | ||
}); | ||
|
||
return { | ||
queryClient, | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
), | ||
}; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
135 changes: 135 additions & 0 deletions
135
..._solution/public/detection_engine/rule_creation_ui/hooks/use_investigation_fields.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,135 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import type { DataViewFieldBase } from '@kbn/es-query'; | ||
|
||
import { useInvestigationFields } from './use_investigation_fields'; | ||
|
||
import { createQueryWrapperMock } from '../../../common/__mocks__/query_wrapper'; | ||
|
||
import { computeIsESQLQueryAggregating } from '@kbn/securitysolution-utils'; | ||
import { fetchFieldsFromESQL } from '@kbn/text-based-editor'; | ||
|
||
jest.mock('@kbn/securitysolution-utils', () => ({ | ||
computeIsESQLQueryAggregating: jest.fn(), | ||
})); | ||
|
||
jest.mock('@kbn/text-based-editor', () => ({ | ||
fetchFieldsFromESQL: jest.fn(), | ||
})); | ||
|
||
const computeIsESQLQueryAggregatingMock = computeIsESQLQueryAggregating as jest.Mock; | ||
const fetchFieldsFromESQLMock = fetchFieldsFromESQL as jest.Mock; | ||
|
||
const { wrapper } = createQueryWrapperMock(); | ||
|
||
const mockEsqlQuery = 'from auditbeat* [metadata _id]'; | ||
const mockIndexPatternFields: DataViewFieldBase[] = [ | ||
{ | ||
name: 'agent.name', | ||
type: 'string', | ||
}, | ||
{ | ||
name: 'agent.type', | ||
type: 'string', | ||
}, | ||
]; | ||
const mockEsqlDatatable = { | ||
type: 'datatable', | ||
rows: [], | ||
columns: [{ id: '_custom_field', name: '_custom_field', meta: { type: 'string' } }], | ||
}; | ||
|
||
describe('useInvestigationFields', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
fetchFieldsFromESQLMock.mockResolvedValue(mockEsqlDatatable); | ||
}); | ||
|
||
it('should return loading true when esql fields still loading', () => { | ||
const { result } = renderHook( | ||
() => | ||
useInvestigationFields({ | ||
esqlQuery: mockEsqlQuery, | ||
indexPatternsFields: mockIndexPatternFields, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
}); | ||
|
||
it('should return only index pattern fields when ES|QL query is empty', async () => { | ||
const { result, waitForNextUpdate } = renderHook( | ||
() => | ||
useInvestigationFields({ | ||
esqlQuery: '', | ||
indexPatternsFields: mockIndexPatternFields, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.investigationFields).toEqual(mockIndexPatternFields); | ||
}); | ||
|
||
it('should return only index pattern fields when ES|QL query is undefined', async () => { | ||
const { result } = renderHook( | ||
() => | ||
useInvestigationFields({ | ||
esqlQuery: undefined, | ||
indexPatternsFields: mockIndexPatternFields, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.investigationFields).toEqual(mockIndexPatternFields); | ||
}); | ||
|
||
it('should return index pattern fields concatenated with ES|QL fields when ES|QL query is non-aggregating', async () => { | ||
computeIsESQLQueryAggregatingMock.mockReturnValue(false); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useInvestigationFields({ | ||
esqlQuery: mockEsqlQuery, | ||
indexPatternsFields: mockIndexPatternFields, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.investigationFields).toEqual([ | ||
{ | ||
name: '_custom_field', | ||
type: 'string', | ||
}, | ||
...mockIndexPatternFields, | ||
]); | ||
}); | ||
|
||
it('should return only ES|QL fields when ES|QL query is aggregating', async () => { | ||
computeIsESQLQueryAggregatingMock.mockReturnValue(true); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useInvestigationFields({ | ||
esqlQuery: mockEsqlQuery, | ||
indexPatternsFields: mockIndexPatternFields, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
expect(result.current.investigationFields).toEqual([ | ||
{ | ||
name: '_custom_field', | ||
type: 'string', | ||
}, | ||
]); | ||
}); | ||
}); |
91 changes: 91 additions & 0 deletions
91
...urity_solution/public/detection_engine/rule_creation_ui/hooks/use_investigation_fields.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,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { useMemo } from 'react'; | ||
import type { Datatable, ExpressionsStart } from '@kbn/expressions-plugin/public'; | ||
import type { DataViewFieldBase } from '@kbn/es-query'; | ||
import { computeIsESQLQueryAggregating } from '@kbn/securitysolution-utils'; | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
|
||
import { getEsqlQueryConfig } from '../../rule_creation/logic/get_esql_query_config'; | ||
|
||
const esqlToFields = ( | ||
data: { error: unknown } | Datatable | undefined | null | ||
): DataViewFieldBase[] => { | ||
if (data && 'error' in data) { | ||
return []; | ||
} | ||
|
||
const fields = (data?.columns ?? []).map(({ id, meta }) => { | ||
return { | ||
name: id, | ||
type: meta.type, | ||
}; | ||
}); | ||
|
||
return fields; | ||
}; | ||
|
||
type UseEsqlFields = (esqlQuery: string | undefined) => { | ||
isLoading: boolean; | ||
fields: DataViewFieldBase[]; | ||
}; | ||
|
||
/** | ||
* fetches ES|QL fields and convert them to DataViewBase fields | ||
*/ | ||
const useEsqlFields: UseEsqlFields = (esqlQuery) => { | ||
const kibana = useKibana<{ expressions: ExpressionsStart }>(); | ||
|
||
const { expressions } = kibana.services; | ||
|
||
const queryConfig = getEsqlQueryConfig({ esqlQuery, expressions }); | ||
const { data, isLoading } = useQuery(queryConfig); | ||
|
||
const fields = useMemo(() => { | ||
return esqlToFields(data); | ||
}, [data]); | ||
|
||
return { | ||
fields, | ||
isLoading, | ||
}; | ||
}; | ||
|
||
type UseInvestigationFields = (params: { | ||
esqlQuery: string | undefined; | ||
indexPatternsFields: DataViewFieldBase[]; | ||
}) => { | ||
isLoading: boolean; | ||
investigationFields: DataViewFieldBase[]; | ||
}; | ||
|
||
export const useInvestigationFields: UseInvestigationFields = ({ | ||
esqlQuery, | ||
indexPatternsFields, | ||
}) => { | ||
const { fields: esqlFields, isLoading } = useEsqlFields(esqlQuery); | ||
|
||
const investigationFields = useMemo(() => { | ||
if (!esqlQuery) { | ||
return indexPatternsFields; | ||
} | ||
|
||
// alerts generated from non-aggregating queries are enriched with source document | ||
// so, index patterns fields should be included in the list of investigation fields | ||
const isEsqlQueryAggregating = computeIsESQLQueryAggregating(esqlQuery); | ||
|
||
return isEsqlQueryAggregating ? esqlFields : [...esqlFields, ...indexPatternsFields]; | ||
}, [esqlFields, esqlQuery, indexPatternsFields]); | ||
|
||
return { | ||
investigationFields, | ||
isLoading, | ||
}; | ||
}; |
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