[Fleet] Fix preconfiguration error when renaming a preconfigured policy (#124953) (#125075)

* Fix preconfiguration error when renaming a preconfigured policy

* Add test + only compare on ID if it's defined on the preconfigured policy

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 94fbacc6bd)

# Conflicts:
#	x-pack/plugins/fleet/server/services/preconfiguration.test.ts
This commit is contained in:
Kyle Pollich 2022-02-09 10:17:45 -05:00 committed by GitHub
parent 64bbe49898
commit 09d2cd1ce5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View file

@ -209,6 +209,7 @@ const spyAgentPolicyServicBumpAllAgentPoliciesForOutput = jest.spyOn(
describe('policy preconfiguration', () => {
beforeEach(() => {
mockedPackagePolicyService.getByIDs.mockReset();
mockedPackagePolicyService.create.mockReset();
mockInstalledPackages.clear();
mockInstallPackageErrors.clear();
@ -391,6 +392,55 @@ describe('policy preconfiguration', () => {
);
});
it('should not try to recreate preconfigure package policy that has been renamed', async () => {
const soClient = getPutPreconfiguredPackagesMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
mockedPackagePolicyService.getByIDs.mockResolvedValue([
{ name: 'Renamed package policy', id: 'test_package1' } as PackagePolicy,
]);
mockConfiguredPolicies.set('test-id', {
name: 'Test policy',
description: 'Test policy description',
unenroll_timeout: 120,
namespace: 'default',
id: 'test-id',
package_policies: [
{
name: 'test_package1',
id: 'test_package1',
},
],
is_managed: true,
} as PreconfiguredAgentPolicy);
await ensurePreconfiguredPackagesAndPolicies(
soClient,
esClient,
[
{
name: 'Test policy',
namespace: 'default',
id: 'test-id',
is_managed: true,
package_policies: [
{
package: { name: 'test_package' },
name: 'test_package1',
id: 'test_package1',
},
],
},
] as PreconfiguredAgentPolicy[],
[{ name: 'test_package', version: '3.0.0' }],
mockDefaultOutput,
DEFAULT_SPACE_ID
);
expect(mockedPackagePolicyService.create).not.toBeCalled();
});
it('should throw an error when trying to install duplicate packages', async () => {
const soClient = getPutPreconfiguredPackagesMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

View file

@ -334,7 +334,9 @@ export async function ensurePreconfiguredPackagesAndPolicies(
const packagePoliciesToAdd = installedPackagePolicies.filter((installablePackagePolicy) => {
return !(agentPolicyWithPackagePolicies?.package_policies as PackagePolicy[]).some(
(packagePolicy) => packagePolicy.name === installablePackagePolicy.name
(packagePolicy) =>
(packagePolicy.id !== undefined && packagePolicy.id === installablePackagePolicy.id) ||
packagePolicy.name === installablePackagePolicy.name
);
});