diff --git a/x-pack/plugins/license_management/server/lib/license.ts b/x-pack/plugins/license_management/server/lib/license.ts index d81d6c9aa278f..f23aba4c1d3ef 100644 --- a/x-pack/plugins/license_management/server/lib/license.ts +++ b/x-pack/plugins/license_management/server/lib/license.ts @@ -5,33 +5,22 @@ * 2.0. */ -import { LicensingPluginSetup } from '../../../licensing/server'; -import { CallAsCurrentUser } from '../types'; - -const getLicensePath = (acknowledge: boolean) => - `/_license${acknowledge ? '?acknowledge=true' : ''}`; +import { IScopedClusterClient } from 'kibana/server'; +import { LicensingPluginStart } from '../../../licensing/server'; interface PutLicenseArg { acknowledge: boolean; - callAsCurrentUser: CallAsCurrentUser; - licensing: LicensingPluginSetup; + client: IScopedClusterClient; + licensing: LicensingPluginStart; license: { [key: string]: any }; } -export async function putLicense({ - acknowledge, - callAsCurrentUser, - licensing, - license, -}: PutLicenseArg) { - const options = { - method: 'POST', - path: getLicensePath(acknowledge), - body: license, - }; - +export async function putLicense({ acknowledge, client, licensing, license }: PutLicenseArg) { try { - const response = await callAsCurrentUser('transport.request', options); + const { body: response } = await client.asCurrentUser.license.post({ + body: license, + acknowledge, + }); const { acknowledged, license_status: licenseStatus } = response; if (acknowledged && licenseStatus === 'valid') { diff --git a/x-pack/plugins/license_management/server/lib/permissions.ts b/x-pack/plugins/license_management/server/lib/permissions.ts index e165ebb36bc82..517854fad8e83 100644 --- a/x-pack/plugins/license_management/server/lib/permissions.ts +++ b/x-pack/plugins/license_management/server/lib/permissions.ts @@ -5,14 +5,14 @@ * 2.0. */ -import { CallAsCurrentUser } from '../types'; +import { IScopedClusterClient } from 'src/core/server'; interface GetPermissionsArg { isSecurityEnabled: boolean; - callAsCurrentUser: CallAsCurrentUser; + client: IScopedClusterClient; } -export async function getPermissions({ isSecurityEnabled, callAsCurrentUser }: GetPermissionsArg) { +export async function getPermissions({ isSecurityEnabled, client }: GetPermissionsArg) { if (!isSecurityEnabled) { // If security isn't enabled, let the user use license management return { @@ -21,15 +21,13 @@ export async function getPermissions({ isSecurityEnabled, callAsCurrentUser }: G } const options = { - method: 'POST', - path: '/_security/user/_has_privileges', body: { cluster: ['manage'], // License management requires "manage" cluster privileges }, }; try { - const response = await callAsCurrentUser('transport.request', options); + const { body: response } = await client.asCurrentUser.security.hasPrivileges(options); return { hasPermission: response.cluster.manage, }; diff --git a/x-pack/plugins/license_management/server/lib/start_basic.ts b/x-pack/plugins/license_management/server/lib/start_basic.ts index e203983c6eb88..e45c758b304de 100644 --- a/x-pack/plugins/license_management/server/lib/start_basic.ts +++ b/x-pack/plugins/license_management/server/lib/start_basic.ts @@ -5,25 +5,18 @@ * 2.0. */ -import { LicensingPluginSetup } from '../../../licensing/server'; -import { CallAsCurrentUser } from '../types'; - -const getStartBasicPath = (acknowledge: boolean) => - `/_license/start_basic${acknowledge ? '?acknowledge=true' : ''}`; +import { IScopedClusterClient } from 'src/core/server'; +import { LicensingPluginStart } from '../../../licensing/server'; interface StartBasicArg { acknowledge: boolean; - callAsCurrentUser: CallAsCurrentUser; - licensing: LicensingPluginSetup; + client: IScopedClusterClient; + licensing: LicensingPluginStart; } -export async function startBasic({ acknowledge, callAsCurrentUser, licensing }: StartBasicArg) { - const options = { - method: 'POST', - path: getStartBasicPath(acknowledge), - }; +export async function startBasic({ acknowledge, client, licensing }: StartBasicArg) { try { - const response = await callAsCurrentUser('transport.request', options); + const { body: response } = await client.asCurrentUser.license.postStartBasic({ acknowledge }); const { basic_was_started: basicWasStarted } = response; if (basicWasStarted) { await licensing.refresh(); diff --git a/x-pack/plugins/license_management/server/lib/start_trial.ts b/x-pack/plugins/license_management/server/lib/start_trial.ts index 7bd771bd678fd..c1558f54b39b2 100644 --- a/x-pack/plugins/license_management/server/lib/start_trial.ts +++ b/x-pack/plugins/license_management/server/lib/start_trial.ts @@ -5,16 +5,12 @@ * 2.0. */ -import { LicensingPluginSetup } from '../../../licensing/server'; -import { CallAsCurrentUser } from '../types'; +import { IScopedClusterClient } from 'src/core/server'; +import { LicensingPluginStart } from '../../../licensing/server'; -export async function canStartTrial(callAsCurrentUser: CallAsCurrentUser) { - const options = { - method: 'GET', - path: '/_license/trial_status', - }; +export async function canStartTrial(client: IScopedClusterClient) { try { - const response = await callAsCurrentUser('transport.request', options); + const { body: response } = await client.asCurrentUser.license.getTrialStatus(); return response.eligible_to_start_trial; } catch (error) { return error.body; @@ -22,17 +18,15 @@ export async function canStartTrial(callAsCurrentUser: CallAsCurrentUser) { } interface StartTrialArg { - callAsCurrentUser: CallAsCurrentUser; - licensing: LicensingPluginSetup; + client: IScopedClusterClient; + licensing: LicensingPluginStart; } -export async function startTrial({ callAsCurrentUser, licensing }: StartTrialArg) { - const options = { - method: 'POST', - path: '/_license/start_trial?acknowledge=true', - }; +export async function startTrial({ client, licensing }: StartTrialArg) { try { - const response = await callAsCurrentUser('transport.request', options); + const { body: response } = await client.asCurrentUser.license.postStartTrial({ + acknowledge: true, + }); const { trial_was_started: trialWasStarted } = response; if (trialWasStarted) { diff --git a/x-pack/plugins/license_management/server/plugin.ts b/x-pack/plugins/license_management/server/plugin.ts index 553d2e3ff69a9..5772b8d9a6518 100644 --- a/x-pack/plugins/license_management/server/plugin.ts +++ b/x-pack/plugins/license_management/server/plugin.ts @@ -8,13 +8,17 @@ import { Plugin, CoreSetup } from 'kibana/server'; import { ApiRoutes } from './routes'; -import { isEsError } from './shared_imports'; -import { Dependencies } from './types'; +import { handleEsError } from './shared_imports'; +import { SetupDependencies, StartDependencies } from './types'; -export class LicenseManagementServerPlugin implements Plugin { +export class LicenseManagementServerPlugin + implements Plugin { private readonly apiRoutes = new ApiRoutes(); - setup({ http }: CoreSetup, { licensing, features, security }: Dependencies) { + setup( + { http, getStartServices }: CoreSetup, + { features, security }: SetupDependencies + ) { const router = http.createRouter(); features.registerElasticsearchFeature({ @@ -30,17 +34,19 @@ export class LicenseManagementServerPlugin implements Plugin { + this.apiRoutes.setup({ + router, + plugins: { + licensing, + }, + lib: { + handleEsError, + }, + config: { + isSecurityEnabled: security !== undefined, + }, + }); }); } diff --git a/x-pack/plugins/license_management/server/routes/api/license/register_license_route.ts b/x-pack/plugins/license_management/server/routes/api/license/register_license_route.ts index 86f87506dfc2c..03e57b1a12cd9 100644 --- a/x-pack/plugins/license_management/server/routes/api/license/register_license_route.ts +++ b/x-pack/plugins/license_management/server/routes/api/license/register_license_route.ts @@ -10,7 +10,11 @@ import { putLicense } from '../../../lib/license'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../helpers'; -export function registerLicenseRoute({ router, plugins: { licensing } }: RouteDependencies) { +export function registerLicenseRoute({ + router, + lib: { handleEsError }, + plugins: { licensing }, +}: RouteDependencies) { router.put( { path: addBasePath(''), @@ -22,15 +26,19 @@ export function registerLicenseRoute({ router, plugins: { licensing } }: RouteDe }, }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client; - return res.ok({ - body: await putLicense({ - acknowledge: Boolean(req.query.acknowledge), - callAsCurrentUser, - licensing, - license: req.body, - }), - }); + const { client } = ctx.core.elasticsearch; + try { + return res.ok({ + body: await putLicense({ + acknowledge: Boolean(req.query.acknowledge), + client, + licensing, + license: req.body, + }), + }); + } catch (error) { + return handleEsError({ error, response: res }); + } } ); } diff --git a/x-pack/plugins/license_management/server/routes/api/license/register_permissions_route.ts b/x-pack/plugins/license_management/server/routes/api/license/register_permissions_route.ts index dd441051872d2..01aae5cd6d441 100644 --- a/x-pack/plugins/license_management/server/routes/api/license/register_permissions_route.ts +++ b/x-pack/plugins/license_management/server/routes/api/license/register_permissions_route.ts @@ -11,13 +11,18 @@ import { addBasePath } from '../../helpers'; export function registerPermissionsRoute({ router, + lib: { handleEsError }, config: { isSecurityEnabled }, }: RouteDependencies) { router.post({ path: addBasePath('/permissions'), validate: false }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client; + const { client } = ctx.core.elasticsearch; - return res.ok({ - body: await getPermissions({ callAsCurrentUser, isSecurityEnabled }), - }); + try { + return res.ok({ + body: await getPermissions({ client, isSecurityEnabled }), + }); + } catch (error) { + return handleEsError({ error, response: res }); + } }); } diff --git a/x-pack/plugins/license_management/server/routes/api/license/register_start_basic_route.ts b/x-pack/plugins/license_management/server/routes/api/license/register_start_basic_route.ts index bc5fb70f7dadd..60e72d297b2ed 100644 --- a/x-pack/plugins/license_management/server/routes/api/license/register_start_basic_route.ts +++ b/x-pack/plugins/license_management/server/routes/api/license/register_start_basic_route.ts @@ -10,21 +10,29 @@ import { startBasic } from '../../../lib/start_basic'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../helpers'; -export function registerStartBasicRoute({ router, plugins: { licensing } }: RouteDependencies) { +export function registerStartBasicRoute({ + router, + lib: { handleEsError }, + plugins: { licensing }, +}: RouteDependencies) { router.post( { path: addBasePath('/start_basic'), validate: { query: schema.object({ acknowledge: schema.string() }) }, }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client; - return res.ok({ - body: await startBasic({ - acknowledge: Boolean(req.query.acknowledge), - callAsCurrentUser, - licensing, - }), - }); + const { client } = ctx.core.elasticsearch; + try { + return res.ok({ + body: await startBasic({ + acknowledge: Boolean(req.query.acknowledge), + client, + licensing, + }), + }); + } catch (error) { + return handleEsError({ error, response: res }); + } } ); } diff --git a/x-pack/plugins/license_management/server/routes/api/license/register_start_trial_routes.ts b/x-pack/plugins/license_management/server/routes/api/license/register_start_trial_routes.ts index 6986e85e7d280..43ab7c5eafdb5 100644 --- a/x-pack/plugins/license_management/server/routes/api/license/register_start_trial_routes.ts +++ b/x-pack/plugins/license_management/server/routes/api/license/register_start_trial_routes.ts @@ -9,16 +9,28 @@ import { canStartTrial, startTrial } from '../../../lib/start_trial'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../../helpers'; -export function registerStartTrialRoutes({ router, plugins: { licensing } }: RouteDependencies) { +export function registerStartTrialRoutes({ + router, + lib: { handleEsError }, + plugins: { licensing }, +}: RouteDependencies) { router.get({ path: addBasePath('/start_trial'), validate: false }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client; - return res.ok({ body: await canStartTrial(callAsCurrentUser) }); + const { client } = ctx.core.elasticsearch; + try { + return res.ok({ body: await canStartTrial(client) }); + } catch (error) { + return handleEsError({ error, response: res }); + } }); router.post({ path: addBasePath('/start_trial'), validate: false }, async (ctx, req, res) => { - const { callAsCurrentUser } = ctx.core.elasticsearch.legacy.client; - return res.ok({ - body: await startTrial({ callAsCurrentUser, licensing }), - }); + const { client } = ctx.core.elasticsearch; + try { + return res.ok({ + body: await startTrial({ client, licensing }), + }); + } catch (error) { + return handleEsError({ error, response: res }); + } }); } diff --git a/x-pack/plugins/license_management/server/shared_imports.ts b/x-pack/plugins/license_management/server/shared_imports.ts index df9b3dd53cc1f..7f55d189457c7 100644 --- a/x-pack/plugins/license_management/server/shared_imports.ts +++ b/x-pack/plugins/license_management/server/shared_imports.ts @@ -5,4 +5,4 @@ * 2.0. */ -export { isEsError } from '../../../../src/plugins/es_ui_shared/server'; +export { handleEsError } from '../../../../src/plugins/es_ui_shared/server'; diff --git a/x-pack/plugins/license_management/server/types.ts b/x-pack/plugins/license_management/server/types.ts index d2f32b47a9973..68219380c6e83 100644 --- a/x-pack/plugins/license_management/server/types.ts +++ b/x-pack/plugins/license_management/server/types.ts @@ -5,32 +5,35 @@ * 2.0. */ -import { LegacyScopedClusterClient, IRouter } from 'kibana/server'; +import { IScopedClusterClient, IRouter } from 'kibana/server'; import { PluginSetupContract as FeaturesPluginSetup } from '../../features/server'; -import { LicensingPluginSetup } from '../../licensing/server'; +import { LicensingPluginStart } from '../../licensing/server'; import { SecurityPluginSetup } from '../../security/server'; -import { isEsError } from './shared_imports'; +import { handleEsError } from './shared_imports'; -export interface Dependencies { - licensing: LicensingPluginSetup; +export interface SetupDependencies { features: FeaturesPluginSetup; security?: SecurityPluginSetup; } +export interface StartDependencies { + licensing: LicensingPluginStart; +} + export interface RouteDependencies { router: IRouter; plugins: { - licensing: LicensingPluginSetup; + licensing: LicensingPluginStart; }; lib: { - isEsError: typeof isEsError; + handleEsError: typeof handleEsError; }; config: { isSecurityEnabled: boolean; }; } -export type CallAsCurrentUser = LegacyScopedClusterClient['callAsCurrentUser']; +export type CallAsCurrentUser = IScopedClusterClient['asCurrentUser']; -export type CallAsInternalUser = LegacyScopedClusterClient['callAsInternalUser']; +export type CallAsInternalUser = IScopedClusterClient['asInternalUser'];