From 090f7aab599af3951ce0201d2b00811c6dde3b30 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 16 Feb 2022 13:21:39 -0500 Subject: [PATCH] [Fleet] Use package policy version to determine managed policy upgrades (#125788) (#125826) (cherry picked from commit 3dd2d75bce2939b39e72b7ef60e32392b3fad876) Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com> --- .../services/managed_package_policies.test.ts | 76 ++++--------------- .../services/managed_package_policies.ts | 22 ++---- 2 files changed, 19 insertions(+), 79 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts b/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts index 7ccfeb4fe7641..af3b3ef4dfccd 100644 --- a/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts +++ b/x-pack/plugins/fleet/server/services/managed_package_policies.test.ts @@ -7,11 +7,11 @@ import { elasticsearchServiceMock, savedObjectsClientMock } from 'src/core/server/mocks'; -import type { Installation, PackageInfo } from '../../common'; +import type { Installation } from '../../common'; import { shouldUpgradePolicies, upgradeManagedPackagePolicies } from './managed_package_policies'; import { packagePolicyService } from './package_policy'; -import { getPackageInfo, getInstallation } from './epm/packages'; +import { getInstallation } from './epm/packages'; jest.mock('./package_policy'); jest.mock('./epm/packages'); @@ -30,7 +30,6 @@ describe('upgradeManagedPackagePolicies', () => { afterEach(() => { (packagePolicyService.get as jest.Mock).mockReset(); (packagePolicyService.getUpgradeDryRunDiff as jest.Mock).mockReset(); - (getPackageInfo as jest.Mock).mockReset(); (getInstallation as jest.Mock).mockReset(); (packagePolicyService.upgrade as jest.Mock).mockReset(); }); @@ -69,17 +68,10 @@ describe('upgradeManagedPackagePolicies', () => { } ); - (getPackageInfo as jest.Mock).mockImplementationOnce( - ({ savedObjectsClient, pkgName, pkgVersion }) => ({ - name: pkgName, - version: pkgVersion, - keepPoliciesUpToDate: false, - }) - ); - (getInstallation as jest.Mock).mockResolvedValueOnce({ id: 'test-installation', version: '0.0.1', + keep_policies_up_to_date: false, }); await upgradeManagedPackagePolicies(soClient, esClient, ['non-managed-package-id']); @@ -121,17 +113,10 @@ describe('upgradeManagedPackagePolicies', () => { } ); - (getPackageInfo as jest.Mock).mockImplementationOnce( - ({ savedObjectsClient, pkgName, pkgVersion }) => ({ - name: pkgName, - version: pkgVersion, - keepPoliciesUpToDate: true, - }) - ); - (getInstallation as jest.Mock).mockResolvedValueOnce({ id: 'test-installation', version: '1.0.0', + keep_policies_up_to_date: true, }); await upgradeManagedPackagePolicies(soClient, esClient, ['managed-package-id']); @@ -177,17 +162,10 @@ describe('upgradeManagedPackagePolicies', () => { } ); - (getPackageInfo as jest.Mock).mockImplementationOnce( - ({ savedObjectsClient, pkgName, pkgVersion }) => ({ - name: pkgName, - version: pkgVersion, - keepPoliciesUpToDate: true, - }) - ); - (getInstallation as jest.Mock).mockResolvedValueOnce({ id: 'test-installation', version: '1.0.0', + keep_policies_up_to_date: true, }); const result = await upgradeManagedPackagePolicies(soClient, esClient, [ @@ -229,19 +207,12 @@ describe('shouldUpgradePolicies', () => { describe('package policy is up-to-date', () => { describe('keep_policies_up_to_date is true', () => { it('returns false', () => { - const packageInfo = { - version: '1.0.0', - keepPoliciesUpToDate: true, - }; - const installedPackage = { version: '1.0.0', + keep_policies_up_to_date: true, }; - const result = shouldUpgradePolicies( - packageInfo as PackageInfo, - installedPackage as Installation - ); + const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation); expect(result).toBe(false); }); @@ -249,19 +220,12 @@ describe('shouldUpgradePolicies', () => { describe('keep_policies_up_to_date is false', () => { it('returns false', () => { - const packageInfo = { - version: '1.0.0', - keepPoliciesUpToDate: false, - }; - const installedPackage = { version: '1.0.0', + keep_policies_up_to_date: false, }; - const result = shouldUpgradePolicies( - packageInfo as PackageInfo, - installedPackage as Installation - ); + const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation); expect(result).toBe(false); }); @@ -271,19 +235,12 @@ describe('shouldUpgradePolicies', () => { describe('package policy is out-of-date', () => { describe('keep_policies_up_to_date is true', () => { it('returns true', () => { - const packageInfo = { - version: '1.0.0', - keepPoliciesUpToDate: true, - }; - const installedPackage = { version: '1.1.0', + keep_policies_up_to_date: true, }; - const result = shouldUpgradePolicies( - packageInfo as PackageInfo, - installedPackage as Installation - ); + const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation); expect(result).toBe(true); }); @@ -291,19 +248,12 @@ describe('shouldUpgradePolicies', () => { describe('keep_policies_up_to_date is false', () => { it('returns false', () => { - const packageInfo = { - version: '1.0.0', - keepPoliciesUpToDate: false, - }; - const installedPackage = { version: '1.1.0', + keep_policies_up_to_date: false, }; - const result = shouldUpgradePolicies( - packageInfo as PackageInfo, - installedPackage as Installation - ); + const result = shouldUpgradePolicies('1.0.0', installedPackage as Installation); expect(result).toBe(false); }); diff --git a/x-pack/plugins/fleet/server/services/managed_package_policies.ts b/x-pack/plugins/fleet/server/services/managed_package_policies.ts index c702cfe96d986..77715ad488feb 100644 --- a/x-pack/plugins/fleet/server/services/managed_package_policies.ts +++ b/x-pack/plugins/fleet/server/services/managed_package_policies.ts @@ -8,14 +8,10 @@ import type { ElasticsearchClient, SavedObjectsClientContract } from 'src/core/server'; import semverGte from 'semver/functions/gte'; -import type { - Installation, - PackageInfo, - UpgradePackagePolicyDryRunResponseItem, -} from '../../common'; +import type { Installation, UpgradePackagePolicyDryRunResponseItem } from '../../common'; import { appContextService } from './app_context'; -import { getInstallation, getPackageInfo } from './epm/packages'; +import { getInstallation } from './epm/packages'; import { packagePolicyService } from './package_policy'; export interface UpgradeManagedPackagePoliciesResult { @@ -42,12 +38,6 @@ export const upgradeManagedPackagePolicies = async ( continue; } - const packageInfo = await getPackageInfo({ - savedObjectsClient: soClient, - pkgName: packagePolicy.package.name, - pkgVersion: packagePolicy.package.version, - }); - const installedPackage = await getInstallation({ savedObjectsClient: soClient, pkgName: packagePolicy.package.name, @@ -62,7 +52,7 @@ export const upgradeManagedPackagePolicies = async ( continue; } - if (shouldUpgradePolicies(packageInfo, installedPackage)) { + if (shouldUpgradePolicies(packagePolicy.package.version, installedPackage)) { // Since upgrades don't report diffs/errors, we need to perform a dry run first in order // to notify the user of any granular policy upgrade errors that occur during Fleet's // preconfiguration check @@ -101,13 +91,13 @@ export const upgradeManagedPackagePolicies = async ( }; export function shouldUpgradePolicies( - packageInfo: PackageInfo, + packagePolicyPackageVersion: string, installedPackage: Installation ): boolean { const isPolicyVersionGteInstalledVersion = semverGte( - packageInfo.version, + packagePolicyPackageVersion, installedPackage.version ); - return !isPolicyVersionGteInstalledVersion && !!packageInfo.keepPoliciesUpToDate; + return !isPolicyVersionGteInstalledVersion && !!installedPackage.keep_policies_up_to_date; }