From 95d2e582a01c34c86c09a48865a61e04255c5c59 Mon Sep 17 00:00:00 2001 From: criamico Date: Fri, 30 Sep 2022 12:40:29 +0200 Subject: [PATCH 1/8] [Fleet] Rely on package content during installation --- .../server/services/epm/package_service.ts | 3 +- .../server/services/epm/registry/index.ts | 55 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/package_service.ts b/x-pack/plugins/fleet/server/services/epm/package_service.ts index 71ab87c7d92bd..b6858b6f633a0 100644 --- a/x-pack/plugins/fleet/server/services/epm/package_service.ts +++ b/x-pack/plugins/fleet/server/services/epm/package_service.ts @@ -19,6 +19,7 @@ import type { InstallablePackage, Installation, RegistryPackage, + ArchivePackage, BundledPackage, } from '../../types'; import { checkSuperuser } from '../../routes/security'; @@ -49,7 +50,7 @@ export interface PackageClient { getRegistryPackage( packageName: string, packageVersion: string - ): Promise<{ packageInfo: RegistryPackage; paths: string[] }>; + ): Promise<{ packageInfo: ArchivePackage; paths: string[] }>; reinstallEsAssets( packageInfo: InstallablePackage, 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 2e5fff50af576..e7fa8581962ce 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -256,29 +256,30 @@ export async function getRegistryPackage( options?: { ignoreUnverified?: boolean; getPkgInfoFromArchive?: boolean } ): Promise<{ paths: string[]; - packageInfo: RegistryPackage; + packageInfo: ArchivePackage; verificationResult?: PackageVerificationResult; }> { const verifyPackage = appContextService.getExperimentalFeatures().packageVerification; let paths = getArchiveFilelist({ name, version }); let verificationResult = verifyPackage ? getVerificationResult({ name, version }) : undefined; + + const { + archiveBuffer, + archivePath, + verificationResult: latestVerificationResult, + } = await withPackageSpan('Fetch package archive from registry', () => + fetchArchiveBuffer({ + pkgName: name, + pkgVersion: version, + shouldVerify: verifyPackage, + ignoreUnverified: options?.ignoreUnverified, + }) + ); + if (latestVerificationResult) { + verificationResult = latestVerificationResult; + setVerificationResult({ name, version }, latestVerificationResult); + } if (!paths || paths.length === 0) { - const { - archiveBuffer, - archivePath, - verificationResult: latestVerificationResult, - } = await withPackageSpan('Fetch package archive from registry', () => - fetchArchiveBuffer({ - pkgName: name, - pkgVersion: version, - shouldVerify: verifyPackage, - ignoreUnverified: options?.ignoreUnverified, - }) - ); - if (latestVerificationResult) { - verificationResult = latestVerificationResult; - setVerificationResult({ name, version }, latestVerificationResult); - } paths = await withPackageSpan('Unpack archive', () => unpackBufferToCache({ name, @@ -287,17 +288,19 @@ export async function getRegistryPackage( contentType: ensureContentType(archivePath), }) ); - const cachedInfo = getPackageInfo({ name, version }); - if (options?.getPkgInfoFromArchive && !cachedInfo) { - const { packageInfo } = await generatePackageInfoFromArchiveBuffer( - archiveBuffer, - ensureContentType(archivePath) - ); - setPackageInfo({ packageInfo, name, version }); - } + } + const { packageInfo } = await generatePackageInfoFromArchiveBuffer( + archiveBuffer, + ensureContentType(archivePath) + ); + const cachedInfo = getPackageInfo({ name, version }); + + if (options?.getPkgInfoFromArchive && !cachedInfo) { + setPackageInfo({ packageInfo, name, version }); } - const packageInfo = await getInfo(name, version); + // replace this function with generatePackageInfoFromArchiveBuffer + // const packageInfo = await getInfo(name, version); return { paths, packageInfo, verificationResult }; } From ccbe77efcdc31d76290b31eeaf05f137fc05ffc5 Mon Sep 17 00:00:00 2001 From: criamico Date: Fri, 30 Sep 2022 16:31:43 +0200 Subject: [PATCH 2/8] Further refactoring --- .../fleet/server/services/epm/packages/get.ts | 10 +---- .../server/services/epm/registry/index.ts | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/packages/get.ts b/x-pack/plugins/fleet/server/services/epm/packages/get.ts index b0aaa64a69239..bcd0ecd2c36ff 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.ts @@ -170,7 +170,6 @@ export async function getPackageInfo({ pkgVersion: resolvedPkgVersion, savedObjectsClient, installedPkg: savedObject?.attributes, - getPkgInfoFromArchive: packageInfo?.type === 'input', ignoreUnverified, })); } @@ -235,13 +234,12 @@ interface PackageResponse { } type GetPackageResponse = PackageResponse | undefined; -// gets package from install_source if it exists otherwise gets from registry +// gets package from install_source export async function getPackageFromSource(options: { pkgName: string; pkgVersion: string; installedPkg?: Installation; savedObjectsClient: SavedObjectsClientContract; - getPkgInfoFromArchive?: boolean; ignoreUnverified?: boolean; }): Promise { const logger = appContextService.getLogger(); @@ -250,7 +248,6 @@ export async function getPackageFromSource(options: { pkgVersion, installedPkg, savedObjectsClient, - getPkgInfoFromArchive = true, ignoreUnverified = false, } = options; let res: GetPackageResponse; @@ -295,10 +292,7 @@ export async function getPackageFromSource(options: { } } else { // else package is not installed or installed and missing from cache and storage and installed from registry - res = await Registry.getRegistryPackage(pkgName, pkgVersion, { - getPkgInfoFromArchive, - ignoreUnverified, - }); + res = await Registry.getRegistryPackage(pkgName, pkgVersion, { ignoreUnverified }); logger.debug(`retrieved uninstalled package ${pkgName}-${pkgVersion} from registry`); } if (!res) { 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 e7fa8581962ce..18216bd6c6b64 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -250,13 +250,35 @@ export async function getInfo(name: string, version: string) { }); } +// Check that the packageInfo exists in cache +// If not, retrieve it from the archive +async function getPackageInfoFromArchiveOrCache( + name: string, + version: string, + archiveBuffer: Buffer, + archivePath: string +): Promise { + const cachedInfo = getPackageInfo({ name, version }); + + if (!cachedInfo) { + const { packageInfo } = await generatePackageInfoFromArchiveBuffer( + archiveBuffer, + ensureContentType(archivePath) + ); + setPackageInfo({ packageInfo, name, version }); + return packageInfo; + } else { + return cachedInfo; + } +} + export async function getRegistryPackage( name: string, version: string, - options?: { ignoreUnverified?: boolean; getPkgInfoFromArchive?: boolean } + options?: { ignoreUnverified?: boolean } ): Promise<{ paths: string[]; - packageInfo: ArchivePackage; + packageInfo: ArchivePackage | RegistryPackage; verificationResult?: PackageVerificationResult; }> { const verifyPackage = appContextService.getExperimentalFeatures().packageVerification; @@ -289,18 +311,13 @@ export async function getRegistryPackage( }) ); } - const { packageInfo } = await generatePackageInfoFromArchiveBuffer( + + const packageInfo = await getPackageInfoFromArchiveOrCache( + name, + version, archiveBuffer, - ensureContentType(archivePath) + archivePath ); - const cachedInfo = getPackageInfo({ name, version }); - - if (options?.getPkgInfoFromArchive && !cachedInfo) { - setPackageInfo({ packageInfo, name, version }); - } - - // replace this function with generatePackageInfoFromArchiveBuffer - // const packageInfo = await getInfo(name, version); return { paths, packageInfo, verificationResult }; } From c73b72776ebe7b3feec0c6d6faf77786d941f4e8 Mon Sep 17 00:00:00 2001 From: criamico Date: Mon, 3 Oct 2022 12:03:13 +0200 Subject: [PATCH 3/8] Fix type --- x-pack/plugins/fleet/server/services/epm/registry/index.ts | 4 ++-- .../get_installed_integrations/installed_integration_set.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 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 18216bd6c6b64..9bb257ba47b7b 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -257,7 +257,7 @@ async function getPackageInfoFromArchiveOrCache( version: string, archiveBuffer: Buffer, archivePath: string -): Promise { +): Promise { const cachedInfo = getPackageInfo({ name, version }); if (!cachedInfo) { @@ -278,7 +278,7 @@ export async function getRegistryPackage( options?: { ignoreUnverified?: boolean } ): Promise<{ paths: string[]; - packageInfo: ArchivePackage | RegistryPackage; + packageInfo: ArchivePackage; verificationResult?: PackageVerificationResult; }> { const verifyPackage = appContextService.getExperimentalFeatures().packageVerification; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/installed_integration_set.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/installed_integration_set.ts index d55b8fdce3a3f..e124e0322192c 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/installed_integration_set.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/installed_integration_set.ts @@ -6,7 +6,7 @@ */ import { capitalize, flatten } from 'lodash'; -import type { PackagePolicy, RegistryPackage } from '@kbn/fleet-plugin/common'; +import type { PackagePolicy, ArchivePackage } from '@kbn/fleet-plugin/common'; import type { InstalledIntegration, InstalledIntegrationArray, @@ -18,7 +18,7 @@ import type { export interface IInstalledIntegrationSet { addPackagePolicy(policy: PackagePolicy): void; - addRegistryPackage(registryPackage: RegistryPackage): void; + addRegistryPackage(registryPackage: ArchivePackage): void; getPackages(): InstalledPackageArray; getIntegrations(): InstalledIntegrationArray; @@ -56,7 +56,7 @@ export const createInstalledIntegrationSet = (): IInstalledIntegrationSet => { } }; - const addRegistryPackage = (registryPackage: RegistryPackage): void => { + const addRegistryPackage = (registryPackage: ArchivePackage): void => { const policyTemplates = registryPackage.policy_templates ?? []; const packageKey = `${registryPackage.name}:${registryPackage.version}`; const existingPackageInfo = packageMap.get(packageKey); From a1cdeb56179f9e9cdbd30f8cc5fb3bc7929c740e Mon Sep 17 00:00:00 2001 From: criamico Date: Wed, 5 Oct 2022 11:50:00 +0200 Subject: [PATCH 4/8] Fix test failure --- .../server/services/epm/registry/index.ts | 3 +- .../apis/agent_policy/agent_policy.ts | 50 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 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 9bb257ba47b7b..3303c4c2501d4 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -289,7 +289,7 @@ export async function getRegistryPackage( archiveBuffer, archivePath, verificationResult: latestVerificationResult, - } = await withPackageSpan('Fetch package archive from registry', () => + } = await withPackageSpan('Fetch package archive from archive buffer', () => fetchArchiveBuffer({ pkgName: name, pkgVersion: version, @@ -297,6 +297,7 @@ export async function getRegistryPackage( ignoreUnverified: options?.ignoreUnverified, }) ); + if (latestVerificationResult) { verificationResult = latestVerificationResult; setVerificationResult({ name, version }, latestVerificationResult); diff --git a/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts b/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts index e71c14a8054c8..277bafb998761 100644 --- a/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts +++ b/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts @@ -17,6 +17,7 @@ export default function (providerContext: FtrProviderContext) { const supertest = getService('supertest'); const esArchiver = getService('esArchiver'); const kibanaServer = getService('kibanaServer'); + describe('fleet_agent_policies', () => { skipIfNoDockerRegistry(providerContext); describe('POST /api/fleet/agent_policies', () => { @@ -287,8 +288,20 @@ export default function (providerContext: FtrProviderContext) { setupFleetAndAgents(providerContext); after(async () => { await esArchiver.unload('x-pack/test/functional/es_archives/fleet/agents'); + if (systemPkgVersion) { + await supertest.delete(`/api/fleet/epm/packages/system-${systemPkgVersion}`); + } + if (packagePoliciesToDeleteIds.length > 0) { + await kibanaServer.savedObjects.bulkDelete({ + objects: packagePoliciesToDeleteIds.map((id) => ({ + id, + type: PACKAGE_POLICY_SAVED_OBJECT_TYPE, + })), + }); + } }); - + let systemPkgVersion: string; + const packagePoliciesToDeleteIds: string[] = []; const TEST_POLICY_ID = 'policy1'; it('should work with valid values', async () => { @@ -327,13 +340,43 @@ export default function (providerContext: FtrProviderContext) { }, } = await supertest.get(`/api/fleet/agent_policies/${policyId}`).expect(200); - const matches = packagePolicies[0].name.match(/^(.*)\s\(copy\s?([0-9]*)\)$/); + const matches = packagePolicies[0]?.name.match(/^(.*)\s\(copy\s?([0-9]*)\)$/); + if (matches) { return parseInt(matches[2], 10) || 1; } - return 0; } + + const policyId = 'package-policy-test-1'; + packagePoliciesToDeleteIds.push(policyId); + const getPkRes = await supertest + .get(`/api/fleet/epm/packages/system`) + .set('kbn-xsrf', 'xxxx') + .expect(200); + systemPkgVersion = getPkRes.body.item.version; + // we must first force install the system package to override package verification error on policy create + const installPromise = supertest + .post(`/api/fleet/epm/packages/system-${systemPkgVersion}`) + .set('kbn-xsrf', 'xxxx') + .send({ force: true }) + .expect(200); + + await Promise.all([ + installPromise, + kibanaServer.savedObjects.create({ + id: policyId, + type: PACKAGE_POLICY_SAVED_OBJECT_TYPE, + overwrite: true, + attributes: { + name: `system-1`, + package: { + name: 'system', + }, + }, + }), + ]); + const { body: { item: { id: originalPolicyId }, @@ -349,7 +392,6 @@ export default function (providerContext: FtrProviderContext) { namespace: 'default', }) .expect(200); - expect(await getSystemPackagePolicyCopyVersion(originalPolicyId)).to.be(0); const { From 036a7841f35bceef874bdc610ebe87580fb669a5 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 5 Oct 2022 10:29:42 +0000 Subject: [PATCH 5/8] [CI] Auto-commit changed files from 'node scripts/build_plugin_list_docs' --- docs/developer/plugin-list.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developer/plugin-list.asciidoc b/docs/developer/plugin-list.asciidoc index 407261c6f1d7e..4853d859b371a 100644 --- a/docs/developer/plugin-list.asciidoc +++ b/docs/developer/plugin-list.asciidoc @@ -71,7 +71,7 @@ as uiSettings within the code. |{kib-repo}blob/{branch}/src/plugins/data_views/README.mdx[dataViews] |The data views API provides a consistent method of structuring and formatting documents -and field lists across the various Kibana apps. Its typically used in conjunction with +and field lists across the various Kibana apps. It's typically used in conjunction with for composing queries. From c50b7f677378a02531b1622adf5e1b0c8cf54d34 Mon Sep 17 00:00:00 2001 From: criamico Date: Thu, 6 Oct 2022 14:51:07 +0200 Subject: [PATCH 6/8] Rename getRegistryPackage to getPackage --- .../server/services/epm/package_service.mock.ts | 2 +- .../fleet/server/services/epm/package_service.ts | 10 +++++----- .../fleet/server/services/epm/packages/get.ts | 12 ++++++------ .../fleet/server/services/epm/packages/install.ts | 2 +- .../fleet/server/services/epm/registry/index.ts | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/package_service.mock.ts b/x-pack/plugins/fleet/server/services/epm/package_service.mock.ts index f703399ca6df7..806f295ec2c4e 100644 --- a/x-pack/plugins/fleet/server/services/epm/package_service.mock.ts +++ b/x-pack/plugins/fleet/server/services/epm/package_service.mock.ts @@ -11,7 +11,7 @@ const createClientMock = (): jest.Mocked => ({ getInstallation: jest.fn(), ensureInstalledPackage: jest.fn(), fetchFindLatestPackage: jest.fn(), - getRegistryPackage: jest.fn(), + getPackage: jest.fn(), reinstallEsAssets: jest.fn(), }); diff --git a/x-pack/plugins/fleet/server/services/epm/package_service.ts b/x-pack/plugins/fleet/server/services/epm/package_service.ts index b6858b6f633a0..45fb673771327 100644 --- a/x-pack/plugins/fleet/server/services/epm/package_service.ts +++ b/x-pack/plugins/fleet/server/services/epm/package_service.ts @@ -26,7 +26,7 @@ import { checkSuperuser } from '../../routes/security'; import { FleetUnauthorizedError } from '../../errors'; import { installTransforms, isTransform } from './elasticsearch/transform/install'; -import { fetchFindLatestPackageOrThrow, getRegistryPackage } from './registry'; +import { fetchFindLatestPackageOrThrow, getPackage } from './registry'; import { ensureInstalledPackage, getInstallation } from './packages'; export type InstalledAssetType = EsAssetReference; @@ -47,7 +47,7 @@ export interface PackageClient { fetchFindLatestPackage(packageName: string): Promise; - getRegistryPackage( + getPackage( packageName: string, packageVersion: string ): Promise<{ packageInfo: ArchivePackage; paths: string[] }>; @@ -121,13 +121,13 @@ class PackageClientImpl implements PackageClient { return fetchFindLatestPackageOrThrow(packageName); } - public async getRegistryPackage( + public async getPackage( packageName: string, packageVersion: string, - options?: Parameters['2'] + options?: Parameters['2'] ) { await this.#runPreflight(); - return getRegistryPackage(packageName, packageVersion, options); + return getPackage(packageName, packageVersion, options); } public async reinstallEsAssets( diff --git a/x-pack/plugins/fleet/server/services/epm/packages/get.ts b/x-pack/plugins/fleet/server/services/epm/packages/get.ts index bcd0ecd2c36ff..0ce4e3c1cbea3 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.ts @@ -277,11 +277,12 @@ export async function getPackageFromSource(options: { logger.debug(`retrieved installed package ${pkgName}-${pkgVersion} from ES`); } } - // for packages not in cache or package storage and installed from registry, check registry + // install source is now archive in all cases + // See https://github.com/elastic/kibana/issues/115032 if (!res && pkgInstallSource === 'registry') { try { - res = await Registry.getRegistryPackage(pkgName, pkgVersion); - logger.debug(`retrieved installed package ${pkgName}-${pkgVersion} from registry`); + res = await Registry.getPackage(pkgName, pkgVersion); + logger.debug(`retrieved installed package ${pkgName}-${pkgVersion}`); } catch (error) { if (error instanceof PackageFailedVerificationError) { throw error; @@ -291,9 +292,8 @@ export async function getPackageFromSource(options: { } } } else { - // else package is not installed or installed and missing from cache and storage and installed from registry - res = await Registry.getRegistryPackage(pkgName, pkgVersion, { ignoreUnverified }); - logger.debug(`retrieved uninstalled package ${pkgName}-${pkgVersion} from registry`); + res = await Registry.getPackage(pkgName, pkgVersion, { ignoreUnverified }); + logger.debug(`retrieved uninstalled package ${pkgName}-${pkgVersion}`); } if (!res) { throw new FleetError(`package info for ${pkgName}-${pkgVersion} does not exist`); 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 91e2c7d241fd7..ebd0bde8b09b4 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -302,7 +302,7 @@ async function installPackageFromRegistry({ Registry.fetchFindLatestPackageOrThrow(pkgName, { ignoreConstraints, }), - Registry.getRegistryPackage(pkgName, pkgVersion, { + Registry.getPackage(pkgName, pkgVersion, { ignoreUnverified: force && !neverIgnoreVerificationError, }), ]); 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 3303c4c2501d4..3f648a7fd73dc 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -272,7 +272,7 @@ async function getPackageInfoFromArchiveOrCache( } } -export async function getRegistryPackage( +export async function getPackage( name: string, version: string, options?: { ignoreUnverified?: boolean } From ce7ad56f84de092396a4d94af2893b9e80128b8e Mon Sep 17 00:00:00 2001 From: criamico Date: Thu, 6 Oct 2022 15:10:06 +0200 Subject: [PATCH 7/8] Change function name in tests --- .../validate_bundled_packages.test.ts | 2 +- .../fleet/server/services/epm/package_service.test.ts | 10 +++++----- .../fleet/server/services/epm/packages/get.test.ts | 6 +++--- .../fleet/server/services/epm/packages/install.test.ts | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/fleet/server/integration_tests/validate_bundled_packages.test.ts b/x-pack/plugins/fleet/server/integration_tests/validate_bundled_packages.test.ts index 79388d7d5b914..f43b1699eab10 100644 --- a/x-pack/plugins/fleet/server/integration_tests/validate_bundled_packages.test.ts +++ b/x-pack/plugins/fleet/server/integration_tests/validate_bundled_packages.test.ts @@ -50,7 +50,7 @@ describe('validate bundled packages', () => { bundledPackages .filter((pkg) => !EXCLUDED_PACKAGES.includes(pkg.name)) .map(async (bundledPackage) => { - const registryPackage = await Registry.getRegistryPackage( + const registryPackage = await Registry.getPackage( bundledPackage.name, bundledPackage.version ); diff --git a/x-pack/plugins/fleet/server/services/epm/package_service.test.ts b/x-pack/plugins/fleet/server/services/epm/package_service.test.ts index 4f98776f53d60..f4f853d778923 100644 --- a/x-pack/plugins/fleet/server/services/epm/package_service.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/package_service.test.ts @@ -31,7 +31,7 @@ const testKeys = [ 'getInstallation', 'ensureInstalledPackage', 'fetchFindLatestPackage', - 'getRegistryPackage', + 'getPackage', 'reinstallEsAssets', ]; @@ -99,16 +99,16 @@ function getTest( break; case testKeys[3]: test = { - method: mocks.packageClient.getRegistryPackage.bind(mocks.packageClient), + method: mocks.packageClient.getPackage.bind(mocks.packageClient), args: ['package name', '8.0.0'], - spy: jest.spyOn(epmRegistry, 'getRegistryPackage'), + spy: jest.spyOn(epmRegistry, 'getPackage'), spyArgs: ['package name', '8.0.0', undefined], spyResponse: { - packageInfo: { name: 'getRegistryPackage test' }, + packageInfo: { name: 'getPackage test' }, paths: ['/some/test/path'], }, expectedReturnValue: { - packageInfo: { name: 'getRegistryPackage test' }, + packageInfo: { name: 'getPackage test' }, paths: ['/some/test/path'], }, }; diff --git a/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts b/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts index af168a8cc1619..1dacf4ed7df8a 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts @@ -188,7 +188,7 @@ describe('When using EPM `get` services', () => { name: 'my-package', version: '1.0.0', } as RegistryPackage); - MockRegistry.getRegistryPackage.mockResolvedValue({ + MockRegistry.getPackage.mockResolvedValue({ paths: [], packageInfo: { name: 'my-package', @@ -366,7 +366,7 @@ describe('When using EPM `get` services', () => { status: 'not_installed', }); - expect(MockRegistry.getRegistryPackage).not.toHaveBeenCalled(); + expect(MockRegistry.getPackage).not.toHaveBeenCalled(); }); // when calling the get package endpoint without a package version we @@ -392,7 +392,7 @@ describe('When using EPM `get` services', () => { status: 'not_installed', }); - expect(MockRegistry.getRegistryPackage).not.toHaveBeenCalled(); + expect(MockRegistry.getPackage).not.toHaveBeenCalled(); }); }); }); diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts index c58700716d18a..d8f4f4a70adaa 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.test.ts @@ -84,7 +84,7 @@ describe('install', () => { .spyOn(Registry, 'fetchFindLatestPackageOrThrow') .mockImplementation(() => Promise.resolve({ version: '1.3.0' } as any)); jest - .spyOn(Registry, 'getRegistryPackage') + .spyOn(Registry, 'getPackage') .mockImplementation(() => Promise.resolve({ packageInfo: { license: 'basic' } } as any)); mockGetBundledPackages.mockReset(); From e69120bc02b5166db6235517b1c4aa16fc2840d4 Mon Sep 17 00:00:00 2001 From: criamico Date: Thu, 6 Oct 2022 16:00:55 +0200 Subject: [PATCH 8/8] Fix more tests --- .../apm/server/routes/fleet/get_latest_apm_package.ts | 2 +- .../lib/metadata/check_metadata_transforms_task.test.ts | 8 ++++---- .../lib/metadata/check_metadata_transforms_task.ts | 2 +- .../get_installed_integrations_route.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/fleet/get_latest_apm_package.ts b/x-pack/plugins/apm/server/routes/fleet/get_latest_apm_package.ts index c7cd083fbda80..671b4d56dad94 100644 --- a/x-pack/plugins/apm/server/routes/fleet/get_latest_apm_package.ts +++ b/x-pack/plugins/apm/server/routes/fleet/get_latest_apm_package.ts @@ -20,7 +20,7 @@ export async function getLatestApmPackage({ const { name, version } = await packageClient.fetchFindLatestPackage( APM_PACKAGE_NAME ); - const registryPackage = await packageClient.getRegistryPackage(name, version); + const registryPackage = await packageClient.getPackage(name, version); const { title, policy_templates: policyTemplates } = registryPackage.packageInfo; const firstTemplate = policyTemplates?.[0]; diff --git a/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.test.ts b/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.test.ts index 84d124a398a93..dabd2feed57e2 100644 --- a/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.test.ts @@ -277,7 +277,7 @@ describe('check metadata transforms task', () => { }); describe('transforms reinstall', () => { - let getRegistryPackageSpy: jest.SpyInstance; + let getPackageSpy: jest.SpyInstance; let reinstallEsAssetsSpy: jest.SpyInstance; let mockPackageClient: jest.Mocked; @@ -291,7 +291,7 @@ describe('check metadata transforms task', () => { mockPackageClient = mockEndpointAppContext.service.getInternalFleetServices() .packages as jest.Mocked; - getRegistryPackageSpy = jest.spyOn(mockPackageClient, 'getRegistryPackage'); + getPackageSpy = jest.spyOn(mockPackageClient, 'getPackage'); reinstallEsAssetsSpy = jest.spyOn(mockPackageClient, 'reinstallEsAssets'); const transformStatsResponseMock = { @@ -313,7 +313,7 @@ describe('check metadata transforms task', () => { packageInfo: { name: 'package name' }, paths: ['some/test/transform/path'], }; - getRegistryPackageSpy.mockResolvedValue(expectedArgs); + getPackageSpy.mockResolvedValue(expectedArgs); reinstallEsAssetsSpy.mockResolvedValue([{}]); await runTask(); @@ -338,7 +338,7 @@ describe('check metadata transforms task', () => { }); it('should return correct runAt', async () => { - getRegistryPackageSpy.mockResolvedValue({ + getPackageSpy.mockResolvedValue({ packageInfo: { name: 'package name' }, paths: ['some/test/transform/path'], }); diff --git a/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.ts b/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.ts index 64d37e07342c4..1c2d51510c3f6 100644 --- a/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.ts +++ b/x-pack/plugins/security_solution/server/endpoint/lib/metadata/check_metadata_transforms_task.ts @@ -256,7 +256,7 @@ export class CheckMetadataTransformsTask { const packageClient = this.endpointAppContext.service.getInternalFleetServices().packages; - const { packageInfo, paths } = await packageClient.getRegistryPackage( + const { packageInfo, paths } = await packageClient.getPackage( FLEET_ENDPOINT_PACKAGE, pkgVersion ); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/get_installed_integrations_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/get_installed_integrations_route.ts index 50c89f989f990..9e9091e1b578c 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/get_installed_integrations_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/fleet/get_installed_integrations/get_installed_integrations_route.ts @@ -50,7 +50,7 @@ export const getInstalledIntegrationsRoute = ( concurrency: MAX_CONCURRENT_REQUESTS_TO_PACKAGE_REGISTRY, items: set.getPackages(), executor: async (packageInfo) => { - const registryPackage = await fleet.packages.getRegistryPackage( + const registryPackage = await fleet.packages.getPackage( packageInfo.package_name, packageInfo.package_version );