-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Support snoozing in rules table (#153083)
**Addresses:** #147735 ## Summary The PR adds an ability to set rule snoozing in the rules management table. Screen recording: https://user-images.githubusercontent.com/3775283/229066538-5effc1af-f481-4749-a964-a071c4393c8f.mov ### Checklist - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
15 changed files
with
493 additions
and
53 deletions.
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
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
62 changes: 62 additions & 0 deletions
62
...tion/public/detection_engine/rule_management/api/hooks/use_fetch_rules_snooze_settings.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,62 @@ | ||
/* | ||
* 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 { INTERNAL_ALERTING_API_FIND_RULES_PATH } from '@kbn/alerting-plugin/common'; | ||
import type { UseQueryOptions } from '@tanstack/react-query'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import type { RuleSnoozeSettings } from '../../logic'; | ||
import { fetchRulesSnoozeSettings } from '../api'; | ||
import { DEFAULT_QUERY_OPTIONS } from './constants'; | ||
|
||
const FETCH_RULE_SNOOZE_SETTINGS_QUERY_KEY = ['GET', INTERNAL_ALERTING_API_FIND_RULES_PATH]; | ||
|
||
/** | ||
* A wrapper around useQuery provides default values to the underlying query, | ||
* like query key, abortion signal. | ||
* | ||
* @param queryArgs - fetch rule snoozing settings ids | ||
* @param queryOptions - react-query options | ||
* @returns useQuery result | ||
*/ | ||
export const useFetchRulesSnoozeSettings = ( | ||
ids: string[], | ||
queryOptions?: UseQueryOptions<RuleSnoozeSettings[], Error, RuleSnoozeSettings[], string[]> | ||
) => { | ||
return useQuery( | ||
[...FETCH_RULE_SNOOZE_SETTINGS_QUERY_KEY, ...ids], | ||
async ({ signal }) => { | ||
const response = await fetchRulesSnoozeSettings({ ids, signal }); | ||
|
||
return response.data; | ||
}, | ||
{ | ||
...DEFAULT_QUERY_OPTIONS, | ||
...queryOptions, | ||
} | ||
); | ||
}; | ||
|
||
/** | ||
* We should use this hook to invalidate the cache. For example, rule | ||
* snooze modification should lead to cache invalidation. | ||
* | ||
* @returns A rules cache invalidation callback | ||
*/ | ||
export const useInvalidateFetchRulesSnoozeSettingsQuery = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useCallback(() => { | ||
/** | ||
* Invalidate all queries that start with FIND_RULES_QUERY_KEY. This | ||
* includes the in-memory query cache and paged query cache. | ||
*/ | ||
queryClient.invalidateQueries(FETCH_RULE_SNOOZE_SETTINGS_QUERY_KEY, { | ||
refetchType: 'active', | ||
}); | ||
}, [queryClient]); | ||
}; |
59 changes: 59 additions & 0 deletions
59
...ecurity_solution/public/detection_engine/rule_management/components/rule_snooze_badge.tsx
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,59 @@ | ||
/* | ||
* 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 { EuiButtonIcon, EuiToolTip } from '@elastic/eui'; | ||
import React, { useMemo } from 'react'; | ||
import { useUserData } from '../../../detections/components/user_info'; | ||
import { hasUserCRUDPermission } from '../../../common/utils/privileges'; | ||
import { useKibana } from '../../../common/lib/kibana'; | ||
import { useInvalidateFetchRulesSnoozeSettingsQuery } from '../api/hooks/use_fetch_rules_snooze_settings'; | ||
import { useRulesTableContext } from '../../rule_management_ui/components/rules_table/rules_table/rules_table_context'; | ||
import * as i18n from './translations'; | ||
|
||
interface RuleSnoozeBadgeProps { | ||
id: string; // Rule SO's id (not ruleId) | ||
} | ||
|
||
export function RuleSnoozeBadge({ id }: RuleSnoozeBadgeProps): JSX.Element { | ||
const RulesListNotifyBadge = useKibana().services.triggersActionsUi.getRulesListNotifyBadge; | ||
const [{ canUserCRUD }] = useUserData(); | ||
const hasCRUDPermissions = hasUserCRUDPermission(canUserCRUD); | ||
const { | ||
state: { rulesSnoozeSettings }, | ||
} = useRulesTableContext(); | ||
const invalidateFetchRuleSnoozeSettings = useInvalidateFetchRulesSnoozeSettingsQuery(); | ||
const rule = useMemo(() => { | ||
const ruleSnoozeSettings = rulesSnoozeSettings.data[id]; | ||
|
||
return { | ||
id: ruleSnoozeSettings?.id ?? '', | ||
muteAll: ruleSnoozeSettings?.mute_all ?? false, | ||
activeSnoozes: ruleSnoozeSettings?.active_snoozes ?? [], | ||
isSnoozedUntil: ruleSnoozeSettings?.is_snoozed_until | ||
? new Date(ruleSnoozeSettings.is_snoozed_until) | ||
: undefined, | ||
snoozeSchedule: ruleSnoozeSettings?.snooze_schedule, | ||
isEditable: hasCRUDPermissions, | ||
}; | ||
}, [id, rulesSnoozeSettings, hasCRUDPermissions]); | ||
|
||
if (rulesSnoozeSettings.isError) { | ||
return ( | ||
<EuiToolTip content={i18n.UNABLE_TO_FETCH_RULE_SNOOZE_SETTINGS}> | ||
<EuiButtonIcon size="s" iconType="bellSlash" disabled /> | ||
</EuiToolTip> | ||
); | ||
} | ||
|
||
return ( | ||
<RulesListNotifyBadge | ||
rule={rule} | ||
isLoading={rulesSnoozeSettings.isLoading} | ||
onRuleChanged={invalidateFetchRuleSnoozeSettings} | ||
/> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
...gins/security_solution/public/detection_engine/rule_management/components/translations.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,15 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const UNABLE_TO_FETCH_RULE_SNOOZE_SETTINGS = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.ruleSnoozeBadge.error.unableToFetch', | ||
{ | ||
defaultMessage: 'Unable to fetch snooze settings', | ||
} | ||
); |
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
Oops, something went wrong.