diff --git a/api_docs/ml.json b/api_docs/ml.json index 3a3463b7397e..efa66ac9c9e8 100644 --- a/api_docs/ml.json +++ b/api_docs/ml.json @@ -5498,17 +5498,11 @@ "type": "Function", "children": [ { - "type": "CompoundType", + "type": "Any", "label": "error", - "isRequired": false, + "isRequired": true, "signature": [ - { - "pluginId": "ml", - "scope": "common", - "docId": "kibMlPluginApi", - "section": "def-common.ErrorType", - "text": "ErrorType" - } + "any" ], "description": [], "source": { @@ -5518,9 +5512,7 @@ } ], "signature": [ - "(error: ", - "ErrorType", - ") => string" + "(error: any) => string" ], "description": [], "label": "extractErrorMessage", diff --git a/api_docs/monitoring.json b/api_docs/monitoring.json index 51f1231a42e2..b24cdd89e424 100644 --- a/api_docs/monitoring.json +++ b/api_docs/monitoring.json @@ -46,6 +46,34 @@ "signature": [ "() => void" ] + }, + { + "tags": [], + "id": "def-server.IBulkUploader.start", + "type": "Function", + "label": "start", + "description": [], + "source": { + "path": "x-pack/plugins/monitoring/server/types.ts", + "lineNumber": 95 + }, + "signature": [ + "() => void" + ] + }, + { + "tags": [], + "id": "def-server.IBulkUploader.handleNotEnabled", + "type": "Function", + "label": "handleNotEnabled", + "description": [], + "source": { + "path": "x-pack/plugins/monitoring/server/types.ts", + "lineNumber": 96 + }, + "signature": [ + "() => void" + ] } ], "source": { @@ -106,7 +134,7 @@ "description": [], "source": { "path": "x-pack/plugins/monitoring/server/types.ts", - "lineNumber": 98 + "lineNumber": 100 }, "signature": [ "() => any" @@ -115,7 +143,7 @@ ], "source": { "path": "x-pack/plugins/monitoring/server/types.ts", - "lineNumber": 97 + "lineNumber": 99 }, "lifecycle": "setup", "initialIsOpen": true diff --git a/x-pack/plugins/monitoring/server/cluster_alerts/alerts_cluster_search.js b/x-pack/plugins/monitoring/server/cluster_alerts/alerts_cluster_search.js index 96c1edccd5ef..05f0524c1252 100644 --- a/x-pack/plugins/monitoring/server/cluster_alerts/alerts_cluster_search.js +++ b/x-pack/plugins/monitoring/server/cluster_alerts/alerts_cluster_search.js @@ -146,8 +146,8 @@ export function createFilterForTime(options) { * @param {Object} req Request object from the API route * @param {String} cluster The cluster being checked */ -export function alertsClusterSearch(req, alertsIndex, cluster, checkLicense, options = {}) { - const verification = verifyMonitoringLicense(req.server); +export async function alertsClusterSearch(req, alertsIndex, cluster, checkLicense, options = {}) { + const verification = await verifyMonitoringLicense(req.server); if (!verification.enabled) { return Promise.resolve({ message: verification.message }); diff --git a/x-pack/plugins/monitoring/server/cluster_alerts/alerts_clusters_aggregation.js b/x-pack/plugins/monitoring/server/cluster_alerts/alerts_clusters_aggregation.js index f6e7fb01b7c3..5c4194d06361 100644 --- a/x-pack/plugins/monitoring/server/cluster_alerts/alerts_clusters_aggregation.js +++ b/x-pack/plugins/monitoring/server/cluster_alerts/alerts_clusters_aggregation.js @@ -9,8 +9,8 @@ import { get, find } from 'lodash'; import { verifyMonitoringLicense } from './verify_monitoring_license'; import { i18n } from '@kbn/i18n'; -export function alertsClustersAggregation(req, alertsIndex, clusters, checkLicense) { - const verification = verifyMonitoringLicense(req.server); +export async function alertsClustersAggregation(req, alertsIndex, clusters, checkLicense) { + const verification = await verifyMonitoringLicense(req.server); if (!verification.enabled) { // return metadata detailing that alerts is disabled because of the monitoring cluster license diff --git a/x-pack/plugins/monitoring/server/cluster_alerts/verify_monitoring_license.js b/x-pack/plugins/monitoring/server/cluster_alerts/verify_monitoring_license.js index 238e9daf4d84..e93db4ea9609 100644 --- a/x-pack/plugins/monitoring/server/cluster_alerts/verify_monitoring_license.js +++ b/x-pack/plugins/monitoring/server/cluster_alerts/verify_monitoring_license.js @@ -16,17 +16,18 @@ import { i18n } from '@kbn/i18n'; * @param {Object} server Server object containing config and plugins * @return {Boolean} {@code true} to indicate that cluster alerts can be used. */ -export function verifyMonitoringLicense(server) { +export async function verifyMonitoringLicense(server) { const config = server.config(); // if cluster alerts are enabled, then ensure that we can use it according to the license if (config.get('monitoring.cluster_alerts.enabled')) { const xpackInfo = get(server.plugins.monitoring, 'info'); if (xpackInfo) { - const watcherFeature = xpackInfo.getWatcherFeature(); + const licenseService = await xpackInfo.getLicenseService(); + const watcherFeature = licenseService.getWatcherFeature(); return { enabled: watcherFeature.isEnabled, - message: xpackInfo.getMessage(), + message: licenseService.getMessage(), }; } diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js index 990a4df85c5f..b282cf94ade2 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js @@ -127,8 +127,8 @@ export async function getClustersFromRequest( clusters.map((cluster) => cluster.cluster_uuid) ); + const verification = await verifyMonitoringLicense(req.server); for (const cluster of clusters) { - const verification = verifyMonitoringLicense(req.server); if (!verification.enabled) { // return metadata detailing that alerts is disabled because of the monitoring cluster license cluster.alerts = { diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js index 9d0de92bc1aa..1895c36699e5 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js @@ -20,7 +20,8 @@ export async function verifyMonitoringAuth(req) { const xpackInfo = get(req.server.plugins.monitoring, 'info'); if (xpackInfo) { - const security = xpackInfo.getSecurityFeature(); + const licenseService = await xpackInfo.getLicenseService(); + const security = licenseService.getSecurityFeature(); // we only need to verify permissions if we're using X-Pack Security if (security.isAvailable && security.isEnabled) { diff --git a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js index e0b8d2c0d098..000c26bff48a 100644 --- a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js +++ b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js @@ -234,7 +234,8 @@ function isBeatFromAPM(bucket) { } async function hasNecessaryPermissions(req) { - const securityFeature = req.server.plugins.monitoring.info.getSecurityFeature(); + const licenseService = await req.server.plugins.monitoring.info.getLicenseService(); + const securityFeature = licenseService.getSecurityFeature(); if (!securityFeature.isAvailable || !securityFeature.isEnabled) { return true; } diff --git a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js index 2d99443dac3d..437e07512d46 100644 --- a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js +++ b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js @@ -47,12 +47,14 @@ const mockReq = ( plugins: { monitoring: { info: { - getSecurityFeature: () => { - return { - isAvailable: securityEnabled, - isEnabled: securityEnabled, - }; - }, + getLicenseService: () => ({ + getSecurityFeature: () => { + return { + isAvailable: securityEnabled, + isEnabled: securityEnabled, + }; + }, + }), }, }, elasticsearch: { diff --git a/x-pack/plugins/monitoring/server/license_service.ts b/x-pack/plugins/monitoring/server/license_service.ts index efd1991688ee..fde22aaefa5d 100644 --- a/x-pack/plugins/monitoring/server/license_service.ts +++ b/x-pack/plugins/monitoring/server/license_service.ts @@ -8,13 +8,13 @@ import { Subscription } from 'rxjs'; import { ILegacyCustomClusterClient } from 'kibana/server'; import { ILicense, LicenseFeature } from '../../licensing/common/types'; -import { LicensingPluginSetup } from '../../licensing/server'; +import { LicensingPluginStart } from '../../licensing/server'; import { MonitoringConfig } from './config'; import { Logger } from '../../../../src/core/server'; import { MonitoringLicenseService } from './types'; interface SetupDeps { - licensing: LicensingPluginSetup; + licensing: LicensingPluginStart; monitoringClient: ILegacyCustomClusterClient; config: MonitoringConfig; log: Logger; diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index a3fc82109849..e7606ece4457 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -105,15 +105,6 @@ export class MonitoringPlugin core.elasticsearch.legacy.createClient )); - // Start our license service which will ensure - // the appropriate licenses are present - this.licenseService = new LicenseService().setup({ - licensing: plugins.licensing, - monitoringClient: cluster, - config, - log: this.log, - }); - Globals.init(core, plugins.cloud, cluster, config, this.getLogger); const serverInfo = core.http.getServerInfo(); const alerts = AlertsFactory.getAll(); @@ -165,34 +156,6 @@ export class MonitoringPlugin }, })); - // If collection is enabled, start it - const kibanaCollectionEnabled = config.kibana.collection.enabled; - if (kibanaCollectionEnabled) { - // Do not use `this.licenseService` as that looks at the monitoring cluster - // whereas we want to check the production cluster here - if (plugins.licensing) { - plugins.licensing.license$.subscribe((license: any) => { - // use updated xpack license info to start/stop bulk upload - const mainMonitoring = license.getFeature('monitoring'); - const monitoringBulkEnabled = - mainMonitoring && mainMonitoring.isAvailable && mainMonitoring.isEnabled; - if (monitoringBulkEnabled) { - bulkUploader.start(); - } else { - bulkUploader.handleNotEnabled(); - } - }); - } else { - kibanaMonitoringLog.warn( - 'Internal collection for Kibana monitoring is disabled due to missing license information.' - ); - } - } else { - kibanaMonitoringLog.info( - 'Internal collection for Kibana monitoring is disabled per configuration.' - ); - } - // If the UI is enabled, then we want to register it so it shows up // and start any other UI-related setup tasks if (config.ui.enabled) { @@ -201,7 +164,6 @@ export class MonitoringPlugin config, legacyConfig, core.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>, - this.licenseService, this.cluster, plugins ); @@ -224,7 +186,46 @@ export class MonitoringPlugin }; } - start() {} + async start(core: CoreStart, { licensing }: PluginsStart) { + const config = createConfig(this.initializerContext.config.get>()); + // Start our license service which will ensure + // the appropriate licenses are present + this.licenseService = new LicenseService().setup({ + licensing, + monitoringClient: this.cluster, + config, + log: this.log, + }); + + // If collection is enabled, start it + const kibanaMonitoringLog = this.getLogger(KIBANA_MONITORING_LOGGING_TAG); + const kibanaCollectionEnabled = config.kibana.collection.enabled; + if (kibanaCollectionEnabled) { + // Do not use `this.licenseService` as that looks at the monitoring cluster + // whereas we want to check the production cluster here + if (this.bulkUploader && licensing) { + licensing.license$.subscribe((license: any) => { + // use updated xpack license info to start/stop bulk upload + const mainMonitoring = license.getFeature('monitoring'); + const monitoringBulkEnabled = + mainMonitoring && mainMonitoring.isAvailable && mainMonitoring.isEnabled; + if (monitoringBulkEnabled) { + this.bulkUploader?.start(); + } else { + this.bulkUploader?.handleNotEnabled(); + } + }); + } else { + kibanaMonitoringLog.warn( + 'Internal collection for Kibana monitoring is disabled due to missing license information.' + ); + } + } else { + kibanaMonitoringLog.info( + 'Internal collection for Kibana monitoring is disabled per configuration.' + ); + } + } stop() { if (this.cluster) { @@ -276,7 +277,6 @@ export class MonitoringPlugin config: MonitoringConfig, legacyConfig: any, getCoreServices: () => Promise<[CoreStart, PluginsStart, {}]>, - licenseService: MonitoringLicenseService, cluster: ILegacyCustomClusterClient, setupPlugins: PluginsSetup ): MonitoringCore { @@ -343,7 +343,9 @@ export class MonitoringPlugin }, plugins: { monitoring: { - info: licenseService, + info: { + getLicenseService: () => this.licenseService, + }, }, elasticsearch: { getCluster: (name: string) => ({ diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index 4a8aba0de6bb..c0bfe32079cd 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -25,7 +25,7 @@ import { PluginSetupContract as AlertingPluginSetupContract, } from '../../alerting/server'; import { InfraPluginSetup } from '../../infra/server'; -import { LicensingPluginSetup } from '../../licensing/server'; +import { LicensingPluginStart } from '../../licensing/server'; import { PluginSetupContract as FeaturesPluginSetupContract } from '../../features/server'; import { EncryptedSavedObjectsPluginSetup } from '../../encrypted_saved_objects/server'; import { CloudSetup } from '../../cloud/server'; @@ -47,7 +47,6 @@ export interface MonitoringElasticsearchConfig { export interface PluginsSetup { encryptedSavedObjects?: EncryptedSavedObjectsPluginSetup; usageCollection?: UsageCollectionSetup; - licensing: LicensingPluginSetup; features: FeaturesPluginSetupContract; alerting?: AlertingPluginSetupContract; infra: InfraPluginSetup; @@ -62,6 +61,7 @@ export interface RequestHandlerContextMonitoringPlugin extends RequestHandlerCon export interface PluginsStart { alerting: AlertingPluginStartContract; actions: ActionsPluginsStartContact; + licensing: LicensingPluginStart; } export interface MonitoringCoreConfig { @@ -92,6 +92,8 @@ export interface LegacyShimDependencies { export interface IBulkUploader { getKibanaStats: () => any; stop: () => void; + start: () => void; + handleNotEnabled: () => void; } export interface MonitoringPluginSetup { @@ -127,7 +129,9 @@ export interface LegacyServer { }; plugins: { monitoring: { - info: MonitoringLicenseService; + info: { + getLicenseService: () => MonitoringLicenseService; + }; }; elasticsearch: { getCluster: (