diff --git a/x-pack/plugins/security_solution/scripts/endpoint/resolver_generator_script.ts b/x-pack/plugins/security_solution/scripts/endpoint/resolver_generator_script.ts index 72f56f13eaddf9..66ac744b3a50ca 100644 --- a/x-pack/plugins/security_solution/scripts/endpoint/resolver_generator_script.ts +++ b/x-pack/plugins/security_solution/scripts/endpoint/resolver_generator_script.ts @@ -15,9 +15,16 @@ import { KbnClient } from '@kbn/test'; import { AxiosResponse } from 'axios'; import { indexHostsAndAlerts } from '../../common/endpoint/index_data'; import { ANCESTRY_LIMIT, EndpointDocGenerator } from '../../common/endpoint/generate_data'; -import { AGENTS_SETUP_API_ROUTES, SETUP_API_ROUTE } from '../../../fleet/common/constants'; import { + AGENTS_SETUP_API_ROUTES, + EPM_API_ROUTES, + SETUP_API_ROUTE, +} from '../../../fleet/common/constants'; +import { + BulkInstallPackageInfo, + BulkInstallPackagesResponse, CreateFleetSetupResponse, + IBulkInstallPackageHTTPError, PostIngestSetupResponse, } from '../../../fleet/common/types/rest_spec'; import { KbnClientWithApiKeySupport } from './kbn_client_with_api_key_support'; @@ -44,6 +51,12 @@ async function deleteIndices(indices: string[], client: Client) { } } +function isFleetBulkInstallError( + installResponse: BulkInstallPackageInfo | IBulkInstallPackageHTTPError +): installResponse is IBulkInstallPackageHTTPError { + return 'error' in installResponse && installResponse.error !== undefined; +} + async function doIngestSetup(kbnClient: KbnClient) { // Setup Ingest try { @@ -76,6 +89,35 @@ async function doIngestSetup(kbnClient: KbnClient) { console.error(error); throw error; } + + // Install/upgrade the endpoint package + try { + const installEndpointPackageResp = (await kbnClient.request({ + path: EPM_API_ROUTES.BULK_INSTALL_PATTERN, + method: 'POST', + body: { + packages: ['endpoint'], + }, + })) as AxiosResponse; + + const bulkResp = installEndpointPackageResp.data.response; + if (bulkResp.length <= 0) { + throw new Error('Installing the Endpoint package failed, response was empty, existing'); + } + + if (isFleetBulkInstallError(bulkResp[0])) { + if (bulkResp[0].error instanceof Error) { + throw new Error( + `Installing the Endpoint package failed: ${bulkResp[0].error.message}, exiting` + ); + } + + throw new Error(bulkResp[0].error); + } + } catch (error) { + console.error(error); + throw error; + } } async function main() {