Skip to content

Commit

Permalink
[Synthetics] Improve reading user permissions (#169601)
Browse files Browse the repository at this point in the history
## Summary

The PR improves the way to determine what permissions user has.
  • Loading branch information
awahab07 authored Oct 31, 2023
1 parent a509a3a commit fce380d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import { selectOverviewStatus } from '../state/overview_status';
import { useCanReadSyntheticsIndex } from '../../../hooks/use_capabilities';
import {
LICENSE_MISSING_ERROR,
LICENSE_NOT_ACTIVE_ERROR,
Expand All @@ -28,9 +29,11 @@ import {
import { useSyntheticsSettingsContext } from '../contexts';

export const useSyntheticsPrivileges = () => {
const { canRead: canReadSyntheticsIndex, loading: isCanReadLoading } =
useCanReadSyntheticsIndex();
const { error } = useSelector(selectOverviewStatus);

if (error?.body?.message?.startsWith('MissingIndicesPrivileges:')) {
if (!isCanReadLoading && !canReadSyntheticsIndex) {
return (
<EuiFlexGroup
alignItems="center"
Expand Down
48 changes: 48 additions & 0 deletions x-pack/plugins/synthetics/public/hooks/use_capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { SYNTHETICS_INDEX_PATTERN } from '../../common/constants';
import { MonitorLocations } from '../../common/runtime_types';

export const useCanEditSynthetics = () => {
Expand All @@ -23,3 +26,48 @@ export const useCanUsePublicLocations = (monLocations?: MonitorLocations) => {

return !!canUsePublicLocations;
};

export const useCanReadSyntheticsIndex = () => {
const {
services: { data: dataPublicPluginStart },
} = useKibana<{ data: DataPublicPluginStart }>();

const { data, loading, status } = useFetcher<
Promise<{ canRead: boolean; error: undefined | unknown }>
>(() => {
return new Promise((resolve) => {
dataPublicPluginStart.search
.search(
{
terminate_after: 1,
params: {
index: SYNTHETICS_INDEX_PATTERN,
size: 0,
},
},
{
legacyHitsTotal: false,
}
)
.subscribe({
next: (_) => {
resolve({ canRead: true, error: undefined });
},
error: (error: { err: { statusCode: number } }) => {
if (error.err?.statusCode >= 400 && error.err?.statusCode < 500) {
resolve({ canRead: false, error });
} else {
resolve({ canRead: true, error });
}
},
});
});
}, []);

return {
canRead: data?.canRead,
error: data?.error,
loading,
status,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ interface EmptyStateErrorProps {

export const EmptyStateError = ({ errors }: EmptyStateErrorProps) => {
const unauthorized = errors.find(
(error) => error.message && error.message.includes('unauthorized')
(error) =>
(error.message && error.message.includes('unauthorized')) ||
(error.body?.message && error.body.message.includes('unauthorized'))
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ export const createGetIndexStatusRoute: UMRestApiRouteFactory = (libs: UMServerL
to: schema.maybe(schema.string()),
}),
},
handler: async ({ uptimeEsClient, request }): Promise<any> => {
handler: async ({ uptimeEsClient, request, response }): Promise<any> => {
const { from, to } = request.query;
return await libs.requests.getIndexStatus({ uptimeEsClient, range: { from, to } });
try {
return await libs.requests.getIndexStatus({ uptimeEsClient, range: { from, to } });
} catch (e) {
if (e.meta?.statusCode === 403) {
return response.customError({
statusCode: 403,
body: {
message:
'unauthorized: You do not have the required permissions to read uptime indices',
},
});
}

throw e;
}
},
});

0 comments on commit fce380d

Please sign in to comment.