2020-03-20 19:02:15 +01:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
import Boom from 'boom';
|
|
|
|
import { combineLatest } from 'rxjs';
|
2020-06-15 18:43:37 +02:00
|
|
|
import { first, map } from 'rxjs/operators';
|
2020-03-20 19:02:15 +01:00
|
|
|
import { i18n } from '@kbn/i18n';
|
|
|
|
import { has, get } from 'lodash';
|
2020-06-15 18:43:37 +02:00
|
|
|
import { TypeOf } from '@kbn/config-schema';
|
2020-03-20 19:02:15 +01:00
|
|
|
import {
|
|
|
|
Logger,
|
|
|
|
PluginInitializerContext,
|
|
|
|
RequestHandlerContext,
|
|
|
|
KibanaRequest,
|
|
|
|
KibanaResponseFactory,
|
|
|
|
CoreSetup,
|
2020-06-29 17:43:31 +02:00
|
|
|
ILegacyCustomClusterClient,
|
2020-03-20 19:02:15 +01:00
|
|
|
CoreStart,
|
2020-04-17 02:07:24 +02:00
|
|
|
CustomHttpResponseOptions,
|
|
|
|
ResponseError,
|
|
|
|
} from 'kibana/server';
|
|
|
|
import {
|
|
|
|
LOGGING_TAG,
|
|
|
|
KIBANA_MONITORING_LOGGING_TAG,
|
|
|
|
KIBANA_STATS_TYPE_MONITORING,
|
2020-07-22 15:45:57 +02:00
|
|
|
ALERTS,
|
2020-04-17 02:07:24 +02:00
|
|
|
} from '../common/constants';
|
2020-06-15 18:43:37 +02:00
|
|
|
import { MonitoringConfig, createConfig, configSchema } from './config';
|
2020-03-20 19:02:15 +01:00
|
|
|
// @ts-ignore
|
|
|
|
import { requireUIRoutes } from './routes';
|
|
|
|
// @ts-ignore
|
|
|
|
import { initBulkUploader } from './kibana_monitoring';
|
|
|
|
// @ts-ignore
|
|
|
|
import { initInfraSource } from './lib/logs/init_infra_source';
|
|
|
|
import { instantiateClient } from './es_client/instantiate_client';
|
|
|
|
import { registerCollectors } from './kibana_monitoring/collectors';
|
|
|
|
import { registerMonitoringCollection } from './telemetry_collection';
|
|
|
|
import { LicenseService } from './license_service';
|
2020-07-14 23:50:22 +02:00
|
|
|
import { AlertsFactory } from './alerts';
|
2020-03-20 19:02:15 +01:00
|
|
|
import {
|
2020-07-14 23:50:22 +02:00
|
|
|
MonitoringCore,
|
|
|
|
MonitoringLicenseService,
|
|
|
|
LegacyShimDependencies,
|
|
|
|
IBulkUploader,
|
|
|
|
PluginsSetup,
|
|
|
|
PluginsStart,
|
|
|
|
LegacyAPI,
|
|
|
|
LegacyRequest,
|
|
|
|
} from './types';
|
2020-03-20 19:02:15 +01:00
|
|
|
|
|
|
|
// This is used to test the version of kibana
|
|
|
|
const snapshotRegex = /-snapshot/i;
|
|
|
|
|
2020-04-17 02:07:24 +02:00
|
|
|
const wrapError = (error: any): CustomHttpResponseOptions<ResponseError> => {
|
|
|
|
const options = { statusCode: error.statusCode ?? 500 };
|
|
|
|
const boom = Boom.isBoom(error) ? error : Boom.boomify(error, options);
|
|
|
|
return {
|
|
|
|
body: boom,
|
|
|
|
headers: boom.output.headers,
|
|
|
|
statusCode: boom.output.statusCode,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-03-20 19:02:15 +01:00
|
|
|
export class Plugin {
|
|
|
|
private readonly initializerContext: PluginInitializerContext;
|
|
|
|
private readonly log: Logger;
|
|
|
|
private readonly getLogger: (...scopes: string[]) => Logger;
|
2020-06-29 17:43:31 +02:00
|
|
|
private cluster = {} as ILegacyCustomClusterClient;
|
2020-03-20 19:02:15 +01:00
|
|
|
private licenseService = {} as MonitoringLicenseService;
|
|
|
|
private monitoringCore = {} as MonitoringCore;
|
|
|
|
private legacyShimDependencies = {} as LegacyShimDependencies;
|
|
|
|
private bulkUploader = {} as IBulkUploader;
|
|
|
|
|
|
|
|
constructor(initializerContext: PluginInitializerContext) {
|
|
|
|
this.initializerContext = initializerContext;
|
|
|
|
this.log = initializerContext.logger.get(LOGGING_TAG);
|
|
|
|
this.getLogger = (...scopes: string[]) => initializerContext.logger.get(LOGGING_TAG, ...scopes);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setup(core: CoreSetup, plugins: PluginsSetup) {
|
|
|
|
const [config, legacyConfig] = await combineLatest([
|
2020-06-15 18:43:37 +02:00
|
|
|
this.initializerContext.config
|
|
|
|
.create<TypeOf<typeof configSchema>>()
|
|
|
|
.pipe(map((rawConfig) => createConfig(rawConfig))),
|
2020-03-20 19:02:15 +01:00
|
|
|
this.initializerContext.config.legacy.globalConfig$,
|
|
|
|
])
|
|
|
|
.pipe(first())
|
|
|
|
.toPromise();
|
|
|
|
|
2020-07-14 23:50:22 +02:00
|
|
|
const router = core.http.createRouter();
|
2020-03-20 19:02:15 +01:00
|
|
|
this.legacyShimDependencies = {
|
2020-07-14 23:50:22 +02:00
|
|
|
router,
|
2020-03-20 19:02:15 +01:00
|
|
|
instanceUuid: core.uuid.getInstanceUuid(),
|
2020-06-01 16:16:44 +02:00
|
|
|
esDataClient: core.elasticsearch.legacy.client,
|
2020-03-23 19:49:38 +01:00
|
|
|
kibanaStatsCollector: plugins.usageCollection?.getCollectorByType(
|
2020-03-20 19:02:15 +01:00
|
|
|
KIBANA_STATS_TYPE_MONITORING
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Monitoring creates and maintains a connection to a potentially
|
|
|
|
// separate ES cluster - create this first
|
|
|
|
const cluster = (this.cluster = instantiateClient(
|
|
|
|
config.ui.elasticsearch,
|
|
|
|
this.log,
|
2020-06-01 16:16:44 +02:00
|
|
|
core.elasticsearch.legacy.createClient
|
2020-03-20 19:02:15 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
await this.licenseService.refresh();
|
|
|
|
|
2020-07-14 23:50:22 +02:00
|
|
|
const serverInfo = core.http.getServerInfo();
|
|
|
|
let kibanaUrl = `${serverInfo.protocol}://${serverInfo.hostname}:${serverInfo.port}`;
|
|
|
|
if (core.http.basePath.serverBasePath) {
|
|
|
|
kibanaUrl += `/${core.http.basePath.serverBasePath}`;
|
|
|
|
}
|
|
|
|
const getUiSettingsService = async () => {
|
|
|
|
const coreStart = (await core.getStartServices())[0];
|
|
|
|
return coreStart.uiSettings;
|
|
|
|
};
|
2020-08-03 21:05:12 +02:00
|
|
|
const isCloud = Boolean(plugins.cloud?.isCloudEnabled);
|
2020-07-14 23:50:22 +02:00
|
|
|
const alerts = AlertsFactory.getAll();
|
|
|
|
for (const alert of alerts) {
|
2020-08-03 21:05:12 +02:00
|
|
|
alert.initializeAlertType(
|
|
|
|
getUiSettingsService,
|
|
|
|
cluster,
|
|
|
|
this.getLogger,
|
|
|
|
config,
|
|
|
|
kibanaUrl,
|
|
|
|
isCloud
|
|
|
|
);
|
2020-07-14 23:50:22 +02:00
|
|
|
plugins.alerts.registerType(alert.getAlertType());
|
2020-03-20 19:02:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 19:49:38 +01:00
|
|
|
// Initialize telemetry
|
|
|
|
if (plugins.telemetryCollectionManager) {
|
|
|
|
registerMonitoringCollection(plugins.telemetryCollectionManager, this.cluster, {
|
|
|
|
maxBucketSize: config.ui.max_bucket_size,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:02:15 +01:00
|
|
|
// Register collector objects for stats to show up in the APIs
|
2020-03-23 19:49:38 +01:00
|
|
|
if (plugins.usageCollection) {
|
2020-04-08 10:08:59 +02:00
|
|
|
registerCollectors(plugins.usageCollection, config);
|
2020-03-23 19:49:38 +01:00
|
|
|
}
|
2020-03-20 19:02:15 +01:00
|
|
|
|
|
|
|
// If collection is enabled, create the bulk uploader
|
|
|
|
const kibanaMonitoringLog = this.getLogger(KIBANA_MONITORING_LOGGING_TAG);
|
|
|
|
const kibanaCollectionEnabled = config.kibana.collection.enabled;
|
|
|
|
if (kibanaCollectionEnabled) {
|
|
|
|
// Start kibana internal collection
|
|
|
|
const bulkUploader = (this.bulkUploader = initBulkUploader({
|
|
|
|
elasticsearch: core.elasticsearch,
|
|
|
|
config,
|
|
|
|
log: kibanaMonitoringLog,
|
|
|
|
kibanaStats: {
|
|
|
|
uuid: core.uuid.getInstanceUuid(),
|
|
|
|
name: serverInfo.name,
|
|
|
|
index: get(legacyConfig, 'kibana.index'),
|
2020-07-07 16:54:39 +02:00
|
|
|
host: serverInfo.hostname,
|
2020-03-24 20:53:43 +01:00
|
|
|
locale: i18n.getLocale(),
|
2020-03-20 19:02:15 +01:00
|
|
|
port: serverInfo.port.toString(),
|
2020-07-07 16:54:39 +02:00
|
|
|
transport_address: `${serverInfo.hostname}:${serverInfo.port}`,
|
2020-03-20 19:02:15 +01:00
|
|
|
version: this.initializerContext.env.packageInfo.version,
|
|
|
|
snapshot: snapshotRegex.test(this.initializerContext.env.packageInfo.version),
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
// 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(plugins.usageCollection);
|
|
|
|
} else {
|
|
|
|
bulkUploader.handleNotEnabled();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} 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) {
|
|
|
|
// Create our shim which is currently used to power our routing
|
|
|
|
this.monitoringCore = this.getLegacyShim(
|
|
|
|
config,
|
|
|
|
legacyConfig,
|
2020-03-26 10:49:05 +01:00
|
|
|
core.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>,
|
2020-03-20 19:02:15 +01:00
|
|
|
this.licenseService,
|
|
|
|
this.cluster
|
|
|
|
);
|
|
|
|
|
|
|
|
this.registerPluginInUI(plugins);
|
2020-07-14 23:50:22 +02:00
|
|
|
requireUIRoutes(this.monitoringCore, {
|
|
|
|
router,
|
|
|
|
licenseService: this.licenseService,
|
2020-07-17 17:13:40 +02:00
|
|
|
encryptedSavedObjects: plugins.encryptedSavedObjects,
|
2020-07-14 23:50:22 +02:00
|
|
|
});
|
2020-03-20 19:02:15 +01:00
|
|
|
initInfraSource(config, plugins.infra);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
// The legacy plugin calls this to register certain legacy dependencies
|
|
|
|
// that are necessary for the plugin to properly run
|
|
|
|
registerLegacyAPI: (legacyAPI: LegacyAPI) => {
|
|
|
|
this.setupLegacy(legacyAPI);
|
|
|
|
},
|
|
|
|
// OSS stats api needs to call this in order to centralize how
|
|
|
|
// we fetch kibana specific stats
|
|
|
|
getKibanaStats: () => this.bulkUploader.getKibanaStats(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if (this.cluster) {
|
|
|
|
this.cluster.close();
|
|
|
|
}
|
|
|
|
if (this.licenseService) {
|
|
|
|
this.licenseService.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPluginInUI(plugins: PluginsSetup) {
|
|
|
|
plugins.features.registerFeature({
|
|
|
|
id: 'monitoring',
|
|
|
|
name: i18n.translate('xpack.monitoring.featureRegistry.monitoringFeatureName', {
|
|
|
|
defaultMessage: 'Stack Monitoring',
|
|
|
|
}),
|
|
|
|
icon: 'monitoringApp',
|
|
|
|
navLinkId: 'monitoring',
|
|
|
|
app: ['monitoring', 'kibana'],
|
|
|
|
catalogue: ['monitoring'],
|
2020-03-24 16:12:49 +01:00
|
|
|
privileges: null,
|
2020-07-22 15:45:57 +02:00
|
|
|
alerting: ALERTS,
|
2020-03-20 19:02:15 +01:00
|
|
|
reserved: {
|
|
|
|
description: i18n.translate('xpack.monitoring.feature.reserved.description', {
|
|
|
|
defaultMessage: 'To grant users access, you should also assign the monitoring_user role.',
|
|
|
|
}),
|
2020-04-08 19:03:15 +02:00
|
|
|
privileges: [
|
|
|
|
{
|
|
|
|
id: 'monitoring',
|
|
|
|
privilege: {
|
|
|
|
app: ['monitoring', 'kibana'],
|
|
|
|
catalogue: ['monitoring'],
|
|
|
|
savedObject: {
|
|
|
|
all: [],
|
|
|
|
read: [],
|
|
|
|
},
|
2020-07-22 15:45:57 +02:00
|
|
|
alerting: {
|
|
|
|
all: ALERTS,
|
|
|
|
},
|
2020-04-08 19:03:15 +02:00
|
|
|
ui: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-03-20 19:02:15 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async setupLegacy(legacyAPI: LegacyAPI) {
|
|
|
|
// Set the stats getter
|
|
|
|
this.bulkUploader.setKibanaStatusGetter(() => legacyAPI.getServerStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
getLegacyShim(
|
|
|
|
config: MonitoringConfig,
|
|
|
|
legacyConfig: any,
|
2020-03-26 10:49:05 +01:00
|
|
|
getCoreServices: () => Promise<[CoreStart, PluginsStart, {}]>,
|
2020-03-20 19:02:15 +01:00
|
|
|
licenseService: MonitoringLicenseService,
|
2020-06-29 17:43:31 +02:00
|
|
|
cluster: ILegacyCustomClusterClient
|
2020-03-20 19:02:15 +01:00
|
|
|
): MonitoringCore {
|
|
|
|
const router = this.legacyShimDependencies.router;
|
|
|
|
const legacyConfigWrapper = () => ({
|
|
|
|
get: (_key: string): string | undefined => {
|
|
|
|
const key = _key.includes('monitoring.') ? _key.split('monitoring.')[1] : _key;
|
|
|
|
if (has(config, key)) {
|
|
|
|
return get(config, key);
|
|
|
|
}
|
|
|
|
if (has(legacyConfig, key)) {
|
|
|
|
return get(legacyConfig, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key === 'server.uuid') {
|
|
|
|
return this.legacyShimDependencies.instanceUuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Unknown key '${_key}'`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
config: legacyConfigWrapper,
|
|
|
|
log: this.log,
|
|
|
|
route: (options: any) => {
|
|
|
|
const method = options.method;
|
|
|
|
const handler = async (
|
|
|
|
context: RequestHandlerContext,
|
|
|
|
req: KibanaRequest<any, any, any, any>,
|
|
|
|
res: KibanaResponseFactory
|
|
|
|
) => {
|
|
|
|
const plugins = (await getCoreServices())[1];
|
2020-07-14 23:50:22 +02:00
|
|
|
const legacyRequest: LegacyRequest = {
|
2020-03-20 19:02:15 +01:00
|
|
|
...req,
|
|
|
|
logger: this.log,
|
|
|
|
getLogger: this.getLogger,
|
|
|
|
payload: req.body,
|
|
|
|
getKibanaStatsCollector: () => this.legacyShimDependencies.kibanaStatsCollector,
|
|
|
|
getUiSettingsService: () => context.core.uiSettings.client,
|
2020-07-14 23:50:22 +02:00
|
|
|
getActionTypeRegistry: () => context.actions?.listTypes(),
|
2020-08-06 21:33:24 +02:00
|
|
|
getAlertsClient: () => {
|
|
|
|
try {
|
|
|
|
return plugins.alerts.getAlertsClientWithRequest(req);
|
|
|
|
} catch (err) {
|
|
|
|
// If security is disabled, this call will throw an error unless a certain config is set for dist builds
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getActionsClient: () => {
|
|
|
|
try {
|
|
|
|
return plugins.actions.getActionsClientWithRequest(req);
|
|
|
|
} catch (err) {
|
|
|
|
// If security is disabled, this call will throw an error unless a certain config is set for dist builds
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2020-03-20 19:02:15 +01:00
|
|
|
server: {
|
|
|
|
config: legacyConfigWrapper,
|
|
|
|
newPlatform: {
|
|
|
|
setup: {
|
|
|
|
plugins,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
monitoring: {
|
|
|
|
info: licenseService,
|
|
|
|
},
|
|
|
|
elasticsearch: {
|
|
|
|
getCluster: (name: string) => ({
|
|
|
|
callWithRequest: async (_req: any, endpoint: string, params: any) => {
|
|
|
|
const client =
|
|
|
|
name === 'monitoring' ? cluster : this.legacyShimDependencies.esDataClient;
|
|
|
|
return client.asScoped(req).callAsCurrentUser(endpoint, params);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-04-17 02:07:24 +02:00
|
|
|
try {
|
|
|
|
const result = await options.handler(legacyRequest);
|
|
|
|
return res.ok({ body: result });
|
|
|
|
} catch (err) {
|
2020-07-14 23:50:22 +02:00
|
|
|
const statusCode: number =
|
|
|
|
err.output?.statusCode || err.statusCode || err.status || 500;
|
2020-04-17 02:07:24 +02:00
|
|
|
if (Boom.isBoom(err) || statusCode !== 500) {
|
|
|
|
return res.customError({ statusCode, body: err });
|
|
|
|
}
|
|
|
|
return res.internalError(wrapError(err));
|
2020-03-20 19:02:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const validate: any = get(options, 'config.validate', false);
|
|
|
|
if (validate && validate.payload) {
|
|
|
|
validate.body = validate.payload;
|
|
|
|
}
|
|
|
|
options.validate = validate;
|
|
|
|
|
|
|
|
if (method === 'POST') {
|
|
|
|
router.post(options, handler);
|
|
|
|
} else if (method === 'GET') {
|
|
|
|
router.get(options, handler);
|
|
|
|
} else if (method === 'PUT') {
|
|
|
|
router.put(options, handler);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupport API method: ' + method);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|