Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fixing licensing after server NP cutover #59275

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions x-pack/legacy/plugins/ml/common/license/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { MlLicense, LicenseStatus, MINIMUM_FULL_LICENSE, MINIMUM_LICENSE } from './ml_license';
78 changes: 78 additions & 0 deletions x-pack/legacy/plugins/ml/common/license/ml_license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 { Observable, Subscription } from 'rxjs';
import { ILicense, LICENSE_CHECK_STATE } from '../../../../../plugins/licensing/common/types';
import { PLUGIN_ID } from '../constants/app';

export const MINIMUM_LICENSE = 'basic';
export const MINIMUM_FULL_LICENSE = 'platinum';

export interface LicenseStatus {
isValid: boolean;
isSecurityEnabled: boolean;
message?: string;
}

export class MlLicense {
private _licenseSubscription: Subscription | null = null;
private _license: ILicense | null = null;
private _isSecurityEnabled: boolean = false;
private _hasLicenseExpired: boolean = false;
private _isMlEnabled: boolean = false;
private _isMinimumLicense: boolean = false;
private _isFullLicense: boolean = false;
private _initialized: boolean = false;

public setup(
license$: Observable<ILicense>,
postInitFunctions?: Array<(lic: MlLicense) => void>
) {
this._licenseSubscription = license$.subscribe(async license => {
const { isEnabled: securityIsEnabled } = license.getFeature('security');

this._license = license;
this._isSecurityEnabled = securityIsEnabled;
this._hasLicenseExpired = this._license.status === 'expired';
this._isMlEnabled = this._license.getFeature(PLUGIN_ID).isEnabled;
this._isMinimumLicense =
this._license.check(PLUGIN_ID, MINIMUM_LICENSE).state === LICENSE_CHECK_STATE.Valid;
this._isFullLicense =
this._license.check(PLUGIN_ID, MINIMUM_FULL_LICENSE).state === LICENSE_CHECK_STATE.Valid;

if (this._initialized === false && postInitFunctions !== undefined) {
postInitFunctions.forEach(f => f(this));
}
this._initialized = true;
});
}

public unsubscribe() {
if (this._licenseSubscription !== null) {
this._licenseSubscription.unsubscribe();
}
}

public isSecurityEnabled() {
return this._isSecurityEnabled;
}

public hasLicenseExpired() {
return this._hasLicenseExpired;
}

public isMlEnabled() {
return this._isMlEnabled;
}

public isMinimumLicense() {
return this._isMinimumLicense;
}

public isFullLicense() {
return this._isFullLicense;
}
}
11 changes: 9 additions & 2 deletions x-pack/legacy/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import { AppMountParameters, CoreStart } from 'kibana/public';

import { DataPublicPluginStart } from 'src/plugins/data/public';
import { SecurityPluginSetup } from '../../../../../plugins/security/public';
import { LicensingPluginSetup } from '../../../../../plugins/licensing/public';

import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
import { setDependencyCache, clearCache } from './util/dependency_cache';
import { setLicenseCache } from './license';

import { MlRouter } from './routing';

export interface MlDependencies extends AppMountParameters {
data: DataPublicPluginStart;
security: SecurityPluginSetup;
licensing: LicensingPluginSetup;
__LEGACY: {
XSRF: string;
};
Expand All @@ -36,22 +39,26 @@ const App: FC<AppProps> = ({ coreStart, deps }) => {
setDependencyCache({
indexPatterns: deps.data.indexPatterns,
timefilter: deps.data.query.timefilter,
fieldFormats: deps.data.fieldFormats,
autocomplete: deps.data.autocomplete,
config: coreStart.uiSettings!,
chrome: coreStart.chrome!,
docLinks: coreStart.docLinks!,
toastNotifications: coreStart.notifications.toasts,
overlays: coreStart.overlays,
recentlyAccessed: coreStart.chrome!.recentlyAccessed,
fieldFormats: deps.data.fieldFormats,
autocomplete: deps.data.autocomplete,
basePath: coreStart.http.basePath,
savedObjectsClient: coreStart.savedObjects.client,
XSRF: deps.__LEGACY.XSRF,
application: coreStart.application,
http: coreStart.http,
security: deps.security,
});

const mlLicense = setLicenseCache(deps.licensing);

deps.onAppLeave(actions => {
mlLicense.unsubscribe();
clearCache();
return actions.default();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getColumns } from './anomalies_table_columns';
jest.mock('../../privilege/check_privilege', () => ({
checkPermission: () => false,
}));
jest.mock('../../license/check_license', () => ({
jest.mock('../../license', () => ({
hasLicenseExpired: () => false,
}));
jest.mock('../../privilege/get_privileges', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ export const MainTabs: FC<Props> = ({ tabId, disableLinks }) => {
return (
<EuiTabs display="condensed">
{tabs.map((tab: Tab) => {
const id = tab.id;
const { id, disabled } = tab;
const testSubject = TAB_DATA[id].testSubject;
const defaultPathId = TAB_DATA[id].pathId || id;
// globalState (e.g. selected jobs and time range) should be retained when changing pages.
// appState will not be considered.
const fullGlobalStateString = globalState !== undefined ? `?_g=${encode(globalState)}` : '';
return (

return disabled ? (
<EuiTab key={`${id}-key`} className={'mlNavigationMenu__mainTab'} disabled={true}>
{tab.name}
</EuiTab>
) : (
<EuiLink
data-test-subj={testSubject + (id === selectedTabId ? ' selected' : '')}
href={`#/${defaultPathId}${fullGlobalStateString}`}
Expand All @@ -98,7 +103,6 @@ export const MainTabs: FC<Props> = ({ tabId, disableLinks }) => {
className={'mlNavigationMenu__mainTab'}
onClick={() => onSelectedTabChanged(id)}
isSelected={id === selectedTabId}
disabled={tab.disabled}
>
{tab.name}
</EuiTab>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { Fragment, FC } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiHorizontalRule } from '@elastic/eui';

import { isFullLicense } from '../../license/check_license';
import { isFullLicense } from '../../license';

import { TopNav } from './top_nav';
import { MainTabs } from './main_tabs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { i18n } from '@kbn/i18n';

import { FormattedMessage } from '@kbn/i18n/react';
import { isFullLicense } from '../license/check_license';
import { isFullLicense } from '../license';
import { useTimefilter } from '../contexts/kibana';

import { NavigationMenu } from '../components/navigation_menu';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import moment from 'moment';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFlexGroup, EuiFlexItem, EuiCard, EuiIcon } from '@elastic/eui';
import { ml } from '../../../../services/ml_api_service';
import { isFullLicense } from '../../../../license/check_license';
import { isFullLicense } from '../../../../license';
import { checkPermission } from '../../../../privilege/check_privilege';
import { mlNodesAvailable } from '../../../../ml_nodes_check/check_ml_nodes';
import { useMlKibana } from '../../../../contexts/kibana';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { SavedSearchSavedObject } from '../../../../common/types/kibana';
import { NavigationMenu } from '../../components/navigation_menu';
import { ML_JOB_FIELD_TYPES } from '../../../../common/constants/field_types';
import { SEARCH_QUERY_LANGUAGE } from '../../../../common/constants/search';
import { isFullLicense } from '../../license/check_license';
import { isFullLicense } from '../../license';
import { checkPermission } from '../../privilege/check_privilege';
import { mlNodesAvailable } from '../../ml_nodes_check/check_ml_nodes';
import { FullTimeRangeSelector } from '../../components/full_time_range_selector';
Expand Down

This file was deleted.

Loading