diff --git a/x-pack/legacy/plugins/reporting/server/legacy.ts b/x-pack/legacy/plugins/reporting/server/legacy.ts index 87ffe75e454b1..37272325b97d0 100644 --- a/x-pack/legacy/plugins/reporting/server/legacy.ts +++ b/x-pack/legacy/plugins/reporting/server/legacy.ts @@ -8,6 +8,7 @@ import { Legacy } from 'kibana'; import { take } from 'rxjs/operators'; import { PluginInitializerContext } from 'src/core/server'; import { ReportingPluginSpecOptions } from '../'; +import { LicensingPluginSetup } from '../../../../plugins/licensing/server'; import { PluginsSetup } from '../../../../plugins/reporting/server'; import { SecurityPluginSetup } from '../../../../plugins/security/server'; import { buildConfig } from './config'; @@ -43,6 +44,7 @@ export const legacyInit = async ( ); await pluginInstance.setup(coreSetup, { elasticsearch: coreSetup.elasticsearch, + licensing: server.newPlatform.setup.plugins.licensing as LicensingPluginSetup, security: server.newPlatform.setup.plugins.security as SecurityPluginSetup, usageCollection: server.newPlatform.setup.plugins.usageCollection, __LEGACY, diff --git a/x-pack/legacy/plugins/reporting/server/types.ts b/x-pack/legacy/plugins/reporting/server/types.ts index 1417f1d7b50cb..bfab568fe9fb3 100644 --- a/x-pack/legacy/plugins/reporting/server/types.ts +++ b/x-pack/legacy/plugins/reporting/server/types.ts @@ -10,6 +10,7 @@ import * as Rx from 'rxjs'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { DataPluginStart } from 'src/plugins/data/server/plugin'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { LicensingPluginSetup } from '../../../../plugins/licensing/server'; import { ReportingPluginSpecOptions } from '../'; import { CancellationToken } from '../../../../plugins/reporting/common'; import { JobStatus } from '../../../../plugins/reporting/common/types'; @@ -160,6 +161,7 @@ export type ScreenshotsObservableFn = ({ export interface ReportingSetupDeps { elasticsearch: ElasticsearchServiceSetup; + licensing: LicensingPluginSetup; security: SecurityPluginSetup; usageCollection?: UsageCollectionSetup; __LEGACY: LegacySetup; diff --git a/x-pack/legacy/plugins/reporting/server/usage/get_export_type_handler.ts b/x-pack/legacy/plugins/reporting/server/usage/get_export_type_handler.ts index 7874f67ef4c0c..020e94b2d54b6 100644 --- a/x-pack/legacy/plugins/reporting/server/usage/get_export_type_handler.ts +++ b/x-pack/legacy/plugins/reporting/server/usage/get_export_type_handler.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main'; import { ExportTypesRegistry } from '../lib/export_types_registry'; +import { FeaturesAvailability } from './'; /* * Gets a handle to the Reporting export types registry and returns a few @@ -21,10 +21,10 @@ export function getExportTypesHandler(exportTypesRegistry: ExportTypesRegistry) * @param {Object} xpackInfo: xpack_main plugin info object * @return {Object} availability of each export type */ - getAvailability(xpackInfo: XPackMainPlugin['info']) { + getAvailability(featuresAvailability: FeaturesAvailability): { [exportType: string]: boolean } { const exportTypesAvailability: { [exportType: string]: boolean } = {}; - const xpackInfoAvailable = xpackInfo && xpackInfo.isAvailable(); - const licenseType: string | undefined = xpackInfo.license.getType(); + const xpackInfoAvailable = featuresAvailability && featuresAvailability.isAvailable(); + const licenseType = featuresAvailability.license.getType(); if (!licenseType) { throw new Error('No license type returned from XPackMainPlugin#info!'); } diff --git a/x-pack/legacy/plugins/reporting/server/usage/get_reporting_usage.ts b/x-pack/legacy/plugins/reporting/server/usage/get_reporting_usage.ts index 6771d61bf263d..2a6d08c0740dd 100644 --- a/x-pack/legacy/plugins/reporting/server/usage/get_reporting_usage.ts +++ b/x-pack/legacy/plugins/reporting/server/usage/get_reporting_usage.ts @@ -7,8 +7,10 @@ import { get } from 'lodash'; import { CallCluster } from 'src/legacy/core_plugins/elasticsearch'; import { ReportingConfig } from '../'; -import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main'; import { ExportTypesRegistry } from '../lib/export_types_registry'; +import { GetLicense } from './'; +import { decorateRangeStats } from './decorate_range_stats'; +import { getExportTypesHandler } from './get_export_type_handler'; import { AggregationResultBuckets, AppCounts, @@ -21,10 +23,6 @@ import { SearchResponse, StatusByAppBucket, } from './types'; -import { decorateRangeStats } from './decorate_range_stats'; -import { getExportTypesHandler } from './get_export_type_handler'; - -type XPackInfo = XPackMainPlugin['info']; const JOB_TYPES_KEY = 'jobTypes'; const JOB_TYPES_FIELD = 'jobtype'; @@ -123,10 +121,10 @@ async function handleResponse(response: SearchResponse): Promise { const reportingIndex = config.get('index'); const params = { @@ -170,6 +168,7 @@ export async function getReportingUsage( }, }; + const featureAvailability = await getLicense(); return callCluster('search', params) .then((response: SearchResponse) => handleResponse(response)) .then( @@ -180,7 +179,7 @@ export async function getReportingUsage( const exportTypesHandler = getExportTypesHandler(exportTypesRegistry); const availability = exportTypesHandler.getAvailability( - xpackMainInfo + featureAvailability ) as FeatureAvailabilityMap; const { last7Days, ...all } = usage; diff --git a/x-pack/legacy/plugins/reporting/server/usage/index.ts b/x-pack/legacy/plugins/reporting/server/usage/index.ts index 141ecb9c77656..d00a8570a024f 100644 --- a/x-pack/legacy/plugins/reporting/server/usage/index.ts +++ b/x-pack/legacy/plugins/reporting/server/usage/index.ts @@ -4,4 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import { LicenseType } from '../../../../../plugins/licensing/server'; + +export interface FeaturesAvailability { + isAvailable: () => boolean; + license: { + getType: () => LicenseType | undefined; + }; +} +export type GetLicense = () => Promise; export { registerReportingUsageCollector } from './reporting_usage_collector'; diff --git a/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.test.ts b/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.test.ts index 2d0934657b2b1..830c6275cd96a 100644 --- a/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.test.ts +++ b/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.test.ts @@ -4,15 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ +import * as Rx from 'rxjs'; import sinon from 'sinon'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { ReportingConfig } from '../'; import { createMockReportingCore } from '../../test_helpers'; import { getExportTypesRegistry } from '../lib/export_types_registry'; -import { ReportingUsageType, SearchResponse } from './types'; +import { ReportingSetupDeps } from '../types'; +import { FeaturesAvailability } from './'; import { getReportingUsageCollector, registerReportingUsageCollector, } from './reporting_usage_collector'; +import { ReportingUsageType, SearchResponse } from './types'; const exportTypesRegistry = getExportTypesRegistry(); @@ -32,30 +36,22 @@ function getMockUsageCollection() { }; } +const getLicenseMock = (licenseType = 'platinum') => () => { + return Promise.resolve({ + isAvailable: () => true, + license: { getType: () => licenseType }, + } as FeaturesAvailability); +}; + function getPluginsMock( { license, usageCollection = getMockUsageCollection() } = { license: 'platinum' } ) { - const mockXpackMain = { - info: { - isAvailable: sinon.stub().returns(true), - feature: () => ({ - getLicenseCheckResults: sinon.stub(), - }), - license: { - isOneOf: sinon.stub().returns(false), - getType: sinon.stub().returns(license), - }, - toJSON: () => ({ b: 1 }), - }, - }; - return { + return ({ + licensing: { license$: Rx.of(getLicenseMock(license)) }, usageCollection, - __LEGACY: { - plugins: { - xpack_main: mockXpackMain, - }, - }, - } as any; + elasticsearch: {}, + security: {}, + } as unknown) as ReportingSetupDeps & { usageCollection: UsageCollectionSetup }; } const getMockReportingConfig = () => ({ @@ -78,7 +74,7 @@ describe('license checks', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock('basic'), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -108,7 +104,7 @@ describe('license checks', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock('none'), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -138,7 +134,7 @@ describe('license checks', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock('platinum'), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -168,7 +164,7 @@ describe('license checks', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock('basic'), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -194,7 +190,7 @@ describe('data modeling', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock(), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -247,7 +243,7 @@ describe('data modeling', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock(), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -300,7 +296,7 @@ describe('data modeling', () => { const { fetch } = getReportingUsageCollector( mockConfig, plugins.usageCollection, - plugins.__LEGACY.plugins.xpack_main.info, + getLicenseMock(), exportTypesRegistry, function isReady() { return Promise.resolve(true); @@ -461,7 +457,7 @@ describe('Ready for collection observable', () => { const makeCollectorSpy = sinon.spy(); usageCollection.makeUsageCollector = makeCollectorSpy; - const plugins = getPluginsMock({ usageCollection } as any); + const plugins = getPluginsMock({ usageCollection, license: 'platinum' }); registerReportingUsageCollector(mockReporting, plugins); const [args] = makeCollectorSpy.firstCall.args; diff --git a/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.ts b/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.ts index 6db9d098db9f0..d77d1b5396844 100644 --- a/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.ts +++ b/x-pack/legacy/plugins/reporting/server/usage/reporting_usage_collector.ts @@ -4,17 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ +import { first, map } from 'rxjs/operators'; import { CallCluster } from 'src/legacy/core_plugins/elasticsearch'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main'; +import { ReportingCore } from '../'; import { KIBANA_REPORTING_TYPE } from '../../common/constants'; -import { ReportingConfig, ReportingCore } from '../../server'; -import { ReportingSetupDeps } from '../../server/types'; +import { ReportingConfig } from '../../server'; import { ExportTypesRegistry } from '../lib/export_types_registry'; -import { RangeStats } from './types'; +import { ReportingSetupDeps } from '../types'; +import { GetLicense } from './'; import { getReportingUsage } from './get_reporting_usage'; - -type XPackInfo = XPackMainPlugin['info']; +import { RangeStats } from './types'; // places the reporting data as kibana stats const METATYPE = 'kibana_stats'; @@ -25,14 +25,14 @@ const METATYPE = 'kibana_stats'; export function getReportingUsageCollector( config: ReportingConfig, usageCollection: UsageCollectionSetup, - xpackMainInfo: XPackInfo, + getLicense: GetLicense, exportTypesRegistry: ExportTypesRegistry, isReady: () => Promise ) { return usageCollection.makeUsageCollector({ type: KIBANA_REPORTING_TYPE, fetch: (callCluster: CallCluster) => - getReportingUsage(config, xpackMainInfo, callCluster, exportTypesRegistry), + getReportingUsage(config, getLicense, callCluster, exportTypesRegistry), isReady, /* @@ -57,23 +57,35 @@ export function getReportingUsageCollector( export function registerReportingUsageCollector( reporting: ReportingCore, - plugins: ReportingSetupDeps + { licensing, usageCollection }: ReportingSetupDeps ) { - if (!plugins.usageCollection) { + if (!usageCollection) { return; } - const xpackMainInfo = plugins.__LEGACY.plugins.xpack_main.info; + const config = reporting.getConfig(); const exportTypesRegistry = reporting.getExportTypesRegistry(); + const getLicense = async () => { + return await licensing.license$ + .pipe( + map(({ isAvailable, type }) => ({ + isAvailable: () => isAvailable, + license: { + getType: () => type, + }, + })), + first() + ) + .toPromise(); + }; const collectionIsReady = reporting.pluginHasStarted.bind(reporting); - const config = reporting.getConfig(); const collector = getReportingUsageCollector( config, - plugins.usageCollection, - xpackMainInfo, + usageCollection, + getLicense, exportTypesRegistry, collectionIsReady ); - plugins.usageCollection.registerCollector(collector); + usageCollection.registerCollector(collector); } diff --git a/x-pack/legacy/plugins/reporting/test_helpers/create_mock_reportingplugin.ts b/x-pack/legacy/plugins/reporting/test_helpers/create_mock_reportingplugin.ts index 274f7344c7261..286e072f1f8f6 100644 --- a/x-pack/legacy/plugins/reporting/test_helpers/create_mock_reportingplugin.ts +++ b/x-pack/legacy/plugins/reporting/test_helpers/create_mock_reportingplugin.ts @@ -22,6 +22,7 @@ const createMockSetupDeps = (setupMock?: any): ReportingSetupDeps => { return { elasticsearch: setupMock.elasticsearch, security: setupMock.security, + licensing: {} as any, usageCollection: {} as any, __LEGACY: { plugins: { xpack_main: { status: new EventEmitter() } } } as any, };