mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem Nothing is tracked on the API keys pages (new and legacy), so we can't tell which keys people copy, how often they create or delete keys, or how quickly projects disable legacy keys. Closes FE-5030. ## Solution Adds five events, defined in `packages/common/telemetry-constants.ts` and fired through `useTrack()`: | Event | When | Properties | | --- | --- | --- | | `api_key_copied` | User copies a publishable, secret, or legacy anon/service_role key with its Copy button. Keyboard copies aren't counted. Not fired when blocked by permissions or when the copy fails. | `keyType` | | `api_key_reveal_clicked` | User reveals a secret or legacy service_role key (not on hide) | `keyType` | | `api_key_created` | Key created from the dialog, or by the first-time setup (fires once per key) | `keyType`, `source` | | `api_key_removed` | Key deleted after confirming in the modal | `keyType` | | `legacy_api_keys_updated` | Legacy keys disabled or re-enabled | `enabled` | - Create, delete, and legacy toggle fire in the mutation hook's `onSuccess`, so failed attempts aren't counted and the event still fires if the component unmounts mid-request. They're attributed to the mutation's own project. Copy and reveal fire in the click handler. - Delete is tracked in `SecretAPIKeys` / `PublishableAPIKeys`, where the confirm modal and mutation live, not in the dropdown item. - No key names or values in properties. - `data-attr` added to the copy, reveal, create, delete, and docs controls (static strings; Studio's PostHog autocapture is currently off, so these send nothing today). `DocsButton` gets an optional `data-attr` prop. - `ui-patterns` `Input` gets optional `onReveal` and `onCopied` callbacks. `onCopied` fires only for the Copy button, so legacy and new keys count copies the same way. - The empty-state sample keys are `inert`, so their buttons don't copy or track. - New `getLegacyApiKeyType` helper maps the legacy `anon` / `service_role` tags to the event's `keyType`. Tests cover copy, reveal, the no-event-on-hide and denied-grant cases, the legacy toggle, and the key-type helper. Success paths for the create dialogs and delete aren't covered. ## How to test - [ ] Open `/project/[ref]/settings/api-keys`, copy a publishable and a secret key, reveal a secret key, and check `api_key_copied` and `api_key_reveal_clicked` in PostHog (staging) with the expected `keyType` - [ ] Create and delete a publishable and a secret key and check `api_key_created` / `api_key_removed` - [ ] On the legacy page, copy anon and service_role, reveal service_role, then disable and re-enable the keys and check `legacy_api_keys_updated`
290 lines
10 KiB
TypeScript
290 lines
10 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { useParams } from 'common'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from 'ui'
|
|
|
|
import Panel from '../Panel'
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { NoPermission } from '@/components/ui/NoPermission'
|
|
import { TextConfirmModal } from '@/components/ui/TextConfirmModalWrapper'
|
|
import { useToggleLegacyAPIKeysMutation } from '@/data/api-keys/legacy-api-key-toggle-mutation'
|
|
import { legacyAPIKeysStatusQueryOptions } from '@/data/api-keys/legacy-api-keys-status-query'
|
|
import { legacyJWTSigningKeyQueryOptions } from '@/data/jwt-signing-keys/legacy-jwt-signing-key-query'
|
|
import { useAuthorizedAppsQuery } from '@/data/oauth/authorized-apps-query'
|
|
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
|
|
import {
|
|
getWriteGrantToastMessage,
|
|
getWriteGrantTooltip,
|
|
useGrant,
|
|
type GrantResult,
|
|
} from '@/lib/grants'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
export const ToggleLegacyApiKeysPanel = () => {
|
|
const { ref: projectRef } = useParams()
|
|
const { data: org } = useSelectedOrganizationQuery()
|
|
|
|
const [isConfirmOpen, setIsConfirmOpen] = useState(false)
|
|
const [isAppsWarningOpen, setIsAppsWarningOpen] = useState(false)
|
|
|
|
const statusGrant = useGrant('GET /v1/projects/{ref}/api-keys/legacy', { projectRef })
|
|
const legacyJWTSecretGrant = useGrant('GET /v1/projects/{ref}/config/auth/signing-keys/legacy', {
|
|
projectRef,
|
|
})
|
|
const toggleGrant = useGrant('PUT /v1/projects/{ref}/api-keys/legacy', { projectRef })
|
|
const canUpdateAPIKeys = toggleGrant.status === 'granted'
|
|
|
|
const { data: legacyAPIKeysStatusData, isSuccess: isLegacyAPIKeysStatusSuccess } = useQuery(
|
|
legacyAPIKeysStatusQueryOptions({ projectRef, grant: statusGrant.grant })
|
|
)
|
|
|
|
// Only used to explain why re-enabling is blocked, so it doesn't gate the panel. A 404 resolves
|
|
// to `null` (not migrated); a real error leaves it undefined and the toggle stays usable.
|
|
const { data: legacyJWTSecret } = useQuery(
|
|
legacyJWTSigningKeyQueryOptions({ projectRef, grant: legacyJWTSecretGrant.grant })
|
|
)
|
|
|
|
const { data: authorizedApps = [], isError: isAuthorizedAppsError } = useAuthorizedAppsQuery({
|
|
slug: org?.slug,
|
|
})
|
|
|
|
const { enabled: isLegacyKeysEnabled } = legacyAPIKeysStatusData || {}
|
|
|
|
const oauthAppsLink = (
|
|
<a
|
|
href={`/dashboard/org/${org?.slug}/apps`}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="underline"
|
|
>
|
|
OAuth apps
|
|
</a>
|
|
)
|
|
|
|
const appsWarning = isAuthorizedAppsError
|
|
? {
|
|
title: 'Check your OAuth apps before continuing',
|
|
description: (
|
|
<>
|
|
Disabling legacy API keys can break apps that integrate with Supabase. Before
|
|
continuing, check your organization's {oauthAppsLink} to ensure none of them depend on
|
|
the legacy API keys.
|
|
</>
|
|
),
|
|
}
|
|
: {
|
|
title: 'Apps using Supabase may break',
|
|
description: (
|
|
<>
|
|
Your project uses apps that integrate with Supabase. Disabling the legacy API keys is a
|
|
brand new feature and the apps you're using may not have added support for this yet. It
|
|
can cause them to stop functioning. Check your {oauthAppsLink} before continuing.
|
|
</>
|
|
),
|
|
}
|
|
|
|
if (statusGrant.status === 'denied') {
|
|
return (
|
|
<section>
|
|
<NoPermission resourceText="view legacy API keys settings" />
|
|
</section>
|
|
)
|
|
}
|
|
|
|
if (statusGrant.status === 'error') {
|
|
return (
|
|
<section>
|
|
<AlertError
|
|
error={statusGrant.error}
|
|
subject="Failed to check legacy API keys permissions"
|
|
/>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
if (!isLegacyAPIKeysStatusSuccess) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<section>
|
|
<Panel>
|
|
<Panel.Content>
|
|
<div className="flex justify-between">
|
|
<div className="flex flex-col gap-2">
|
|
<p className="text-sm">
|
|
{isLegacyKeysEnabled ? 'Disable legacy API keys' : 'Re-enabling legacy API keys'}
|
|
</p>
|
|
<p className="text-foreground-light text-sm">
|
|
{isLegacyKeysEnabled
|
|
? 'Make sure you are no longer using your legacy API keys before proceeding.'
|
|
: 'We recommend you use the new API keys whenever possible, but re-enabling is an option.'}
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center">
|
|
<ButtonTooltip
|
|
onClick={
|
|
isLegacyKeysEnabled && (authorizedApps?.length || isAuthorizedAppsError)
|
|
? () => setIsAppsWarningOpen(true)
|
|
: () => setIsConfirmOpen(true)
|
|
}
|
|
disabled={
|
|
!canUpdateAPIKeys ||
|
|
(!isLegacyKeysEnabled && legacyJWTSecret?.status === 'revoked')
|
|
}
|
|
tooltip={{
|
|
content: {
|
|
side: 'bottom',
|
|
text:
|
|
getWriteGrantTooltip(
|
|
toggleGrant.status,
|
|
'enable or disable',
|
|
'JWT-based API keys'
|
|
) ??
|
|
(!isLegacyKeysEnabled && legacyJWTSecret?.status === 'revoked'
|
|
? 'The legacy JWT secret is revoked. Re-enabling is not possible until it is at least moved to previously used.'
|
|
: undefined),
|
|
},
|
|
}}
|
|
>
|
|
{legacyAPIKeysStatusData.enabled
|
|
? 'Disable JWT-based API keys'
|
|
: 'Re-enable JWT-based API keys'}
|
|
</ButtonTooltip>
|
|
</div>
|
|
</div>
|
|
</Panel.Content>
|
|
</Panel>
|
|
|
|
<ToggleApiKeysModal
|
|
visible={isConfirmOpen}
|
|
onClose={() => setIsConfirmOpen(false)}
|
|
legacyAPIKeysStatusData={legacyAPIKeysStatusData}
|
|
toggleGrant={toggleGrant}
|
|
/>
|
|
|
|
<AlertDialog open={isAppsWarningOpen} onOpenChange={(value) => setIsAppsWarningOpen(value)}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>{appsWarning.title}</AlertDialogTitle>
|
|
<AlertDialogDescription>{appsWarning.description}</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction variant="danger" onClick={() => setIsConfirmOpen(true)}>
|
|
Disable API keys
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
const ToggleApiKeysModal = ({
|
|
visible,
|
|
onClose,
|
|
legacyAPIKeysStatusData,
|
|
toggleGrant,
|
|
}: {
|
|
visible: boolean
|
|
onClose: () => void
|
|
legacyAPIKeysStatusData: { enabled: boolean }
|
|
toggleGrant: GrantResult<'PUT /v1/projects/{ref}/api-keys/legacy'>
|
|
}) => {
|
|
const { ref: projectRef } = useParams()
|
|
const track = useTrack()
|
|
const { enabled: isLegacyKeysEnabled } = legacyAPIKeysStatusData || {}
|
|
|
|
const { mutate: toggleLegacyAPIKey, isPending: isTogglingLegacyAPIKey } =
|
|
useToggleLegacyAPIKeysMutation({
|
|
// Hook-level so the event still fires if the modal unmounts before the request finishes
|
|
onSuccess: (_data, variables) =>
|
|
track(
|
|
'legacy_api_keys_updated',
|
|
{ enabled: variables.enabled },
|
|
{ project: variables.projectRef }
|
|
),
|
|
})
|
|
|
|
const onToggleLegacyAPIKeysEnabled = () => {
|
|
if (!toggleGrant.grant) {
|
|
toast.error(
|
|
getWriteGrantToastMessage(toggleGrant.status, 'enable or disable', 'JWT-based API keys')
|
|
)
|
|
return
|
|
}
|
|
const enabled = !legacyAPIKeysStatusData?.enabled
|
|
|
|
toggleLegacyAPIKey(
|
|
{ projectRef, grant: toggleGrant.grant, enabled },
|
|
{
|
|
onSuccess: () => {
|
|
toast.success(
|
|
enabled
|
|
? 'Your anon and service_role keys have been re-enabled!'
|
|
: 'Your anon and service_role keys have been disabled!'
|
|
)
|
|
onClose()
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<TextConfirmModal
|
|
size="medium"
|
|
visible={visible}
|
|
onCancel={() => onClose()}
|
|
onConfirm={onToggleLegacyAPIKeysEnabled}
|
|
title={isLegacyKeysEnabled ? 'Disable JWT-based keys' : 'Re-enable JWT-based keys'}
|
|
confirmString={isLegacyKeysEnabled ? 'disable' : 're-enable'}
|
|
confirmLabel={`Confirm to ${isLegacyKeysEnabled ? 'disable' : 're-enable'} anon and service_role`}
|
|
confirmPlaceholder={isLegacyKeysEnabled ? 'disable' : 're-enable'}
|
|
loading={isTogglingLegacyAPIKey}
|
|
variant={isLegacyKeysEnabled ? 'destructive' : 'default'}
|
|
alert={
|
|
isLegacyKeysEnabled
|
|
? {
|
|
title: 'Ensure legacy keys are no longer in use before disabling',
|
|
description: (
|
|
<span className="prose text-sm">
|
|
Disabling <code>anon</code> and <code>service_role</code> keys while they are in
|
|
use will cause downtime for your application. Ensure they are no longer in use
|
|
before proceeding. If you have not created a publishable and at least one secret
|
|
API key, some dashboard functionality may become unavailable.
|
|
<br />
|
|
<br />
|
|
<span className="text-danger">
|
|
This disables API keys when used in the <code>apikey</code> header. They remain
|
|
valid as a JWT.
|
|
</span>
|
|
</span>
|
|
),
|
|
}
|
|
: {
|
|
title: 'Publishable and secret keys are preferred',
|
|
description: (
|
|
<span className="prose text-sm">
|
|
Re-enabling <code>anon</code> and <code>service_role</code> keys may be
|
|
appropriate in certain cases, but using a publishable and secret key is more
|
|
secure. We recommend against re-enabling legacy API keys.
|
|
</span>
|
|
),
|
|
}
|
|
}
|
|
/>
|
|
)
|
|
}
|