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

[Synthetics] Improve reading user permissions #169601

Merged
merged 4 commits into from
Oct 31, 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
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;
}
},
});