Skip to content

Commit

Permalink
[8.11] fix(slo): handle permission error (#167933) (#168248)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.11`:
- [fix(slo): handle permission error
(#167933)](#167933)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Kevin
Delemme","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-10-05T13:17:32Z","message":"fix(slo):
handle permission error
(#167933)","sha":"335fc9b2409855f4aeebf360c0747141b2fcf03b","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:skip","Team:
Actionable
Observability","Feature:SLO","v8.12.0"],"number":167933,"url":"https://github.com/elastic/kibana/pull/167933","mergeCommit":{"message":"fix(slo):
handle permission error
(#167933)","sha":"335fc9b2409855f4aeebf360c0747141b2fcf03b"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/167933","number":167933,"mergeCommit":{"message":"fix(slo):
handle permission error
(#167933)","sha":"335fc9b2409855f4aeebf360c0747141b2fcf03b"}},{"url":"https://github.com/elastic/kibana/pull/168231","number":168231,"branch":"8.10","state":"OPEN"}]}]
BACKPORT-->
  • Loading branch information
kdelemme authored Oct 10, 2023
1 parent 11871e6 commit 636a833
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
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

0 comments on commit 636a833

Please sign in to comment.