From 85398b791e289fcfe8b16e4488674f88bed4151c Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Thu, 14 May 2020 13:30:17 +0200 Subject: [PATCH 1/5] Better handle non-available package registry --- .../server/routes/setup/handlers.ts | 6 ++++++ .../server/services/epm/packages/install.ts | 18 +++++++----------- .../server/services/epm/registry/requests.ts | 7 ++++++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index abe5f3620d214e..241bbb802f0dcc 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -72,6 +72,12 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request body: { isInitialized: true }, }); } catch (e) { + if (e.isBoom) { + return response.customError({ + statusCode: e.output.statusCode, + body: { message: e.output.payload.message }, + }); + } return response.customError({ statusCode: 500, body: { message: e.message }, diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts index 632bc3ac9b69ff..79a5e98b9507db 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts @@ -73,17 +73,13 @@ export async function ensureInstalledPackage(options: { if (installedPackage) { return installedPackage; } - // if the requested packaged was not found to be installed, try installing - try { - await installLatestPackage({ - savedObjectsClient, - pkgName, - callCluster, - }); - return await getInstallation({ savedObjectsClient, pkgName }); - } catch (err) { - throw new Error(err.message); - } + // if the requested packaged was not found to be installed, install + await installLatestPackage({ + savedObjectsClient, + pkgName, + callCluster, + }); + return await getInstallation({ savedObjectsClient, pkgName }); } export async function installPackage(options: { diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts index 654aa8aae13559..757683c848508c 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts @@ -7,17 +7,22 @@ import Boom from 'boom'; import fetch, { Response } from 'node-fetch'; import { streamToString } from './streams'; +import { appContextService } from '../..'; export async function getResponse(url: string): Promise { + const logger = appContextService.getLogger(); try { const response = await fetch(url); if (response.ok) { return response; } else { + logger.error(`Error connecting to package registry: ${response.statusText}`); throw new Boom(response.statusText, { statusCode: response.status }); } } catch (e) { - throw Boom.boomify(e); + const message = `Error connecting to package registry: ${e.message}`; + logger.error(message); + throw new Boom(message, { statusCode: 502 }); } } From dcf36dc6bad9e4cdeaf1d5925916bc238b622d48 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 18 May 2020 14:17:22 +0200 Subject: [PATCH 2/5] Log errors in route handler only --- .../plugins/ingest_manager/server/routes/setup/handlers.ts | 4 ++++ .../ingest_manager/server/services/epm/registry/requests.ts | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index ebb79bb53a04db..8bc80c69ce9b2d 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -74,6 +74,7 @@ export const createFleetSetupHandler: RequestHandler< export const ingestManagerSetupHandler: RequestHandler = async (context, request, response) => { const soClient = context.core.savedObjects.client; const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser; + const logger = appContextService.getLogger(); try { await setupIngestManager(soClient, callCluster); return response.ok({ @@ -81,11 +82,14 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request }); } catch (e) { if (e.isBoom) { + logger.error(e.output.payload.message); return response.customError({ statusCode: e.output.statusCode, body: { message: e.output.payload.message }, }); } + logger.error(e.message); + logger.error(e.stack); return response.customError({ statusCode: 500, body: { message: e.message }, diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts index 757683c848508c..6c38f086cb3f7d 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts @@ -10,19 +10,15 @@ import { streamToString } from './streams'; import { appContextService } from '../..'; export async function getResponse(url: string): Promise { - const logger = appContextService.getLogger(); try { const response = await fetch(url); if (response.ok) { return response; } else { - logger.error(`Error connecting to package registry: ${response.statusText}`); throw new Boom(response.statusText, { statusCode: response.status }); } } catch (e) { - const message = `Error connecting to package registry: ${e.message}`; - logger.error(message); - throw new Boom(message, { statusCode: 502 }); + throw new Boom(`Error connecting to package registry: ${e.message}`, { statusCode: 502 }); } } From 0092b2f652b157a081b9c36c3931098d501d771f Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 18 May 2020 14:18:36 +0200 Subject: [PATCH 3/5] Await installation of prebuilt component templates --- .../epm/elasticsearch/template/install.ts | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts index 6ef6f863753b55..b7c35a04a19d43 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import Boom from 'boom'; import { Dataset, RegistryPackage, ElasticsearchAssetType, TemplateRef } from '../../../../types'; import { CallESAsCurrentUser } from '../../../../types'; import { Field, loadFieldsFromYaml, processFields } from '../../fields/field'; @@ -20,8 +21,8 @@ export const installTemplates = async ( // install any pre-built index template assets, // atm, this is only the base package's global index templates // Install component templates first, as they are used by the index templates - installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster); - installPreBuiltTemplates(pkgName, pkgVersion, callCluster); + await installPreBuiltComponentTemplates(pkgName, pkgVersion, callCluster); + await installPreBuiltTemplates(pkgName, pkgVersion, callCluster); // build templates per dataset from yml files const datasets = registryPackage.datasets; @@ -53,16 +54,7 @@ const installPreBuiltTemplates = async ( pkgVersion, (entry: Registry.ArchiveEntry) => isTemplate(entry) ); - // templatePaths.forEach(async path => { - // const { file } = Registry.pathParts(path); - // const templateName = file.substr(0, file.lastIndexOf('.')); - // const content = JSON.parse(Registry.getAsset(path).toString('utf8')); - // await callCluster('indices.putTemplate', { - // name: templateName, - // body: content, - // }); - // }); - templatePaths.forEach(async path => { + const templateInstallPromises = templatePaths.map(async path => { const { file } = Registry.pathParts(path); const templateName = file.substr(0, file.lastIndexOf('.')); const content = JSON.parse(Registry.getAsset(path).toString('utf8')); @@ -91,8 +83,15 @@ const installPreBuiltTemplates = async ( // The existing convenience endpoint `indices.putTemplate` only sends to _template, // which does not support v2 templates. // See src/core/server/elasticsearch/api_types.ts for available endpoints. - await callCluster('transport.request', callClusterParams); + return await callCluster('transport.request', callClusterParams); }); + try { + return await Promise.all(templateInstallPromises); + } catch (e) { + throw new Boom(`Error installing prebuilt index templates ${e.message}`, { + statusCode: 400, + }); + } }; const installPreBuiltComponentTemplates = async ( @@ -105,7 +104,7 @@ const installPreBuiltComponentTemplates = async ( pkgVersion, (entry: Registry.ArchiveEntry) => isComponentTemplate(entry) ); - templatePaths.forEach(async path => { + const templateInstallPromises = templatePaths.map(async path => { const { file } = Registry.pathParts(path); const templateName = file.substr(0, file.lastIndexOf('.')); const content = JSON.parse(Registry.getAsset(path).toString('utf8')); @@ -124,8 +123,15 @@ const installPreBuiltComponentTemplates = async ( // This uses the catch-all endpoint 'transport.request' because there is no // convenience endpoint for component templates yet. // See src/core/server/elasticsearch/api_types.ts for available endpoints. - await callCluster('transport.request', callClusterParams); + return await callCluster('transport.request', callClusterParams); }); + try { + return await Promise.all(templateInstallPromises); + } catch (e) { + throw new Boom(`Error installing prebuilt component templates ${e.message}`, { + statusCode: 400, + }); + } }; const isTemplate = ({ path }: Registry.ArchiveEntry) => { From d6b4c49fbdc7f2e493f723f208921ad03400ac50 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 18 May 2020 14:27:15 +0200 Subject: [PATCH 4/5] Remove leftover import --- .../ingest_manager/server/services/epm/registry/requests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts index 6c38f086cb3f7d..93e475cbc5956b 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/requests.ts @@ -7,7 +7,6 @@ import Boom from 'boom'; import fetch, { Response } from 'node-fetch'; import { streamToString } from './streams'; -import { appContextService } from '../..'; export async function getResponse(url: string): Promise { try { From 9ff1e60c5a688eccaabe64f966504438391acf65 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Mon, 18 May 2020 17:11:30 +0200 Subject: [PATCH 5/5] Remove useless use of await. --- .../server/services/epm/elasticsearch/template/install.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts index b7c35a04a19d43..2c452f16cc104b 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts @@ -83,7 +83,7 @@ const installPreBuiltTemplates = async ( // The existing convenience endpoint `indices.putTemplate` only sends to _template, // which does not support v2 templates. // See src/core/server/elasticsearch/api_types.ts for available endpoints. - return await callCluster('transport.request', callClusterParams); + return callCluster('transport.request', callClusterParams); }); try { return await Promise.all(templateInstallPromises); @@ -123,7 +123,7 @@ const installPreBuiltComponentTemplates = async ( // This uses the catch-all endpoint 'transport.request' because there is no // convenience endpoint for component templates yet. // See src/core/server/elasticsearch/api_types.ts for available endpoints. - return await callCluster('transport.request', callClusterParams); + return callCluster('transport.request', callClusterParams); }); try { return await Promise.all(templateInstallPromises);