[Logs UI] Return 403s rather than 500s for ML privilege errors (#74506)
* Add ML privileges error checks to all routes
This commit is contained in:
parent
7f33e72ba7
commit
2e38f5a705
|
@ -6,6 +6,12 @@
|
||||||
|
|
||||||
/* eslint-disable max-classes-per-file */
|
/* eslint-disable max-classes-per-file */
|
||||||
|
|
||||||
|
import {
|
||||||
|
UnknownMLCapabilitiesError,
|
||||||
|
InsufficientMLCapabilities,
|
||||||
|
MLPrivilegesUninitialized,
|
||||||
|
} from '../../../../ml/server';
|
||||||
|
|
||||||
export class NoLogAnalysisMlJobError extends Error {
|
export class NoLogAnalysisMlJobError extends Error {
|
||||||
constructor(message?: string) {
|
constructor(message?: string) {
|
||||||
super(message);
|
super(message);
|
||||||
|
@ -33,3 +39,11 @@ export class InsufficientAnomalyMlJobsConfigured extends Error {
|
||||||
Object.setPrototypeOf(this, new.target.prototype);
|
Object.setPrototypeOf(this, new.target.prototype);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isMlPrivilegesError = (error: any) => {
|
||||||
|
return (
|
||||||
|
error instanceof UnknownMLCapabilitiesError ||
|
||||||
|
error instanceof InsufficientMLCapabilities ||
|
||||||
|
error instanceof MLPrivilegesUninitialized
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
|
@ -25,6 +25,7 @@ import {
|
||||||
InsufficientAnomalyMlJobsConfigured,
|
InsufficientAnomalyMlJobsConfigured,
|
||||||
InsufficientLogAnalysisMlJobConfigurationError,
|
InsufficientLogAnalysisMlJobConfigurationError,
|
||||||
UnknownCategoryError,
|
UnknownCategoryError,
|
||||||
|
isMlPrivilegesError,
|
||||||
} from './errors';
|
} from './errors';
|
||||||
import { decodeOrThrow } from '../../../common/runtime_types';
|
import { decodeOrThrow } from '../../../common/runtime_types';
|
||||||
import {
|
import {
|
||||||
|
@ -65,7 +66,10 @@ async function getCompatibleAnomaliesJobIds(
|
||||||
jobIds.push(logRateJobId);
|
jobIds.push(logRateJobId);
|
||||||
jobSpans = [...jobSpans, ...spans];
|
jobSpans = [...jobSpans, ...spans];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Job wasn't found
|
if (isMlPrivilegesError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
// An error is also thrown when no jobs are found
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -75,7 +79,10 @@ async function getCompatibleAnomaliesJobIds(
|
||||||
jobIds.push(logCategoriesJobId);
|
jobIds.push(logCategoriesJobId);
|
||||||
jobSpans = [...jobSpans, ...spans];
|
jobSpans = [...jobSpans, ...spans];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Job wasn't found
|
if (isMlPrivilegesError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
// An error is also thrown when no jobs are found
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import {
|
||||||
import { createValidationFunction } from '../../../../common/runtime_types';
|
import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
import { getLogEntryAnomalies } from '../../../lib/log_analysis';
|
import { getLogEntryAnomalies } from '../../../lib/log_analysis';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryAnomaliesRoute = ({ framework }: InfraBackendLibs) => {
|
export const initGetLogEntryAnomaliesRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -73,6 +74,15 @@ export const initGetLogEntryAnomaliesRoute = ({ framework }: InfraBackendLibs) =
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
||||||
import { getLogEntryAnomaliesDatasets } from '../../../lib/log_analysis';
|
import { getLogEntryAnomaliesDatasets } from '../../../lib/log_analysis';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBackendLibs) => {
|
export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -55,6 +56,15 @@ export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBacken
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
||||||
import { getTopLogEntryCategories } from '../../../lib/log_analysis';
|
import { getTopLogEntryCategories } from '../../../lib/log_analysis';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryCategoriesRoute = ({ framework }: InfraBackendLibs) => {
|
export const initGetLogEntryCategoriesRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -66,6 +67,15 @@ export const initGetLogEntryCategoriesRoute = ({ framework }: InfraBackendLibs)
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
||||||
import { getLogEntryCategoryDatasets } from '../../../lib/log_analysis';
|
import { getLogEntryCategoryDatasets } from '../../../lib/log_analysis';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryCategoryDatasetsRoute = ({ framework }: InfraBackendLibs) => {
|
export const initGetLogEntryCategoryDatasetsRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -55,6 +56,15 @@ export const initGetLogEntryCategoryDatasetsRoute = ({ framework }: InfraBackend
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
import type { InfraBackendLibs } from '../../../lib/infra_types';
|
||||||
import { getLogEntryCategoryExamples } from '../../../lib/log_analysis';
|
import { getLogEntryCategoryExamples } from '../../../lib/log_analysis';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryCategoryExamplesRoute = ({ framework, sources }: InfraBackendLibs) => {
|
export const initGetLogEntryCategoryExamplesRoute = ({ framework, sources }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -65,6 +66,15 @@ export const initGetLogEntryCategoryExamplesRoute = ({ framework, sources }: Inf
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {
|
||||||
getLogEntryExamplesSuccessReponsePayloadRT,
|
getLogEntryExamplesSuccessReponsePayloadRT,
|
||||||
LOG_ANALYSIS_GET_LOG_ENTRY_RATE_EXAMPLES_PATH,
|
LOG_ANALYSIS_GET_LOG_ENTRY_RATE_EXAMPLES_PATH,
|
||||||
} from '../../../../common/http_api/log_analysis';
|
} from '../../../../common/http_api/log_analysis';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryExamplesRoute = ({ framework, sources }: InfraBackendLibs) => {
|
export const initGetLogEntryExamplesRoute = ({ framework, sources }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -68,6 +69,15 @@ export const initGetLogEntryExamplesRoute = ({ framework, sources }: InfraBacken
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
|
@ -15,6 +15,7 @@ import {
|
||||||
import { createValidationFunction } from '../../../../common/runtime_types';
|
import { createValidationFunction } from '../../../../common/runtime_types';
|
||||||
import { getLogEntryRateBuckets } from '../../../lib/log_analysis';
|
import { getLogEntryRateBuckets } from '../../../lib/log_analysis';
|
||||||
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
import { assertHasInfraMlPlugins } from '../../../utils/request_context';
|
||||||
|
import { isMlPrivilegesError } from '../../../lib/log_analysis/errors';
|
||||||
|
|
||||||
export const initGetLogEntryRateRoute = ({ framework }: InfraBackendLibs) => {
|
export const initGetLogEntryRateRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
framework.registerRoute(
|
framework.registerRoute(
|
||||||
|
@ -56,6 +57,15 @@ export const initGetLogEntryRateRoute = ({ framework }: InfraBackendLibs) => {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMlPrivilegesError(error)) {
|
||||||
|
return response.customError({
|
||||||
|
statusCode: 403,
|
||||||
|
body: {
|
||||||
|
message: error.message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return response.customError({
|
return response.customError({
|
||||||
statusCode: error.statusCode ?? 500,
|
statusCode: error.statusCode ?? 500,
|
||||||
body: {
|
body: {
|
||||||
|
|
Loading…
Reference in a new issue