diff --git a/x-pack/plugins/fleet/server/services/epm/packages/cleanup.ts b/x-pack/plugins/fleet/server/services/epm/packages/cleanup.ts index b612e261a0eca..bf48f207589a8 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/cleanup.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/cleanup.ts @@ -17,22 +17,34 @@ import { appContextService } from '../..'; export async function removeOldAssets(options: { savedObjectsClient: SavedObjectsClientContract; pkgName: string; - oldVersion: string; + currentVersion: string; }) { - const { savedObjectsClient, pkgName, oldVersion } = options; + const { savedObjectsClient, pkgName, currentVersion } = options; - // check if old version has assets - const assets = await savedObjectsClient.find({ + // find all assets of older versions + const aggs = { + versions: { terms: { field: `${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_version` } }, + }; + const oldVersionsAgg = (await savedObjectsClient.find({ type: ASSETS_SAVED_OBJECT_TYPE, - filter: `${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_name:${pkgName} AND ${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_version:${oldVersion}`, - // how to query if more than one page? - perPage: 100, - fields: ['id'], - }); - const refs = assets.saved_objects.map( - (obj) => ({ id: obj.id, type: ASSETS_SAVED_OBJECT_TYPE } as PackageAssetReference) - ); + filter: `${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_name:${pkgName} AND ${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_version<${currentVersion}`, + aggs, + page: 0, + perPage: 0, + })) as any; + + const oldVersions = oldVersionsAgg.aggregations.versions.buckets.map((obj) => obj.key); + + for (const oldVersion of oldVersions) { + await removeAssetsFromVersion(savedObjectsClient, pkgName, oldVersion); + } +} +async function removeAssetsFromVersion( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + oldVersion: string +) { // check if any policies are using this package version const { total } = await packagePolicyService.list(savedObjectsClient, { kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${pkgName} AND ${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.version:${oldVersion}`, @@ -47,5 +59,20 @@ export async function removeOldAssets(options: { return; } - await removeArchiveEntries({ savedObjectsClient, refs }); + // check if old version has assets + const finder = await savedObjectsClient.createPointInTimeFinder({ + type: ASSETS_SAVED_OBJECT_TYPE, + filter: `${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_name:${pkgName} AND ${ASSETS_SAVED_OBJECT_TYPE}.attributes.package_version:${oldVersion}`, + perPage: 1000, + fields: ['id'], + }); + + for await (const assets of finder.find()) { + const refs = assets.saved_objects.map( + (obj) => ({ id: obj.id, type: ASSETS_SAVED_OBJECT_TYPE } as PackageAssetReference) + ); + + await removeArchiveEntries({ savedObjectsClient, refs }); + } + await finder.close(); } diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.ts index 2568f40594f10..871dae6edc064 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -38,6 +38,7 @@ import { isUnremovablePackage, getInstallation, getInstallationObject } from './ import { removeInstallation } from './remove'; import { getPackageSavedObjects } from './get'; import { _installPackage } from './_install_package'; +import { removeOldAssets } from './cleanup'; export async function isPackageInstalled(options: { savedObjectsClient: SavedObjectsClientContract; @@ -267,7 +268,12 @@ async function installPackageFromRegistry({ installType, installSource: 'registry', }) - .then((assets) => { + .then(async (assets) => { + await removeOldAssets({ + savedObjectsClient, + pkgName: packageInfo.name, + currentVersion: packageInfo.version, + }); return { assets, status: 'installed', installType }; }) .catch(async (err: Error) => { diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index 443bec6c9b850..45d2f9da1eacf 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -291,6 +291,8 @@ class PackagePolicyService { })); } + i = 0; + public async list( soClient: SavedObjectsClientContract, options: ListWithKuery @@ -578,8 +580,8 @@ class PackagePolicyService { }); await removeOldAssets({ savedObjectsClient: soClient, - pkgName: packagePolicy.package.name, - oldVersion: packagePolicy.package.version, + pkgName: packageInfo.name, + currentVersion: packageInfo.version, }); } catch (error) { result.push({