Skip to content

Commit

Permalink
[Fleet] update package policies to remove deleted agent policy ref (e…
Browse files Browse the repository at this point in the history
…lastic#187370)

## Summary

Closes elastic#187331

Update package policies with multiple agent policy references to remove
deleted agent policy id.

See steps to verify in the linked issue.

- create an integration policy and add it to multiple agent policies
- delete one agent policy
- verify that the edit integration policy page loads, and the
integration policy is linked to the remaining agent policies

<img width="1305" alt="image"
src="https://github.com/elastic/kibana/assets/90178898/a0c3dc22-b703-42ab-b40d-91c2723cce01">
<img width="1257" alt="image"
src="https://github.com/elastic/kibana/assets/90178898/218bf1aa-7208-49f5-8bdc-e09b47c2924e">


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
juliaElastic authored Jul 3, 2024
1 parent 19db776 commit 837cd30
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
38 changes: 12 additions & 26 deletions x-pack/plugins/fleet/server/services/agent_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,31 +530,6 @@ describe('Agent policy', () => {
})
);
});

it('should delete all integration polices', async () => {
mockedPackagePolicyService.findAllForAgentPolicy.mockReturnValue([
{
id: 'package-1',
policy_id: ['policy_1'],
policy_ids: ['policy_1', 'int_policy_2'],
},
{
id: 'package-2',
policy_id: ['policy_1'],
policy_ids: ['policy_1'],
},
{
id: 'package-3',
},
] as any);
await agentPolicyService.delete(soClient, esClient, 'mocked');
expect(mockedPackagePolicyService.delete).toBeCalledWith(
expect.anything(),
expect.anything(),
['package-1', 'package-2', 'package-3'],
expect.anything()
);
});
});

describe('with enableReusableIntegrationPolicies enabled', () => {
Expand Down Expand Up @@ -667,13 +642,24 @@ describe('Agent policy', () => {
id: 'package-3',
},
] as any);
await agentPolicyService.delete(soClient, esClient, 'mocked');
await agentPolicyService.delete(soClient, esClient, 'policy_1');
expect(mockedPackagePolicyService.delete).toBeCalledWith(
expect.anything(),
expect.anything(),
['package-2', 'package-3'],
expect.anything()
);
expect(mockedPackagePolicyService.bulkUpdate).toBeCalledWith(
expect.anything(),
expect.anything(),
[
{
id: 'package-1',
policy_id: 'int_policy_2',
policy_ids: ['int_policy_2'],
},
]
);
});
});
});
Expand Down
71 changes: 49 additions & 22 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,22 +1008,45 @@ class AgentPolicyService {
`Cannot delete agent policy ${id} that contains managed package policies`
);
}
const packagePoliciesToDelete = this.packagePoliciesWithoutMultiplePolicies(packagePolicies);
const { policiesWithSingleAP: packagePoliciesToDelete, policiesWithMultipleAP } =
this.packagePoliciesWithSingleAndMultiplePolicies(packagePolicies);

if (packagePoliciesToDelete.length > 0) {
await packagePolicyService.delete(
soClient,
esClient,
packagePoliciesToDelete.map((p) => p.id),
{
force: options?.force,
skipUnassignFromAgentPolicies: true,
}
);
logger.debug(
`Deleted package policies with single agent policy with ids ${packagePoliciesToDelete
.map((policy) => policy.id)
.join(', ')}`
);
}

await packagePolicyService.delete(
soClient,
esClient,
packagePoliciesToDelete.map((p) => p.id),
{
force: options?.force,
skipUnassignFromAgentPolicies: true,
}
);
logger.debug(
`Deleted package policies with ids ${packagePoliciesToDelete
.map((policy) => policy.id)
.join(', ')}`
);
if (policiesWithMultipleAP.length > 0) {
await packagePolicyService.bulkUpdate(
soClient,
esClient,
policiesWithMultipleAP.map((policy) => {
const newPolicyIds = policy.policy_ids.filter((policyId) => policyId !== id);
return {
...policy,
policy_id: newPolicyIds[0],
policy_ids: newPolicyIds,
};
})
);
logger.debug(
`Updated package policies with multiple agent policies with ids ${policiesWithMultipleAP
.map((policy) => policy.id)
.join(', ')}`
);
}
}

if (agentPolicy.is_preconfigured && !options?.force) {
Expand Down Expand Up @@ -1557,14 +1580,18 @@ class AgentPolicyService {
}
}

private packagePoliciesWithoutMultiplePolicies(packagePolicies: PackagePolicy[]) {
private packagePoliciesWithSingleAndMultiplePolicies(packagePolicies: PackagePolicy[]): {
policiesWithSingleAP: PackagePolicy[];
policiesWithMultipleAP: PackagePolicy[];
} {
// Find package policies that don't have multiple agent policies and mark them for deletion
if (appContextService.getExperimentalFeatures().enableReusableIntegrationPolicies) {
return packagePolicies.filter(
(policy) => !policy?.policy_ids || policy?.policy_ids.length <= 1
);
}
return packagePolicies;
const policiesWithSingleAP = packagePolicies.filter(
(policy) => !policy?.policy_ids || policy?.policy_ids.length <= 1
);
const policiesWithMultipleAP = packagePolicies.filter(
(policy) => policy?.policy_ids && policy?.policy_ids.length > 1
);
return { policiesWithSingleAP, policiesWithMultipleAP };
}
}

Expand Down

0 comments on commit 837cd30

Please sign in to comment.