[8.6] [Security Solution] [Exceptions] Hook for exceptions to fetch the security solution default data view was missing the space id (#146915) (#146994)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Security Solution] [Exceptions] Hook for exceptions to fetch the
security solution default data view was missing the space id
(#146915)](https://github.com/elastic/kibana/pull/146915)

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

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

<!--BACKPORT [{"author":{"name":"Devin W.
Hurley","email":"devin.hurley@elastic.co"},"sourceCommit":{"committedDate":"2022-12-05T13:16:15Z","message":"[Security
Solution] [Exceptions] Hook for exceptions to fetch the security
solution default data view was missing the space id (#146915)\n\n##
Summary\r\n\r\nappend space id to security solution default data view
in\r\n`useFetchIndexPatterns` hook in rule
exceptions\r\n\r\nCo-authored-by: Gloria Hornero
<gloria.hornero@elastic.co>","sha":"ed9e9627706d9b894f429a5b8084f5afa9f5f271","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","review","release_note:skip","impact:critical","Feature:Rule
Exceptions","Team:Security Solution
Platform","v8.6.0","v8.7.0"],"number":146915,"url":"https://github.com/elastic/kibana/pull/146915","mergeCommit":{"message":"[Security
Solution] [Exceptions] Hook for exceptions to fetch the security
solution default data view was missing the space id (#146915)\n\n##
Summary\r\n\r\nappend space id to security solution default data view
in\r\n`useFetchIndexPatterns` hook in rule
exceptions\r\n\r\nCo-authored-by: Gloria Hornero
<gloria.hornero@elastic.co>","sha":"ed9e9627706d9b894f429a5b8084f5afa9f5f271"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/146915","number":146915,"mergeCommit":{"message":"[Security
Solution] [Exceptions] Hook for exceptions to fetch the security
solution default data view was missing the space id (#146915)\n\n##
Summary\r\n\r\nappend space id to security solution default data view
in\r\n`useFetchIndexPatterns` hook in rule
exceptions\r\n\r\nCo-authored-by: Gloria Hornero
<gloria.hornero@elastic.co>","sha":"ed9e9627706d9b894f429a5b8084f5afa9f5f271"}}]}]
BACKPORT-->

Co-authored-by: Devin W. Hurley <devin.hurley@elastic.co>
This commit is contained in:
Kibana Machine 2022-12-05 09:48:34 -05:00 committed by GitHub
parent 7131e1baa6
commit fbcb7951bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,19 +24,32 @@ export interface ReturnUseFetchExceptionFlyoutData {
*
*/
export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExceptionFlyoutData => {
const { data } = useKibana().services;
const { data, spaces } = useKibana().services;
const [dataViewLoading, setDataViewLoading] = useState(false);
const [activeSpaceId, setActiveSpaceId] = useState('');
const isSingleRule = useMemo(() => rules != null && rules.length === 1, [rules]);
const isMLRule = useMemo(
() => rules != null && isSingleRule && rules[0].type === 'machine_learning',
[isSingleRule, rules]
);
useEffect(() => {
const fetchAndSetActiveSpace = async () => {
if (spaces) {
const aSpace = await spaces.getActiveSpace();
setActiveSpaceId(aSpace.id);
}
};
fetchAndSetActiveSpace();
}, [spaces]);
// If data view is defined, it superceeds use of rule defined index patterns.
// If no rule is available, use fields from default data view id.
const memoDataViewId = useMemo(
() =>
rules != null && isSingleRule ? rules[0].data_view_id || null : 'security-solution-default',
[isSingleRule, rules]
rules != null && isSingleRule
? rules[0].data_view_id || null
: `security-solution-${activeSpaceId}`,
[isSingleRule, rules, activeSpaceId]
);
const memoNonDataViewIndexPatterns = useMemo(
@ -74,7 +87,10 @@ export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExcep
const [dataViewIndexPatterns, setDataViewIndexPatterns] = useState<DataViewBase | null>(null);
useEffect(() => {
const fetchSingleDataView = async () => {
if (memoDataViewId) {
// ensure the memoized data view includes a space id, otherwise
// we could be trying to fetch a data view that does not exist, which would
// throw an error here.
if (activeSpaceId !== '' && memoDataViewId) {
setDataViewLoading(true);
const dv = await data.dataViews.get(memoDataViewId);
setDataViewLoading(false);
@ -83,7 +99,7 @@ export const useFetchIndexPatterns = (rules: Rule[] | null): ReturnUseFetchExcep
};
fetchSingleDataView();
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns]);
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId]);
// Determine whether to use index patterns or data views
const indexPatternsToUse = useMemo(