diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts index 4b9b6e40d4dda..41554a1df120c 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts @@ -137,18 +137,16 @@ export const createRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) => message: `To create a rule, the index must exist first. Index ${finalIndex} does not exist`, }); } - if (ruleId != null) { - if ( - foundRules != null && - foundRules.length > 0 && - foundRules.find((rule) => rule.params.ruleId === ruleId) - ) { - return createBulkErrorObject({ - ruleId, - statusCode: 409, - message: `rule_id: "${ruleId}" already exists`, - }); - } + if ( + ruleId != null && + foundRules != null && + foundRules.some((rule) => rule.params.ruleId === ruleId) + ) { + return createBulkErrorObject({ + ruleId, + statusCode: 409, + message: `rule_id: "${ruleId}" already exists`, + }); } const createdRule = await createRules({ alertsClient, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/import_rules_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/import_rules_route.ts index 7d5a4dedfe512..80fc5d8e70421 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/import_rules_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/import_rules_route.ts @@ -194,10 +194,7 @@ export const importRulesRoute = (router: IRouter, config: ConfigType, ml: SetupP throwHttpError(await mlAuthz.validateRuleType(type)); - let rule; - if (rules != null) { - rule = rules.find((aRule) => aRule.params.ruleId === ruleId); - } + const rule = rules?.find((aRule) => aRule.params.ruleId === ruleId); if (rule == null) { await createRules({ diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts index ec32dadc6a7c7..70e1a9743d201 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts @@ -25,6 +25,7 @@ import { updateRulesNotifications } from '../../rules/update_rules_notifications import { ruleStatusSavedObjectsClientFactory } from '../../signals/rule_status_saved_objects_client'; import { readRules } from '../../rules/read_rules'; import { PartialFilter } from '../../types'; +import { RuleAlertType } from '../../rules/types'; export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) => { router.patch( @@ -58,7 +59,7 @@ export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) => } return acc; }, []); - const foundRules = await readRules({ + const foundRules: RuleAlertType[] | null = await readRules({ alertsClient, ruleIds, id: undefined, @@ -118,14 +119,12 @@ export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) => throwHttpError(await mlAuthz.validateRuleType(type)); } - const foundRule = foundRules?.find((rule) => rule.params.ruleId === ruleId); - let existingRule = foundRule != null ? [foundRule] : null; - if (foundRule == null) { - // try the id - existingRule = await readRules({ alertsClient, ruleIds: undefined, id }); - } + const searchedRule = foundRules?.find((rule) => rule.params.ruleId === ruleId); + const existingRule = searchedRule + ? [searchedRule] + : await readRules({ alertsClient, ruleIds: undefined, id }); - if (existingRule != null && existingRule.length > 0 && existingRule[0].params.type) { + if (existingRule != null && existingRule[0].params.type) { // reject an unauthorized modification of an ML rule throwHttpError(await mlAuthz.validateRuleType(existingRule[0].params.type)); }