Skip to content

Commit

Permalink
show selected rules on edit; options should only include enabled rule…
Browse files Browse the repository at this point in the history
…s on edit (#1231)

Signed-off-by: Amardeepsingh Siglani <[email protected]>
(cherry picked from commit fa0f407)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Dec 18, 2024
1 parent b14e632 commit 12b594e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ export default class AlertConditionPanel extends Component<
this.prepareMessage(false /* updateMessage */, true /* onMount */);
}

componentDidUpdate(
prevProps: Readonly<AlertConditionPanelProps>,
_prevState: Readonly<AlertConditionPanelState>
): void {
const { rulesOptions, alertCondition } = this.props;

if (prevProps.rulesOptions !== rulesOptions) {
const selectedNames: EuiComboBoxOptionOption<string>[] = [];
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any, { detectorHit: DetectorHit }> {
Expand Down Expand Up @@ -86,34 +91,35 @@ 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);

const allRules: { [id: string]: RuleSource } = {};
const rulesOptions = new Set<RuleOptions>();

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) {
Expand Down

0 comments on commit 12b594e

Please sign in to comment.