Skip to content

Commit

Permalink
Consume License service in Watcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Apr 2, 2021
1 parent 4ba7a29 commit 5e573a1
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 203 deletions.
3 changes: 1 addition & 2 deletions x-pack/plugins/cross_cluster_replication/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ import {
LegacyAPICaller,
} from 'src/core/server';

import { License } from '../../../../src/plugins/es_ui_shared/server';
import { Index } from '../../index_management/server';
import { PLUGIN } from '../common/constants';
import type { SetupDependencies, CcrRequestHandlerContext } from './types';
import { registerApiRoutes } from './routes';
import { elasticsearchJsPlugin } from './client/elasticsearch_ccr';
import { CrossClusterReplicationConfig } from './config';
import { isEsError } from './shared_imports';
import { License, isEsError } from './shared_imports';
import { formatEsError } from './lib/format_es_error';

async function getCustomEsClient(getStartServices: CoreSetup['getStartServices']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export { isEsError } from '../../../../src/plugins/es_ui_shared/server';
export { License, isEsError } from '../../../../src/plugins/es_ui_shared/server';
4 changes: 1 addition & 3 deletions x-pack/plugins/cross_cluster_replication/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import { PluginSetupContract as FeaturesPluginSetup } from '../../features/serve
import { LicensingPluginSetup, LicensingPluginStart } from '../../licensing/server';
import { IndexManagementPluginSetup } from '../../index_management/server';
import { RemoteClustersPluginSetup } from '../../remote_clusters/server';
import { isEsError } from './shared_imports';
import { License, isEsError } from './shared_imports';
import { formatEsError } from './lib/format_es_error';

import type { License } from '../../../../src/plugins/es_ui_shared/server';

export interface SetupDependencies {
licensing: LicensingPluginSetup;
indexManagement: IndexManagementPluginSetup;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

58 changes: 28 additions & 30 deletions x-pack/plugins/watcher/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';

import {
CoreSetup,
ILegacyCustomClusterClient,
Logger,
Plugin,
PluginInitializerContext,
} from 'kibana/server';

import { PLUGIN, INDEX_NAMES } from '../common/constants';
import type {
Dependencies,
LicenseStatus,
SetupDependencies,
StartDependencies,
RouteDependencies,
WatcherRequestHandlerContext,
} from './types';
Expand All @@ -28,6 +31,7 @@ import { registerWatchRoutes } from './routes/api/watch';
import { registerListFieldsRoute } from './routes/api/register_list_fields_route';
import { registerLoadHistoryRoute } from './routes/api/register_load_history_route';
import { elasticsearchJsPlugin } from './lib/elasticsearch_js_plugin';
import { License, isEsError } from './shared_imports';

async function getCustomEsClient(getStartServices: CoreSetup['getStartServices']) {
const [core] = await getStartServices();
Expand All @@ -36,23 +40,20 @@ async function getCustomEsClient(getStartServices: CoreSetup['getStartServices']
}

export class WatcherServerPlugin implements Plugin<void, void, any, any> {
private readonly log: Logger;
private readonly license: License;
private readonly logger: Logger;
private watcherESClient?: ILegacyCustomClusterClient;

private licenseStatus: LicenseStatus = {
hasRequired: false,
};

constructor(ctx: PluginInitializerContext) {
this.log = ctx.logger.get();
this.logger = ctx.logger.get();
this.license = new License();
}

setup({ http, getStartServices }: CoreSetup, { licensing, features }: Dependencies) {
const router = http.createRouter<WatcherRequestHandlerContext>();
const routeDependencies: RouteDependencies = {
router,
getLicenseStatus: () => this.licenseStatus,
};
setup({ http, getStartServices }: CoreSetup, { licensing, features }: SetupDependencies) {
this.license.setup({
pluginName: PLUGIN.getI18nName(i18n),
logger: this.logger,
});

features.registerElasticsearchFeature({
id: 'watcher',
Expand Down Expand Up @@ -90,33 +91,30 @@ export class WatcherServerPlugin implements Plugin<void, void, any, any> {
}
);

const router = http.createRouter<WatcherRequestHandlerContext>();
const routeDependencies: RouteDependencies = {
router,
license: this.license,
lib: { isEsError },
};

registerListFieldsRoute(routeDependencies);
registerLoadHistoryRoute(routeDependencies);
registerIndicesRoutes(routeDependencies);
registerLicenseRoutes(routeDependencies);
registerSettingsRoutes(routeDependencies);
registerWatchesRoutes(routeDependencies);
registerWatchRoutes(routeDependencies);
}

licensing.license$.subscribe(async (license) => {
const { state, message } = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);
const hasMinimumLicense = state === 'valid';
if (hasMinimumLicense && license.getFeature(PLUGIN.ID)) {
this.log.info('Enabling Watcher plugin.');
this.licenseStatus = {
hasRequired: true,
};
} else {
this.licenseStatus = {
hasRequired: false,
message,
};
}
start(core: CoreStart, { licensing }: StartDependencies) {
this.license.start({
pluginId: PLUGIN.ID,
minimumLicenseType: PLUGIN.MINIMUM_LICENSE_REQUIRED,
licensing,
});
}

start() {}

stop() {
if (this.watcherESClient) {
this.watcherESClient.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import { schema } from '@kbn/config-schema';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { reduce, size } from 'lodash';
import { isEsError } from '../../../shared_imports';
import { RouteDependencies } from '../../../types';
import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory';

const bodySchema = schema.object({ pattern: schema.string() }, { unknowns: 'allow' });

Expand Down Expand Up @@ -65,15 +63,15 @@ function getIndices(dataClient: ILegacyScopedClusterClient, pattern: string, lim
});
}

export function registerGetRoute(deps: RouteDependencies) {
deps.router.post(
export function registerGetRoute({ router, license, lib: { isEsError } }: RouteDependencies) {
router.post(
{
path: '/api/watcher/indices',
validate: {
body: bodySchema,
},
},
licensePreRoutingFactory(deps, async (ctx, request, response) => {
license.guardApiRoute(async (ctx, request, response) => {
const { pattern } = request.body;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
*/

import { RouteDependencies } from '../../../types';
import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory';
/*
In order for the client to have the most up-to-date snapshot of the current license,
it needs to make a round-trip to the kibana server. This refresh endpoint is provided
for when the client needs to check the license, but doesn't need to pull data from the
server for any reason, i.e., when adding a new watch.
*/

export function registerRefreshRoute(deps: RouteDependencies) {
deps.router.get(
export function registerRefreshRoute({ router, license }: RouteDependencies) {
router.get(
{
path: '/api/watcher/license/refresh',
validate: false,
},
licensePreRoutingFactory(deps, (ctx, request, response) => {
license.guardApiRoute((ctx, request, response) => {
return response.ok({ body: { success: true } });
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import { schema } from '@kbn/config-schema';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { isEsError } from '../../shared_imports';
// @ts-ignore
import { Fields } from '../../models/fields/index';
import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory';
import { RouteDependencies } from '../../types';

const bodySchema = schema.object({
Expand All @@ -29,15 +27,19 @@ function fetchFields(dataClient: ILegacyScopedClusterClient, indexes: string[])
return dataClient.callAsCurrentUser('fieldCaps', params);
}

export function registerListFieldsRoute(deps: RouteDependencies) {
deps.router.post(
export function registerListFieldsRoute({
router,
license,
lib: { isEsError },
}: RouteDependencies) {
router.post(
{
path: '/api/watcher/fields',
validate: {
body: bodySchema,
},
},
licensePreRoutingFactory(deps, async (ctx, request, response) => {
license.guardApiRoute(async (ctx, request, response) => {
const { indexes } = request.body;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import { schema } from '@kbn/config-schema';
import { get } from 'lodash';
import { ILegacyScopedClusterClient } from 'kibana/server';
import { isEsError } from '../../shared_imports';
import { INDEX_NAMES } from '../../../common/constants';
import { RouteDependencies } from '../../types';
import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory';
// @ts-ignore
import { WatchHistoryItem } from '../../models/watch_history_item/index';

Expand All @@ -32,15 +30,19 @@ function fetchHistoryItem(dataClient: ILegacyScopedClusterClient, watchHistoryIt
});
}

export function registerLoadHistoryRoute(deps: RouteDependencies) {
deps.router.get(
export function registerLoadHistoryRoute({
router,
license,
lib: { isEsError },
}: RouteDependencies) {
router.get(
{
path: '/api/watcher/history/{id}',
validate: {
params: paramsSchema,
},
},
licensePreRoutingFactory(deps, async (ctx, request, response) => {
license.guardApiRoute(async (ctx, request, response) => {
const id = request.params.id;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
*/

import { ILegacyScopedClusterClient } from 'kibana/server';
import { isEsError } from '../../../shared_imports';
// @ts-ignore
import { Settings } from '../../../models/settings/index';
import { RouteDependencies } from '../../../types';
import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory';

function fetchClusterSettings(client: ILegacyScopedClusterClient) {
return client.callAsInternalUser('cluster.getSettings', {
Expand All @@ -19,13 +17,13 @@ function fetchClusterSettings(client: ILegacyScopedClusterClient) {
});
}

export function registerLoadRoute(deps: RouteDependencies) {
deps.router.get(
export function registerLoadRoute({ router, license, lib: { isEsError } }: RouteDependencies) {
router.get(
{
path: '/api/watcher/settings',
validate: false,
},
licensePreRoutingFactory(deps, async (ctx, request, response) => {
license.guardApiRoute(async (ctx, request, response) => {
try {
const settings = await fetchClusterSettings(ctx.watcher!.client);
return response.ok({ body: Settings.fromUpstreamJson(settings).downstreamJson });
Expand Down
Loading

0 comments on commit 5e573a1

Please sign in to comment.