Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.17] Fix edit trigger for detectors #1236

Open
wants to merge 1 commit into
base: 2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading