Skip to content
Draft
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
@@ -0,0 +1,244 @@
import moment from 'moment-timezone';
import {z} from 'zod';

import {Alert} from '@sentry/scraps/alert';
import {Button} from '@sentry/scraps/button';
import {DateTime} from '@sentry/scraps/dateTime';
import {
defaultFormOptions,
setFieldErrors,
useScrapsForm,
useStore,
} from '@sentry/scraps/form';
import {Flex, Stack} from '@sentry/scraps/layout';
import type {ModalRenderProps} from '@sentry/scraps/modal';
import {Heading, Text} from '@sentry/scraps/text';
import {toast} from '@sentry/scraps/toast';

import {t, tct} from 'sentry/locale';
import {getRequestErrorUserMessage} from 'sentry/utils/requestError/getRequestErrorUserMessage';
import {RequestError} from 'sentry/utils/requestError/requestError';
import {useOrganization} from 'sentry/utils/useOrganization';

import {useCreateServiceAccountToken} from './serviceAccountApi';
import {getDetailFieldErrors, MAX_NAME_LENGTH} from './serviceAccountForm';
import type {NewServiceAccountToken, ServiceAccount} from './types';

const EXPIRATION = z.enum(['7', '30', '90', '365', 'never']);

type Expiration = z.infer<typeof EXPIRATION>;

const EXPIRATION_LABELS: Record<Expiration, string> = {
'7': t('7 days'),
'30': t('30 days'),
'90': t('90 days'),
'365': t('1 year'),
never: t('No expiration'),
};

const EXPIRATION_OPTIONS = EXPIRATION.options.map(value => ({
value,
label: EXPIRATION_LABELS[value],
}));

const DEFAULT_EXPIRATION: Expiration = '30';

const API_FIELDS = ['name', 'scopes', 'expiresAt'] as const;

const schema = z.object({
name: z.string().trim().min(1, t('Name is required')).max(MAX_NAME_LENGTH),
scopeMode: z.enum(['all', 'choose']),
scopes: z.array(z.string()),
expiration: EXPIRATION,
});

type FormValues = z.infer<typeof schema>;

/** Puts each API error on the form field that shows it. */
function getFormErrors(error: RequestError, scopeMode: FormValues['scopeMode']) {
const {name, scopes, expiresAt} = getDetailFieldErrors(error, API_FIELDS);
return {
...(name && {name}),
// The scopes field is hidden while every available scope is picked.
...(scopes && (scopeMode === 'all' ? {scopeMode: scopes} : {scopes})),
...(expiresAt && {expiration: expiresAt}),
};
}

function getExpiryDate(expiration: Expiration) {
return expiration === 'never'
? null
: moment().add(Number(expiration), 'days').toDate();
}

interface Props extends ModalRenderProps {
account: ServiceAccount;
onCreated: (token: NewServiceAccountToken) => void;
}

export function CreateTokenModal({
Header,
Body,
Footer,
closeModal,
account,
onCreated,
}: Props) {
const organization = useOrganization();
const createToken = useCreateServiceAccountToken(organization, account.id);
// The API rejects scopes the caller doesn't hold, even when the account has them.
const callerScopes = new Set<string>(organization.access);
const grantableScopes = account.scopes.filter(scope => callerScopes.has(scope));

const defaultValues: FormValues = {
name: '',
scopeMode: 'all',
scopes: grantableScopes,
expiration: DEFAULT_EXPIRATION,
};

const form = useScrapsForm({
...defaultFormOptions,
defaultValues,
validators: {onDynamic: schema},
onSubmit: async ({value, formApi}) => {
try {
const token = await createToken.mutateAsync({
name: value.name.trim(),
scopes: value.scopeMode === 'all' ? grantableScopes : value.scopes,
expiresAt: getExpiryDate(value.expiration)?.toISOString() ?? null,
});
closeModal();
onCreated(token);
} catch (error) {
toast.error(getRequestErrorUserMessage(error, t('Unable to create token.')));
if (error instanceof RequestError) {
setFieldErrors(formApi, getFormErrors(error, value.scopeMode));
}
}
},
});

const values = useStore(form.store, state => state.values);
const expiryDate = getExpiryDate(values.expiration);
const noScopesPicked =
values.scopeMode === 'all'
? grantableScopes.length === 0
: values.scopes.length === 0;

return (
<form.AppForm form={form}>
<Header closeButton>
<Heading as="h4">{t('Create Token for %s', account.name)}</Heading>
</Header>
<Body>
<Stack gap="xl">
<form.AppField name="name">
{field => (
<field.Layout.Stack
label={t('Name')}
hintText={t('Where you’ll use it, e.g. "GitHub Actions: prod".')}
required
>
<field.Input
value={field.state.value}
onChange={field.handleChange}
autoFocus
/>
</field.Layout.Stack>
)}
</form.AppField>

<form.AppField name="scopeMode">
{field => (
<field.RadioGroup value={field.state.value} onChange={field.handleChange}>
{({Item}) => (
<field.Layout.Stack
label={t('Scopes')}
hintText={t(
'A token can’t do more than its service account, or more than you can.'
)}
>
<Stack gap="md">
<Item value="all">
{t('All available scopes (%s)', grantableScopes.length)}
</Item>
<Item value="choose">{t('Choose scopes')}</Item>
</Stack>
</field.Layout.Stack>
)}
</field.RadioGroup>
)}
</form.AppField>

{values.scopeMode === 'choose' && (
<form.AppField name="scopes">
{field => (
<field.CheckboxGroup
value={field.state.value}
onChange={field.handleChange}
>
{({Item}) => (
<field.Layout.Stack label={t('Token scopes')} variant="compact">
<Flex wrap="wrap" gap="sm lg">
{grantableScopes.map(scope => (
<Item key={scope} value={scope}>
<Text monospace>{scope}</Text>
</Item>
))}
</Flex>
</field.Layout.Stack>
)}
</field.CheckboxGroup>
)}
</form.AppField>
)}

<form.AppField name="expiration">
{field => (
<field.Layout.Stack
label={t('Expiration')}
hintText={
expiryDate
? tct('Expires [date].', {
date: <DateTime date={expiryDate} mode="date" year="always" />,
})
: undefined
}
>
<field.Select
value={field.state.value}
onChange={field.handleChange}
options={EXPIRATION_OPTIONS}
/>
</field.Layout.Stack>
)}
</form.AppField>

{values.expiration === 'never' && (
<Alert.Container>
<Alert variant="warning">
{t(
'A token that never expires is riskier if it leaks. Use this only for systems that can’t rotate tokens.'
)}
</Alert>
</Alert.Container>
)}
</Stack>
</Body>
<Footer>
<Flex gap="md" justify="end">
<Button onClick={closeModal}>{t('Cancel')}</Button>
<form.SubmitButton
disabled={noScopesPicked}
tooltipProps={
noScopesPicked ? {title: t('Pick at least one scope.')} : undefined
}
>
{t('Create Token')}
</form.SubmitButton>
</Flex>
</Footer>
</form.AppForm>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {Fragment} from 'react';

import {Alert} from '@sentry/scraps/alert';
import {Button} from '@sentry/scraps/button';
import {Stack} from '@sentry/scraps/layout';
import type {ModalRenderProps} from '@sentry/scraps/modal';
import {Heading, Text} from '@sentry/scraps/text';

import {TextCopyInput} from 'sentry/components/textCopyInput';
import {t} from 'sentry/locale';

import type {NewServiceAccountToken} from './types';

/**
* The API returns a token's value only when it's created, so this modal is the only
* place it's shown. Keep it out of the URL, storage and analytics.
*/
export function NewTokenModal({
Header,
Body,
Footer,
closeModal,
token,
}: ModalRenderProps & {token: NewServiceAccountToken}) {
return (
<Fragment>
<Header>
<Heading as="h4">{t('Token created')}</Heading>
</Header>
<Body>
<Stack gap="xl">
<Alert.Container>
<Alert variant="warning">
{t('Copy this token now. You won’t be able to see it again.')}
</Alert>
</Alert.Container>
<Stack gap="sm">
<Text bold>{token.name}</Text>
<TextCopyInput aria-label={t('Generated token')}>{token.token}</TextCopyInput>
</Stack>
<Text size="sm" variant="muted">
{t(
'Send it in the Authorization header as a Bearer token. Store it somewhere secret, such as your CI provider’s secrets.'
)}
</Text>
</Stack>
</Body>
<Footer>
<Button variant="primary" onClick={closeModal}>
{t('Done')}
</Button>
</Footer>
</Fragment>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {apiOptions} from 'sentry/utils/api/apiOptions';
import {getApiUrl} from 'sentry/utils/api/getApiUrl';
import {fetchMutation} from 'sentry/utils/queryClient';

import type {ServiceAccount, ServiceAccountInput} from './types';
import type {
NewServiceAccountToken,
ServiceAccount,
ServiceAccountInput,
ServiceAccountToken,
ServiceAccountTokenInput,
} from './types';

export function serviceAccountsApiOptions(
organization: Organization,
Expand Down Expand Up @@ -92,3 +98,61 @@ export function useDeleteServiceAccount(
}),
});
}

export function serviceAccountTokensApiOptions(
organization: Organization,
serviceAccountId: string,
{cursor}: {cursor?: string} = {}
) {
return apiOptions.as<ServiceAccountToken[]>()(
'/organizations/$organizationIdOrSlug/service-accounts/$serviceAccountId/tokens/',
{
path: {organizationIdOrSlug: organization.slug, serviceAccountId},
query: {cursor},
staleTime: 0,
}
);
}

export function useCreateServiceAccountToken(
organization: Organization,
serviceAccountId: string
) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: ServiceAccountTokenInput) =>
fetchMutation<NewServiceAccountToken>({
url: getApiUrl(
'/organizations/$organizationIdOrSlug/service-accounts/$serviceAccountId/tokens/',
{path: {organizationIdOrSlug: organization.slug, serviceAccountId}}
),
method: 'POST',
data: {...data},
}),
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: serviceAccountTokensApiOptions(organization, serviceAccountId).queryKey,
}),
});
}

export function useRevokeServiceAccountToken(
organization: Organization,
serviceAccountId: string
) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (tokenId: string) =>
fetchMutation({
url: getApiUrl(
'/organizations/$organizationIdOrSlug/service-accounts/$serviceAccountId/tokens/$tokenId/',
{path: {organizationIdOrSlug: organization.slug, serviceAccountId, tokenId}}
),
method: 'DELETE',
}),
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: serviceAccountTokensApiOptions(organization, serviceAccountId).queryKey,
}),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe('ServiceAccountDetails', () => {
beforeEach(() => {
MockApiClient.clearMockResponses();
ProjectsStore.loadInitialData([ProjectFixture({id: '1', slug: 'frontend'})]);
MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/service-accounts/10/tokens/`,
body: [],
});
});

it('shows the account and the access each resource has', async () => {
Expand Down
Loading
Loading