-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Siem query rule - reduce field_caps usage #184890
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e21707
limiting changes to query rule
mattkime e35bfa1
remove some comments, redundant code
mattkime 8a55ba3
improve mocks
mattkime 67acf8c
Merge branch 'main' into siem_field_caps_diet
mattkime 838c4c0
improved filter handling when building field list to load
mattkime 947dc6e
Merge branch 'siem_field_caps_diet' of github.com:mattkime/kibana int…
mattkime 5d783e9
remove comment
mattkime 9fd6711
minor cleanup
mattkime 4910dfc
Merge branch 'main' into siem_field_caps_diet
mattkime 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
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
58 changes: 58 additions & 0 deletions
58
src/plugins/data/common/search/search_source/query_to_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,58 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { DataViewLazy } from '@kbn/data-views-plugin/common'; | ||
import { fromKueryExpression, getKqlFieldNames } from '@kbn/es-query'; | ||
import type { SearchRequest } from './fetch'; | ||
import { EsQuerySortValue } from '../..'; | ||
|
||
export async function queryToFields({ | ||
dataView, | ||
sort, | ||
request, | ||
}: { | ||
dataView: DataViewLazy; | ||
sort?: EsQuerySortValue | EsQuerySortValue[]; | ||
request: SearchRequest; | ||
}) { | ||
let fields = dataView.timeFieldName ? [dataView.timeFieldName] : []; | ||
if (sort) { | ||
const sortArr = Array.isArray(sort) ? sort : [sort]; | ||
fields.push(...sortArr.flatMap((s) => Object.keys(s))); | ||
} | ||
for (const query of request.query) { | ||
if (query.query) { | ||
const nodes = fromKueryExpression(query.query); | ||
const queryFields = getKqlFieldNames(nodes); | ||
fields = fields.concat(queryFields); | ||
} | ||
} | ||
const filters = request.filters; | ||
if (filters) { | ||
const filtersArr = Array.isArray(filters) ? filters : [filters]; | ||
for (const f of filtersArr) { | ||
// unified search bar filters have meta object and key (regular filters) | ||
// unified search bar "custom" filters ("Edit as query DSL", where meta.key is not present but meta is) | ||
// Any other Elasticsearch query DSL filter that gets passed in by consumers (not coming from unified search, and these probably won't have a meta key at all) | ||
if (f?.meta?.key && f.meta.disabled !== true) { | ||
fields.push(f.meta.key); | ||
} | ||
} | ||
} | ||
|
||
// if source filtering is enabled, we need to fetch all the fields | ||
const fieldName = | ||
dataView.getSourceFiltering() && dataView.getSourceFiltering().excludes.length ? ['*'] : fields; | ||
|
||
if (fieldName.length) { | ||
return (await dataView.getFields({ fieldName })).getFieldMapSorted(); | ||
} | ||
|
||
// no fields needed to be loaded for query | ||
return {}; | ||
} |
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
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
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
67 changes: 67 additions & 0 deletions
67
...ity_solution/server/lib/detection_engine/rule_types/utils/get_query_filter_load_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,67 @@ | ||
/* | ||
* 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 type { Language } from '@kbn/securitysolution-io-ts-alerting-types'; | ||
import type { Filter, EsQueryConfig, DataViewFieldBase } from '@kbn/es-query'; | ||
import { DataView } from '@kbn/data-views-plugin/server'; | ||
import { queryToFields } from '@kbn/data-plugin/common'; | ||
import type { DataViewsContract } from '@kbn/data-views-plugin/common'; | ||
import type { FieldFormatsStartCommon } from '@kbn/field-formats-plugin/common'; | ||
import { buildEsQuery } from '@kbn/es-query'; | ||
import type { ESBoolQuery } from '../../../../../common/typed_json'; | ||
import { getAllFilters } from './get_query_filter'; | ||
import type { | ||
IndexPatternArray, | ||
RuleQuery, | ||
} from '../../../../../common/api/detection_engine/model/rule_schema'; | ||
|
||
export const getQueryFilterLoadFields = | ||
(dataViewsService: DataViewsContract) => | ||
async ({ | ||
query, | ||
language, | ||
filters, | ||
index, | ||
exceptionFilter, | ||
}: { | ||
query: RuleQuery; | ||
language: Language; | ||
filters: unknown; | ||
index: IndexPatternArray; | ||
exceptionFilter: Filter | undefined; | ||
fields?: DataViewFieldBase[]; | ||
}): Promise<ESBoolQuery> => { | ||
const config: EsQueryConfig = { | ||
allowLeadingWildcards: true, | ||
queryStringOptions: { analyze_wildcard: true }, | ||
ignoreFilterIfFieldNotInIndex: false, | ||
dateFormatTZ: 'Zulu', | ||
}; | ||
|
||
const initialQuery = { query, language }; | ||
const allFilters = getAllFilters(filters as Filter[], exceptionFilter); | ||
|
||
const title = (index ?? []).join(); | ||
|
||
const dataViewLazy = await dataViewsService.createDataViewLazy({ title }); | ||
|
||
const flds = await queryToFields({ | ||
dataView: dataViewLazy, | ||
request: { query: [initialQuery], filters: allFilters }, | ||
}); | ||
|
||
const dataViewLimitedFields = new DataView({ | ||
spec: { title }, | ||
fieldFormats: {} as unknown as FieldFormatsStartCommon, | ||
shortDotsEnable: false, | ||
metaFields: [], | ||
}); | ||
|
||
dataViewLimitedFields.fields.replaceAll(Object.values(flds).map((fld) => fld.toSpec())); | ||
|
||
return buildEsQuery(dataViewLimitedFields, initialQuery, allFilters, config); | ||
}; |
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.
A note for follow up from security: we should split this function into 3 functions, one that loads fields automatically, one that requires fields to be passed in, and one that does not handle fields at all. Then we should explicitly call the version we want for each use case instead of trying to ensure that the parameters are passed in as expected.
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.
Hey @marshallmain - can you open a ticket to track this a tech debt?
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.
#185890