diff --git a/x-pack/plugins/infra/server/lib/log_analysis/errors.ts b/x-pack/plugins/infra/server/lib/log_analysis/errors.ts index a6d0db25084e..ad46ebf71026 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/errors.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/errors.ts @@ -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); @@ -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 + ); +}; diff --git a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts index 950de4261bda..8fc8e588f312 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts @@ -25,6 +25,7 @@ import { InsufficientAnomalyMlJobsConfigured, InsufficientLogAnalysisMlJobConfigurationError, UnknownCategoryError, + isMlPrivilegesError, } from './errors'; import { decodeOrThrow } from '../../../common/runtime_types'; import { @@ -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 { @@ -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 { diff --git a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts index f1f1a1681a90..8b36f32f318a 100644 --- a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts +++ b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts @@ -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( @@ -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: {