Files
supabase/apps/studio/data/vault/vault-secret-decrypted-value-query.ts
Andrew Valleteau 31aad403de fix(studio): early fail query when x-connection-encrypted is invalid (#35331)
* fix(studio): early fail query when x-connection-encrypted is invalid

* fix(studio): uniformize readDatabase and projectDetails connString handling

* chore: update api types

* chore: add connectionString null option

* fix: only enforce x-connection-encrypted on platform

* chore: refactor connString check in a single point

* chore: fix guard logic

* chore: fix pgMetaGuard

* chore: fix types
2025-05-08 12:11:03 +02:00

60 lines
1.7 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { Query } from '@supabase/pg-meta/src/query'
import { executeSql } from '../sql/execute-sql-query'
import { vaultSecretsKeys } from './keys'
export const vaultSecretDecryptedValueQuery = (id: string) => {
const sql = new Query()
.from('decrypted_secrets', 'vault')
.select('decrypted_secret')
.match({ id })
.toSql()
return sql
}
export type VaultSecretsDecryptedValueVariables = {
projectRef?: string
connectionString?: string | null
id: string
}
export const getDecryptedValue = async (
{ projectRef, connectionString, id }: VaultSecretsDecryptedValueVariables,
signal?: AbortSignal
) => {
const sql = vaultSecretDecryptedValueQuery(id)
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: vaultSecretsKeys.getDecryptedValue(projectRef, id),
},
signal
)
return result
}
export type VaultSecretsDecryptedValueData = string
export type VaultSecretsDecryptedValueError = unknown
export const useVaultSecretDecryptedValueQuery = <TData = VaultSecretsDecryptedValueData>(
{ projectRef, connectionString, id }: VaultSecretsDecryptedValueVariables,
{
enabled = true,
...options
}: UseQueryOptions<VaultSecretsDecryptedValueData, VaultSecretsDecryptedValueError, TData> = {}
) =>
useQuery<VaultSecretsDecryptedValueData, VaultSecretsDecryptedValueError, TData>(
vaultSecretsKeys.getDecryptedValue(projectRef, id),
({ signal }) => getDecryptedValue({ projectRef, connectionString, id }, signal),
{
select(data) {
return (data[0] as any).decrypted_secret
},
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)