Skip to content

Commit

Permalink
[ML] Adds API license check.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jun 29, 2022
1 parent cd026c2 commit 28b8f40
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/aiops/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"description": "AIOps plugin maintained by ML team.",
"server": true,
"ui": true,
"requiredPlugins": ["data"],
"requiredPlugins": [
"data",
"licensing"
],
"optionalPlugins": [],
"requiredBundles": ["kibanaReact"],
"extraPublicDirs": ["common"]
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/aiops/server/lib/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { ILicense, LicenseType } from '@kbn/licensing-plugin/common/types';

export function isActiveLicense(licenseType: LicenseType, license?: ILicense): boolean {
return (license && license.isActive && license.hasAtLeast(licenseType)) || false;
}
23 changes: 20 additions & 3 deletions x-pack/plugins/aiops/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
* 2.0.
*/

import { Subscription } from 'rxjs';

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin, Logger } from '@kbn/core/server';
import type { DataRequestHandlerContext } from '@kbn/data-plugin/server';

import { AIOPS_ENABLED } from '../common';

import { isActiveLicense } from './lib/license';
import {
AiopsLicense,
AiopsPluginSetup,
AiopsPluginStart,
AiopsPluginSetupDeps,
Expand All @@ -22,19 +26,29 @@ export class AiopsPlugin
implements Plugin<AiopsPluginSetup, AiopsPluginStart, AiopsPluginSetupDeps, AiopsPluginStartDeps>
{
private readonly logger: Logger;
private licenseSubscription: Subscription | null = null;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(core: CoreSetup<AiopsPluginStartDeps>, deps: AiopsPluginSetupDeps) {
public setup(core: CoreSetup<AiopsPluginStartDeps>, plugins: AiopsPluginSetupDeps) {
this.logger.debug('aiops: Setup');

// Subscribe to license changes and store the current license in `currentLicense`.
// This way we can pass on license changes to the route factory having always
// the current license because it's stored in a mutable attribute.
const aiopsLicense: AiopsLicense = { isActivePlatinumLicense: false };
this.licenseSubscription = plugins.licensing.license$.subscribe(async (license) => {
aiopsLicense.isActivePlatinumLicense = isActiveLicense('platinum', license);
});

const router = core.http.createRouter<DataRequestHandlerContext>();

// Register server side APIs
if (AIOPS_ENABLED) {
core.getStartServices().then(([_, depsStart]) => {
defineExplainLogRateSpikesRoute(router, this.logger);
defineExplainLogRateSpikesRoute(router, aiopsLicense, this.logger);
});
}

Expand All @@ -46,5 +60,8 @@ export class AiopsPlugin
return {};
}

public stop() {}
public stop() {
this.logger.debug('aiops: Stop');
this.licenseSubscription?.unsubscribe();
}
}
7 changes: 7 additions & 0 deletions x-pack/plugins/aiops/server/routes/explain_log_rate_spikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
import { API_ENDPOINT } from '../../common/api';
import type { ChangePoint } from '../../common/types';

import type { AiopsLicense } from '../types';

import { fetchFieldCandidates } from './queries/fetch_field_candidates';
import { fetchChangePointPValues } from './queries/fetch_change_point_p_values';

Expand All @@ -29,6 +31,7 @@ const PROGRESS_STEP_P_VALUES = 0.8;

export const defineExplainLogRateSpikesRoute = (
router: IRouter<DataRequestHandlerContext>,
license: AiopsLicense,
logger: Logger
) => {
router.post(
Expand All @@ -39,6 +42,10 @@ export const defineExplainLogRateSpikesRoute = (
},
},
async (context, request, response) => {
if (!license.isActivePlatinumLicense) {
response.forbidden();
}

const client = (await context.core).elasticsearch.client.asCurrentUser;

const controller = new AbortController();
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/aiops/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* 2.0.
*/

import { PluginSetup, PluginStart } from '@kbn/data-plugin/server';
import type { PluginSetup, PluginStart } from '@kbn/data-plugin/server';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';

export interface AiopsPluginSetupDeps {
data: PluginSetup;
licensing: LicensingPluginStart;
}

export interface AiopsPluginStartDeps {
Expand All @@ -26,3 +28,7 @@ export interface AiopsPluginSetup {}
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AiopsPluginStart {}

export interface AiopsLicense {
isActivePlatinumLicense: boolean;
}

0 comments on commit 28b8f40

Please sign in to comment.