[Fleet] Use package policy version to determine managed policy upgrades (#125788) (#125826)

(cherry picked from commit 3dd2d75bce)

Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-02-16 13:21:39 -05:00 committed by GitHub
parent a1a5b914fb
commit 090f7aab59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 79 deletions

View file

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

View file

@ -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;
}