Skip to content
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
29 changes: 9 additions & 20 deletions x-pack/plugins/license_management/server/lib/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
10 changes: 4 additions & 6 deletions x-pack/plugins/license_management/server/lib/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
};
Expand Down
19 changes: 6 additions & 13 deletions x-pack/plugins/license_management/server/lib/start_basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 10 additions & 16 deletions x-pack/plugins/license_management/server/lib/start_trial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@
* 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;
}
}

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) {
Expand Down
36 changes: 21 additions & 15 deletions x-pack/plugins/license_management/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void, void, any, any> {
export class LicenseManagementServerPlugin
implements Plugin<void, void, SetupDependencies, StartDependencies> {
private readonly apiRoutes = new ApiRoutes();

setup({ http }: CoreSetup, { licensing, features, security }: Dependencies) {
setup(
{ http, getStartServices }: CoreSetup<StartDependencies>,
{ features, security }: SetupDependencies
) {
const router = http.createRouter();

features.registerElasticsearchFeature({
Expand All @@ -30,17 +34,19 @@ export class LicenseManagementServerPlugin implements Plugin<void, void, any, an
],
});

this.apiRoutes.setup({
router,
plugins: {
licensing,
},
lib: {
isEsError,
},
config: {
isSecurityEnabled: security !== undefined,
},
getStartServices().then(([, { licensing }]) => {
this.apiRoutes.setup({
router,
plugins: {
licensing,
},
lib: {
handleEsError,
},
config: {
isSecurityEnabled: security !== undefined,
},
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(''),
Expand All @@ -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 });
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
}
2 changes: 1 addition & 1 deletion x-pack/plugins/license_management/server/shared_imports.ts
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 { handleEsError } from '../../../../src/plugins/es_ui_shared/server';
21 changes: 12 additions & 9 deletions x-pack/plugins/license_management/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];