Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.9 KB
Raw Blame
import { useQueryClient } from 'react-query';
import { Trash2, X } from '@signozhq/icons';
import { Button } from '@signozhq/ui/button';
import AuthZButton from 'lib/authz/components/AuthZButton/AuthZButton';
import {
	buildAPIKeyDeletePermission,
	buildSADetachPermission,
} from 'lib/authz/hooks/useAuthZ/permissions/service-account.permissions';
import { DialogWrapper } from '@signozhq/ui/dialog';
import { toast } from '@signozhq/ui/sonner';
import { convertToApiError } from 'api/ErrorResponseHandlerForGeneratedAPIs';
import {
	getListServiceAccountKeysQueryKey,
	invalidateListServiceAccountKeys,
	useRevokeServiceAccountKey,
} from 'api/generated/services/serviceaccount';
import type {
	RenderErrorResponseDTO,
	ServiceaccounttypesGettableFactorAPIKeyDTO,
} from 'api/generated/services/sigNoz.schemas';
import { AxiosError } from 'axios';
import { SA_QUERY_PARAMS } from 'container/ServiceAccountsSettings/constants';
import { parseAsString, useQueryState } from 'nuqs';
import { useErrorModal } from 'providers/ErrorModalProvider';
import APIError from 'types/api/error';

export interface RevokeKeyFooterProps {
	isRevoking: boolean;
	onCancel: () => void;
	onConfirm: () => void;
	accountId?: string;
	keyId?: string;
}

export function RevokeKeyFooter({
	isRevoking,
	onCancel,
	onConfirm,
	accountId,
	keyId,
}: RevokeKeyFooterProps): JSX.Element {
	return (
		<>
			<Button
				size="md"
				variant="solid"
				color="secondary"
				onClick={onCancel}
				prefix={<X size={12} />}
			>
				Cancel
			</Button>
			<AuthZButton
				size="md"
				checks={[
					buildAPIKeyDeletePermission(keyId ?? ''),
					buildSADetachPermission(accountId ?? ''),
				]}
				authZEnabled={!!accountId && !!keyId}
				variant="solid"
				color="danger"
				loading={isRevoking}
				onClick={onConfirm}
				testId="revoke-key-confirm-btn"
				prefix={<Trash2 size={12} />}
			>
				Revoke Key
			</AuthZButton>
		</>
	);
}

function RevokeKeyModal(): JSX.Element {
	const queryClient = useQueryClient();
	const { showErrorModal, isErrorModalVisible } = useErrorModal();
	const [accountId] = useQueryState(SA_QUERY_PARAMS.ACCOUNT);
	const [revokeKeyId, setRevokeKeyId] = useQueryState(
		SA_QUERY_PARAMS.REVOKE_KEY,
		parseAsString.withDefault(''),
	);
	const open = !!revokeKeyId && !!accountId;

	const cachedKeys = accountId
		? queryClient.getQueryData<{
				data: ServiceaccounttypesGettableFactorAPIKeyDTO[];
			}>(getListServiceAccountKeysQueryKey({ id: accountId }))
		: null;
	const keyName = cachedKeys?.data?.find((k) => k.id === revokeKeyId)?.name;

	const { mutate: revokeKey, isLoading: isRevoking } =
		useRevokeServiceAccountKey({
			mutation: {
				onSuccess: async () => {
					toast.success('Key revoked successfully');
					await setRevokeKeyId(null);
					if (accountId) {
						await invalidateListServiceAccountKeys(queryClient, { id: accountId });
					}
				},
				onError: (error) => {
					showErrorModal(
						convertToApiError(
							error as AxiosError<RenderErrorResponseDTO, unknown> | null,
						) as APIError,
					);
				},
			},
		});

	function handleConfirm(): void {
		if (!revokeKeyId || !accountId) {
			return;
		}
		revokeKey({ pathParams: { id: accountId, fid: revokeKeyId } });
	}

	function handleCancel(): void {
		void setRevokeKeyId(null);
	}

	return (
		<DialogWrapper
			open={open}
			onOpenChange={(isOpen): void => {
				if (!isOpen) {
					handleCancel();
				}
			}}
			title={`Revoke ${keyName ?? 'key'}?`}
			width="narrow"
			className="alert-dialog sa-delete-dialog"
			showCloseButton={false}
			disableOutsideClick={isErrorModalVisible}
			footer={
				<RevokeKeyFooter
					isRevoking={isRevoking}
					onCancel={handleCancel}
					onConfirm={handleConfirm}
					accountId={accountId ?? undefined}
					keyId={revokeKeyId || undefined}
				/>
			}
		>
			Revoking this key will permanently invalidate it. Any systems using this key
			will lose access immediately.
		</DialogWrapper>
	);
}

export default RevokeKeyModal;