Skip to content

Commit

Permalink
[Security Solution][Admin][Endpoint Generator] Endpoint generator use…
Browse files Browse the repository at this point in the history
…s correct merge to preserve array values (#131273)
  • Loading branch information
parkiino authored Apr 30, 2022
1 parent 578364c commit 7cc364a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Client } from '@elastic/elasticsearch';
import { cloneDeep, merge } from 'lodash';
import { cloneDeep } from 'lodash';
import { AxiosResponse } from 'axios';
import uuid from 'uuid';
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -35,7 +35,7 @@ import {
indexFleetEndpointPolicy,
} from './index_fleet_endpoint_policy';
import { metadataCurrentIndexPattern } from '../constants';
import { EndpointDataLoadingError, wrapErrorAndRejectPromise } from './utils';
import { EndpointDataLoadingError, mergeAndAppendArrays, wrapErrorAndRejectPromise } from './utils';

export interface IndexedHostsResponse
extends IndexedFleetAgentResponse,
Expand Down Expand Up @@ -141,7 +141,7 @@ export async function indexEndpointHostDocs({
epmEndpointPackage.version
);

merge(response, createdPolicies);
mergeAndAppendArrays(response, createdPolicies);

// eslint-disable-next-line require-atomic-updates
realPolicies[appliedPolicyId] = createdPolicies.integrationPolicies[0];
Expand All @@ -160,7 +160,7 @@ export async function indexEndpointHostDocs({
);

enrolledAgent = indexedAgentResponse.agents[0];
merge(response, indexedAgentResponse);
mergeAndAppendArrays(response, indexedAgentResponse);
}
// Update the Host metadata record with the ID of the "real" policy along with the enrolled agent id
hostMetadata = {
Expand Down Expand Up @@ -317,9 +317,9 @@ export const deleteIndexedEndpointHosts = async (
.catch(wrapErrorAndRejectPromise);
}

merge(response, await deleteIndexedFleetAgents(esClient, indexedData));
merge(response, await deleteIndexedEndpointAndFleetActions(esClient, indexedData));
merge(response, await deleteIndexedFleetEndpointPolicies(kbnClient, indexedData));
mergeAndAppendArrays(response, await deleteIndexedFleetAgents(esClient, indexedData));
mergeAndAppendArrays(response, await deleteIndexedEndpointAndFleetActions(esClient, indexedData));
mergeAndAppendArrays(response, await deleteIndexedFleetEndpointPolicies(kbnClient, indexedData));

return response;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { mergeWith } from 'lodash';

export class EndpointDataLoadingError extends Error {
constructor(message: string, public meta?: unknown) {
super(message);
Expand All @@ -18,3 +20,13 @@ export const wrapErrorIfNeeded = (error: Error): EndpointDataLoadingError =>

// Use it in Promise's `.catch()` as `.catch(wrapErrorAndRejectPromise)`
export const wrapErrorAndRejectPromise = (error: Error) => Promise.reject(wrapErrorIfNeeded(error));

export const mergeAndAppendArrays = <T, S>(destinationObj: T, srcObj: S): T => {
const customizer = (objValue: T[keyof T], srcValue: S[keyof S]) => {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
};

return mergeWith(destinationObj, srcObj, customizer);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import seedrandom from 'seedrandom';
// eslint-disable-next-line import/no-extraneous-dependencies
import { KbnClient } from '@kbn/test';
import { AxiosResponse } from 'axios';
import { merge } from 'lodash';
import {
CreatePackagePolicyResponse,
EPM_API_ROUTES,
Expand All @@ -26,6 +25,7 @@ import {
import { enableFleetServerIfNecessary } from './data_loaders/index_fleet_server';
import { indexAlerts } from './data_loaders/index_alerts';
import { setupFleetForEndpoint } from './data_loaders/setup_fleet_for_endpoint';
import { mergeAndAppendArrays } from './data_loaders/utils';

export type IndexedHostsAndAlertsResponse = IndexedHostsResponse;

Expand Down Expand Up @@ -105,7 +105,7 @@ export async function indexHostsAndAlerts(
generator,
});

merge(response, indexedHosts);
mergeAndAppendArrays(response, indexedHosts);

await indexAlerts({
client,
Expand Down

0 comments on commit 7cc364a

Please sign in to comment.