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

[Security Solution][Admin][Endpoint Generator] Endpoint generator uses correct merge to preserve array values #131273

Merged
merged 3 commits into from
Apr 30, 2022
Merged
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 @@ -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