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

[8.11] fix(slo): handle permission error (#167933) #168248

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions x-pack/plugins/observability/public/hooks/slo/use_clone_slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* 2.0.
*/

import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import type { CreateSLOInput, CreateSLOResponse, FindSLOResponse } from '@kbn/slo-schema';
import { QueryKey, useMutation, useQueryClient } from '@tanstack/react-query';
import { v1 as uuidv1 } from 'uuid';
import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';

type ServerError = IHttpFetchError<ResponseErrorBody>;

export function useCloneSlo() {
const {
http,
Expand All @@ -21,7 +24,7 @@ export function useCloneSlo() {

return useMutation<
CreateSLOResponse,
string,
ServerError,
{ slo: CreateSLOInput; originalSloId?: string },
{ previousData?: FindSLOResponse; queryKey?: QueryKey }
>(
Expand Down Expand Up @@ -58,16 +61,17 @@ export function useCloneSlo() {
return { queryKey, previousData };
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (_err, { slo }, context) => {
onError: (error, { slo }, context) => {
if (context?.previousData && context?.queryKey) {
queryClient.setQueryData(context.queryKey, context.previousData);
}
toasts.addDanger(
i18n.translate('xpack.observability.slo.clone.errorNotification', {

toasts.addError(new Error(error.body?.message ?? error.message), {
title: i18n.translate('xpack.observability.slo.clone.errorNotification', {
defaultMessage: 'Failed to clone {name}',
values: { name: slo.name },
})
);
}),
});
},
onSuccess: (_data, { slo }) => {
toasts.addSuccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { encode } from '@kbn/rison';
import type { CreateSLOInput, CreateSLOResponse, FindSLOResponse } from '@kbn/slo-schema';
Expand All @@ -14,6 +15,8 @@ import { paths } from '../../../common/locators/paths';
import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';

type ServerError = IHttpFetchError<ResponseErrorBody>;

export function useCreateSlo() {
const {
application: { navigateToUrl },
Expand All @@ -24,7 +27,7 @@ export function useCreateSlo() {

return useMutation<
CreateSLOResponse,
string,
ServerError,
{ slo: CreateSLOInput },
{ previousData?: FindSLOResponse; queryKey?: QueryKey }
>(
Expand Down Expand Up @@ -72,7 +75,7 @@ export function useCreateSlo() {
queryClient.setQueryData(context.queryKey, context.previousData);
}

toasts.addError(new Error(String(error)), {
toasts.addError(new Error(error.body?.message ?? error.message), {
title: i18n.translate('xpack.observability.slo.create.errorNotification', {
defaultMessage: 'Something went wrong while creating {name}',
values: { name: slo.name },
Expand Down
15 changes: 9 additions & 6 deletions x-pack/plugins/observability/public/hooks/slo/use_delete_slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import { QueryKey, useMutation, useQueryClient } from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import { FindSLOResponse } from '@kbn/slo-schema';
import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';

type ServerError = IHttpFetchError<ResponseErrorBody>;

export function useDeleteSlo() {
const {
http,
Expand All @@ -20,7 +23,7 @@ export function useDeleteSlo() {

return useMutation<
string,
string,
ServerError,
{ id: string; name: string },
{ previousData?: FindSLOResponse; queryKey?: QueryKey }
>(
Expand Down Expand Up @@ -60,17 +63,17 @@ export function useDeleteSlo() {
return { previousData, queryKey };
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (_err, { name }, context) => {
onError: (error, { name }, context) => {
if (context?.previousData && context?.queryKey) {
queryClient.setQueryData(context.queryKey, context.previousData);
}

toasts.addDanger(
i18n.translate('xpack.observability.slo.slo.delete.errorNotification', {
toasts.addError(new Error(error.body?.message ?? error.message), {
title: i18n.translate('xpack.observability.slo.slo.delete.errorNotification', {
defaultMessage: 'Failed to delete {name}',
values: { name },
})
);
}),
});
},
onSuccess: (_data, { name }) => {
toasts.addSuccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* 2.0.
*/

import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import type { FindSLOResponse, UpdateSLOInput, UpdateSLOResponse } from '@kbn/slo-schema';
import { QueryKey, useMutation, useQueryClient } from '@tanstack/react-query';
import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';

type ServerError = IHttpFetchError<ResponseErrorBody>;

export function useUpdateSlo() {
const {
http,
Expand All @@ -20,7 +23,7 @@ export function useUpdateSlo() {

return useMutation<
UpdateSLOResponse,
string,
ServerError,
{ sloId: string; slo: UpdateSLOInput },
{ previousData?: FindSLOResponse; queryKey?: QueryKey }
>(
Expand Down Expand Up @@ -69,7 +72,7 @@ export function useUpdateSlo() {
queryClient.setQueryData(context.queryKey, context.previousData);
}

toasts.addError(new Error(String(error)), {
toasts.addError(new Error(error.body?.message ?? error.message), {
title: i18n.translate('xpack.observability.slo.update.errorNotification', {
defaultMessage: 'Something went wrong when updating {name}',
values: { name },
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/observability/server/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export class InternalQueryError extends ObservabilityError {}
export class NotSupportedError extends ObservabilityError {}
export class IllegalArgumentError extends ObservabilityError {}
export class InvalidTransformError extends ObservabilityError {}

export class SecurityException extends ObservabilityError {}
5 changes: 5 additions & 0 deletions x-pack/plugins/observability/server/errors/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CompositeSLOIdConflict,
CompositeSLONotFound,
ObservabilityError,
SecurityException,
SLOIdConflict,
SLONotFound,
} from './errors';
Expand All @@ -22,5 +23,9 @@ export function getHTTPResponseCode(error: ObservabilityError): number {
return 409;
}

if (error instanceof SecurityException) {
return 403;
}

return 400;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { ElasticsearchClient, Logger } from '@kbn/core/server';

import { SLO, IndicatorTypes } from '../../domain/models';
import { SecurityException } from '../../errors';
import { retryTransientEsErrors } from '../../utils/retry';
import { TransformGenerator } from './transform_generators';

Expand Down Expand Up @@ -42,6 +43,10 @@ export class DefaultTransformManager implements TransformManager {
});
} catch (err) {
this.logger.error(`Cannot create SLO transform for indicator type [${slo.indicator.type}]`);
if (err.meta?.body?.error?.type === 'security_exception') {
throw new SecurityException(err.meta.body.error.reason);
}

throw err;
}

Expand Down
Loading