Skip to content

Commit

Permalink
remove ruletype param from useAlertSuppression hook since all rule ty…
Browse files Browse the repository at this point in the history
…pes support suppression, with eql sequence dependent on feature flag, so that is now the only parameter necessary
  • Loading branch information
dhurley14 committed Dec 4, 2024
1 parent 7abac02 commit a38142f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
const areSuppressionFieldsSelected = isThresholdRule || Boolean(alertSuppressionFields?.length);

const { isSuppressionEnabled: isAlertSuppressionEnabled } = useAlertSuppression(
ruleType,
isEqlSequenceQuery(queryBar?.query?.query as string)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ export const RuleDefinitionSection = ({
ruleType: rule.type,
});

const { isSuppressionEnabled } = useAlertSuppression(rule.type);
const { isSuppressionEnabled } = useAlertSuppression();

const definitionSectionListItems = prepareDefinitionSectionListItems(
rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,23 @@
*/

import { renderHook } from '@testing-library/react';
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types';
import * as useIsExperimentalFeatureEnabledMock from '../../../common/hooks/use_experimental_features';
import { useAlertSuppression } from './use_alert_suppression';

describe('useAlertSuppression', () => {
jest
.spyOn(useIsExperimentalFeatureEnabledMock, 'useIsExperimentalFeatureEnabled')
.mockReturnValue(false);
(
[
'new_terms',
'threat_match',
'saved_query',
'query',
'threshold',
'eql',
'esql',
'machine_learning',
] as Type[]
).forEach((ruleType) => {
it(`should return the isSuppressionEnabled true for ${ruleType} rule type that exists in SUPPRESSIBLE_ALERT_RULES`, () => {
const { result } = renderHook(() => useAlertSuppression(ruleType));

expect(result.current.isSuppressionEnabled).toBe(true);
});
});

it('should return false if rule type is undefined', () => {
const { result } = renderHook(() => useAlertSuppression(undefined));
expect(result.current.isSuppressionEnabled).toBe(false);
it(`should return the isSuppressionEnabled true if query for all rule types is not an eql sequence query`, () => {
const { result } = renderHook(() => useAlertSuppression());
expect(result.current.isSuppressionEnabled).toBe(true);
});

it('should return false if rule type is not a suppressible rule', () => {
const { result } = renderHook(() => useAlertSuppression('OTHER_RULE_TYPE' as Type));

jest
.spyOn(useIsExperimentalFeatureEnabledMock, 'useIsExperimentalFeatureEnabled')
.mockReturnValue(false);
it('should return isSuppressionEnabled false for eql sequence query when feature flag is disabled', () => {
const { result } = renderHook(() => useAlertSuppression(true));
expect(result.current.isSuppressionEnabled).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,24 @@
* 2.0.
*/
import { useCallback } from 'react';
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types';
import { isEqlRule, isSuppressibleAlertRule } from '../../../../common/detection_engine/utils';
import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features';

export interface UseAlertSuppressionReturn {
isSuppressionEnabled: boolean;
}

export const useAlertSuppression = (
ruleType: Type | undefined,
isEqlSequenceQuery = false
): UseAlertSuppressionReturn => {
export const useAlertSuppression = (isEqlSequenceQuery = false): UseAlertSuppressionReturn => {
const isAlertSuppressionForSequenceEQLRuleEnabled = useIsExperimentalFeatureEnabled(
'alertSuppressionForSequenceEqlRuleEnabled'
);

const isSuppressionEnabledForRuleType = useCallback(() => {
if (!ruleType) {
return false;
}

if (isEqlRule(ruleType) && isEqlSequenceQuery) {
if (isEqlSequenceQuery) {
return isAlertSuppressionForSequenceEQLRuleEnabled;
}

return isSuppressibleAlertRule(ruleType);
}, [ruleType, isAlertSuppressionForSequenceEQLRuleEnabled, isEqlSequenceQuery]);
return true;
}, [isAlertSuppressionForSequenceEQLRuleEnabled, isEqlSequenceQuery]);

return {
isSuppressionEnabled: isSuppressionEnabledForRuleType(),
Expand Down

0 comments on commit a38142f

Please sign in to comment.