Skip to content

Commit

Permalink
[Fleet] Use package policy version to determine managed policy upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Feb 16, 2022
1 parent 04e87d5 commit c126917
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -229,39 +207,25 @@ 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);
});
});

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);
});
Expand All @@ -271,39 +235,25 @@ 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);
});
});

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);
});
Expand Down
22 changes: 6 additions & 16 deletions x-pack/plugins/fleet/server/services/managed_package_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit c126917

Please sign in to comment.