Files
supabase/apps/studio/data/storage/iceberg-wrapper-create-mutation.ts
Charis 597d1ce56e fix(studio): pass caller options to grant-gated query factories (#53088)
## Context

Grant-gated query factories returned `queryFn: skipToken` when the
caller had no grant. TanStack Query keeps one options object per cache
entry, overwritten by whichever observer rendered last. When that was a
grant-less observer, invalidation refetched with `skipToken` and failed
with `Missing queryFn`, breaking the query for every other observer on
the key. #53075 patched two call sites; this fixes the factories.

## Changes

- New `grantedQueryOptions` helper (`data/granted-query-options.ts`).
Without a grant, the factory sets `queryFn: undefined` instead of
`skipToken`, so TanStack borrows the query function from a subscribed
observer that has one. The caller's `enabled` applies only when the
factory has a query function.
- `apiKeysQueryOptions`, `revealedApiKeysQueryOptions`,
`branchesQueryOptions`, `branchQueryOptions` and
`branchDiffQueryOptions` take caller options as a second argument. Call
sites change from `useQuery({ ...f(vars), enabled })` to
`useQuery(f(vars, { enabled }))`, so callers can't override the grant
gate.
- New type-aware `fga/no-spread-granted-query-options` lint rule
(error). `grantedQueryOptions` returns a type-only brand, and the rule
flags any spread or `Object.assign` of a branded value, however it was
built or named.
- Restores typed linting as a separate pass
(`eslint.type-checks.config.cjs`, run by `pnpm lint` via
`lint:type-checks`), as #44678 did. Enabling type info in the main
config changes what other plugins report, so it stays separate.
- `studio-queries` skill documents the pattern.

Callers still pass their own grant, so the UI gates on the same grant
the request carries.

## Testing

- `data/api-keys/api-keys-query.test.tsx`: a granted and a grant-less
component share the key, with the grant-less one rendering last;
invalidation via the delete mutation refetches successfully. Fails on
master with `Missing queryFn`.
- `data/granted-query-options.test.ts`: `enabled` gating, factory fields
win over caller fields, `select` type inference.
- Lint rule cases in `scripts/__tests__/fga-eslint-rules.test.ts`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-06 11:04:00 -05:00

130 lines
4.7 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useS3AccessKeyCreateMutation } from './s3-access-key-create-mutation'
import { WRAPPERS } from '@/components/interfaces/Integrations/Wrappers/Wrappers.constants'
import {
getAnalyticsBucketFDWServerName,
getAnalyticsBucketS3KeyName,
} from '@/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils'
import {
getCatalogURI,
getConnectionURL,
} from '@/components/interfaces/Storage/StorageSettings/StorageSettings.utils'
import { getKeys, revealedApiKeysQueryOptions } from '@/data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { FDWCreateVariables, useFDWCreateMutation } from '@/data/fdw/fdw-create-mutation'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useGrant } from '@/lib/grants'
type ApiKeyState = 'ready' | 'loading' | 'error' | 'missing'
function getApiKeyState({
hasApiKey,
isError,
hasData,
}: {
hasApiKey: boolean
isError: boolean
hasData: boolean
}): ApiKeyState {
if (hasApiKey) return 'ready'
if (isError) return 'error'
if (hasData) return 'missing'
return 'loading'
}
export const useIcebergWrapperCreateMutation = () => {
const { data: project } = useSelectedProjectQuery()
// The wrapper's catalog token is a secret API key, so it needs the `reveal` grant
const apiKeysGrant = useGrant(
'GET /v1/projects/{ref}/api-keys',
{ projectRef: project?.ref },
{ variant: 'reveal' }
)
const { data: apiKeysData, isError: isApiKeysError } = useQuery(
revealedApiKeysQueryOptions(
{ projectRef: project?.ref, grant: apiKeysGrant.grant },
{ select: getKeys }
)
)
const { secretKey, serviceKey } = apiKeysData ?? {}
const { data: settings } = useProjectSettingsV2Query({ projectRef: project?.ref })
const protocol = settings?.app_config?.protocol ?? 'https'
const endpoint = settings?.app_config?.storage_endpoint || settings?.app_config?.endpoint
const apiKey = secretKey?.api_key ?? serviceKey?.api_key
// Only meaningful once the grant is `granted`; before that the grant itself explains why
const apiKeyState = getApiKeyState({
hasApiKey: Boolean(apiKey),
isError: isApiKeysError,
hasData: Boolean(apiKeysData),
})
const wrapperMeta = WRAPPERS.find((wrapper) => wrapper.name === 'iceberg_wrapper')
const { can: canCreateCredentials } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.STORAGE_CONFIG_WRITE
)
const { mutateAsync: createS3AccessKey, isPending: isCreatingS3AccessKey } =
useS3AccessKeyCreateMutation()
const { mutateAsync: createFDW, isPending: isCreatingFDW } = useFDWCreateMutation()
const mutateAsync = async ({ bucketName }: { bucketName: string }) => {
// Without the keys the wrapper would be saved with a placeholder token that never works
if (apiKeysGrant.status !== 'granted') {
throw new Error(
'You need additional permissions to view API keys, which are needed to create an Iceberg wrapper.'
)
}
// Never fall back to a placeholder token: the wrapper would be created but never work
if (!apiKey) {
throw new Error(
'Unable to load a secret API key, which is needed to create an Iceberg wrapper.'
)
}
const createS3KeyData = await createS3AccessKey({
projectRef: project?.ref,
description: getAnalyticsBucketS3KeyName(bucketName),
})
const params: FDWCreateVariables = {
projectRef: project?.ref,
connectionString: project?.connectionString,
wrapperMeta: wrapperMeta!,
formState: {
server_name: getAnalyticsBucketFDWServerName(bucketName),
vault_aws_access_key_id: createS3KeyData?.access_key,
vault_aws_secret_access_key: createS3KeyData?.secret_key,
vault_token: apiKey,
warehouse: bucketName,
's3.endpoint': getConnectionURL(project?.ref ?? '', protocol, endpoint),
catalog_uri: getCatalogURI(project?.ref ?? '', protocol, endpoint),
},
mode: 'skip',
tables: [],
sourceSchema: '',
targetSchema: '',
}
await createFDW(params)
}
return {
mutateAsync,
isPending: isCreatingFDW || isCreatingS3AccessKey,
hasPermission: canCreateCredentials,
/** Callers should block creation and explain when this isn't `granted`. */
apiKeysGrant,
/** True once the grant is `granted` and a usable secret key has been revealed. */
isApiKeyReady: apiKeysGrant.status === 'granted' && apiKeyState === 'ready',
/** Progress of revealing the key, for callers that explain why creation is blocked. */
apiKeyState,
}
}