Skip to content

Commit

Permalink
[Monitoring] Change license service to use recommended API (#93620)
Browse files Browse the repository at this point in the history
* Change license service to use recommended API

* Remove unused type

* Update this code too

* Generate docs

* New docs
  • Loading branch information
chrisronline authored Mar 15, 2021
1 parent 644e08b commit 46253d6
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 76 deletions.
16 changes: 4 additions & 12 deletions api_docs/ml.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -5518,9 +5512,7 @@
}
],
"signature": [
"(error: ",
"ErrorType",
") => string"
"(error: any) => string"
],
"description": [],
"label": "extractErrorMessage",
Expand Down
32 changes: 30 additions & 2 deletions api_docs/monitoring.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -106,7 +134,7 @@
"description": [],
"source": {
"path": "x-pack/plugins/monitoring/server/types.ts",
"lineNumber": 98
"lineNumber": 100
},
"signature": [
"() => any"
Expand All @@ -115,7 +143,7 @@
],
"source": {
"path": "x-pack/plugins/monitoring/server/types.ts",
"lineNumber": 97
"lineNumber": 99
},
"lifecycle": "setup",
"initialIsOpen": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ const mockReq = (
plugins: {
monitoring: {
info: {
getSecurityFeature: () => {
return {
isAvailable: securityEnabled,
isEnabled: securityEnabled,
};
},
getLicenseService: () => ({
getSecurityFeature: () => {
return {
isAvailable: securityEnabled,
isEnabled: securityEnabled,
};
},
}),
},
},
elasticsearch: {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/server/license_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
84 changes: 43 additions & 41 deletions x-pack/plugins/monitoring/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -201,7 +164,6 @@ export class MonitoringPlugin
config,
legacyConfig,
core.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>,
this.licenseService,
this.cluster,
plugins
);
Expand All @@ -224,7 +186,46 @@ export class MonitoringPlugin
};
}

start() {}
async start(core: CoreStart, { licensing }: PluginsStart) {
const config = createConfig(this.initializerContext.config.get<TypeOf<typeof configSchema>>());
// 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) {
Expand Down Expand Up @@ -276,7 +277,6 @@ export class MonitoringPlugin
config: MonitoringConfig,
legacyConfig: any,
getCoreServices: () => Promise<[CoreStart, PluginsStart, {}]>,
licenseService: MonitoringLicenseService,
cluster: ILegacyCustomClusterClient,
setupPlugins: PluginsSetup
): MonitoringCore {
Expand Down Expand Up @@ -343,7 +343,9 @@ export class MonitoringPlugin
},
plugins: {
monitoring: {
info: licenseService,
info: {
getLicenseService: () => this.licenseService,
},
},
elasticsearch: {
getCluster: (name: string) => ({
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/monitoring/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -47,7 +47,6 @@ export interface MonitoringElasticsearchConfig {
export interface PluginsSetup {
encryptedSavedObjects?: EncryptedSavedObjectsPluginSetup;
usageCollection?: UsageCollectionSetup;
licensing: LicensingPluginSetup;
features: FeaturesPluginSetupContract;
alerting?: AlertingPluginSetupContract;
infra: InfraPluginSetup;
Expand All @@ -62,6 +61,7 @@ export interface RequestHandlerContextMonitoringPlugin extends RequestHandlerCon
export interface PluginsStart {
alerting: AlertingPluginStartContract;
actions: ActionsPluginsStartContact;
licensing: LicensingPluginStart;
}

export interface MonitoringCoreConfig {
Expand Down Expand Up @@ -92,6 +92,8 @@ export interface LegacyShimDependencies {
export interface IBulkUploader {
getKibanaStats: () => any;
stop: () => void;
start: () => void;
handleNotEnabled: () => void;
}

export interface MonitoringPluginSetup {
Expand Down Expand Up @@ -127,7 +129,9 @@ export interface LegacyServer {
};
plugins: {
monitoring: {
info: MonitoringLicenseService;
info: {
getLicenseService: () => MonitoringLicenseService;
};
};
elasticsearch: {
getCluster: (
Expand Down

0 comments on commit 46253d6

Please sign in to comment.