From 34e5a4642dbd453983fb2b26c4bf036af9b721d1 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 12 Dec 2022 06:11:12 -0500 Subject: [PATCH] [8.6] [Fleet] fix for apm not found in epr + prerelease flag (#147257) (#147335) # Backport This will backport the following commits from `main` to `8.6`: - [[Fleet] fix for apm not found in epr + prerelease flag (#147257)](https://github.com/elastic/kibana/pull/147257) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> --- .../create_package_policy_page/index.tsx | 16 +++++++++- .../multi_page_layout/index.tsx | 3 +- .../single_page_layout/index.test.tsx | 6 +++- .../single_page_layout/index.tsx | 3 +- .../create_package_policy_page/types.ts | 1 + .../edit_package_policy_page/index.tsx | 16 ++++++++-- .../fleet/server/services/epm/packages/get.ts | 19 ++++++++++-- .../server/services/epm/registry/index.ts | 29 ++++++++++++------- .../apis/epm/install_bundled.ts | 11 +++++++ 9 files changed, 85 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx index 6fc3fbc4809c9..e6e904d0ebba8 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/index.tsx @@ -4,10 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React from 'react'; +import React, { useEffect } from 'react'; import { useMemo } from 'react'; import { useLocation, useRouteMatch } from 'react-router-dom'; +import { useGetSettings } from '../../../hooks'; + import type { AddToPolicyParams, EditPackagePolicyFrom } from './types'; import { CreatePackagePolicySinglePage } from './single_page_layout'; @@ -22,6 +24,17 @@ export const CreatePackagePolicyPage: React.FC<{}> = () => { () => queryParams.get('policyId') ?? undefined, [queryParams] ); + const [prerelease, setPrerelease] = React.useState(false); + + const { data: settings } = useGetSettings(); + + useEffect(() => { + const isEnabled = Boolean(settings?.item.prerelease_integrations_enabled); + if (settings?.item) { + setPrerelease(isEnabled); + } + }, [settings?.item]); + /** * Please note: policyId can come from one of two sources. The URL param (in the URL path) or * in the query params (?policyId=foo). @@ -39,6 +52,7 @@ export const CreatePackagePolicyPage: React.FC<{}> = () => { const pageParams = { from, queryParamsPolicyId, + prerelease, }; if (useMultiPageLayout) { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/index.tsx index 60b12f76eb8be..a51129de187a0 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/multi_page_layout/index.tsx @@ -52,6 +52,7 @@ const standaloneSteps = [addIntegrationStep, installAgentStep, confirmDataStep]; export const CreatePackagePolicyMultiPage: CreatePackagePolicyParams = ({ from, queryParamsPolicyId, + prerelease, }) => { const { params } = useRouteMatch(); const { pkgkey, policyId, integration } = params; @@ -70,7 +71,7 @@ export const CreatePackagePolicyMultiPage: CreatePackagePolicyParams = ({ data: packageInfoData, error: packageInfoError, isLoading: isPackageInfoLoading, - } = useGetPackageInfoByKey(pkgName, pkgVersion, { prerelease: true, full: true }); + } = useGetPackageInfoByKey(pkgName, pkgVersion, { prerelease, full: true }); const { agentPolicy, diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.test.tsx index c359bc9679e44..4746aee6ae8bb 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.test.tsx @@ -109,7 +109,11 @@ describe('when on the package policy create page', () => { const render = (queryParamsPolicyId?: string) => (renderResult = testRenderer.render( - + )); let mockPackageInfo: any; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.tsx index ae2c067a95aee..e1f25b27a7785 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/index.tsx @@ -73,6 +73,7 @@ const CustomEuiBottomBar = styled(EuiBottomBar)` export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({ from, queryParamsPolicyId, + prerelease, }) => { const { agents: { enabled: isFleetEnabled }, @@ -99,7 +100,7 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({ data: packageInfoData, error: packageInfoError, isLoading: isPackageInfoLoading, - } = useGetPackageInfoByKey(pkgName, pkgVersion, { full: true, prerelease: true }); + } = useGetPackageInfoByKey(pkgName, pkgVersion, { full: true, prerelease }); const packageInfo = useMemo(() => { if (packageInfoData && packageInfoData.item) { return packageInfoData.item; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/types.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/types.ts index 2b327774ac47e..c17086d3365f3 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/types.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/types.ts @@ -32,4 +32,5 @@ export interface AddToPolicyParams { export type CreatePackagePolicyParams = React.FunctionComponent<{ from: EditPackagePolicyFrom; queryParamsPolicyId?: string; + prerelease: boolean; }>; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx index 3802d7afa039c..c916976a7d094 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx @@ -35,6 +35,7 @@ import { sendGetPackageInfoByKey, sendUpgradePackagePolicyDryRun, useAuthz, + useGetSettings, } from '../../../hooks'; import { useBreadcrumbs as useIntegrationsBreadcrumbs, @@ -129,6 +130,17 @@ export const EditPackagePolicyForm = memo<{ const canWriteIntegrationPolicies = useAuthz().integrations.writeIntegrationPolicies; + const [prerelease, setPrerelease] = React.useState(false); + + const { data: settings } = useGetSettings(); + + useEffect(() => { + const isEnabled = Boolean(settings?.item.prerelease_integrations_enabled); + if (settings?.item) { + setPrerelease(isEnabled); + } + }, [settings?.item]); + useEffect(() => { if (forceUpgrade) { setIsUpgrade(true); @@ -266,7 +278,7 @@ export const EditPackagePolicyForm = memo<{ const { data: packageData } = await sendGetPackageInfoByKey( _packageInfo!.name, _packageInfo!.version, - { prerelease: true, full: true } + { prerelease, full: true } ); if (packageData?.item) { @@ -293,7 +305,7 @@ export const EditPackagePolicyForm = memo<{ setIsLoadingData(false); }; getData(); - }, [policyId, packagePolicyId, isUpgrade]); + }, [policyId, packagePolicyId, isUpgrade, prerelease]); // Retrieve agent count const [agentCount, setAgentCount] = useState(0); 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 82c3175583a75..8776ca6014e55 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.ts @@ -23,7 +23,12 @@ import type { GetCategoriesRequest, } from '../../../../common/types'; import type { Installation, PackageInfo } from '../../../types'; -import { FleetError, PackageFailedVerificationError, PackageNotFoundError } from '../../../errors'; +import { + FleetError, + PackageFailedVerificationError, + PackageNotFoundError, + RegistryResponseError, +} from '../../../errors'; import { appContextService } from '../..'; import * as Registry from '../registry'; import { getEsPackage } from '../archive/storage'; @@ -305,8 +310,16 @@ export async function getPackageFromSource(options: { if (res) { logger.debug(`retrieved package ${pkgName}-${pkgVersion} from cache`); } else { - res = await Registry.getPackage(pkgName, pkgVersion, { ignoreUnverified }); - logger.debug(`retrieved package ${pkgName}-${pkgVersion} from registry`); + try { + res = await Registry.getPackage(pkgName, pkgVersion, { ignoreUnverified }); + logger.debug(`retrieved package ${pkgName}-${pkgVersion} from registry`); + } catch (err) { + if (err instanceof RegistryResponseError && err.status === 404) { + res = await Registry.getBundledArchive(pkgName, pkgVersion); + } else { + throw err; + } + } } } 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 685820613a4be..1f3eab32860ed 100644 --- a/x-pack/plugins/fleet/server/services/epm/registry/index.ts +++ b/x-pack/plugins/fleet/server/services/epm/registry/index.ts @@ -184,24 +184,33 @@ export async function fetchInfo( 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' - ); - + const archivePackage = await getBundledArchive(pkgName, pkgVersion); + if (archivePackage) { return archivePackage.packageInfo; } - throw new PackageNotFoundError(`${pkgName}@${pkgVersion} not found`); } throw err; } } +export async function getBundledArchive( + pkgName: string, + pkgVersion: string +): Promise<{ paths: string[]; packageInfo: ArchivePackage } | undefined> { + // 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; + } +} + export async function getFile( pkgName: string, pkgVersion: string, diff --git a/x-pack/test/fleet_api_integration/apis/epm/install_bundled.ts b/x-pack/test/fleet_api_integration/apis/epm/install_bundled.ts index 2fb227c5afc1a..4ac6d6091a927 100644 --- a/x-pack/test/fleet_api_integration/apis/epm/install_bundled.ts +++ b/x-pack/test/fleet_api_integration/apis/epm/install_bundled.ts @@ -100,6 +100,17 @@ export default function (providerContext: FtrProviderContext) { expect(updateResponse.body._meta.install_source).to.be('bundled'); }); + + it('should load package archive from bundled package', async () => { + await bundlePackage('nginx-1.2.1'); + + const response = await supertest + .get(`/api/fleet/epm/packages/nginx/1.2.1?full=true`) + .expect(200); + + expect(response.body.item.name).to.eql('nginx'); + expect(response.body.item.version).to.eql('1.2.1'); + }); }); describe('with registry', () => {