Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Refactor in preparation for new es client #74552

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 23 additions & 6 deletions x-pack/plugins/ml/server/lib/license/ml_server_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,47 @@
import {
KibanaRequest,
KibanaResponseFactory,
RequestHandler,
RequestHandlerContext,
ILegacyScopedClusterClient,
IScopedClusterClient,
RequestHandler,
} from 'kibana/server';

import { MlLicense } from '../../../common/license';

type Handler = (handlerParams: {
legacyClient: ILegacyScopedClusterClient;
client: IScopedClusterClient;
request: KibanaRequest<any, any, any, any>;
response: KibanaResponseFactory;
context: RequestHandlerContext;
}) => ReturnType<RequestHandler>;

export class MlServerLicense extends MlLicense {
public fullLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
public fullLicenseAPIGuard(handler: Handler) {
return guard(() => this.isFullLicense(), handler);
}
public basicLicenseAPIGuard(handler: RequestHandler<any, any, any>) {
public basicLicenseAPIGuard(handler: Handler) {
return guard(() => this.isMinimumLicense(), handler);
}
}

function guard(check: () => boolean, handler: RequestHandler<any, any, any>) {
function guard(check: () => boolean, handler: Handler) {
return (
context: RequestHandlerContext,
request: KibanaRequest,
request: KibanaRequest<any, any, any, any>,
response: KibanaResponseFactory
) => {
if (check() === false) {
return response.forbidden();
}
return handler(context, request, response);

return handler({
legacyClient: context.ml!.mlClient,
client: context.core.elasticsearch.client,
request,
response,
context,
});
};
}
20 changes: 8 additions & 12 deletions x-pack/plugins/ml/server/routes/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export function annotationRoutes(
tags: ['access:ml:canGetAnnotations'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { getAnnotations } = annotationServiceProvider(context.ml!.mlClient);
const { getAnnotations } = annotationServiceProvider(legacyClient);
const resp = await getAnnotations(request.body);

return response.ok({
Expand Down Expand Up @@ -91,16 +91,14 @@ export function annotationRoutes(
tags: ['access:ml:canCreateAnnotation'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
context.ml!.mlClient
);
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
if (annotationsFeatureAvailable === false) {
throw getAnnotationsFeatureUnavailableErrorMessage();
}

const { indexAnnotation } = annotationServiceProvider(context.ml!.mlClient);
const { indexAnnotation } = annotationServiceProvider(legacyClient);

const currentUser =
securityPlugin !== undefined ? securityPlugin.authc.getCurrentUser(request) : {};
Expand Down Expand Up @@ -136,17 +134,15 @@ export function annotationRoutes(
tags: ['access:ml:canDeleteAnnotation'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(
context.ml!.mlClient
);
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(legacyClient);
if (annotationsFeatureAvailable === false) {
throw getAnnotationsFeatureUnavailableErrorMessage();
}

const annotationId = request.params.annotationId;
const { deleteAnnotation } = annotationServiceProvider(context.ml!.mlClient);
const { deleteAnnotation } = annotationServiceProvider(legacyClient);
const resp = await deleteAnnotation(annotationId);

return response.ok({
Expand Down
79 changes: 41 additions & 38 deletions x-pack/plugins/ml/server/routes/anomaly_detectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs');
const results = await legacyClient.callAsInternalUser('ml.jobs');
return response.ok({
body: results,
});
Expand Down Expand Up @@ -74,10 +74,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs', { jobId });
const results = await legacyClient.callAsInternalUser('ml.jobs', { jobId });
return response.ok({
body: results,
});
Expand Down Expand Up @@ -105,9 +105,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats');
const results = await legacyClient.callAsInternalUser('ml.jobStats');
return response.ok({
body: results,
});
Expand Down Expand Up @@ -136,10 +136,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats', { jobId });
const results = await legacyClient.callAsInternalUser('ml.jobStats', { jobId });
return response.ok({
body: results,
});
Expand Down Expand Up @@ -172,10 +172,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.addJob', {
const results = await legacyClient.callAsInternalUser('ml.addJob', {
jobId,
body: request.body,
});
Expand Down Expand Up @@ -209,10 +209,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canUpdateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateJob', {
const results = await legacyClient.callAsInternalUser('ml.updateJob', {
jobId,
body: request.body,
});
Expand Down Expand Up @@ -244,10 +244,10 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canOpenJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsInternalUser('ml.openJob', {
const results = await legacyClient.callAsInternalUser('ml.openJob', {
jobId,
});
return response.ok({
Expand Down Expand Up @@ -278,7 +278,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCloseJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { jobId: string; force?: boolean } = {
jobId: request.params.jobId,
Expand All @@ -287,7 +287,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
if (force !== undefined) {
options.force = force;
}
const results = await context.ml!.mlClient.callAsInternalUser('ml.closeJob', options);
const results = await legacyClient.callAsInternalUser('ml.closeJob', options);
return response.ok({
body: results,
});
Expand Down Expand Up @@ -316,7 +316,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canDeleteJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const options: { jobId: string; force?: boolean } = {
jobId: request.params.jobId,
Expand All @@ -325,7 +325,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
if (force !== undefined) {
options.force = force;
}
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteJob', options);
const results = await legacyClient.callAsInternalUser('ml.deleteJob', options);
return response.ok({
body: results,
});
Expand All @@ -352,9 +352,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.validateDetector', {
const results = await legacyClient.callAsInternalUser('ml.validateDetector', {
body: request.body,
});
return response.ok({
Expand Down Expand Up @@ -387,11 +387,11 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canForecastJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const jobId = request.params.jobId;
const duration = request.body.duration;
const results = await context.ml!.mlClient.callAsInternalUser('ml.forecast', {
const results = await legacyClient.callAsInternalUser('ml.forecast', {
jobId,
duration,
});
Expand Down Expand Up @@ -428,9 +428,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.records', {
const results = await legacyClient.callAsInternalUser('ml.records', {
jobId: request.params.jobId,
body: request.body,
});
Expand Down Expand Up @@ -467,9 +467,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.buckets', {
const results = await legacyClient.callAsInternalUser('ml.buckets', {
jobId: request.params.jobId,
timestamp: request.params.timestamp,
body: request.body,
Expand Down Expand Up @@ -507,9 +507,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.overallBuckets', {
const results = await legacyClient.callAsInternalUser('ml.overallBuckets', {
jobId: request.params.jobId,
top_n: request.body.topN,
bucket_span: request.body.bucketSpan,
Expand Down Expand Up @@ -544,9 +544,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.categories', {
const results = await legacyClient.callAsInternalUser('ml.categories', {
jobId: request.params.jobId,
categoryId: request.params.categoryId,
});
Expand Down Expand Up @@ -578,11 +578,14 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
jobId: request.params.jobId,
});
// const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: commented code

// jobId: request.params.jobId,
// });
return response.ok({
body: results,
});
Expand Down Expand Up @@ -611,9 +614,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canGetJobs'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', {
const results = await legacyClient.callAsInternalUser('ml.modelSnapshots', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
});
Expand Down Expand Up @@ -647,9 +650,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.updateModelSnapshot', {
const results = await legacyClient.callAsInternalUser('ml.updateModelSnapshot', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
body: request.body,
Expand Down Expand Up @@ -682,9 +685,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) {
tags: ['access:ml:canCreateJob'],
},
},
mlLicense.fullLicenseAPIGuard(async (context, request, response) => {
mlLicense.fullLicenseAPIGuard(async ({ legacyClient, request, response }) => {
try {
const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteModelSnapshot', {
const results = await legacyClient.callAsInternalUser('ml.deleteModelSnapshot', {
jobId: request.params.jobId,
snapshotId: request.params.snapshotId,
});
Expand Down
Loading