From 607014a021e6f87eb7a43cda49269cf37ec8cb11 Mon Sep 17 00:00:00 2001 From: Kyle Pollich Date: Thu, 22 Sep 2022 11:12:30 -0400 Subject: [PATCH 1/3] Check bundled packages when querying package info in getInfo API --- .../server/services/epm/registry/index.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 fee603f188e29..0b1c1d0bd562f 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -24,6 +24,7 @@ import type { RegistrySearchResults, GetCategoriesRequest, PackageVerificationResult, + ArchivePackage, } from '../../../types'; import { getArchiveFilelist, @@ -159,7 +160,10 @@ export async function fetchFindLatestPackageOrUndefined( } } -export async function fetchInfo(pkgName: string, pkgVersion: string): Promise { +export async function fetchInfo( + pkgName: string, + pkgVersion: string +): Promise { const registryUrl = getRegistryUrl(); try { // Trailing slash avoids 301 redirect / extra hop @@ -168,6 +172,18 @@ export async function fetchInfo(pkgName: string, pkgVersion: string): Promise Date: Thu, 22 Sep 2022 13:20:48 -0400 Subject: [PATCH 2/3] Add tests for fetchInfo --- .../services/epm/registry/index.test.ts | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts b/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts index ba309a15b04ee..49dfb402b3b75 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts @@ -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 '.'; @@ -21,6 +24,11 @@ const mockLogger = mockLoggerFactory.get('mock logger'); const mockGetBundledPackageByName = jest.fn(); const mockFetchUrl = jest.fn(); + +const MockArchive = Archive as jest.Mocked; + +jest.mock('../archive'); + jest.mock('../..', () => ({ appContextService: { getLogger: () => mockLogger, @@ -150,6 +158,10 @@ describe('fetch package', () => { }); describe('getLicensePath', () => { + beforeEach(() => { + MockArchive.getPathParts = jest.requireActual('../archive').getPathParts; + }); + it('returns first license path if found', () => { const path = getLicensePath([ '/package/good-1.0.0/NOTICE.txt', @@ -171,3 +183,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); + } + }); +}); From c2ec0b53ad7e29432f0843d42345dc6b10bf04bc Mon Sep 17 00:00:00 2001 From: Kyle Pollich Date: Thu, 22 Sep 2022 13:24:50 -0400 Subject: [PATCH 3/3] Remove needless beforeEach --- .../plugins/fleet/server/services/epm/registry/index.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts b/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts index 49dfb402b3b75..6504b6548f078 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.test.ts @@ -158,9 +158,7 @@ describe('fetch package', () => { }); describe('getLicensePath', () => { - beforeEach(() => { - MockArchive.getPathParts = jest.requireActual('../archive').getPathParts; - }); + MockArchive.getPathParts = jest.requireActual('../archive').getPathParts; it('returns first license path if found', () => { const path = getLicensePath([