Skip to content

Commit

Permalink
[ML] Disabling ML if license feature is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Jul 24, 2020
1 parent 2178a14 commit b343af3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ml/common/license/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export {
MINIMUM_LICENSE,
isFullLicense,
isMinimumLicense,
isMlEnabled,
} from './ml_license';
4 changes: 4 additions & 0 deletions x-pack/plugins/ml/common/license/ml_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 12 additions & 1 deletion x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -63,7 +65,15 @@ export type MlCoreSetup = CoreSetup<MlStartDependencies, MlPluginStart>;
export class MlPlugin implements Plugin<MlPluginSetup, MlPluginStart> {
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(
Expand Down Expand Up @@ -117,6 +127,7 @@ export class MlPlugin implements Plugin<MlPluginSetup, MlPluginStart> {
initManagementSection(pluginsSetup, core);
registerEmbeddables(pluginsSetup.embeddable, core);
registerMlUiActions(pluginsSetup.uiActions, core);
// });

return {};
}
Expand Down
20 changes: 15 additions & 5 deletions x-pack/plugins/ml/server/lib/capabilities/capabilities_switcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -30,19 +30,23 @@ function getSwitcher(license$: Observable<ILicense>, 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)) {
Expand All @@ -56,3 +60,9 @@ function getSwitcher(license$: Observable<ILicense>, logger: Logger): Capabiliti
}
};
}

function disableAllCapabilities(mlCaps: MlCapabilities) {
Object.keys(mlCaps).forEach((k) => {
mlCaps[k as keyof MlCapabilities] = false;
});
}

0 comments on commit b343af3

Please sign in to comment.