Skip to content

Commit

Permalink
[8.6] [Security Solution] Failed to Fetch Rules and Timeline in Previ…
Browse files Browse the repository at this point in the history
…ew Flyout (#146904) (#146921)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Security Solution] Failed to Fetch Rules and Timeline in Preview
Flyout (#146904)](#146904)

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

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

<!--BACKPORT [{"author":{"name":"Ievgen
Sorokopud","email":"[email protected]"},"sourceCommit":{"committedDate":"2022-12-02T20:17:54Z","message":"[Security
Solution] Failed to Fetch Rules and Timeline in Preview Flyout
(#146904)\n\n## Summary\r\n\r\nThese changes fixes the issue with the
unexpected \"Failed to Fetch Rule\"\r\nerror toast that appears when
user opens the alert details from the Rule\r\nPreview page.\r\n\r\nThe
issue was introduced by
[recent\r\nrefactoring](#142950)
where we\r\nswitched from the optional toast to showing error toast
always when it\r\nis not possible to fetch the rule (see
`useRuleWithFallback` hook). In\r\ncase of Rule Preview the rule does
not exist and thus server returns 404\r\n(not found) error. In case of
Rule Preview we would like to ignore this\r\nerror and build the rule
from the alert.\r\n\r\nBug Ticket:
#146858","sha":"1b4c66101b02dd4c01582a0e7b684eb5b499101a","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:
SecuritySolution","Team:Detection Rules","Team:Detection
Alerts","backport:prev-minor","ci:cloud-deploy","v8.7.0"],"number":146904,"url":"https://github.com/elastic/kibana/pull/146904","mergeCommit":{"message":"[Security
Solution] Failed to Fetch Rules and Timeline in Preview Flyout
(#146904)\n\n## Summary\r\n\r\nThese changes fixes the issue with the
unexpected \"Failed to Fetch Rule\"\r\nerror toast that appears when
user opens the alert details from the Rule\r\nPreview page.\r\n\r\nThe
issue was introduced by
[recent\r\nrefactoring](#142950)
where we\r\nswitched from the optional toast to showing error toast
always when it\r\nis not possible to fetch the rule (see
`useRuleWithFallback` hook). In\r\ncase of Rule Preview the rule does
not exist and thus server returns 404\r\n(not found) error. In case of
Rule Preview we would like to ignore this\r\nerror and build the rule
from the alert.\r\n\r\nBug Ticket:
#146858","sha":"1b4c66101b02dd4c01582a0e7b684eb5b499101a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/146904","number":146904,"mergeCommit":{"message":"[Security
Solution] Failed to Fetch Rules and Timeline in Preview Flyout
(#146904)\n\n## Summary\r\n\r\nThese changes fixes the issue with the
unexpected \"Failed to Fetch Rule\"\r\nerror toast that appears when
user opens the alert details from the Rule\r\nPreview page.\r\n\r\nThe
issue was introduced by
[recent\r\nrefactoring](#142950)
where we\r\nswitched from the optional toast to showing error toast
always when it\r\nis not possible to fetch the rule (see
`useRuleWithFallback` hook). In\r\ncase of Rule Preview the rule does
not exist and thus server returns 404\r\n(not found) error. In case of
Rule Preview we would like to ignore this\r\nerror and build the rule
from the alert.\r\n\r\nBug Ticket:
#146858","sha":"1b4c66101b02dd4c01582a0e7b684eb5b499101a"}}]}]
BACKPORT-->

Co-authored-by: Ievgen Sorokopud <[email protected]>
  • Loading branch information
kibanamachine and e40pud authored Dec 2, 2022
1 parent f0da283 commit a3b8bc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ import * as i18n from './translations';
* @param id desired Rule ID's (not rule_id)
*
*/
export const useRule = (id: string) => {
export const useRule = (id: string, showToast = true) => {
const { addError } = useAppToasts();

return useFetchRuleByIdQuery(id, {
onError: (error) => {
addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE });
},
});
return useFetchRuleByIdQuery(
id,
showToast
? {
onError: (error) => {
addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE });
},
}
: undefined
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const buildLastAlertQuery = (ruleId: string) => ({
* In that case, try to fetch the latest alert generated by the rule and retrieve the rule data from the alert (fallback).
*/
export const useRuleWithFallback = (ruleId: string): UseRuleWithFallback => {
const { isLoading: ruleLoading, data: ruleData, error, refetch } = useRule(ruleId);
const { isLoading: ruleLoading, data: ruleData, error, refetch } = useRule(ruleId, false);
const { addError } = useAppToasts();

const isExistingRule = !isNotFoundError(error);
Expand Down

0 comments on commit a3b8bc3

Please sign in to comment.