[8.8] [Security Solution] Failed getFieldsForIndexPattern calls can result in Exception Flyout getting stuck in loading state (#158371) (#158681)

# Backport

This will backport the following commits from `main` to `8.8`:
- [[Security Solution] Failed getFieldsForIndexPattern calls can result
in Exception Flyout getting stuck in loading state
(#158371)](https://github.com/elastic/kibana/pull/158371)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ievgen
Sorokopud","email":"ievgen.sorokopud@elastic.co"},"sourceCommit":{"committedDate":"2023-05-30T20:23:17Z","message":"[Security
Solution] Failed getFieldsForIndexPattern calls can result in Exception
Flyout getting stuck in loading state (#158371)\n\n##
Summary\r\n\r\nOriginal ticket
https://github.com/elastic/kibana/issues/158110\r\n\r\nThese changes
fixes the issue with the exception flyout which can be\r\nstuck in
loading state in case `getFieldsForIndexPattern` throws
an\r\nexception.\r\n\r\nFixed by putting the `getFieldsForIndexPattern`
call in try/catch. We\r\nuse this call to fetch extended information
about the fields [to show\r\nwarning to the user in case there are some
index\r\nissues](https://github.com/elastic/kibana/pull/149149).
If\r\n`getFieldsForIndexPattern` fails and throws an exception we
will\r\ncontinue using fields without conflicts/unmapped
information.\r\n\r\nI also, noticed that we do not fetch extended
information for the case\r\nwhere user uses index patterns instead of
data views. Fixed this issue\r\nin `useFetchIndex`.\r\n\r\ncc @dhurley14
We will need to adjust either of our PRs depending whose\r\nchanges will
go in first :-)\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"1a3cad1b2744a2fb4be070bc5e60754bc183682c","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:
SecuritySolution","ci:cloud-deploy","v8.9.0","v8.8.1","Team:Detection
Engine"],"number":158371,"url":"https://github.com/elastic/kibana/pull/158371","mergeCommit":{"message":"[Security
Solution] Failed getFieldsForIndexPattern calls can result in Exception
Flyout getting stuck in loading state (#158371)\n\n##
Summary\r\n\r\nOriginal ticket
https://github.com/elastic/kibana/issues/158110\r\n\r\nThese changes
fixes the issue with the exception flyout which can be\r\nstuck in
loading state in case `getFieldsForIndexPattern` throws
an\r\nexception.\r\n\r\nFixed by putting the `getFieldsForIndexPattern`
call in try/catch. We\r\nuse this call to fetch extended information
about the fields [to show\r\nwarning to the user in case there are some
index\r\nissues](https://github.com/elastic/kibana/pull/149149).
If\r\n`getFieldsForIndexPattern` fails and throws an exception we
will\r\ncontinue using fields without conflicts/unmapped
information.\r\n\r\nI also, noticed that we do not fetch extended
information for the case\r\nwhere user uses index patterns instead of
data views. Fixed this issue\r\nin `useFetchIndex`.\r\n\r\ncc @dhurley14
We will need to adjust either of our PRs depending whose\r\nchanges will
go in first :-)\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"1a3cad1b2744a2fb4be070bc5e60754bc183682c"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/158371","number":158371,"mergeCommit":{"message":"[Security
Solution] Failed getFieldsForIndexPattern calls can result in Exception
Flyout getting stuck in loading state (#158371)\n\n##
Summary\r\n\r\nOriginal ticket
https://github.com/elastic/kibana/issues/158110\r\n\r\nThese changes
fixes the issue with the exception flyout which can be\r\nstuck in
loading state in case `getFieldsForIndexPattern` throws
an\r\nexception.\r\n\r\nFixed by putting the `getFieldsForIndexPattern`
call in try/catch. We\r\nuse this call to fetch extended information
about the fields [to show\r\nwarning to the user in case there are some
index\r\nissues](https://github.com/elastic/kibana/pull/149149).
If\r\n`getFieldsForIndexPattern` fails and throws an exception we
will\r\ncontinue using fields without conflicts/unmapped
information.\r\n\r\nI also, noticed that we do not fetch extended
information for the case\r\nwhere user uses index patterns instead of
data views. Fixed this issue\r\nin `useFetchIndex`.\r\n\r\ncc @dhurley14
We will need to adjust either of our PRs depending whose\r\nchanges will
go in first :-)\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"1a3cad1b2744a2fb4be070bc5e60754bc183682c"}},{"branch":"8.8","label":"v8.8.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ievgen Sorokopud <ievgen.sorokopud@elastic.co>
This commit is contained in:
Kibana Machine 2023-05-31 10:51:55 -04:00 committed by GitHub
parent cf1a516130
commit caa8e8e695
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View file

@ -13,3 +13,10 @@ export const ERROR_INDEX_FIELDS_SEARCH = i18n.translate(
defaultMessage: `An error has occurred creating the ad-hoc data view`,
}
);
export const FETCH_FIELDS_WITH_UNMAPPED_DATA_ERROR = i18n.translate(
'xpack.securitySolution.dataView.fetchFields.warning',
{
defaultMessage: 'Failed to fetch detailed fields information',
}
);

View file

@ -8,11 +8,14 @@
import { useEffect, useState, useMemo } from 'react';
import type { DataViewBase } from '@kbn/es-query';
import { useAppToasts } from '../../../common/hooks/use_app_toasts';
import type { Rule } from '../../rule_management/logic/types';
import { useGetInstalledJob } from '../../../common/components/ml/hooks/use_get_jobs';
import { useKibana } from '../../../common/lib/kibana';
import { useFetchIndex } from '../../../common/containers/source';
import * as i18n from '../../../common/containers/source/translations';
export interface ReturnUseFetchExceptionFlyoutData {
isLoading: boolean;
indexPatterns: DataViewBase;
@ -25,6 +28,7 @@ export interface ReturnUseFetchExceptionFlyoutData {
*/
export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExceptionFlyoutData => {
const { data, spaces } = useKibana().services;
const { addWarning } = useAppToasts();
const [dataViewLoading, setDataViewLoading] = useState(false);
const [activeSpaceId, setActiveSpaceId] = useState('');
const isSingleRule = useMemo(() => rules != null && rules.length === 1, [rules]);
@ -97,20 +101,25 @@ export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExcep
if (activeSpaceId !== '' && memoDataViewId) {
setDataViewLoading(true);
const dv = await data.dataViews.get(memoDataViewId);
const fieldsWithUnmappedInfo = await data.dataViews.getFieldsForIndexPattern(dv, {
pattern: '',
includeUnmapped: true,
});
let fieldsWithUnmappedInfo = null;
try {
fieldsWithUnmappedInfo = await data.dataViews.getFieldsForIndexPattern(dv, {
pattern: '',
includeUnmapped: true,
});
} catch (error) {
addWarning(error, { title: i18n.FETCH_FIELDS_WITH_UNMAPPED_DATA_ERROR });
}
setDataViewLoading(false);
setDataViewIndexPatterns({
...dv,
fields: fieldsWithUnmappedInfo,
...(fieldsWithUnmappedInfo ? { fields: fieldsWithUnmappedInfo } : {}),
});
}
};
fetchSingleDataView();
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId]);
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId, addWarning]);
// Determine whether to use index patterns or data views
const indexPatternsToUse = useMemo(