Skip to content

Commit

Permalink
[Fleet] Fix preconfiguration error when renaming a preconfigured poli…
Browse files Browse the repository at this point in the history
…cy (#124953) (#126141)

* 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 94fbacc)

# Conflicts:
#	x-pack/plugins/fleet/server/services/preconfiguration.test.ts

Co-authored-by: Kyle Pollich <kyle.pollich@elastic.co>
  • Loading branch information
joshdover and kpollich authored Feb 22, 2022
1 parent 610a684 commit ca9117d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
49 changes: 49 additions & 0 deletions x-pack/plugins/fleet/server/services/preconfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const spyAgentPolicyServicBumpAllAgentPoliciesForOutput = jest.spyOn(

describe('policy preconfiguration', () => {
beforeEach(() => {
mockedPackagePolicyService.getByIDs.mockReset();
mockedPackagePolicyService.create.mockReset();
mockInstalledPackages.clear();
mockInstallPackageErrors.clear();
Expand Down Expand Up @@ -517,6 +518,54 @@ describe('policy preconfiguration', () => {
expect(nonFatalErrorsB.length).toBe(0);
});

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
);

expect(mockedPackagePolicyService.create).not.toBeCalled();
});

it('should update a managed policy if top level fields are changed', async () => {
const soClient = getPutPreconfiguredPackagesMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/services/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

Expand Down

0 comments on commit ca9117d

Please sign in to comment.