Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Generate temp API keys when doing mutations for Analytics buckets (#40680)

Ivan Vasilov8ce41157a321
diff --git a/apps/studio/components/interfaces/Database/ETL/DestinationPanel/DestinationPanel.tsx b/apps/studio/components/interfaces/Database/ETL/DestinationPanel/DestinationPanel.tsx
index ac7291671b0..700d5ae77ea 100644
--- a/apps/studio/components/interfaces/Database/ETL/DestinationPanel/DestinationPanel.tsx
+++ b/apps/studio/components/interfaces/Database/ETL/DestinationPanel/DestinationPanel.tsx
@@ -113,7 +113,7 @@ export const DestinationPanel = ({
useS3AccessKeyCreateMutation()
const { mutateAsync: createNamespace, isPending: isCreatingNamespace } =
- useIcebergNamespaceCreateMutation({ projectRef })
+ useIcebergNamespaceCreateMutation()
const {
data: publications = [],
@@ -232,6 +232,7 @@ export const DestinationPanel = ({
const catalogUri = getCatalogURI(project?.ref ?? '', protocol, endpoint)
await createNamespace({
+ projectRef,
catalogUri,
warehouse: data.warehouseName!,
namespace: data.newNamespaceName,
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/TableRowComponent.tsx b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/TableRowComponent.tsx
index ffd58a652cf..7bda0f9e4b2 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/TableRowComponent.tsx
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/TableRowComponent.tsx
@@ -79,7 +79,7 @@ export const TableRowComponent = ({ table, schema, namespace }: TableRowComponen
const { mutateAsync: updateFDW } = useFDWUpdateMutation()
const { mutateAsync: dropForeignTable } = useFDWDropForeignTableMutation()
const { mutateAsync: deleteNamespaceTable, isLoading: isDeletingNamespaceTable } =
- useIcebergNamespaceTableDeleteMutation({ projectRef, onError: () => {} })
+ useIcebergNamespaceTableDeleteMutation({ onError: () => {} })
const { mutateAsync: updatePublication } = useUpdatePublicationMutation()
const { mutateAsync: startPipeline } = useStartPipelineMutation()
@@ -226,6 +226,7 @@ export const TableRowComponent = ({ table, schema, namespace }: TableRowComponen
const wrapperValues = convertKVStringArrayToJson(wrapperInstance?.server_options ?? [])
await deleteNamespaceTable({
+ projectRef,
catalogUri: wrapperValues.catalog_uri,
warehouse: wrapperValues.warehouse,
namespace: namespace,
@@ -255,6 +256,7 @@ export const TableRowComponent = ({ table, schema, namespace }: TableRowComponen
setIsRemovingTable(true)
const wrapperValues = convertKVStringArrayToJson(wrapperInstance?.server_options ?? [])
await deleteNamespaceTable({
+ projectRef,
catalogUri: wrapperValues.catalog_uri,
warehouse: wrapperValues.warehouse,
namespace: namespace,
diff --git a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/index.tsx b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/index.tsx
index 9672e9b7bf1..12c9c4ee83d 100644
--- a/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/index.tsx
+++ b/apps/studio/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/NamespaceWithTables/index.tsx
@@ -123,11 +123,9 @@ export const NamespaceWithTables = ({
const { mutateAsync: importForeignSchema, isPending: isImportingForeignSchema } =
useFDWImportForeignSchemaMutation()
- const { mutateAsync: deleteNamespace } = useIcebergNamespaceDeleteMutation({ projectRef })
+ const { mutateAsync: deleteNamespace } = useIcebergNamespaceDeleteMutation()
const { mutateAsync: dropForeignTable } = useFDWDropForeignTableMutation()
- const { mutateAsync: deleteNamespaceTable } = useIcebergNamespaceTableDeleteMutation({
- projectRef,
- })
+ const { mutateAsync: deleteNamespaceTable } = useIcebergNamespaceTableDeleteMutation()
const rescanNamespace = async () => {
if (!icebergWrapper) return console.error('Iceberg wrapper cannot be found')
@@ -194,6 +192,7 @@ export const NamespaceWithTables = ({
await Promise.all(
allTables.map((table) =>
deleteNamespaceTable({
+ projectRef,
catalogUri,
warehouse: bucketId,
namespace,
@@ -214,7 +213,7 @@ export const NamespaceWithTables = ({
)
)
- await deleteNamespace({ catalogUri, warehouse: bucketId, namespace })
+ await deleteNamespace({ projectRef, catalogUri, warehouse: bucketId, namespace })
toast.success(`Successfully deleted namespace "${namespace}"`)
setShowConfirmDeleteNamespace(false)
diff --git a/apps/studio/data/api-keys/temp-api-keys-query.ts b/apps/studio/data/api-keys/temp-api-keys-query.ts
index eed65a90814..44e821a62e6 100644
--- a/apps/studio/data/api-keys/temp-api-keys-query.ts
+++ b/apps/studio/data/api-keys/temp-api-keys-query.ts
@@ -35,12 +35,14 @@ export async function getTemporaryAPIKey(
export type TemporaryAPIKeyData = Awaited<ReturnType<typeof getTemporaryAPIKey>>
export const useTemporaryAPIKeyQuery = <TData = TemporaryAPIKeyData>(
- { projectRef, expiry = 300 }: getTemporaryAPIKeyVariables,
+ { projectRef }: { projectRef?: string },
{
enabled = true,
...options
}: UseCustomQueryOptions<TemporaryAPIKeyData, ResponseError, TData> = {}
) => {
+ // The expiry time is set to 60 seconds in the API.
+ const expiry = 60
return useQuery<TemporaryAPIKeyData, ResponseError, TData>({
queryKey: apiKeysKeys.temporary(projectRef),
queryFn: ({ signal }) => getTemporaryAPIKey({ projectRef, expiry }, signal),
diff --git a/apps/studio/data/storage/iceberg-namespace-create-mutation.ts b/apps/studio/data/storage/iceberg-namespace-create-mutation.ts
index 7a86983df7c..fc638a387ff 100644
--- a/apps/studio/data/storage/iceberg-namespace-create-mutation.ts
+++ b/apps/studio/data/storage/iceberg-namespace-create-mutation.ts
@@ -1,12 +1,13 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
+import { getOrRefreshTemporaryApiKey } from 'data/api-keys/temp-api-keys-utils'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from 'types'
import { storageKeys } from './keys'
type CreateIcebergNamespaceVariables = {
+ projectRef?: string
catalogUri: string
warehouse: string
namespace: string
@@ -15,13 +16,16 @@ type CreateIcebergNamespaceVariables = {
const errorPrefix = 'Failed to create Iceberg namespace'
async function createIcebergNamespace({
+ projectRef,
catalogUri,
warehouse,
namespace,
- tempApiKey,
-}: CreateIcebergNamespaceVariables & { tempApiKey?: string }) {
+}: CreateIcebergNamespaceVariables) {
try {
- if (!tempApiKey) throw new Error(`${errorPrefix}: API Key missing`)
+ if (!projectRef) throw new Error(`${errorPrefix}: projectRef is required`)
+
+ const tempApiKeyObj = await getOrRefreshTemporaryApiKey(projectRef)
+ const tempApiKey = tempApiKeyObj.apiKey
let headers = new Headers()
headers = await constructHeaders({
@@ -52,11 +56,10 @@ async function createIcebergNamespace({
type IcebergNamespaceCreateData = Awaited<ReturnType<typeof createIcebergNamespace>>
export const useIcebergNamespaceCreateMutation = ({
- projectRef,
onSuccess,
onError,
...options
-}: { projectRef?: string } & Omit<
+}: Omit<
UseCustomMutationOptions<
IcebergNamespaceCreateData,
ResponseError,
@@ -65,15 +68,13 @@ export const useIcebergNamespaceCreateMutation = ({
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- const { data } = useTemporaryAPIKeyQuery({ projectRef })
- const tempApiKey = data?.api_key
return useMutation<IcebergNamespaceCreateData, ResponseError, CreateIcebergNamespaceVariables>({
- mutationFn: (vars) => createIcebergNamespace({ ...vars, tempApiKey }),
+ mutationFn: (vars) => createIcebergNamespace({ ...vars }),
async onSuccess(data, variables, context) {
await queryClient.invalidateQueries({
queryKey: storageKeys.icebergNamespace({
- projectRef,
+ projectRef: variables.projectRef,
catalog: variables.catalogUri,
warehouse: variables.warehouse,
namespace: variables.namespace,
@@ -81,7 +82,7 @@ export const useIcebergNamespaceCreateMutation = ({
})
await queryClient.invalidateQueries({
queryKey: storageKeys.icebergNamespaces({
- projectRef,
+ projectRef: variables.projectRef,
catalog: variables.catalogUri,
warehouse: variables.warehouse,
}),
diff --git a/apps/studio/data/storage/iceberg-namespace-delete-mutation.ts b/apps/studio/data/storage/iceberg-namespace-delete-mutation.ts
index b22c82c6286..86bbc58faf2 100644
--- a/apps/studio/data/storage/iceberg-namespace-delete-mutation.ts
+++ b/apps/studio/data/storage/iceberg-namespace-delete-mutation.ts
@@ -1,12 +1,13 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
+import { getOrRefreshTemporaryApiKey } from 'data/api-keys/temp-api-keys-utils'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from 'types'
import { storageKeys } from './keys'
type DeleteIcebergNamespaceVariables = {
+ projectRef?: string
catalogUri: string
warehouse: string
namespace: string
@@ -15,13 +16,16 @@ type DeleteIcebergNamespaceVariables = {
const errorPrefix = 'Failed to delete Iceberg namespace'
async function deleteIcebergNamespace({
+ projectRef,
catalogUri,
warehouse,
namespace,
- tempApiKey,
-}: DeleteIcebergNamespaceVariables & { tempApiKey?: string }) {
+}: DeleteIcebergNamespaceVariables) {
try {
- if (!tempApiKey) throw new Error(`${errorPrefix}: API Key missing`)
+ if (!projectRef) throw new Error(`${errorPrefix}: projectRef is required`)
+
+ const tempApiKeyObj = await getOrRefreshTemporaryApiKey(projectRef)
+ const tempApiKey = tempApiKeyObj.apiKey
let headers = new Headers()
headers = await constructHeaders({
@@ -48,11 +52,10 @@ async function deleteIcebergNamespace({
type IcebergNamespaceDeleteData = Awaited<ReturnType<typeof deleteIcebergNamespace>>
export const useIcebergNamespaceDeleteMutation = ({
- projectRef,
onSuccess,
onError,
...options
-}: { projectRef?: string } & Omit<
+}: Omit<
UseCustomMutationOptions<
IcebergNamespaceDeleteData,
ResponseError,
@@ -61,15 +64,13 @@ export const useIcebergNamespaceDeleteMutation = ({
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- const { data } = useTemporaryAPIKeyQuery({ projectRef })
- const tempApiKey = data?.api_key
return useMutation<IcebergNamespaceDeleteData, ResponseError, DeleteIcebergNamespaceVariables>({
- mutationFn: (vars) => deleteIcebergNamespace({ ...vars, tempApiKey }),
+ mutationFn: (vars) => deleteIcebergNamespace({ ...vars }),
async onSuccess(data, variables, context) {
await queryClient.invalidateQueries({
queryKey: storageKeys.icebergNamespaces({
- projectRef,
+ projectRef: variables.projectRef,
catalog: variables.catalogUri,
warehouse: variables.warehouse,
}),
diff --git a/apps/studio/data/storage/iceberg-namespace-table-delete-mutation.ts b/apps/studio/data/storage/iceberg-namespace-table-delete-mutation.ts
index c89bc814074..23ebca37862 100644
--- a/apps/studio/data/storage/iceberg-namespace-table-delete-mutation.ts
+++ b/apps/studio/data/storage/iceberg-namespace-table-delete-mutation.ts
@@ -1,7 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
+import { getOrRefreshTemporaryApiKey } from 'data/api-keys/temp-api-keys-utils'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from 'types'
import { storageKeys } from './keys'
@@ -17,14 +17,17 @@ type DeleteIcebergNamespaceTableVariables = {
const errorPrefix = 'Failed to delete Iceberg namespace table'
async function deleteIcebergNamespaceTable({
+ projectRef,
catalogUri,
warehouse,
namespace,
table,
- tempApiKey,
-}: DeleteIcebergNamespaceTableVariables & { tempApiKey?: string }) {
+}: DeleteIcebergNamespaceTableVariables) {
try {
- if (!tempApiKey) throw new Error(`${errorPrefix}: API Key missing`)
+ if (!projectRef) throw new Error(`${errorPrefix}: projectRef is required`)
+
+ const tempApiKeyObj = await getOrRefreshTemporaryApiKey(projectRef)
+ const tempApiKey = tempApiKeyObj.apiKey
let headers = new Headers()
headers = await constructHeaders({
@@ -49,11 +52,10 @@ async function deleteIcebergNamespaceTable({
type IcebergNamespaceTableDeleteData = Awaited<ReturnType<typeof deleteIcebergNamespaceTable>>
export const useIcebergNamespaceTableDeleteMutation = ({
- projectRef,
onSuccess,
onError,
...options
-}: { projectRef?: string } & Omit<
+}: Omit<
UseCustomMutationOptions<
IcebergNamespaceTableDeleteData,
ResponseError,
@@ -62,19 +64,17 @@ export const useIcebergNamespaceTableDeleteMutation = ({
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- const { data } = useTemporaryAPIKeyQuery({ projectRef })
- const tempApiKey = data?.api_key
return useMutation<
IcebergNamespaceTableDeleteData,
ResponseError,
DeleteIcebergNamespaceTableVariables
>({
- mutationFn: (vars) => deleteIcebergNamespaceTable({ ...vars, tempApiKey }),
+ mutationFn: (vars) => deleteIcebergNamespaceTable({ ...vars }),
async onSuccess(data, variables, context) {
await queryClient.invalidateQueries({
queryKey: storageKeys.icebergNamespace({
- projectRef,
+ projectRef: variables.projectRef,
catalog: variables.catalogUri,
warehouse: variables.warehouse,
namespace: variables.namespace,
diff --git a/apps/studio/data/storage/iceberg-namespace-tables-query.ts b/apps/studio/data/storage/iceberg-namespace-tables-query.ts
index 1f05938d9ae..e5edcf0bdca 100644
--- a/apps/studio/data/storage/iceberg-namespace-tables-query.ts
+++ b/apps/studio/data/storage/iceberg-namespace-tables-query.ts
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query'
-import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
+import { getOrRefreshTemporaryApiKey } from 'data/api-keys/temp-api-keys-utils'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { storageKeys } from './keys'
@@ -15,13 +15,16 @@ type GetNamespaceTablesVariables = {
const errorPrefix = 'Failed to retrieve Iceberg namespace tables'
async function getNamespaceTables({
+ projectRef,
catalogUri,
warehouse,
namespace,
- tempApiKey,
-}: GetNamespaceTablesVariables & { tempApiKey?: string }) {
+}: GetNamespaceTablesVariables) {
try {
- if (!tempApiKey) throw new Error(`${errorPrefix}: API Key missing`)
+ if (!projectRef) throw new Error(`${errorPrefix}: projectRef is required`)
+
+ const tempApiKeyObj = await getOrRefreshTemporaryApiKey(projectRef)
+ const tempApiKey = tempApiKeyObj.apiKey
let headers = new Headers()
headers = await constructHeaders({
@@ -61,8 +64,6 @@ export const useIcebergNamespaceTablesQuery = <TData = IcebergNamespaceTablesDat
}: UseCustomQueryOptions<IcebergNamespaceTablesData, IcebergNamespaceTablesError, TData> = {}
) => {
const { projectRef, catalogUri, warehouse, namespace } = params
- const { data } = useTemporaryAPIKeyQuery({ projectRef })
- const tempApiKey = data?.api_key
return useQuery<IcebergNamespaceTablesData, IcebergNamespaceTablesError, TData>({
queryKey: storageKeys.icebergNamespaceTables({
@@ -71,11 +72,10 @@ export const useIcebergNamespaceTablesQuery = <TData = IcebergNamespaceTablesDat
namespace,
catalog: catalogUri,
}),
- queryFn: () => getNamespaceTables({ ...params, tempApiKey }),
+ queryFn: () => getNamespaceTables({ ...params }),
enabled:
enabled &&
typeof projectRef !== 'undefined' &&
- typeof tempApiKey !== 'undefined' &&
typeof warehouse !== 'undefined' &&
typeof namespace !== 'undefined' &&
typeof catalogUri !== 'undefined',
diff --git a/apps/studio/data/storage/iceberg-namespaces-query.ts b/apps/studio/data/storage/iceberg-namespaces-query.ts
index 6f311e68526..be5e4057388 100644
--- a/apps/studio/data/storage/iceberg-namespaces-query.ts
+++ b/apps/studio/data/storage/iceberg-namespaces-query.ts
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query'
-import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
+import { getOrRefreshTemporaryApiKey } from 'data/api-keys/temp-api-keys-utils'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { storageKeys } from './keys'
@@ -13,13 +13,12 @@ type GetNamespacesVariables = {
const errorPrefix = 'Failed to retrieve Iceberg namespaces'
-async function getNamespaces({
- catalogUri,
- warehouse,
- tempApiKey,
-}: GetNamespacesVariables & { tempApiKey?: string }) {
+async function getNamespaces({ projectRef, catalogUri, warehouse }: GetNamespacesVariables) {
try {
- if (!tempApiKey) throw new Error(`${errorPrefix}: API Key missing`)
+ if (!projectRef) throw new Error(`${errorPrefix}: projectRef is required`)
+
+ const tempApiKeyObj = await getOrRefreshTemporaryApiKey(projectRef)
+ const tempApiKey = tempApiKeyObj.apiKey
let headers = new Headers()
headers = await constructHeaders({
@@ -56,8 +55,6 @@ export const useIcebergNamespacesQuery = <TData = IcebergNamespacesData>(
}: UseCustomQueryOptions<IcebergNamespacesData, IcebergNamespacesError, TData> = {}
) => {
const { projectRef, catalogUri, warehouse } = params
- const { data } = useTemporaryAPIKeyQuery({ projectRef })
- const tempApiKey = data?.api_key
return useQuery<IcebergNamespacesData, IcebergNamespacesError, TData>({
queryKey: storageKeys.icebergNamespaces({
@@ -65,11 +62,10 @@ export const useIcebergNamespacesQuery = <TData = IcebergNamespacesData>(
warehouse,
catalog: catalogUri,
}),
- queryFn: () => getNamespaces({ ...params, tempApiKey }),
+ queryFn: () => getNamespaces({ ...params }),
enabled:
options &&
typeof projectRef !== 'undefined' &&
- typeof tempApiKey !== 'undefined' &&
typeof catalogUri !== 'undefined' &&
catalogUri.length > 0 &&
typeof warehouse !== 'undefined' &&
diff --git a/apps/studio/hooks/use-supabase-client-query.ts b/apps/studio/hooks/use-supabase-client-query.ts
index acada318d47..032214560bc 100644
--- a/apps/studio/hooks/use-supabase-client-query.ts
+++ b/apps/studio/hooks/use-supabase-client-query.ts
@@ -38,7 +38,7 @@ export const useSupabaseClientQuery = (
{ enabled = true, ...options } = {}
) => {
const { data: settings } = useProjectSettingsV2Query({ projectRef })
- const { data: temporaryApiKeyData } = useTemporaryAPIKeyQuery({ projectRef, expiry: 3600 })
+ const { data: temporaryApiKeyData } = useTemporaryAPIKeyQuery({ projectRef })
const endpoint = settings
? `${settings?.app_config?.protocol ?? 'https'}://${settings?.app_config?.endpoint}`