Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Resolve latest package version from bundled packages if possible #126492

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions x-pack/plugins/fleet/server/services/epm/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -74,64 +75,73 @@ async function _fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
) {
const logger = appContextService.getLogger();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fetch functions could really use some unit tests at this point now that there are varying fallback cases, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I added an item in our board 👍

const { ignoreConstraints = false } = options ?? {};

const bundledPackage = await getBundledPackageByName(packageName);

const registryUrl = getRegistryUrl();
const url = new URL(`${registryUrl}/search?package=${packageName}&experimental=true`);

if (!ignoreConstraints) {
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;
}
}

Expand Down