Skip to content

Commit

Permalink
[Fleet] Check bundled packages when querying package info in getInfo …
Browse files Browse the repository at this point in the history
…API (#141462)

* Check bundled packages when querying package info in getInfo API

* Add tests for fetchInfo

* Remove needless beforeEach
  • Loading branch information
kpollich authored Sep 22, 2022
1 parent 4137fb3 commit 8774859
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
49 changes: 48 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/registry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

import { PackageNotFoundError } from '../../../errors';
import { PackageNotFoundError, RegistryResponseError } from '../../../errors';

import * as Archive from '../archive';

import {
splitPkgKey,
fetchFindLatestPackageOrUndefined,
fetchFindLatestPackageOrThrow,
fetchInfo,
getLicensePath,
} from '.';

Expand All @@ -21,6 +24,11 @@ const mockLogger = mockLoggerFactory.get('mock logger');

const mockGetBundledPackageByName = jest.fn();
const mockFetchUrl = jest.fn();

const MockArchive = Archive as jest.Mocked<typeof Archive>;

jest.mock('../archive');

jest.mock('../..', () => ({
appContextService: {
getLogger: () => mockLogger,
Expand Down Expand Up @@ -150,6 +158,8 @@ describe('fetch package', () => {
});

describe('getLicensePath', () => {
MockArchive.getPathParts = jest.requireActual('../archive').getPathParts;

it('returns first license path if found', () => {
const path = getLicensePath([
'/package/good-1.0.0/NOTICE.txt',
Expand All @@ -171,3 +181,40 @@ describe('getLicensePath', () => {
expect(path).toEqual(undefined);
});
});

describe('fetchInfo', () => {
beforeEach(() => {
jest.resetAllMocks();

mockFetchUrl.mockRejectedValueOnce(new RegistryResponseError('Not found', 404));
mockGetBundledPackageByName.mockResolvedValueOnce({
name: 'test-package',
version: '1.0.0',
buffer: Buffer.from(''),
});
MockArchive.generatePackageInfoFromArchiveBuffer.mockResolvedValueOnce({
paths: [],
packageInfo: {
name: 'test-package',
title: 'Test Package',
version: '1.0.0',
description: 'Test package',
owner: { github: 'elastic' },
format_version: '1.0.0',
},
});
});

it('falls back to bundled package when one exists', async () => {
const fetchedInfo = await fetchInfo('test-package', '1.0.0');
expect(fetchedInfo).toBeTruthy();
});

it('throws when no corresponding bundled package exists', async () => {
try {
await fetchInfo('test-package', '1.0.0');
} catch (e) {
expect(e).toBeInstanceOf(PackageNotFoundError);
}
});
});
18 changes: 17 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
RegistrySearchResults,
GetCategoriesRequest,
PackageVerificationResult,
ArchivePackage,
} from '../../../types';
import {
getArchiveFilelist,
Expand Down Expand Up @@ -159,7 +160,10 @@ export async function fetchFindLatestPackageOrUndefined(
}
}

export async function fetchInfo(pkgName: string, pkgVersion: string): Promise<RegistryPackage> {
export async function fetchInfo(
pkgName: string,
pkgVersion: string
): Promise<RegistryPackage | ArchivePackage> {
const registryUrl = getRegistryUrl();
try {
// Trailing slash avoids 301 redirect / extra hop
Expand All @@ -168,6 +172,18 @@ export async function fetchInfo(pkgName: string, pkgVersion: string): Promise<Re
return res;
} catch (err) {
if (err instanceof RegistryResponseError && err.status === 404) {
// Check bundled packages in case the exact package being requested is available on disk
const bundledPackage = await getBundledPackageByName(pkgName);

if (bundledPackage && bundledPackage.version === pkgVersion) {
const archivePackage = await generatePackageInfoFromArchiveBuffer(
bundledPackage.buffer,
'application/zip'
);

return archivePackage.packageInfo;
}

throw new PackageNotFoundError(`${pkgName}@${pkgVersion} not found`);
}
throw err;
Expand Down

0 comments on commit 8774859

Please sign in to comment.