[Security Solution] When adding an Exception and Source value on the Severity override option, are not offering suggestions (#150701)

## Summary

These changes fixes the issue with the broken field source value
auto-suggestion which was caused by [this
PR](https://github.com/elastic/kibana/pull/149149/files#diff-2b9142306df119b8c31e8a66c9b93b2a83f74239b1655919881165ac06398623R148).

The problem is that the `stringifyIndices` variable is used to:
1. represent all indices down the road where we request suggestions for
a field in
`src/plugins/unified_search/public/autocomplete/providers/value_suggestion_provider.ts`
2. be a parameter to check whether we should memoize the results of
`getIndexFields` and `getDataViewStateFromIndexFields` methods

By modifying it to include the notion of whether we should include
unmapped information, solves the second point but breaks the first one.
Thus, suggestion request stopped working.

The fix is to revert changes to `stringifyIndices` variable and add
extra parameter to `getIndexFields` and
`getDataViewStateFromIndexFields` methods, so that we can take into
account `includeUnmapped` flag during memoization.

Bug ticket #150663

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Ievgen Sorokopud 2023-02-14 09:41:29 +01:00 committed by GitHub
parent da89dde7ac
commit ace5c10c2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View file

@ -74,7 +74,7 @@ describe('Exceptions flyout', () => {
createExceptionList(getExceptionList(), getExceptionList().list_id).then((response) =>
createCustomRule({
...getNewRule(),
dataSource: { index: ['exceptions-*'], type: 'indexPatterns' },
dataSource: { index: ['auditbeat-*', 'exceptions-*'], type: 'indexPatterns' },
exceptionLists: [
{
id: response.body.id,
@ -289,11 +289,28 @@ describe('Exceptions flyout', () => {
openExceptionFlyoutFromEmptyViewerPrompt();
cy.get(FIELD_INPUT).eq(0).click({ force: true });
cy.get(FIELD_INPUT).eq(0).type('unique');
cy.get(EXCEPTION_FIELD_LIST).contains('unique_value.test');
closeExceptionBuilderFlyout();
});
it('Validates auto-suggested fields correctly', () => {
// open add exception modal
openExceptionFlyoutFromEmptyViewerPrompt();
// add exception item name
addExceptionFlyoutItemName('My item name');
// add an entry with a value and submit button should enable
addExceptionEntryFieldValue('agent.type', 0);
cy.get(VALUES_INPUT).eq(0).type(`{enter}`);
cy.get(VALUES_INPUT).eq(0).type(`{downarrow}{enter}`);
cy.get(CONFIRM_BTN).should('be.enabled');
closeExceptionBuilderFlyout();
});
// TODO - Add back in error states into modal
describe.skip('flyout errors', () => {
beforeEach(() => {

View file

@ -43,7 +43,7 @@ export const getAllFieldsByName = (
keyBy('name', getAllBrowserFields(browserFields));
export const getIndexFields = memoizeOne(
(title: string, fields: IndexField[]): DataViewBase =>
(title: string, fields: IndexField[], _includeUnmapped: boolean = false): DataViewBase =>
fields && fields.length > 0
? {
fields: fields.map((field) =>
@ -63,7 +63,10 @@ export const getIndexFields = memoizeOne(
title,
}
: { fields: [], title },
(newArgs, lastArgs) => newArgs[0] === lastArgs[0] && newArgs[1].length === lastArgs[1].length
(newArgs, lastArgs) =>
newArgs[0] === lastArgs[0] &&
newArgs[1].length === lastArgs[1].length &&
newArgs[2] === lastArgs[2]
);
/**
@ -145,21 +148,24 @@ export const useFetchIndex = (
if (isCompleteResponse(response)) {
Promise.resolve().then(() => {
ReactDOM.unstable_batchedUpdates(() => {
const stringifyIndices = `${response.indicesExist
.sort()
.join()} (includeUnmapped: ${includeUnmapped})`;
const stringifyIndices = response.indicesExist.sort().join();
previousIndexesName.current = response.indicesExist;
const { browserFields } = getDataViewStateFromIndexFields(
stringifyIndices,
response.indexFields
response.indexFields,
includeUnmapped
);
setLoading(false);
setState({
browserFields,
indexes: response.indicesExist,
indexExists: response.indicesExist.length > 0,
indexPatterns: getIndexFields(stringifyIndices, response.indexFields),
indexPatterns: getIndexFields(
stringifyIndices,
response.indexFields,
includeUnmapped
),
});
searchSubscription$.current.unsubscribe();

View file

@ -50,7 +50,7 @@ interface DataViewInfo {
* VERY mutatious on purpose to improve the performance of the transform.
*/
export const getDataViewStateFromIndexFields = memoizeOne(
(_title: string, fields: IndexField[]): DataViewInfo => {
(_title: string, fields: IndexField[], _includeUnmapped: boolean = false): DataViewInfo => {
// Adds two dangerous casts to allow for mutations within this function
type DangerCastForMutation = Record<string, {}>;
@ -78,7 +78,10 @@ export const getDataViewStateFromIndexFields = memoizeOne(
}
);
},
(newArgs, lastArgs) => newArgs[0] === lastArgs[0] && newArgs[1].length === lastArgs[1].length
(newArgs, lastArgs) =>
newArgs[0] === lastArgs[0] &&
newArgs[1].length === lastArgs[1].length &&
newArgs[2] === lastArgs[2]
);
export const useDataView = (): {