Skip to content

Commit

Permalink
Add ML privileges helper and amend datasets route
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry350 committed Aug 5, 2020
1 parent 22d6f09 commit 9be4c25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/infra/server/lib/log_analysis/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

/* eslint-disable max-classes-per-file */

import {
UnknownMLCapabilitiesError,
InsufficientMLCapabilities,
MLPrivilegesUninitialized,
} from '../../../../ml/server';

export class NoLogAnalysisMlJobError extends Error {
constructor(message?: string) {
super(message);
Expand Down Expand Up @@ -33,3 +39,11 @@ export class InsufficientAnomalyMlJobsConfigured extends Error {
Object.setPrototypeOf(this, new.target.prototype);
}
}

export const isMlPrivilegesError = (error: any) => {
return (
error instanceof UnknownMLCapabilitiesError ||
error instanceof InsufficientMLCapabilities ||
error instanceof MLPrivilegesUninitialized
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
InsufficientAnomalyMlJobsConfigured,
InsufficientLogAnalysisMlJobConfigurationError,
UnknownCategoryError,
isMlPrivilegesError,
} from './errors';
import { decodeOrThrow } from '../../../common/runtime_types';
import {
Expand Down Expand Up @@ -65,7 +66,10 @@ async function getCompatibleAnomaliesJobIds(
jobIds.push(logRateJobId);
jobSpans = [...jobSpans, ...spans];
} catch (e) {
// Job wasn't found
if (isMlPrivilegesError(e)) {
throw e;
}
// An error is also thrown when no jobs are found
}

try {
Expand All @@ -75,7 +79,10 @@ async function getCompatibleAnomaliesJobIds(
jobIds.push(logCategoriesJobId);
jobSpans = [...jobSpans, ...spans];
} catch (e) {
// Job wasn't found
if (isMlPrivilegesError(e)) {
throw e;
}
// An error is also thrown when no jobs are found
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createValidationFunction } from '../../../../common/runtime_types';
import type { InfraBackendLibs } from '../../../lib/infra_types';
import { getLogEntryAnomaliesDatasets } from '../../../lib/log_analysis';
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';

export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBackendLibs) => {
framework.registerRoute(
Expand Down Expand Up @@ -55,6 +56,15 @@ export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBacken
throw error;
}

if (isMlPrivilegesError(error)) {
return response.customError({
statusCode: 403,
body: {
message: error.message,
},
});
}

return response.customError({
statusCode: error.statusCode ?? 500,
body: {
Expand Down

0 comments on commit 9be4c25

Please sign in to comment.