diff --git a/x-pack/plugins/beats_management/server/lib/adapters/database/kibana_database_adapter.ts b/x-pack/plugins/beats_management/server/lib/adapters/database/kibana_database_adapter.ts index 9a9424fc54e4e..fe7d45d0cf0ee 100644 --- a/x-pack/plugins/beats_management/server/lib/adapters/database/kibana_database_adapter.ts +++ b/x-pack/plugins/beats_management/server/lib/adapters/database/kibana_database_adapter.ts @@ -32,7 +32,7 @@ export class KibanaDatabaseAdapter implements DatabaseAdapter { } public async putTemplate(user: FrameworkUser, params: DatabasePutTemplateParams): Promise { const callES = this.getCallType(user); - const result = await callES('indices.putTemplate', params); + const result: { acknowledged: boolean } = await callES('indices.putTemplate', params); return result; } diff --git a/x-pack/plugins/beats_management/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts b/x-pack/plugins/beats_management/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts index 53038651cc4c5..625628fa6647a 100644 --- a/x-pack/plugins/beats_management/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts +++ b/x-pack/plugins/beats_management/server/lib/adapters/tokens/elasticsearch_tokens_adapter.ts @@ -61,12 +61,17 @@ export class ElasticsearchTokensAdapter implements CMTokensAdapter { ]) ); - await this.database.bulk(user, { + const result = await this.database.bulk(user, { body, index: INDEX_NAMES.BEATS, refresh: 'wait_for', type: '_doc', }); + + if (result.errors) { + throw new Error(result.items[0].result); + } + return tokens; } } diff --git a/x-pack/plugins/beats_management/server/lib/framework.ts b/x-pack/plugins/beats_management/server/lib/framework.ts index 3a79afa1c1ef7..849129f6c2efa 100644 --- a/x-pack/plugins/beats_management/server/lib/framework.ts +++ b/x-pack/plugins/beats_management/server/lib/framework.ts @@ -6,15 +6,16 @@ import Boom from 'boom'; import { difference } from 'lodash'; -import { FrameworkRouteHandler } from './adapters/framework/adapter_types'; -import { FrameworkRequest } from './adapters/framework/adapter_types'; import { BackendFrameworkAdapter, + FrameworkRequest, FrameworkResponse, + FrameworkRouteHandler, FrameworkRouteOptions, } from './adapters/framework/adapter_types'; export class BackendFrameworkLib { + public log = this.adapter.log; public exposeStaticDir = this.adapter.exposeStaticDir; public internalUser = this.adapter.internalUser; constructor(private readonly adapter: BackendFrameworkAdapter) { diff --git a/x-pack/plugins/beats_management/server/lib/types.ts b/x-pack/plugins/beats_management/server/lib/types.ts index d9f2a922ebb34..ce18830da174b 100644 --- a/x-pack/plugins/beats_management/server/lib/types.ts +++ b/x-pack/plugins/beats_management/server/lib/types.ts @@ -6,7 +6,6 @@ import { DatabaseAdapter } from './adapters/database/adapter_types'; import { FrameworkUser } from './adapters/framework/adapter_types'; - import { CMBeatsDomain } from './beats'; import { BackendFrameworkLib } from './framework'; import { CMTagsDomain } from './tags'; @@ -27,3 +26,13 @@ export enum BeatEnrollmentStatus { ExpiredEnrollmentToken = 'Expired enrollment token', InvalidEnrollmentToken = 'Invalid enrollment token', } + +export interface AsyncResponse { + error: { + code: number | string; + message: string; + }; +} +export interface AsyncResponse { + data: DataType; +} diff --git a/x-pack/plugins/beats_management/server/management_server.ts b/x-pack/plugins/beats_management/server/management_server.ts index 679f8f3423867..dca0bff015198 100644 --- a/x-pack/plugins/beats_management/server/management_server.ts +++ b/x-pack/plugins/beats_management/server/management_server.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { INDEX_NAMES } from '../common/constants/index_names'; import { CMServerLibs } from './lib/types'; import { createGetBeatConfigurationRoute } from './rest_api/beats/configuration'; import { createBeatEnrollmentRoute } from './rest_api/beats/enroll'; @@ -19,10 +20,10 @@ import { createSetTagRoute } from './rest_api/tags/set'; import { createTokensRoute } from './rest_api/tokens/create'; import { beatsIndexTemplate } from './utils/index_templates'; -export const initManagementServer = (libs: CMServerLibs) => { +export const initManagementServer = async (libs: CMServerLibs) => { if (libs.database) { - libs.database.putTemplate(libs.framework.internalUser, { - name: '.management-beats', + await libs.database.putTemplate(libs.framework.internalUser, { + name: INDEX_NAMES.BEATS, body: beatsIndexTemplate, }); } diff --git a/x-pack/plugins/beats_management/server/rest_api/tokens/create.ts b/x-pack/plugins/beats_management/server/rest_api/tokens/create.ts index 1b70bd3b3ad8f..1a2a5fd561f94 100644 --- a/x-pack/plugins/beats_management/server/rest_api/tokens/create.ts +++ b/x-pack/plugins/beats_management/server/rest_api/tokens/create.ts @@ -4,12 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ +import Boom from 'boom'; import Joi from 'joi'; import { get } from 'lodash'; import { REQUIRED_LICENSES } from 'x-pack/plugins/beats_management/common/constants'; import { FrameworkRequest } from '../../lib/adapters/framework/adapter_types'; import { CMServerLibs } from '../../lib/types'; -import { wrapEsError } from '../../utils/error_wrappers'; // TODO: write to Kibana audit log file const DEFAULT_NUM_TOKENS = 1; @@ -35,8 +35,8 @@ export const createTokensRoute = (libs: CMServerLibs) => ({ const tokens = await libs.tokens.createEnrollmentTokens(request.user, numTokens); return { tokens }; } catch (err) { - // TODO move this to kibana route thing in adapter - return wrapEsError(err); + libs.framework.log(err.message); + return Boom.internal(); } }, });