The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
delete secrets/keys when deleting a fdw
Alaister Young1746e641c0a5
diff --git a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx b/studio/components/interfaces/Database/Wrappers/WrapperCard.tsxdeleted file mode 100644index 533629fee89..00000000000--- a/studio/components/interfaces/Database/Wrappers/WrapperCard.tsx+++ /dev/null@@ -1,101 +0,0 @@-import { PermissionAction } from '@supabase/shared-types/out/constants'-import { observer } from 'mobx-react-lite'-import { useState } from 'react'-import { IconLoader, Toggle } from 'ui'--import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmModal'-import { checkPermissions, useStore } from 'hooks'-import { Wrapper } from './Wrappers.types'-import WrapperEditor from './WrapperEditor'-import { useFDWDeleteMutation } from 'data/fdw/fdw-delete-mutation'-import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'-import Image from 'next/image'--// [Joshen TODO] No longer used, can remove after WrapperRow is working--export type WrapperCardProps = {- wrapper: Wrapper- enabled?: boolean-}--const WrapperCard = ({ wrapper, enabled = false }: WrapperCardProps) => {- const { project } = useProjectContext()- const { ui } = useStore()-- const canUpdateWrappers = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'wrappers')-- const [isEditorVisible, setIsEditorVisible] = useState(false)-- const { mutateAsync: deleteFDW, isLoading } = useFDWDeleteMutation()-- async function enableWrapper() {- setIsEditorVisible(true)- }-- async function disableWrapper() {- confirmAlert({- title: 'Confirm to disable wrapper',- message: `Are you sure you want to turn OFF "${wrapper.name}" wrapper? This will also delete ALL tables created with this wrapper.`,- onAsyncConfirm: async () => {- try {- await deleteFDW({- projectRef: project?.ref,- connectionString: project?.connectionString,- name: wrapper.name,- })- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Disabling ${wrapper.name} failed: ${error.message}`,- })- }- },- })- }-- return (- <>- <div- className={[- 'flex border-panel-border-light dark:border-panel-border-dark',- 'flex-col overflow-hidden rounded border shadow-sm',- ].join(' ')}- >- <div- className={[- 'border-panel-border-light bg-panel-header-light dark:bg-panel-header-dark',- 'flex justify-between border-b p-4 px-6 dark:border-panel-border-dark',- ].join(' ')}- >- <div className="flex items-center space-x-3">- <Image src={wrapper.icon} height={20} width={20} />- <h3- title={wrapper.label}- className="flex-1 h-5 m-0 text-base uppercase truncate text-scale-1200 capitalize"- >- {wrapper.label}- </h3>- </div>- {isLoading ? (- <IconLoader className="animate-spin" size={16} />- ) : (- <Toggle- size="tiny"- checked={enabled}- disabled={!canUpdateWrappers}- onChange={() => (enabled ? disableWrapper() : enableWrapper())}- />- )}- </div>- </div>-- <WrapperEditor- visible={isEditorVisible}- wrapper={wrapper}- onCancel={() => setIsEditorVisible(false)}- />- </>- )-}--export default observer(WrapperCard)diff --git a/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx b/studio/components/interfaces/Database/Wrappers/WrapperRow.tsxindex 29fc74949c3..7f25fd48ef8 100644--- a/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx+++ b/studio/components/interfaces/Database/Wrappers/WrapperRow.tsx@@ -57,7 +57,7 @@ const InputField: FC<{ option: any; value: any; error: any; onChange: any }> = (type={!option.hidden ? 'text' : showHidden ? 'text' : 'password'}actions={option.hidden ? (- <div className="mr-1 flex items-center justify-center">+ <div className="flex items-center justify-center mr-1"><Buttontype="default"icon={showHidden ? <IconEye /> : <IconEyeOff />}@@ -154,7 +154,7 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }await deleteFDW({projectRef: project?.ref,connectionString: project?.connectionString,- name: wrapper.name,+ wrapper,})} catch (error: any) {ui.setNotification({@@ -189,11 +189,11 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }<Collapsible.Trigger asChild><buttontype="button"- className="group flex w-full items-center justify-between rounded py-3 px-6 text-scale-1200"+ className="flex items-center justify-between w-full px-6 py-3 rounded group text-scale-1200"><div className="flex items-center gap-3"><IconChevronUp- className="text-scale-800 transition data-open-parent:rotate-0 data-closed-parent:rotate-180"+ className="transition text-scale-800 data-open-parent:rotate-0 data-closed-parent:rotate-180"strokeWidth={2}width={14}/>@@ -207,18 +207,18 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }</div><div className="flex items-center gap-3">{isLoading ? (- <div className="flex items-center gap-1 rounded-full py-1 px-1 text-xs">+ <div className="flex items-center gap-1 px-1 py-1 text-xs rounded-full"><IconLoader className="animate-spin" size={18} /></div>) : isEnabled ? (- <div className="flex items-center gap-1 rounded-full border border-brand-700 bg-brand-200 py-1 px-1 text-xs text-brand-900">+ <div className="flex items-center gap-1 px-1 py-1 text-xs border rounded-full border-brand-700 bg-brand-200 text-brand-900"><span className="rounded-full bg-brand-900 p-0.5 text-xs text-brand-200"><IconCheck strokeWidth={2} size={12} /></span><span className="px-1">Enabled</span></div>) : (- <div className="rounded-md border border-scale-500 bg-scale-100 py-1 px-3 text-xs text-scale-900 dark:border-scale-700 dark:bg-scale-300">+ <div className="px-3 py-1 text-xs border rounded-md border-scale-500 bg-scale-100 text-scale-900 dark:border-scale-700 dark:bg-scale-300">Disabled</div>)}@@ -229,8 +229,8 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }<>{isEnabled ? (<Collapsible.Content>- <div className="group border-t border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300">- <div className="max-w-lg mx-auto space-y-6 my-6">+ <div className="px-6 py-6 border-t group border-scale-500 bg-scale-100 text-scale-1200 dark:bg-scale-300">+ <div className="max-w-lg mx-auto my-6 space-y-6"><InformationBoxhideCollapsedefaultVisibility@@ -264,8 +264,8 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }</Collapsible.Content>) : (<Collapsible.Content>- <div className="group border-t border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300">- <div className="max-w-lg mx-auto space-y-6 my-6">+ <div className="px-6 py-6 border-t group border-scale-500 bg-scale-100 text-scale-1200 dark:bg-scale-300">+ <div className="max-w-lg mx-auto my-6 space-y-6">{wrapper.server.options.map((option) => (<InputFieldoption={option}@@ -288,14 +288,14 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }</div><div className="space-y-2">{newTables.length === 0 && (- <div className="border border-scale-600 px-4 py-4 rounded-md flex items-center justify-center">+ <div className="flex items-center justify-center px-4 py-4 border rounded-md border-scale-600"><p className="text-sm text-scale-1000">Add foreign tables to query from after the wrapper is enabled</p></div>)}{newTables.map((table, i) => (- <div className="border border-scale-600 px-4 py-2 rounded-md space-y-1 flex items-center justify-between">+ <div className="flex items-center justify-between px-4 py-2 space-y-1 border rounded-md border-scale-600"><div><p className="text-sm">{table.schema_name}.{table.table_name}@@ -326,7 +326,7 @@ const WrapperRow: FC<Props> = ({ wrapper, isLoading, isEnabled, isOpen, onOpen }</div>))}{newTables.length === 0 && formErrors.tables && (- <p className="text-red-900 text-sm">{formErrors.tables}</p>+ <p className="text-sm text-red-900">{formErrors.tables}</p>)}</div><div className="flex items-center justify-between !mt-8">diff --git a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx b/studio/components/interfaces/Database/Wrappers/Wrappers.tsxindex 69b34a66e90..79cba226ea3 100644--- a/studio/components/interfaces/Database/Wrappers/Wrappers.tsx+++ b/studio/components/interfaces/Database/Wrappers/Wrappers.tsx@@ -77,10 +77,10 @@ const Wrappers = () => {return (<div>- <div className="mb-6 flex items-center justify-between">+ <div className="flex items-center justify-between mb-6"><div>- <h3 className="text-scale-1200 mb-2 text-xl">Foreign Data Wrappers</h3>- <div className="text-scale-900 text-sm">+ <h3 className="mb-2 text-xl text-scale-1200">Foreign Data Wrappers</h3>+ <div className="text-sm text-scale-900">Query your data warehouse directly from your database, or third-party APIs using SQL.</div></div>@@ -121,9 +121,10 @@ const Wrappers = () => {{isWrappersEnabled ? (<div>- {WRAPPERS.map((wrapper) => {+ {WRAPPERS.map((wrapper, i) => {return (<WrapperRow+ key={i}wrapper={wrapper}isLoading={isLoading}isEnabled={enabledWrapperNamesSet.has(wrapper.server.name)}@@ -139,7 +140,7 @@ const Wrappers = () => {) : (<div><div- className="px-12 py-12 w-full bg-white dark:bg-scale-200 border border-scale-500 rounded bg-no-repeat"+ className="w-full px-12 py-12 bg-white bg-no-repeat border rounded dark:bg-scale-200 border-scale-500"style={{backgroundSize: '45%',backgroundPosition: '105% 40%',diff --git a/studio/data/fdw/fdw-delete-mutation.ts b/studio/data/fdw/fdw-delete-mutation.tsindex 854555343d4..c387d213173 100644--- a/studio/data/fdw/fdw-delete-mutation.ts+++ b/studio/data/fdw/fdw-delete-mutation.ts@@ -1,27 +1,48 @@import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import { Wrapper } from 'components/interfaces/Database/Wrappers/Wrappers.types'import { executeSql } from 'data/sql/execute-sql-query'import { sqlKeys } from 'data/sql/keys'export type FDWDeleteVariables = {projectRef?: stringconnectionString?: string- name: string+ wrapper: Wrapper}-export const getDeleteFDWSql = ({ name }: Pick<FDWDeleteVariables, 'name'>) => {+export const getDeleteFDWSql = ({ wrapper }: Pick<FDWDeleteVariables, 'wrapper'>) => {+ const encryptedOptions = wrapper.server.options.filter((option) => option.encrypted)++ const deleteEncryptedSecretsSqlArray = encryptedOptions.map((option) => {+ const key = `${wrapper.name}_${option.name}`++ return /* SQL */ `+ delete from vault.secrets where key_id = (select id from pgsodium.valid_key where name = '${key}');++ delete from pgsodium.key where name = '${key}';+ `+ })++ const deleteEncryptedSecretsSql = deleteEncryptedSecretsSqlArray.join('\n')+const sql = /* SQL */ `- drop foreign data wrapper if exists ${name} cascade;+ begin;++ drop foreign data wrapper if exists ${wrapper.name} cascade;++ ${deleteEncryptedSecretsSql}++ commit;`return sql}-export async function deleteFDW({ projectRef, connectionString, name }: FDWDeleteVariables) {+export async function deleteFDW({ projectRef, connectionString, wrapper }: FDWDeleteVariables) {if (!projectRef) {throw new Error('projectRef is required')}- const sql = getDeleteFDWSql({ name })+ const sql = getDeleteFDWSql({ wrapper })const { result } = await executeSql({ projectRef, connectionString, sql })