diff --git a/x-pack/plugins/ml/common/license/index.ts b/x-pack/plugins/ml/common/license/index.ts index e87986a26a3bd..e09040a9a26f4 100644 --- a/x-pack/plugins/ml/common/license/index.ts +++ b/x-pack/plugins/ml/common/license/index.ts @@ -11,4 +11,5 @@ export { MINIMUM_LICENSE, isFullLicense, isMinimumLicense, + isMlEnabled, } from './ml_license'; diff --git a/x-pack/plugins/ml/common/license/ml_license.ts b/x-pack/plugins/ml/common/license/ml_license.ts index e4367c9b921f4..44ed892ff0d0a 100644 --- a/x-pack/plugins/ml/common/license/ml_license.ts +++ b/x-pack/plugins/ml/common/license/ml_license.ts @@ -82,3 +82,7 @@ export function isFullLicense(license: ILicense) { export function isMinimumLicense(license: ILicense) { return license.check(PLUGIN_ID, MINIMUM_LICENSE).state === 'valid'; } + +export function isMlEnabled(license: ILicense) { + return license.getFeature(PLUGIN_ID).isEnabled; +} diff --git a/x-pack/plugins/ml/public/plugin.ts b/x-pack/plugins/ml/public/plugin.ts index 449d8baa2a184..931af6a7eb6d2 100644 --- a/x-pack/plugins/ml/public/plugin.ts +++ b/x-pack/plugins/ml/public/plugin.ts @@ -12,6 +12,7 @@ import { AppMountParameters, PluginInitializerContext, } from 'kibana/public'; +import { take } from 'rxjs/operators'; import { ManagementSetup } from 'src/plugins/management/public'; import { SharePluginSetup, SharePluginStart, UrlGeneratorState } from 'src/plugins/share/public'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; @@ -32,6 +33,7 @@ import { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actio import { registerMlUiActions } from './ui_actions'; import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; import { MlUrlGenerator, MlUrlGeneratorState, ML_APP_URL_GENERATOR } from './url_generator'; +import { isMlEnabled } from '../common/license'; export interface MlStartDependencies { data: DataPublicPluginStart; @@ -63,7 +65,15 @@ export type MlCoreSetup = CoreSetup; export class MlPlugin implements Plugin { constructor(private initializerContext: PluginInitializerContext) {} - setup(core: MlCoreSetup, pluginsSetup: MlSetupDependencies) { + async setup(core: MlCoreSetup, pluginsSetup: MlSetupDependencies) { + // const license = pluginsSetup.licensing.license$.pipe(take(1)); + const license = await pluginsSetup.licensing.license$.pipe(take(1)).toPromise(); + // licensing.subscribe((license) => { + // console.dir(license.getFeature('ml')); + // console.dir(license.getFeature('graph')); + if (isMlEnabled(license) === false) { + return {}; + } const baseUrl = core.http.basePath.prepend('/app/ml'); pluginsSetup.share.urlGenerators.registerUrlGenerator( @@ -117,6 +127,7 @@ export class MlPlugin implements Plugin { initManagementSection(pluginsSetup, core); registerEmbeddables(pluginsSetup.embeddable, core); registerMlUiActions(pluginsSetup.uiActions, core); + // }); return {}; } diff --git a/x-pack/plugins/ml/server/lib/capabilities/capabilities_switcher.ts b/x-pack/plugins/ml/server/lib/capabilities/capabilities_switcher.ts index 5b8cbc4bdbbe8..cf1c5b110f398 100644 --- a/x-pack/plugins/ml/server/lib/capabilities/capabilities_switcher.ts +++ b/x-pack/plugins/ml/server/lib/capabilities/capabilities_switcher.ts @@ -9,7 +9,7 @@ import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; import { CapabilitiesSwitcher, CoreSetup, Logger } from 'src/core/server'; import { ILicense } from '../../../../licensing/common/types'; -import { isFullLicense, isMinimumLicense } from '../../../common/license'; +import { isFullLicense, isMinimumLicense, isMlEnabled } from '../../../common/license'; import { MlCapabilities, basicLicenseMlCapabilities } from '../../../common/types/capabilities'; export const setupCapabilitiesSwitcher = ( @@ -30,19 +30,23 @@ function getSwitcher(license$: Observable, logger: Logger): Capabiliti try { const license = await license$.pipe(take(1)).toPromise(); + const mlCaps = capabilities.ml as MlCapabilities; + // console.dir(license.getFeature('ml')); + // console.dir(license.getFeature('graph')); + if (isMlEnabled(license) === false) { + disableAllCapabilities(mlCaps); + return capabilities; + } // full license, leave capabilities as they were if (isFullLicense(license)) { return capabilities; } - const mlCaps = capabilities.ml as MlCapabilities; const originalCapabilities = cloneDeep(mlCaps); // not full licence, switch off all capabilities - Object.keys(mlCaps).forEach((k) => { - mlCaps[k as keyof MlCapabilities] = false; - }); + disableAllCapabilities(mlCaps); // for a basic license, reapply the original capabilities for the basic license features if (isMinimumLicense(license)) { @@ -56,3 +60,9 @@ function getSwitcher(license$: Observable, logger: Logger): Capabiliti } }; } + +function disableAllCapabilities(mlCaps: MlCapabilities) { + Object.keys(mlCaps).forEach((k) => { + mlCaps[k as keyof MlCapabilities] = false; + }); +}