Fixing the generator to use bulk api to install endpoint package (#103094)

This commit is contained in:
Jonathan Buttner 2021-06-23 12:30:12 -04:00 committed by GitHub
parent 5b0d325d7e
commit 28162810ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<BulkInstallPackagesResponse>;
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() {