Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution] Updating generator to install/update the endpoint package during setup #103094

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down