From fa0f4074e3869dda9052b55f868fb5866e218ee7 Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Wed, 18 Dec 2024 12:48:25 -0800 Subject: [PATCH] show selected rules on edit; options should only include enabled rules on edit (#1231) Signed-off-by: Amardeepsingh Siglani --- .../AlertCondition/AlertConditionPanel.tsx | 18 +++++++ .../UpdateAlertConditions.tsx | 48 +++++++++++-------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/public/pages/CreateDetector/components/ConfigureAlerts/components/AlertCondition/AlertConditionPanel.tsx b/public/pages/CreateDetector/components/ConfigureAlerts/components/AlertCondition/AlertConditionPanel.tsx index 93042ddc..62707764 100644 --- a/public/pages/CreateDetector/components/ConfigureAlerts/components/AlertCondition/AlertConditionPanel.tsx +++ b/public/pages/CreateDetector/components/ConfigureAlerts/components/AlertCondition/AlertConditionPanel.tsx @@ -73,6 +73,24 @@ export default class AlertConditionPanel extends Component< this.prepareMessage(false /* updateMessage */, true /* onMount */); } + componentDidUpdate( + prevProps: Readonly, + _prevState: Readonly + ): void { + const { rulesOptions, alertCondition } = this.props; + + if (prevProps.rulesOptions !== rulesOptions) { + const selectedNames: EuiComboBoxOptionOption[] = []; + alertCondition.ids.forEach((ruleId) => { + const rule = rulesOptions.find((option) => option.id === ruleId); + if (rule) { + selectedNames.push({ label: rule.name, value: ruleId }); + } + }); + this.setState({ selectedNames }); + } + } + onDetectionTypeChange(detectionType: 'rules' | 'threat_intel', enabled: boolean) { const detectionTypes = new Set(this.props.alertCondition.detection_types); enabled ? detectionTypes.add(detectionType) : detectionTypes.delete(detectionType); diff --git a/public/pages/Detectors/components/UpdateAlertConditions/UpdateAlertConditions.tsx b/public/pages/Detectors/components/UpdateAlertConditions/UpdateAlertConditions.tsx index 885cb8c6..a4e0fff8 100644 --- a/public/pages/Detectors/components/UpdateAlertConditions/UpdateAlertConditions.tsx +++ b/public/pages/Detectors/components/UpdateAlertConditions/UpdateAlertConditions.tsx @@ -19,7 +19,12 @@ import { NotificationsStart } from 'opensearch-dashboards/public'; import { errorNotificationToast, successNotificationToast } from '../../../../utils/helpers'; import { ServerResponse } from '../../../../../server/models/types'; import { DataStore } from '../../../../store/DataStore'; -import { Detector, DetectorCreationStep, DetectorResponse } from '../../../../../types'; +import { + Detector, + DetectorCreationStep, + DetectorResponse, + RuleItemInfoBase, +} from '../../../../../types'; export interface UpdateAlertConditionsProps extends RouteComponentProps { @@ -86,6 +91,11 @@ export default class UpdateAlertConditions extends Component< } } const terms = { 'rule.category': [detector.detector_type.toLowerCase()] }; + const enabledRules = new Set( + detector.inputs[0].detector_input.custom_rules + .map((rule) => rule.id) + .concat(detector.inputs[0].detector_input.pre_packaged_rules.map((rule) => rule.id)) + ); const customRules = await DataStore.rules.getCustomRules(terms); const prePackagedRules = await DataStore.rules.getPrePackagedRules(terms); @@ -93,27 +103,23 @@ export default class UpdateAlertConditions extends Component< const allRules: { [id: string]: RuleSource } = {}; const rulesOptions = new Set(); - prePackagedRules.forEach((hit) => { - allRules[hit._id] = hit._source; - const rule = allRules[hit._id]; - rulesOptions.add({ - name: rule.title, - id: hit._id, - severity: rule.level, - tags: rule.tags.map((tag) => tag.value), + const processRules = (rules: RuleItemInfoBase[]) => { + rules.forEach((hit) => { + allRules[hit._id] = hit._source; + if (enabledRules.has(hit._id)) { + const rule = allRules[hit._id]; + rulesOptions.add({ + name: rule.title, + id: hit._id, + severity: rule.level, + tags: rule.tags.map((tag) => tag.value), + }); + } }); - }); - - customRules.forEach((hit) => { - allRules[hit._id] = hit._source; - const rule = allRules[hit._id]; - rulesOptions.add({ - name: rule.title, - id: hit._id, - severity: rule.level, - tags: rule.tags.map((tag) => tag.value), - }); - }); + }; + + processRules(prePackagedRules); + processRules(customRules); this.setState({ rules: allRules, rulesOptions: Array.from(rulesOptions) }); } catch (e: any) {