Skip to content

Commit

Permalink
[Defend workflows] Replace differenceWith with xorWith and remove the…
Browse files Browse the repository at this point in the history
… helper (#157966)
  • Loading branch information
tomsonpl authored May 17, 2023
1 parent 68f2c77 commit 9abdd90
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { partition } from 'lodash/fp';
import { differenceWith, isEqual } from 'lodash';
import pMap from 'p-map';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -385,17 +384,3 @@ export const convertAlertSuppressionToSnake = (
missing_fields_strategy: input.missingFieldsStrategy,
}
: undefined;

export const findDifferenceInArrays = <T1, T2>(
arr1: T1[] = [],
arr2: T2[] = []
): Array<T1 | T2> => {
if (arr1.length === 0) {
return arr2;
}

if (arr2.length === 0) {
return arr1;
}
return differenceWith(arr1, arr2, isEqual);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { validateNonExact } from '@kbn/securitysolution-io-ts-utils';

import type { PartialRule } from '@kbn/alerting-plugin/server';
import type { Rule } from '@kbn/alerting-plugin/common';
import { isEqual, xorWith } from 'lodash';
import {
RESPONSE_ACTION_API_COMMANDS_TO_CONSOLE_COMMAND_MAP,
RESPONSE_CONSOLE_ACTION_COMMANDS_TO_REQUIRED_AUTHZ,
Expand All @@ -26,7 +27,7 @@ import type { RuleParams, RuleAlertType, UnifiedQueryRuleParams } from '../../ru
import { isAlertType } from '../../rule_schema';
import type { BulkError } from '../../routes/utils';
import { createBulkErrorObject } from '../../routes/utils';
import { findDifferenceInArrays, transform } from './utils';
import { transform } from './utils';
import { internalRuleToAPIResponse } from '../normalization/rule_converters';
import type {
ResponseAction,
Expand Down Expand Up @@ -95,12 +96,14 @@ export const validateResponseActionsPermissions = async (

const endpointAuthz = await securitySolution.getEndpointAuthz();

const differences = findDifferenceInArrays<ResponseAction, RuleResponseAction>(
// finds elements that are not included in both arrays
const symmetricDifference = xorWith<ResponseAction | RuleResponseAction>(
ruleUpdate.response_actions,
existingRule?.params?.responseActions
existingRule?.params?.responseActions,
isEqual
);

differences.forEach((action) => {
symmetricDifference.forEach((action) => {
if (!('command' in action?.params)) {
return;
}
Expand Down

0 comments on commit 9abdd90

Please sign in to comment.