The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat(api-keys): migrate last-used indicator to ClickHouse endpoint (#47458)
Jordi Enric4096267623c9
diff --git a/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx b/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsxindex 10c8f7576f2..6ba579816b0 100644--- a/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx+++ b/apps/studio/components/interfaces/APIKeys/APIKeyRow.tsx@@ -1,4 +1,4 @@-import { IS_PLATFORM, useFlag } from 'common'+import { IS_PLATFORM } from 'common'import { motion } from 'framer-motion'import { MoreVertical } from 'lucide-react'import {@@ -9,8 +9,6 @@ import {TableCell,TableRow,} from 'ui'-import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'-import { TimestampInfo } from 'ui-patterns/TimestampInfo'import { APIKeyDeleteDialog } from './APIKeyDeleteDialog'import { ApiKeyPill } from './ApiKeyPill'@@ -19,25 +17,18 @@ import type { APIKeysData } from '@/data/api-keys/api-keys-query'export const APIKeyRow = ({apiKey,- lastSeen,isDeleting,isDeleteModalOpen,- isLoadingLastSeen = false,- showLastSeen = true,onDelete,setKeyToDelete,}: {apiKey: Extract<APIKeysData[number], { type: 'secret' | 'publishable' }>- lastSeen?: { timestamp: number; relative: string }- showLastSeen?: booleanisDeleting: booleanisDeleteModalOpen: boolean- isLoadingLastSeen?: booleanonDelete: () => voidsetKeyToDelete: (id: string | null) => void}) => {const MotionTableRow = motion.create(TableRow)- const showApiKeysLastUsed = useFlag('showApiKeysLastUsed')return (<>@@ -68,24 +59,6 @@ export const APIKeyRow = ({</div></TableCell>- {showLastSeen && showApiKeysLastUsed && (- <TableCell className="py-2 min-w-0 whitespace-nowrap hidden lg:table-cell">- <div className="truncate" title={lastSeen?.timestamp.toString() || 'Never used'}>- {isLoadingLastSeen ? (- <ShimmeringLoader />- ) : lastSeen?.timestamp ? (- <TimestampInfo- className="text-sm"- utcTimestamp={lastSeen?.timestamp}- label={lastSeen.relative}- />- ) : (- <span className="text-foreground-lighter">Never used</span>- )}- </div>- </TableCell>- )}-{IS_PLATFORM && (<TableCell className="py-2"><div className="flex justify-end">@@ -123,9 +96,8 @@ export const APIKeyRow = ({variant="destructive"alert={{title: 'This cannot be undone',- description: lastSeen- ? `This API key was used ${lastSeen.timestamp}. Make sure all backend components using it have been updated. Deletion will cause them to receive HTTP 401 Unauthorized status codes on all Supabase APIs.`- : `This API key has not been used in the past 24 hours. Make sure you've updated all backend components using it before deletion.`,+ description:+ 'Make sure all applications and services using it have been updated before deletion. Deletion will cause them to receive HTTP 401 Unauthorized status codes on all Supabase APIs.',}}/></>diff --git a/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx b/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsxindex fdb6dcb24f8..b594ae89e54 100644--- a/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx+++ b/apps/studio/components/interfaces/APIKeys/PublishableAPIKeys.tsx@@ -131,7 +131,6 @@ export const PublishableAPIKeys = () => {)}{publishableApiKeys.map((apiKey) => (<APIKeyRow- showLastSeen={false}key={apiKey.id}apiKey={apiKey}isDeleting={apiKeyToDelete?.id === apiKey.id && isDeletingAPIKey}diff --git a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsxindex e359a35113f..366fa02005a 100644--- a/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx+++ b/apps/studio/components/interfaces/APIKeys/SecretAPIKeys.tsx@@ -1,8 +1,7 @@import { PermissionAction } from '@supabase/shared-types/out/constants'-import { IS_PLATFORM, useFlag, useParams } from 'common'-import dayjs from 'dayjs'+import { IS_PLATFORM, useParams } from 'common'import { parseAsString, useQueryState } from 'nuqs'-import { useEffect, useMemo, useRef } from 'react'+import { useEffect, useMemo } from 'react'import { toast } from 'sonner'import { Card } from 'ui'import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'@@ -22,51 +21,8 @@ import { NoPermission } from '@/components/ui/NoPermission'import { useAPIKeyDeleteMutation } from '@/data/api-keys/api-key-delete-mutation'import type { APIKeysData } from '@/data/api-keys/api-keys-query'import { useAPIKeysQuery } from '@/data/api-keys/api-keys-query'-import { useLogsQuery } from '@/hooks/analytics/useLogsQuery'import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'-interface LastSeenData {- [hash: string]: { timestamp: number; relative: string }-}--function useLastSeen({ projectRef, enabled }: { projectRef: string; enabled?: boolean }): {- data?: LastSeenData- isLoading: boolean-} {- const now = useRef(new Date()).current-- const query = useLogsQuery({- enabled,- projectRef,- initialParams: {- iso_timestamp_start: new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(),- iso_timestamp_end: now.toISOString(),- sql: "-- last-used-secret-api-keys\nSELECT unix_millis(max(timestamp)) as timestamp, apikey.`hash` FROM edge_logs cross join unnest(metadata) as m cross join unnest(m.request) as request cross join unnest(request.sb) as sb cross join unnest(sb.apikey) as sbapikey cross join unnest(sbapikey.apikey) as apikey WHERE apikey.error is null and apikey.`hash` is not null and apikey.prefix like 'sb_secret_%' GROUP BY apikey.`hash`",- },- })-- return useMemo(() => {- if (query.isLoading || !query.logData) {- return { data: undefined, isLoading: query.isLoading }- }-- const now = dayjs()-- const lastSeen = (query.logData as unknown as { timestamp: number; hash: string }[]).reduce(- (a, i) => {- a[i.hash] = {- timestamp: i.timestamp,- relative: `${dayjs.duration(now.diff(dayjs(i.timestamp))).humanize(false)} ago`,- }- return a- },- {} as LastSeenData- )-- return { data: lastSeen, isLoading: query.isLoading }- }, [query])-}-export const SecretAPIKeys = () => {const { ref: projectRef } = useParams()const { can: canReadAPIKeys, isLoading: isLoadingPermissions } = useAsyncCheckPermissions(@@ -82,12 +38,6 @@ export const SecretAPIKeys = () => {isError: isErrorApiKeys,} = useAPIKeysQuery({ projectRef, reveal: false }, { enabled: canReadAPIKeys })- const showApiKeysLastUsed = useFlag('showApiKeysLastUsed')- const { data: lastSeen, isLoading: isLoadingLastSeen } = useLastSeen({- projectRef: projectRef ?? '',- enabled: showApiKeysLastUsed,- })-const secretApiKeys = useMemo(() =>apiKeysData?.filter(@@ -156,9 +106,6 @@ export const SecretAPIKeys = () => {<TableRow className="bg-200"><TableHead>Name</TableHead><TableHead>API Key</TableHead>- {showApiKeysLastUsed && (- <TableHead className="hidden lg:table-cell">Last Used</TableHead>- )}{IS_PLATFORM && <TableHead />}</TableRow></TableHeader>@@ -167,8 +114,6 @@ export const SecretAPIKeys = () => {<APIKeyRowkey={apiKey.id}apiKey={apiKey}- lastSeen={lastSeen?.[apiKey.hash]}- isLoadingLastSeen={isLoadingLastSeen}isDeleting={apiKeyToDelete?.id === apiKey.id && isDeletingAPIKey}onDelete={() => onDeleteAPIKey(apiKey)}setKeyToDelete={setDeleteId}diff --git a/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.tsxindex 353c449308e..21ebe784368 100644--- a/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx+++ b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx@@ -3,13 +3,14 @@ import { JwtSecretUpdateStatus } from '@supabase/shared-types/out/events'import { useFlag, useParams } from 'common'import { AlertCircle, Loader2 } from 'lucide-react'import Link from 'next/link'-import { useMemo } from 'react'+import { useMemo, useRef } from 'react'import { toast } from 'sonner'import { Input } from 'ui-patterns/DataInputs/Input'import { FormLayout } from 'ui-patterns/form/Layout/FormLayout'-import { getLastUsedAPIKeys, useLastUsedAPIKeysLogQuery } from './DisplayApiSettings.utils'+import { getLastUsedAPIKeys } from './DisplayApiSettings.utils'import Panel from '@/components/ui/Panel'+import { useApiKeysLastUsedQuery } from '@/data/analytics/api-keys-last-used-query'import { useJwtSecretUpdatingStatusQuery } from '@/data/config/jwt-secret-updating-status-query'import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'@@ -51,11 +52,20 @@ export const DisplayApiSettings = ({// api keys should not be empty. However it can be populated with a delay on project creationconst isApiKeysEmpty = apiKeys.length === 0+ const now = useRef(new Date()).currentconst showApiKeyLastUsed = useFlag('showApiKeysLastUsed')- const { isLoading: isLoadingLastUsed, logData: lastUsedLogData } = useLastUsedAPIKeysLogQuery({- projectRef: projectRef ?? '',- enabled: showApiKeyLastUsed,- })+ const {+ isLoading: isLoadingLastUsed,+ isError: isLastUsedError,+ data: lastUsedLogData,+ } = useApiKeysLastUsedQuery(+ {+ projectRef,+ isoTimestampStart: new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(),+ isoTimestampEnd: now.toISOString(),+ },+ { enabled: showApiKeyLastUsed }+ )const lastUsedAPIKeys = useMemo(() => {if (@@ -211,9 +221,11 @@ export const DisplayApiSettings = ({className="pt-2 text-foreground-lighter w-full text-sm data-[invisible=true]:invisible"data-invisible={isLoadingLastUsed}>- {lastUsedAPIKeys[x.api_key]- ? `Last request was ${lastUsedAPIKeys[x.api_key]} ago.`- : 'No requests in the past 24 hours.'}+ {isLastUsedError+ ? 'Unable to load requests from the past 24 hours.'+ : lastUsedAPIKeys[x.api_key]+ ? `Last request was ${lastUsedAPIKeys[x.api_key]} ago.`+ : 'No requests in the past 24 hours.'}</div>)}</Panel.Content>diff --git a/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.test.ts b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.test.tsnew file mode 100644index 00000000000..ac974209155--- /dev/null+++ b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.test.ts@@ -0,0 +1,73 @@+import dayjs from 'dayjs'+import duration from 'dayjs/plugin/duration'+import relativeTime from 'dayjs/plugin/relativeTime'+import { describe, expect, it } from 'vitest'++import { getLastUsedAPIKeys } from './DisplayApiSettings.utils'++// getLastUsedAPIKeys humanizes a duration, which needs both plugins.+dayjs.extend(duration)+dayjs.extend(relativeTime)++// JWT-shaped keys: `header.payload.signature`. Matching is done on the signature+// segment ([2]) via startsWith(signature_prefix).+const anonKey = { tags: 'anon', api_key: 'header.payload.anonsignature123' }+const serviceRoleKey = { tags: 'service_role', api_key: 'header.payload.servicesignature456' }++describe('getLastUsedAPIKeys', () => {+ it('returns an empty object when there are no api keys', () => {+ expect(+ getLastUsedAPIKeys([], [{ timestamp: 1, role: 'anon', signature_prefix: 'anon' }])+ ).toEqual({})+ })++ it('returns an empty object when log data is null or empty', () => {+ expect(getLastUsedAPIKeys([anonKey], null)).toEqual({})+ expect(getLastUsedAPIKeys([anonKey], undefined)).toEqual({})+ expect(getLastUsedAPIKeys([anonKey], [])).toEqual({})+ })++ it('maps a matching key to a humanized last-used duration', () => {+ const result = getLastUsedAPIKeys(+ [anonKey],+ [+ {+ timestamp: dayjs().subtract(2, 'hour').valueOf(),+ role: 'anon',+ signature_prefix: 'anonsig',+ },+ ]+ )++ expect(Object.keys(result)).toEqual([anonKey.api_key])+ expect(result[anonKey.api_key]).toContain('hours')+ })++ it('only matches the key whose role and signature prefix line up', () => {+ const result = getLastUsedAPIKeys(+ [anonKey, serviceRoleKey],+ [+ {+ timestamp: dayjs().subtract(1, 'day').valueOf(),+ role: 'service_role',+ signature_prefix: 'servicesig',+ },+ ]+ )++ expect(Object.keys(result)).toEqual([serviceRoleKey.api_key])+ expect(result[anonKey.api_key]).toBeUndefined()+ })++ it('ignores log rows missing a role or signature prefix', () => {+ const result = getLastUsedAPIKeys(+ [anonKey],+ [+ { timestamp: 1, role: 'anon' },+ { timestamp: 2, signature_prefix: 'anonsig' },+ ]+ )++ expect(result).toEqual({})+ })+})diff --git a/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.ts b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.tsindex 6281e3c6361..ac715eebad8 100644--- a/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.ts+++ b/apps/studio/components/ui/ProjectSettings/DisplayApiSettings.utils.ts@@ -1,40 +1,13 @@import dayjs from 'dayjs'-import { useRef } from 'react'-import { useLogsQuery } from '@/hooks/analytics/useLogsQuery'--export function useLastUsedAPIKeysLogQuery({- projectRef,- enabled,-}: {- projectRef: string- enabled?: boolean-}) {- const now = useRef(new Date()).current-- return useLogsQuery({- projectRef,- enabled,- initialParams: {- iso_timestamp_start: new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString(),- iso_timestamp_end: now.toISOString(),- sql: "-- last-used-anon--service_role-api-keys\nSELECT unix_millis(max(timestamp)) as timestamp, payload.role, payload.signature_prefix FROM edge_logs cross join unnest(metadata) as m cross join unnest(m.request) as request cross join unnest(request.sb) as sb cross join unnest(sb.jwt) as jwt cross join unnest(jwt.apikey) as apikey cross join unnest(apikey.payload) as payload WHERE apikey.invalid is null and payload.issuer = 'supabase' and payload.algorithm = 'HS256' and payload.role in ('anon', 'service_role') GROUP BY payload.role, payload.signature_prefix",- },- })-}+import type { ApiKeyLastUsed } from '@/data/analytics/api-keys-last-used-query'export function getLastUsedAPIKeys(apiKeys: {tags: stringapi_key: string}[],- logData:- | {- timestamp: number- role?: 'anon' | 'service_role' | string- signature_prefix?: string- }[]- | null+ logData: ApiKeyLastUsed[] | null | undefined) {if (apiKeys.length < 1 || !logData || logData.length < 1) {return {}diff --git a/apps/studio/data/analytics/api-keys-last-used-query.ts b/apps/studio/data/analytics/api-keys-last-used-query.tsnew file mode 100644index 00000000000..7bf56f96080--- /dev/null+++ b/apps/studio/data/analytics/api-keys-last-used-query.ts@@ -0,0 +1,71 @@+import { useQuery } from '@tanstack/react-query'++import { analyticsKeys } from './keys'+import { get, handleError } from '@/data/fetchers'+import { UseCustomQueryOptions } from '@/types'++export type ApiKeysLastUsedVariables = {+ projectRef?: string+ isoTimestampStart?: string+ isoTimestampEnd?: string+}++// One row per (role, signature_prefix). `timestamp` is unix millis of the most+// recent edge-log entry seen for that anon / service_role JWT api key fingerprint.+export type ApiKeyLastUsed = {+ timestamp: number+ role?: 'anon' | 'service_role' | string+ signature_prefix?: string+}++export async function getApiKeysLastUsed(+ { projectRef, isoTimestampStart, isoTimestampEnd }: ApiKeysLastUsedVariables,+ signal?: AbortSignal+) {+ if (!projectRef) {+ throw new Error('projectRef is required')+ }++ const { data, error } = await get(+ '/platform/projects/{ref}/analytics/endpoints/api_keys.last_used.otel',+ {+ params: {+ path: { ref: projectRef },+ query: {+ iso_timestamp_start: isoTimestampStart,+ iso_timestamp_end: isoTimestampEnd,+ },+ },+ signal,+ }+ )++ if (error) handleError(error)++ const response = data as { error?: string | object | null; result?: unknown[] }+ if (response?.error) {+ throw new Error(+ typeof response.error === 'string' ? response.error : 'Failed to fetch last-used API keys'+ )+ }++ return (response?.result ?? []) as ApiKeyLastUsed[]+}++export type ApiKeysLastUsedData = Awaited<ReturnType<typeof getApiKeysLastUsed>>+export type ApiKeysLastUsedError = unknown++export const useApiKeysLastUsedQuery = <TData = ApiKeysLastUsedData>(+ { projectRef, isoTimestampStart, isoTimestampEnd }: ApiKeysLastUsedVariables,+ {+ enabled = true,+ ...options+ }: UseCustomQueryOptions<ApiKeysLastUsedData, ApiKeysLastUsedError, TData> = {}+) =>+ useQuery<ApiKeysLastUsedData, ApiKeysLastUsedError, TData>({+ queryKey: analyticsKeys.apiKeysLastUsed(projectRef, { isoTimestampStart, isoTimestampEnd }),+ queryFn: ({ signal }) =>+ getApiKeysLastUsed({ projectRef, isoTimestampStart, isoTimestampEnd }, signal),+ enabled: enabled && typeof projectRef !== 'undefined',+ ...options,+ })diff --git a/apps/studio/data/analytics/keys.ts b/apps/studio/data/analytics/keys.tsindex d95f98d4da9..c0c3ac50763 100644--- a/apps/studio/data/analytics/keys.ts+++ b/apps/studio/data/analytics/keys.ts@@ -150,6 +150,17 @@ export const analyticsKeys = {databaseIdentifier,},] as const,+ apiKeysLastUsed: (+ projectRef: string | undefined,+ {+ isoTimestampStart,+ isoTimestampEnd,+ }: {+ isoTimestampStart?: string+ isoTimestampEnd?: string+ }+ ) =>+ ['projects', projectRef, 'api-keys-last-used', { isoTimestampStart, isoTimestampEnd }] as const,usageApiCounts: (projectRef: string | undefined, interval: string | undefined) =>['projects', projectRef, 'usage.api-counts', interval] as const,diff --git a/packages/api-types/types/platform.d.ts b/packages/api-types/types/platform.d.tsindex b64598a4287..7e769843178 100644--- a/packages/api-types/types/platform.d.ts+++ b/packages/api-types/types/platform.d.ts@@ -2507,6 +2507,23 @@ export interface paths {patch: operations['ProjectsRefController_updateProject']trace?: never}+ '/platform/projects/{ref}/analytics/endpoints/api_keys.last_used.otel': {+ parameters: {+ query?: never+ header?: never+ path?: never+ cookie?: never+ }+ /** Gets the project's last-used API keys */+ get: operations['ApiKeysLastUsedController_getApiKeysLastUsed']+ put?: never+ post?: never+ delete?: never+ options?: never+ head?: never+ patch?: never+ trace?: never+ }'/platform/projects/{ref}/analytics/endpoints/auth.metrics': {parameters: {query?: never@@ -22185,6 +22202,60 @@ export interface operations {}}}+ ApiKeysLastUsedController_getApiKeysLastUsed: {+ parameters: {+ query?: {+ iso_timestamp_start?: string+ iso_timestamp_end?: string+ days?: string+ }+ header?: never+ path: {+ /** @description Project ref */+ ref: string+ }+ cookie?: never+ }+ requestBody?: never+ responses: {+ 200: {+ headers: {+ [name: string]: unknown+ }+ content: {+ 'application/json': components['schemas']['AnalyticsResponse']+ }+ }+ /** @description Unauthorized */+ 401: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Forbidden action */+ 403: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Rate limit exceeded */+ 429: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Failed to get project's last-used API keys */+ 500: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ }+ }AuthMetricsController_getAuthMetrics: {parameters: {query: {