From 4706a8bf58b12113cf22463b06da6945fa2ad640 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Tue, 23 Nov 2021 11:55:17 +0100 Subject: [PATCH] [Uptime] Synthetics monitor saved object (#119318) --- .../type_registrations.test.ts | 1 + x-pack/plugins/uptime/common/types/index.ts | 16 ++++ x-pack/plugins/uptime/server/kibana.index.ts | 2 +- .../telemetry/kibana_telemetry_adapter.ts | 2 +- .../uptime/server/lib/alerts/tls.test.ts | 2 +- .../plugins/uptime/server/lib/alerts/tls.ts | 2 +- .../uptime/server/lib/alerts/tls_legacy.ts | 2 +- x-pack/plugins/uptime/server/lib/lib.ts | 2 +- .../server/lib/requests/get_index_pattern.ts | 2 +- .../uptime/server/lib/saved_objects.ts | 82 ------------------- .../server/lib/saved_objects/saved_objects.ts | 53 ++++++++++++ .../lib/saved_objects/synthetics_monitor.ts | 47 +++++++++++ .../lib/saved_objects/uptime_settings.ts | 46 +++++++++++ x-pack/plugins/uptime/server/plugin.ts | 6 +- .../server/rest_api/dynamic_settings.ts | 2 +- .../api_integration/apis/uptime/rest/index.ts | 2 +- x-pack/test/functional/apps/uptime/index.ts | 2 +- 17 files changed, 177 insertions(+), 94 deletions(-) delete mode 100644 x-pack/plugins/uptime/server/lib/saved_objects.ts create mode 100644 x-pack/plugins/uptime/server/lib/saved_objects/saved_objects.ts create mode 100644 x-pack/plugins/uptime/server/lib/saved_objects/synthetics_monitor.ts create mode 100644 x-pack/plugins/uptime/server/lib/saved_objects/uptime_settings.ts diff --git a/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts b/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts index 7597657e7706c..fcbb0e6b57c8b 100644 --- a/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts +++ b/src/core/server/saved_objects/migrations/integration_tests/type_registrations.test.ts @@ -89,6 +89,7 @@ const previouslyRegisteredTypes = [ 'siem-ui-timeline-pinned-event', 'space', 'spaces-usage-stats', + 'synthetics-monitor', 'tag', 'task', 'telemetry', diff --git a/x-pack/plugins/uptime/common/types/index.ts b/x-pack/plugins/uptime/common/types/index.ts index ef7130ff96673..734cfcc5f42d4 100644 --- a/x-pack/plugins/uptime/common/types/index.ts +++ b/x-pack/plugins/uptime/common/types/index.ts @@ -5,6 +5,8 @@ * 2.0. */ +import { SimpleSavedObject } from 'kibana/public'; + /** Represents the average monitor duration ms at a point in time. */ export interface MonitorDurationAveragePoint { /** The timeseries value for this point. */ @@ -28,3 +30,17 @@ export interface MonitorDurationResult { export interface MonitorIdParam { monitorId: string; } + +export type SyntheticsMonitorSavedObject = SimpleSavedObject<{ + name: string; + runOnce: boolean; + urls?: string[]; + tags?: string[]; + schedule: string; + type: 'http' | 'tcp' | 'icmp' | 'browser'; + source?: { + inline: { + script: string; + }; + }; +}>; diff --git a/x-pack/plugins/uptime/server/kibana.index.ts b/x-pack/plugins/uptime/server/kibana.index.ts index 589b09e5a13d7..131510e62c5d9 100644 --- a/x-pack/plugins/uptime/server/kibana.index.ts +++ b/x-pack/plugins/uptime/server/kibana.index.ts @@ -12,7 +12,7 @@ import { PLUGIN } from '../common/constants/plugin'; import { compose } from './lib/compose/kibana'; import { initUptimeServer } from './uptime_server'; import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework'; -import { umDynamicSettings } from './lib/saved_objects'; +import { umDynamicSettings } from './lib/saved_objects/uptime_settings'; import { UptimeRuleRegistry } from './plugin'; export interface KibanaRouteOptions { diff --git a/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts b/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts index 631bcf1b245db..d829044da5047 100644 --- a/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts +++ b/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts @@ -9,7 +9,7 @@ import moment from 'moment'; import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server'; import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { PageViewParams, UptimeTelemetry, Usage } from './types'; -import { savedObjectsAdapter } from '../../saved_objects'; +import { savedObjectsAdapter } from '../../saved_objects/saved_objects'; import { UptimeESClient, createUptimeESClient } from '../../lib'; interface UptimeTelemetryCollector { diff --git a/x-pack/plugins/uptime/server/lib/alerts/tls.test.ts b/x-pack/plugins/uptime/server/lib/alerts/tls.test.ts index b069d368b2a39..cc5a13d606e28 100644 --- a/x-pack/plugins/uptime/server/lib/alerts/tls.test.ts +++ b/x-pack/plugins/uptime/server/lib/alerts/tls.test.ts @@ -12,7 +12,7 @@ import { CertResult, DynamicSettings } from '../../../common/runtime_types'; import { createRuleTypeMocks, bootstrapDependencies } from './test_utils'; import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants'; -import { savedObjectsAdapter, UMSavedObjectsAdapter } from '../saved_objects'; +import { savedObjectsAdapter, UMSavedObjectsAdapter } from '../saved_objects/saved_objects'; /** * This function aims to provide an easy way to give mock props that will diff --git a/x-pack/plugins/uptime/server/lib/alerts/tls.ts b/x-pack/plugins/uptime/server/lib/alerts/tls.ts index 8e49c07f62c42..12b4fdb23a1a1 100644 --- a/x-pack/plugins/uptime/server/lib/alerts/tls.ts +++ b/x-pack/plugins/uptime/server/lib/alerts/tls.ts @@ -17,7 +17,7 @@ import { TlsTranslations } from '../../../common/translations'; import { ActionGroupIdsOf } from '../../../../alerting/common'; -import { savedObjectsAdapter } from '../saved_objects'; +import { savedObjectsAdapter } from '../saved_objects/saved_objects'; import { createUptimeESClient } from '../lib'; export type ActionGroupIds = ActionGroupIdsOf; diff --git a/x-pack/plugins/uptime/server/lib/alerts/tls_legacy.ts b/x-pack/plugins/uptime/server/lib/alerts/tls_legacy.ts index fe49fc8416482..8236af03de85c 100644 --- a/x-pack/plugins/uptime/server/lib/alerts/tls_legacy.ts +++ b/x-pack/plugins/uptime/server/lib/alerts/tls_legacy.ts @@ -18,7 +18,7 @@ import { ActionGroupIdsOf } from '../../../../alerting/common'; import { AlertInstanceContext } from '../../../../alerting/common'; import { AlertInstance } from '../../../../alerting/server'; -import { savedObjectsAdapter } from '../saved_objects'; +import { savedObjectsAdapter } from '../saved_objects/saved_objects'; import { createUptimeESClient } from '../lib'; import { DEFAULT_FROM, diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index fbd0494a3ca82..151f7b25adc1e 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -16,7 +16,7 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { UMBackendFrameworkAdapter } from './adapters'; import { UMLicenseCheck } from './domains'; import { UptimeRequests } from './requests'; -import { savedObjectsAdapter } from './saved_objects'; +import { savedObjectsAdapter } from './saved_objects/saved_objects'; import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch'; import { RequestStatus } from '../../../../../src/plugins/inspector'; import { getInspectResponse } from '../../../observability/server'; diff --git a/x-pack/plugins/uptime/server/lib/requests/get_index_pattern.ts b/x-pack/plugins/uptime/server/lib/requests/get_index_pattern.ts index a0eab63492c2d..e79779133e78c 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_index_pattern.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_index_pattern.ts @@ -7,7 +7,7 @@ import { FieldDescriptor, IndexPatternsFetcher } from '../../../../../../src/plugins/data/server'; import { UptimeESClient } from '../lib'; -import { savedObjectsAdapter } from '../saved_objects'; +import { savedObjectsAdapter } from '../saved_objects/saved_objects'; export interface IndexPatternTitleAndFields { title: string; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects.ts b/x-pack/plugins/uptime/server/lib/saved_objects.ts deleted file mode 100644 index cf5b86564a714..0000000000000 --- a/x-pack/plugins/uptime/server/lib/saved_objects.ts +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import { DYNAMIC_SETTINGS_DEFAULTS } from '../../common/constants'; -import { DynamicSettings } from '../../common/runtime_types'; -import { SavedObjectsType, SavedObjectsErrorHelpers } from '../../../../../src/core/server'; -import { UMSavedObjectsQueryFn } from './adapters'; -import { UptimeConfig } from '../config'; - -export interface UMSavedObjectsAdapter { - config: UptimeConfig; - getUptimeDynamicSettings: UMSavedObjectsQueryFn; - setUptimeDynamicSettings: UMSavedObjectsQueryFn; -} - -export const settingsObjectType = 'uptime-dynamic-settings'; -export const settingsObjectId = 'uptime-dynamic-settings-singleton'; - -export const umDynamicSettings: SavedObjectsType = { - name: settingsObjectType, - hidden: false, - namespaceType: 'single', - mappings: { - dynamic: false, - properties: { - /* Leaving these commented to make it clear that these fields exist, even though we don't want them indexed. - When adding new fields please add them here. If they need to be searchable put them in the uncommented - part of properties. - heartbeatIndices: { - type: 'keyword', - }, - certAgeThreshold: { - type: 'long', - }, - certExpirationThreshold: { - type: 'long', - }, - defaultConnectors: { - type: 'keyword', - }, - */ - }, - }, - management: { - importableAndExportable: true, - icon: 'uptimeApp', - getTitle: () => - i18n.translate('xpack.uptime.uptimeSettings.index', { - defaultMessage: 'Uptime Settings - Index', - }), - }, -}; - -export const savedObjectsAdapter: UMSavedObjectsAdapter = { - config: null, - getUptimeDynamicSettings: async (client): Promise => { - try { - const obj = await client.get(umDynamicSettings.name, settingsObjectId); - return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS; - } catch (getErr) { - const config = savedObjectsAdapter.config; - if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) { - if (config?.index) { - return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index }; - } - return DYNAMIC_SETTINGS_DEFAULTS; - } - throw getErr; - } - }, - setUptimeDynamicSettings: async (client, settings): Promise => { - await client.create(umDynamicSettings.name, settings, { - id: settingsObjectId, - overwrite: true, - }); - }, -}; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects/saved_objects.ts b/x-pack/plugins/uptime/server/lib/saved_objects/saved_objects.ts new file mode 100644 index 0000000000000..4bdd101e7ad15 --- /dev/null +++ b/x-pack/plugins/uptime/server/lib/saved_objects/saved_objects.ts @@ -0,0 +1,53 @@ +/* + * 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 { + SavedObjectsErrorHelpers, + SavedObjectsServiceSetup, +} from '../../../../../../src/core/server'; +import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants'; +import { DynamicSettings } from '../../../common/runtime_types'; +import { UMSavedObjectsQueryFn } from '../adapters'; +import { UptimeConfig } from '../../config'; +import { settingsObjectId, umDynamicSettings } from './uptime_settings'; +import { syntheticsMonitor } from './synthetics_monitor'; + +export interface UMSavedObjectsAdapter { + config: UptimeConfig; + getUptimeDynamicSettings: UMSavedObjectsQueryFn; + setUptimeDynamicSettings: UMSavedObjectsQueryFn; +} + +export const registerUptimeSavedObjects = (savedObjectsService: SavedObjectsServiceSetup) => { + savedObjectsService.registerType(umDynamicSettings); + savedObjectsService.registerType(syntheticsMonitor); +}; + +export const savedObjectsAdapter: UMSavedObjectsAdapter = { + config: null, + getUptimeDynamicSettings: async (client): Promise => { + try { + const obj = await client.get(umDynamicSettings.name, settingsObjectId); + return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS; + } catch (getErr) { + const config = savedObjectsAdapter.config; + if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) { + if (config?.index) { + return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index }; + } + return DYNAMIC_SETTINGS_DEFAULTS; + } + throw getErr; + } + }, + setUptimeDynamicSettings: async (client, settings): Promise => { + await client.create(umDynamicSettings.name, settings, { + id: settingsObjectId, + overwrite: true, + }); + }, +}; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects/synthetics_monitor.ts b/x-pack/plugins/uptime/server/lib/saved_objects/synthetics_monitor.ts new file mode 100644 index 0000000000000..88e9ad6f7f0fb --- /dev/null +++ b/x-pack/plugins/uptime/server/lib/saved_objects/synthetics_monitor.ts @@ -0,0 +1,47 @@ +/* + * 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 { SavedObjectsType } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; + +export const syntheticsMonitorType = 'synthetics-monitor'; + +export const syntheticsMonitor: SavedObjectsType = { + name: syntheticsMonitorType, + hidden: false, + namespaceType: 'single', + mappings: { + dynamic: false, + properties: { + name: { + type: 'keyword', + }, + id: { + type: 'keyword', + }, + type: { + type: 'keyword', + }, + urls: { + type: 'keyword', + }, + tags: { + type: 'keyword', + }, + }, + }, + management: { + importableAndExportable: true, + icon: 'uptimeApp', + getTitle: (savedObject) => + savedObject.attributes.name + + ' - ' + + i18n.translate('xpack.uptime.syntheticsMonitors', { + defaultMessage: 'Uptime - Monitor', + }), + }, +}; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects/uptime_settings.ts b/x-pack/plugins/uptime/server/lib/saved_objects/uptime_settings.ts new file mode 100644 index 0000000000000..0360b0ed11eb2 --- /dev/null +++ b/x-pack/plugins/uptime/server/lib/saved_objects/uptime_settings.ts @@ -0,0 +1,46 @@ +/* + * 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 { SavedObjectsType } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +export const settingsObjectType = 'uptime-dynamic-settings'; +export const settingsObjectId = 'uptime-dynamic-settings-singleton'; + +export const umDynamicSettings: SavedObjectsType = { + name: settingsObjectType, + hidden: false, + namespaceType: 'single', + mappings: { + dynamic: false, + properties: { + /* Leaving these commented to make it clear that these fields exist, even though we don't want them indexed. + When adding new fields please add them here. If they need to be searchable put them in the uncommented + part of properties. + heartbeatIndices: { + type: 'keyword', + }, + certAgeThreshold: { + type: 'long', + }, + certExpirationThreshold: { + type: 'long', + }, + defaultConnectors: { + type: 'keyword', + }, + */ + }, + }, + management: { + importableAndExportable: true, + icon: 'uptimeApp', + getTitle: () => + i18n.translate('xpack.uptime.uptimeSettings.index', { + defaultMessage: 'Uptime Settings - Index', + }), + }, +}; diff --git a/x-pack/plugins/uptime/server/plugin.ts b/x-pack/plugins/uptime/server/plugin.ts index efb613dfda826..5cd68988cefc1 100644 --- a/x-pack/plugins/uptime/server/plugin.ts +++ b/x-pack/plugins/uptime/server/plugin.ts @@ -16,7 +16,7 @@ import { import { uptimeRuleFieldMap } from '../common/rules/uptime_rule_field_map'; import { initServerWithKibana } from './kibana.index'; import { KibanaTelemetryAdapter, UptimeCorePlugins } from './lib/adapters'; -import { savedObjectsAdapter, umDynamicSettings } from './lib/saved_objects'; +import { registerUptimeSavedObjects, savedObjectsAdapter } from './lib/saved_objects/saved_objects'; import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map'; import { Dataset } from '../../rule_registry/server'; import { UptimeConfig } from './config'; @@ -59,7 +59,9 @@ export class Plugin implements PluginType { ruleDataClient, this.logger ); - core.savedObjects.registerType(umDynamicSettings); + + registerUptimeSavedObjects(core.savedObjects); + KibanaTelemetryAdapter.registerUsageCollector( plugins.usageCollection, () => this.savedObjectsClient 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 9068a8f6da52f..79fe6f67908a6 100644 --- a/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts +++ b/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts @@ -11,7 +11,7 @@ import { PathReporter } from 'io-ts/lib/PathReporter'; import { UMServerLibs } from '../lib/lib'; import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types'; import { UMRestApiRouteFactory } from '.'; -import { savedObjectsAdapter } from '../lib/saved_objects'; +import { savedObjectsAdapter } from '../lib/saved_objects/saved_objects'; import { VALUE_MUST_BE_GREATER_THAN_ZERO, VALUE_MUST_BE_AN_INTEGER, diff --git a/x-pack/test/api_integration/apis/uptime/rest/index.ts b/x-pack/test/api_integration/apis/uptime/rest/index.ts index a37a4ab8f299d..dc3c00b03f712 100644 --- a/x-pack/test/api_integration/apis/uptime/rest/index.ts +++ b/x-pack/test/api_integration/apis/uptime/rest/index.ts @@ -9,7 +9,7 @@ import { FtrProviderContext } from '../../../ftr_provider_context'; import { settingsObjectId, settingsObjectType, -} from '../../../../../plugins/uptime/server/lib/saved_objects'; +} from '../../../../../plugins/uptime/server/lib/saved_objects/uptime_settings'; export default function ({ getService, loadTestFile }: FtrProviderContext) { const esArchiver = getService('esArchiver'); diff --git a/x-pack/test/functional/apps/uptime/index.ts b/x-pack/test/functional/apps/uptime/index.ts index ef5a4e576da88..79e0fbd56e39e 100644 --- a/x-pack/test/functional/apps/uptime/index.ts +++ b/x-pack/test/functional/apps/uptime/index.ts @@ -9,7 +9,7 @@ import { FtrProviderContext } from '../../ftr_provider_context'; import { settingsObjectId, settingsObjectType, -} from '../../../../plugins/uptime/server/lib/saved_objects'; +} from '../../../../plugins/uptime/server/lib/saved_objects/uptime_settings'; const ARCHIVE = 'x-pack/test/functional/es_archives/uptime/full_heartbeat';