Skip to content

Commit

Permalink
Unlink policy from TA when removing the entire policy. Also fixes an …
Browse files Browse the repository at this point in the history
…issue with Without method from lodash
  • Loading branch information
dasansol92 committed Aug 13, 2021
1 parent 2b0efcf commit aca93ba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ export const deleteAgentPoliciesHandler: RequestHandler<
const body: DeleteAgentPolicyResponse = await agentPolicyService.delete(
soClient,
esClient,
request.body.agentPolicyId
request.body.agentPolicyId,
context,
request
);
return response.ok({
body,
Expand Down
20 changes: 18 additions & 2 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
ElasticsearchClient,
SavedObjectsClientContract,
SavedObjectsBulkUpdateResponse,
KibanaRequest,
RequestHandlerContext,
} from 'src/core/server';

import { SavedObjectsErrorHelpers } from '../../../../../src/core/server';
Expand Down Expand Up @@ -39,6 +41,7 @@ import {
packageToPackagePolicy,
AGENT_POLICY_INDEX,
} from '../../common';
import type { DeletePackagePoliciesResponse } from '../../common';
import type {
DeleteAgentPolicyResponse,
Settings,
Expand Down Expand Up @@ -585,7 +588,9 @@ class AgentPolicyService {
public async delete(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
id: string
id: string,
context: RequestHandlerContext,
request: KibanaRequest<unknown, unknown, unknown, any>
): Promise<DeleteAgentPolicyResponse> {
const agentPolicy = await this.get(soClient, id, false);
if (!agentPolicy) {
Expand Down Expand Up @@ -616,14 +621,25 @@ class AgentPolicyService {
}

if (agentPolicy.package_policies && agentPolicy.package_policies.length) {
await packagePolicyService.delete(
const body: DeletePackagePoliciesResponse = await packagePolicyService.delete(
soClient,
esClient,
agentPolicy.package_policies as string[],
{
skipUnassignFromAgentPolicies: true,
}
);
try {
await packagePolicyService.runExternalCallbacks(
'postPackagePolicyDelete',
body,
context,
request
);
} catch (error) {
const logger = appContextService.getLogger();
logger.error(`An error occurred executing external callback: ${error}`);
}
}

if (agentPolicy.is_preconfigured) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ describe('ingest_integration tests ', () => {
listId: 'fake',
comments: [],
entries: [],
itemId: '1',
namespaceType: 'agnostic',
item_id: '1',
namespace_type: 'agnostic',
name: 'TA with policy assigned',
osTypes: [],
os_types: [],
description: 'TA with policy assigned ',
meta: undefined,
tags: [`policy:${policyId}`],
Expand Down Expand Up @@ -341,6 +341,9 @@ describe('ingest_integration tests ', () => {

expect(exceptionListClient.updateExceptionListItem).toHaveBeenCalledWith({
...fakeTA,
itemId: fakeTA.item_id,
namespaceType: fakeTA.namespace_type,
osTypes: fakeTA.os_types,
tags: [],
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ export const getPackagePolicyDeleteCallback = (
if (!exceptionsClient) {
return;
}
const promises: Array<Promise<void>> = [];
const policiesToRemove: Array<Promise<void>> = [];
for (const policy of deletePackagePolicy) {
if (isEndpointPackagePolicy(policy)) {
if (experimentalFeatures?.trustedAppsByPolicyEnabled) {
promises.push(removePolicyFromTrustedApps(exceptionsClient, policy));
policiesToRemove.push(removePolicyFromTrustedApps(exceptionsClient, policy));
}
}
}
await Promise.all(promises);
await Promise.all(policiesToRemove);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import { ENDPOINT_TRUSTED_APPS_LIST_ID } from '@kbn/securitysolution-list-constants';
import { without } from 'lodash/fp';
import { ExceptionListClient, UpdateExceptionListItemOptions } from '../../../../lists/server';
import { ExceptionListClient } from '../../../../lists/server';

interface DeletePolicy {
id: string;
Expand Down Expand Up @@ -53,8 +52,11 @@ export const removePolicyFromTrustedApps = async (
for (const trustedApp of trustedApps) {
updates.push(
exceptionsClient.updateExceptionListItem({
...((trustedApp as unknown) as UpdateExceptionListItemOptions),
tags: without(trustedApp.tags, `policy:${policy.id}`),
...trustedApp,
itemId: trustedApp.item_id,
namespaceType: trustedApp.namespace_type,
osTypes: trustedApp.os_types,
tags: trustedApp.tags.filter((currentPolicy) => currentPolicy !== `policy:${policy.id}`),
})
);
}
Expand Down

0 comments on commit aca93ba

Please sign in to comment.