From 14001b5a5c56788f0c77c0b3c3fa6eb9b906a140 Mon Sep 17 00:00:00 2001 From: Kyle Pollich Date: Tue, 1 Mar 2022 09:22:00 -0500 Subject: [PATCH] Resolve latest package version from bundled packages if possible (#126492) (#126579) (cherry picked from commit 50e89720c36b7d735eb8183328aef2b1288cd562) --- .../server/services/epm/registry/index.ts | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/registry/index.ts b/x-pack/plugins/fleet/server/services/epm/registry/index.ts index c70b064684a96..db6a324352ca1 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -8,6 +8,7 @@ import { URL } from 'url'; import mime from 'mime-types'; +import semverGte from 'semver/functions/gte'; import type { Response } from 'node-fetch'; @@ -74,8 +75,11 @@ async function _fetchFindLatestPackage( packageName: string, options?: FetchFindLatestPackageOptions ) { + const logger = appContextService.getLogger(); const { ignoreConstraints = false } = options ?? {}; + const bundledPackage = await getBundledPackageByName(packageName); + const registryUrl = getRegistryUrl(); const url = new URL(`${registryUrl}/search?package=${packageName}&experimental=true`); @@ -83,55 +87,61 @@ async function _fetchFindLatestPackage( setKibanaVersion(url); } - const res = await fetchUrl(url.toString(), 1); - const searchResults: RegistryPackage[] = JSON.parse(res); - - return searchResults; -} - -export async function fetchFindLatestPackageOrThrow( - packageName: string, - options?: FetchFindLatestPackageOptions -) { try { - const searchResults = await _fetchFindLatestPackage(packageName, options); + const res = await fetchUrl(url.toString(), 1); + const searchResults: RegistryPackage[] = JSON.parse(res); + + const latestPackageFromRegistry = searchResults[0] ?? null; - if (!searchResults.length) { - throw new PackageNotFoundError(`[${packageName}] package not found in registry`); + if (bundledPackage && semverGte(bundledPackage.version, latestPackageFromRegistry.version)) { + return bundledPackage; } - return searchResults[0]; + return latestPackageFromRegistry; } catch (error) { - const bundledPackage = await getBundledPackageByName(packageName); + logger.error( + `Failed to fetch latest version of ${packageName} from registry: ${error.message}` + ); - if (!bundledPackage) { - throw error; + // Fall back to the bundled version of the package if it exists + if (bundledPackage) { + return bundledPackage; } - return bundledPackage; + // Otherwise, return null and allow callers to determine whether they'll consider this an error or not + return null; + } +} + +export async function fetchFindLatestPackageOrThrow( + packageName: string, + options?: FetchFindLatestPackageOptions +) { + const latestPackage = await _fetchFindLatestPackage(packageName, options); + + if (!latestPackage) { + throw new PackageNotFoundError(`[${packageName}] package not found in registry`); } + + return latestPackage; } export async function fetchFindLatestPackageOrUndefined( packageName: string, options?: FetchFindLatestPackageOptions ) { + const logger = appContextService.getLogger(); + try { - const searchResults = await _fetchFindLatestPackage(packageName, options); + const latestPackage = await _fetchFindLatestPackage(packageName, options); - if (!searchResults.length) { + if (!latestPackage) { return undefined; } - - return searchResults[0]; + return latestPackage; } catch (error) { - const bundledPackage = await getBundledPackageByName(packageName); - - if (!bundledPackage) { - return undefined; - } - - return bundledPackage; + logger.warn(`Error fetching latest package for ${packageName}: ${error.message}`); + return undefined; } }