Files
supabase/apps/studio/data/sql/execute-sql-query.ts
Alaister Young e6006b9653 feat: user impersonation (#18945)
* feat: user impersonation

* refactor

* add anon impersonation

* add user impersonation to graphiql

* thanks Ivan for this one

* add radio buttons

* progress

* working user selector

* adds loading and error states to user selector

* nicer popover button

* default graphiql to use service role key

* styling updates

* fix padding in graphiql

* add service role warning to graphiql

* add user impersonation to realtime inspector

* add feature flag

* use Alert_Shadcn_ instead of old Alert

* Update apps/studio/lib/role-impersonation.ts

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* update title

* Update apps/studio/components/interfaces/RoleImpersonationSelector/UserImpersonationSelector.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/Icons.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update apps/studio/components/interfaces/RoleImpersonationSelector/UserImpersonationSelector.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* add constant padding at the bottom of user selector

* update graphiql buttons font size

* make rls warning dismissable

* use text-foreground on rls warning title

* fix no results while impersonating a role

* fix error line number when impersonating a role

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2023-12-04 04:45:11 +00:00

161 lines
4.5 KiB
TypeScript

import {
QueryClient,
QueryKey,
useQuery,
useQueryClient,
UseQueryOptions,
} from '@tanstack/react-query'
import md5 from 'blueimp-md5'
import { useCallback } from 'react'
import { post } from 'data/fetchers'
import {
ROLE_IMPERSONATION_NO_RESULTS,
ROLE_IMPERSONATION_SQL_LINE_COUNT,
} from 'lib/role-impersonation'
import { getRoleImpersonationStateSnapshot } from 'state/role-impersonation-state'
import { sqlKeys } from './keys'
export type Error = { code: number; message: string; requestId: string }
export type ExecuteSqlVariables = {
projectRef?: string
connectionString?: string
sql: string
queryKey?: QueryKey
handleError?: (error: { code: number; message: string; requestId: string }) => any
}
export async function executeSql(
{
projectRef,
connectionString,
sql,
queryKey,
handleError,
}: Pick<
ExecuteSqlVariables,
'projectRef' | 'connectionString' | 'sql' | 'queryKey' | 'handleError'
>,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const isRoleImpersonationEnabled = getRoleImpersonationStateSnapshot().role?.type === 'postgrest'
let { data, error } = await post('/platform/pg-meta/{ref}/query', {
signal,
params: {
header: { 'x-connection-encrypted': connectionString ?? '' },
path: { ref: projectRef },
// @ts-ignore: This is just a client side thing to identify queries better
query: { key: queryKey?.filter((seg) => typeof seg === 'string').join('-') ?? '' },
},
body: { query: sql },
headers: Object.fromEntries(headers),
})
if (error) {
if (
isRoleImpersonationEnabled &&
typeof error === 'object' &&
error !== null &&
'error' in error &&
'formattedError' in error
) {
let updatedError = error as { error: string; formattedError: string }
const regex = /LINE (\d+):/im
const [, lineNumberStr] = regex.exec(updatedError.error) ?? []
const lineNumber = Number(lineNumberStr)
if (!isNaN(lineNumber)) {
updatedError = {
...updatedError,
error: updatedError.error.replace(
regex,
`LINE ${lineNumber - ROLE_IMPERSONATION_SQL_LINE_COUNT}:`
),
formattedError: updatedError.formattedError.replace(
regex,
`LINE ${lineNumber - ROLE_IMPERSONATION_SQL_LINE_COUNT}:`
),
}
}
error = updatedError as any
}
if (handleError !== undefined) return handleError(error as any)
else throw error
}
if (
isRoleImpersonationEnabled &&
Array.isArray(data) &&
data?.[0]?.[ROLE_IMPERSONATION_NO_RESULTS] === 1
) {
return { result: [] }
}
return { result: data }
}
export type ExecuteSqlData = Awaited<ReturnType<typeof executeSql>>
export type ExecuteSqlError = unknown
export const useExecuteSqlQuery = <TData = ExecuteSqlData>(
{ projectRef, connectionString, sql, queryKey, handleError }: ExecuteSqlVariables,
{ enabled = true, ...options }: UseQueryOptions<ExecuteSqlData, ExecuteSqlError, TData> = {}
) =>
useQuery<ExecuteSqlData, ExecuteSqlError, TData>(
sqlKeys.query(projectRef, queryKey ?? [md5(sql)]),
({ signal }) =>
executeSql({ projectRef, connectionString, sql, queryKey, handleError }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)
export const prefetchExecuteSql = (
client: QueryClient,
{ projectRef, connectionString, sql, queryKey, handleError }: ExecuteSqlVariables
) => {
return client.prefetchQuery(sqlKeys.query(projectRef, queryKey ?? [md5(sql)]), ({ signal }) =>
executeSql({ projectRef, connectionString, sql, queryKey, handleError }, signal)
)
}
/**
* useExecuteSqlPrefetch is used for prefetching a SQL query. For example, starting a query loading before a page is navigated to.
*
* @example
* const prefetch = useExecuteSqlPrefetch()
*
* return (
* <Link onMouseEnter={() => prefetch({ ...args })}>
* Start loading on hover
* </Link>
* )
*/
export const useExecuteSqlPrefetch = () => {
const client = useQueryClient()
return useCallback(
({ projectRef, connectionString, sql, queryKey, handleError }: ExecuteSqlVariables) => {
if (projectRef) {
return prefetchExecuteSql(client, {
projectRef,
connectionString,
sql,
queryKey,
handleError,
})
}
return Promise.resolve()
},
[client]
)
}