From 0c5154d6140bb8c7d487282533f2b3c0c82fb8f2 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Wed, 20 Oct 2021 16:03:14 +0200 Subject: [PATCH] Uptime index config using kibana.yml --- .../resources/base/bin/kibana-docker | 1 + x-pack/plugins/uptime/server/config.ts | 17 +++++++++++++++++ x-pack/plugins/uptime/server/index.ts | 2 ++ x-pack/plugins/uptime/server/kibana.index.ts | 6 ++++-- .../lib/adapters/framework/adapter_types.ts | 2 ++ .../plugins/uptime/server/lib/saved_objects.ts | 5 ++++- x-pack/plugins/uptime/server/plugin.ts | 6 +++++- .../server/rest_api/create_route_with_auth.ts | 2 ++ .../uptime/server/rest_api/dynamic_settings.ts | 4 ++-- x-pack/plugins/uptime/server/rest_api/types.ts | 8 +++++++- .../server/rest_api/uptime_route_wrapper.ts | 3 ++- x-pack/plugins/uptime/server/uptime_server.ts | 6 ++++-- 12 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 x-pack/plugins/uptime/server/config.ts diff --git a/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker b/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker index 4b5c2e25084ed..ab70b39693d74 100755 --- a/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker +++ b/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker @@ -380,6 +380,7 @@ kibana_vars=( xpack.task_manager.poll_interval xpack.task_manager.request_capacity xpack.task_manager.version_conflict_threshold + xpack.uptime.index ) longopts='' diff --git a/x-pack/plugins/uptime/server/config.ts b/x-pack/plugins/uptime/server/config.ts new file mode 100644 index 0000000000000..90933234653f4 --- /dev/null +++ b/x-pack/plugins/uptime/server/config.ts @@ -0,0 +1,17 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { PluginConfigDescriptor } from 'kibana/server'; +import { schema, TypeOf } from '@kbn/config-schema'; + +export const config: PluginConfigDescriptor = { + schema: schema.object({ + index: schema.string(), + }), +}; + +export type UptimeConfig = TypeOf; diff --git a/x-pack/plugins/uptime/server/index.ts b/x-pack/plugins/uptime/server/index.ts index 4894c73c625c1..a48ae37d077f9 100644 --- a/x-pack/plugins/uptime/server/index.ts +++ b/x-pack/plugins/uptime/server/index.ts @@ -10,3 +10,5 @@ import { Plugin } from './plugin'; export const plugin = (initializerContext: PluginInitializerContext) => new Plugin(initializerContext); + +export { config } from './config'; diff --git a/x-pack/plugins/uptime/server/kibana.index.ts b/x-pack/plugins/uptime/server/kibana.index.ts index 3b1001daf0515..d7ae1e4092e65 100644 --- a/x-pack/plugins/uptime/server/kibana.index.ts +++ b/x-pack/plugins/uptime/server/kibana.index.ts @@ -14,6 +14,7 @@ import { initUptimeServer } from './uptime_server'; import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework'; import { umDynamicSettings } from './lib/saved_objects'; import { UptimeRuleRegistry } from './plugin'; +import { UptimeConfig } from './config'; export interface KibanaRouteOptions { path: string; @@ -31,7 +32,8 @@ export const initServerWithKibana = ( server: UptimeCoreSetup, plugins: UptimeCorePlugins, ruleRegistry: UptimeRuleRegistry, - logger: Logger + logger: Logger, + config: UptimeConfig ) => { const { features } = plugins; const libs = compose(server); @@ -113,5 +115,5 @@ export const initServerWithKibana = ( }, }); - initUptimeServer(server, libs, plugins, ruleRegistry, logger); + initUptimeServer(server, libs, plugins, ruleRegistry, logger, config); }; diff --git a/x-pack/plugins/uptime/server/lib/adapters/framework/adapter_types.ts b/x-pack/plugins/uptime/server/lib/adapters/framework/adapter_types.ts index d5b938d78c864..8e7e669f914dc 100644 --- a/x-pack/plugins/uptime/server/lib/adapters/framework/adapter_types.ts +++ b/x-pack/plugins/uptime/server/lib/adapters/framework/adapter_types.ts @@ -18,6 +18,7 @@ import { MlPluginSetup as MlSetup } from '../../../../../ml/server'; import { RuleRegistryPluginSetupContract } from '../../../../../rule_registry/server'; import { UptimeESClient } from '../../lib'; import type { UptimeRouter } from '../../../types'; +import { UptimeConfig } from '../../../config'; export type UMElasticsearchQueryFn = ( params: { @@ -28,6 +29,7 @@ export type UMElasticsearchQueryFn = ( export type UMSavedObjectsQueryFn = ( client: SavedObjectsClientContract | ISavedObjectsRepository, + config: UptimeConfig, params?: P ) => Promise | T; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects.ts b/x-pack/plugins/uptime/server/lib/saved_objects.ts index 3e9888df55aa0..dbd57083251be 100644 --- a/x-pack/plugins/uptime/server/lib/saved_objects.ts +++ b/x-pack/plugins/uptime/server/lib/saved_objects.ts @@ -55,12 +55,15 @@ export const umDynamicSettings: SavedObjectsType = { }; export const savedObjectsAdapter: UMSavedObjectsAdapter = { - getUptimeDynamicSettings: async (client): Promise => { + getUptimeDynamicSettings: async (client, config): Promise => { try { const obj = await client.get(umDynamicSettings.name, settingsObjectId); return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS; } catch (getErr) { if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) { + if (config?.index) { + return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index }; + } return DYNAMIC_SETTINGS_DEFAULTS; } throw getErr; diff --git a/x-pack/plugins/uptime/server/plugin.ts b/x-pack/plugins/uptime/server/plugin.ts index 736cbed51084c..d68a31b4fb4ee 100644 --- a/x-pack/plugins/uptime/server/plugin.ts +++ b/x-pack/plugins/uptime/server/plugin.ts @@ -19,6 +19,7 @@ import { KibanaTelemetryAdapter, UptimeCorePlugins } from './lib/adapters'; import { umDynamicSettings } from './lib/saved_objects'; import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map'; import { Dataset } from '../../rule_registry/server'; +import { UptimeConfig } from './config'; export type UptimeRuleRegistry = ReturnType['ruleRegistry']; @@ -32,6 +33,8 @@ export class Plugin implements PluginType { } public setup(core: CoreSetup, plugins: UptimeCorePlugins) { + const config = this.initContext.config.get(); + this.logger = this.initContext.logger.get(); const { ruleDataService } = plugins.ruleRegistry; @@ -52,7 +55,8 @@ export class Plugin implements PluginType { { router: core.http.createRouter() }, plugins, ruleDataClient, - this.logger + this.logger, + config ); core.savedObjects.registerType(umDynamicSettings); KibanaTelemetryAdapter.registerUsageCollector( diff --git a/x-pack/plugins/uptime/server/rest_api/create_route_with_auth.ts b/x-pack/plugins/uptime/server/rest_api/create_route_with_auth.ts index 8b6add27f889a..66e0e0e42e614 100644 --- a/x-pack/plugins/uptime/server/rest_api/create_route_with_auth.ts +++ b/x-pack/plugins/uptime/server/rest_api/create_route_with_auth.ts @@ -20,6 +20,7 @@ export const createRouteWithAuth = ( request, response, savedObjectsClient, + config, }) => { const { statusCode, message } = libs.license(context.licensing.license); if (statusCode === 200) { @@ -29,6 +30,7 @@ export const createRouteWithAuth = ( request, response, savedObjectsClient, + config, }); } switch (statusCode) { diff --git a/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts b/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts index 637ac72e12ee5..9bf3268bcdded 100644 --- a/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts +++ b/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts @@ -21,8 +21,8 @@ export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSer method: 'GET', path: '/api/uptime/dynamic_settings', validate: false, - handler: async ({ savedObjectsClient }): Promise => { - return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient); + handler: async ({ savedObjectsClient, config }): Promise => { + return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient, config); }, }); diff --git a/x-pack/plugins/uptime/server/rest_api/types.ts b/x-pack/plugins/uptime/server/rest_api/types.ts index ea083fc04174e..464cff70e55b2 100644 --- a/x-pack/plugins/uptime/server/rest_api/types.ts +++ b/x-pack/plugins/uptime/server/rest_api/types.ts @@ -17,6 +17,7 @@ import { } from 'kibana/server'; import { UMServerLibs, UptimeESClient } from '../lib/lib'; import type { UptimeRequestHandlerContext } from '../types'; +import { UptimeConfig } from '../config'; /** * Defines the basic properties employed by Uptime routes. @@ -58,7 +59,10 @@ export type UMRestApiRouteFactory = (libs: UMServerLibs) => UptimeRoute; * Functions of this type accept our internal route format and output a route * object that the Kibana platform can consume. */ -export type UMKibanaRouteWrapper = (uptimeRoute: UptimeRoute) => UMKibanaRoute; +export type UMKibanaRouteWrapper = ( + uptimeRoute: UptimeRoute, + config: UptimeConfig +) => UMKibanaRoute; /** * This is the contract we specify internally for route handling. @@ -69,10 +73,12 @@ export type UMRouteHandler = ({ request, response, savedObjectsClient, + config, }: { uptimeEsClient: UptimeESClient; context: UptimeRequestHandlerContext; request: KibanaRequest, Record, Record>; response: KibanaResponseFactory; savedObjectsClient: SavedObjectsClientContract; + config: UptimeConfig; }) => IKibanaResponse | Promise>; diff --git a/x-pack/plugins/uptime/server/rest_api/uptime_route_wrapper.ts b/x-pack/plugins/uptime/server/rest_api/uptime_route_wrapper.ts index ddde993cc9c70..20ec62bf2b8dc 100644 --- a/x-pack/plugins/uptime/server/rest_api/uptime_route_wrapper.ts +++ b/x-pack/plugins/uptime/server/rest_api/uptime_route_wrapper.ts @@ -12,7 +12,7 @@ import { createUptimeESClient, inspectableEsQueriesMap } from '../lib/lib'; import { KibanaResponse } from '../../../../../src/core/server/http/router'; import { enableInspectEsQueries } from '../../../observability/common'; -export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute) => ({ +export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute, config) => ({ ...uptimeRoute, options: { tags: ['access:uptime-read', ...(uptimeRoute?.writeAccess ? ['access:uptime-write'] : [])], @@ -40,6 +40,7 @@ export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute) => ({ context, request, response, + config, }); if (res instanceof KibanaResponse) { diff --git a/x-pack/plugins/uptime/server/uptime_server.ts b/x-pack/plugins/uptime/server/uptime_server.ts index ded76027a3c3a..d989051f03242 100644 --- a/x-pack/plugins/uptime/server/uptime_server.ts +++ b/x-pack/plugins/uptime/server/uptime_server.ts @@ -15,16 +15,18 @@ import { statusCheckAlertFactory } from './lib/alerts/status_check'; import { tlsAlertFactory } from './lib/alerts/tls'; import { tlsLegacyAlertFactory } from './lib/alerts/tls_legacy'; import { durationAnomalyAlertFactory } from './lib/alerts/duration_anomaly'; +import { UptimeConfig } from './config'; export const initUptimeServer = ( server: UptimeCoreSetup, libs: UMServerLibs, plugins: UptimeCorePlugins, ruleDataClient: IRuleDataClient, - logger: Logger + logger: Logger, + config: UptimeConfig ) => { restApiRoutes.forEach((route) => - libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route))) + libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route), config)) ); const {