fix(slo): enhance error handling for slo routes (#164636)

This commit is contained in:
Kevin Delemme 2023-08-24 08:11:10 -04:00 committed by GitHub
parent 6659376838
commit 0cfa3e2080
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 23 deletions

View file

@ -13,7 +13,10 @@ import {
} from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import type { PublicLicenseJSON } from '@kbn/licensing-plugin/public';
import type { SecurityGetUserPrivilegesResponse } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
SecurityGetUserPrivilegesResponse,
TransformGetTransformStatsResponse,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { useKibana } from '../../utils/kibana_react';
import { convertErrorForUseInToast } from './helpers/convert_error_for_use_in_toast';
import { sloKeys } from './query_key_factory';
@ -24,6 +27,10 @@ interface SloGlobalDiagnosisResponse {
sloResources: {
[x: string]: 'OK' | 'NOT_OK';
};
sloSummaryResources: {
[x: string]: 'OK' | 'NOT_OK';
};
sloSummaryTransformsStats: TransformGetTransformStatsResponse;
}
export interface UseFetchSloGlobalDiagnoseResponse {

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import { errors } from '@elastic/elasticsearch';
import { failedDependency, forbidden } from '@hapi/boom';
import {
createSLOParamsSchema,
@ -301,7 +302,7 @@ const getDiagnosisRoute = createObservabilityServerRoute({
const response = await getGlobalDiagnosis(esClient, licensing);
return response;
} catch (error) {
if (error.cause.statusCode === 403) {
if (error instanceof errors.ResponseError && error.statusCode === 403) {
throw forbidden('Insufficient Elasticsearch cluster permissions to access feature.');
}
throw failedDependency(error);

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import { errors } from '@elastic/elasticsearch';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { LicensingApiRequestHandlerContext } from '@kbn/licensing-plugin/server';
import {
@ -28,27 +29,23 @@ export async function getGlobalDiagnosis(
esClient: ElasticsearchClient,
licensing: LicensingApiRequestHandlerContext
) {
try {
const licenseInfo = licensing.license.toJSON();
const userPrivileges = await esClient.security.getUserPrivileges();
const sloResources = await getSloResourcesDiagnosis(esClient);
const sloSummaryResources = await getSloSummaryResourcesDiagnosis(esClient);
const licenseInfo = licensing.license.toJSON();
const userPrivileges = await esClient.security.getUserPrivileges();
const sloResources = await getSloResourcesDiagnosis(esClient);
const sloSummaryResources = await getSloSummaryResourcesDiagnosis(esClient);
const sloSummaryTransformsStats = await esClient.transform.getTransformStats({
transform_id: `${SLO_SUMMARY_TRANSFORM_NAME_PREFIX}*`,
allow_no_match: true,
});
const sloSummaryTransformsStats = await esClient.transform.getTransformStats({
transform_id: `${SLO_SUMMARY_TRANSFORM_NAME_PREFIX}*`,
allow_no_match: true,
});
return {
licenseAndFeatures: licenseInfo,
userPrivileges,
sloResources,
sloSummaryResources,
sloSummaryTransformsStats,
};
} catch (error) {
throw error;
}
return {
licenseAndFeatures: licenseInfo,
userPrivileges,
sloResources,
sloSummaryResources,
sloSummaryTransformsStats,
};
}
export async function getSloDiagnosis(
@ -115,7 +112,10 @@ async function getSloResourcesDiagnosis(esClient: ElasticsearchClient) {
[SLO_INGEST_PIPELINE_NAME]: ingestPipelineExists ? OK : NOT_OK,
};
} catch (err) {
if (err.meta.statusCode === 403) {
if (
err instanceof errors.ResponseError &&
(err.statusCode === 403 || err.meta.statusCode === 403)
) {
throw new Error('Insufficient permissions to access Elasticsearch Cluster', { cause: err });
}
}
@ -141,7 +141,10 @@ async function getSloSummaryResourcesDiagnosis(esClient: ElasticsearchClient) {
[SLO_SUMMARY_COMPONENT_TEMPLATE_SETTINGS_NAME]: settingsTemplateExists ? OK : NOT_OK,
};
} catch (err) {
if (err.meta.statusCode === 403) {
if (
err instanceof errors.ResponseError &&
(err.statusCode === 403 || err.meta.statusCode === 403)
) {
throw new Error('Insufficient permissions to access Elasticsearch Cluster', { cause: err });
}
}