Skip to content

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

Add secrets management for edge functions in project settings

Joshen Limf0bcca649e26
diff --git a/studio/components/interfaces/Functions/EdgeFunctionSecrets/AddNewSecretModal.tsx b/studio/components/interfaces/Functions/EdgeFunctionSecrets/AddNewSecretModal.tsx
new file mode 100644
index 00000000000..10598801ade
--- /dev/null
+++ b/studio/components/interfaces/Functions/EdgeFunctionSecrets/AddNewSecretModal.tsx
@@ -0,0 +1,99 @@
+import { useParams } from 'common'
+import { useRef, useState } from 'react'
+import { Button, Form, IconEye, IconEyeOff, Input, Modal } from 'ui'
+
+import { useSecretsCreateMutation } from 'data/secrets/secrets-create-mutation'
+import { useStore } from 'hooks'
+
+interface AddNewSecretModalProps {
+ visible: boolean
+ onClose: () => void
+}
+
+const AddNewSecretModal = ({ visible, onClose }: AddNewSecretModalProps) => {
+ const { ui } = useStore()
+ const { ref: projectRef } = useParams()
+ const submitRef = useRef<HTMLButtonElement>(null)
+ const [showSecretValue, setShowSecretValue] = useState(false)
+
+ const { mutate: createSecret, isLoading: isCreating } = useSecretsCreateMutation({
+ onSuccess: (res, variables) => {
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully created new secret "${variables.secrets[0].name}"`,
+ })
+ onClose()
+ },
+ })
+
+ const validate = (values: any) => {
+ const errors: any = {}
+ if (values.name.length === 0) errors.name = 'Please provide a name for your secret'
+ if (values.value.length === 0) errors.value = 'Please provide a value for your secret'
+ console.log('validate', errors)
+ return errors
+ }
+
+ const onSubmit = (values: any) => {
+ createSecret({ projectRef, secrets: [values] })
+ }
+
+ return (
+ <Modal
+ size="medium"
+ visible={visible}
+ onCancel={onClose}
+ header="Create a new secret"
+ alignFooter="right"
+ customFooter={
+ <div className="flex items-center gap-2">
+ <Button type="default" onClick={onClose} disabled={isCreating}>
+ Cancel
+ </Button>
+ <Button
+ type="primary"
+ disabled={isCreating}
+ loading={isCreating}
+ onClick={() => submitRef?.current?.click()}
+ >
+ {isCreating ? 'Creating secret' : 'Create secret'}
+ </Button>
+ </div>
+ }
+ >
+ <Form
+ validateOnBlur
+ initialValues={{ name: '', value: '' }}
+ validate={validate}
+ onSubmit={onSubmit}
+ >
+ {() => (
+ <>
+ <Modal.Content className="py-4 space-y-3">
+ <Input id="name" label="Secret name" />
+ <Input
+ id="value"
+ label="Secret value"
+ className="input-mono"
+ type={showSecretValue ? 'text' : 'password'}
+ actions={
+ <div className="mr-1">
+ <Button
+ type="default"
+ className="px-1"
+ icon={showSecretValue ? <IconEyeOff /> : <IconEye />}
+ onClick={() => setShowSecretValue(!showSecretValue)}
+ />
+ </div>
+ }
+ />
+ </Modal.Content>
+ <button className="hidden" type="submit" ref={submitRef} />
+ </>
+ )}
+ </Form>
+ </Modal>
+ )
+}
+
+export default AddNewSecretModal
diff --git a/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecret.tsx b/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecret.tsx
new file mode 100644
index 00000000000..9dc14031078
--- /dev/null
+++ b/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecret.tsx
@@ -0,0 +1,54 @@
+import Table from 'components/to-be-cleaned/Table'
+import { ProjectSecret } from 'data/secrets/secrets-query'
+import { useState } from 'react'
+import { Button, IconEye, IconEyeOff, IconTrash, Input } from 'ui'
+
+interface EdgeFunctionSecretProps {
+ secret: ProjectSecret
+ onSelectDelete: () => void
+}
+
+const EdgeFunctionSecret = ({ secret, onSelectDelete }: EdgeFunctionSecretProps) => {
+ const [show, setShow] = useState(false)
+
+ return (
+ <Table.tr>
+ <Table.td>
+ <p className="truncate py-2 w-[250px]">{secret.name}</p>
+ </Table.td>
+ <Table.td>
+ <div className="flex items-center space-x-2">
+ <Button
+ type="text"
+ icon={
+ show ? (
+ <IconEyeOff size={16} strokeWidth={1.5} />
+ ) : (
+ <IconEye size={16} strokeWidth={1.5} />
+ )
+ }
+ className="px-1"
+ onClick={() => setShow(!show)}
+ />
+ {show ? (
+ <Input copy value={secret.value} size="small" className="font-mono w-full" />
+ ) : (
+ <p className="text-sm font-mono">••••••••••••••••••</p>
+ )}
+ </div>
+ </Table.td>
+ <Table.td>
+ <div className="flex items-center justify-end">
+ <Button
+ type="text"
+ icon={<IconTrash />}
+ className="px-1"
+ onClick={() => onSelectDelete()}
+ />
+ </div>
+ </Table.td>
+ </Table.tr>
+ )
+}
+
+export default EdgeFunctionSecret
diff --git a/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecrets.tsx b/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecrets.tsx
new file mode 100644
index 00000000000..b726f4a679e
--- /dev/null
+++ b/studio/components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecrets.tsx
@@ -0,0 +1,103 @@
+import { useParams } from 'common'
+import { useState } from 'react'
+import { Button, Modal } from 'ui'
+
+import Table from 'components/to-be-cleaned/Table'
+import AlertError from 'components/ui/AlertError'
+import ConfirmationModal from 'components/ui/ConfirmationModal'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import { useSecretsDeleteMutation } from 'data/secrets/secrets-delete-mutation'
+import { ProjectSecret, useSecretsQuery } from 'data/secrets/secrets-query'
+import { useStore } from 'hooks'
+import AddNewSecretModal from './AddNewSecretModal'
+import EdgeFunctionSecret from './EdgeFunctionSecret'
+
+const EdgeFunctionSecrets = () => {
+ const { ui } = useStore()
+ const { ref: projectRef } = useParams()
+ const [showCreateSecret, setShowCreateSecret] = useState(false)
+ const [selectedSecret, setSelectedSecret] = useState<ProjectSecret>()
+
+ const {
+ data: secrets,
+ error,
+ isLoading,
+ isSuccess,
+ isError,
+ } = useSecretsQuery({
+ projectRef: projectRef ?? '',
+ })
+
+ const { mutate: deleteSecret, isLoading: isDeleting } = useSecretsDeleteMutation({
+ onSuccess: () => {
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully deleted ${selectedSecret?.name}`,
+ })
+ setSelectedSecret(undefined)
+ },
+ })
+
+ return (
+ <>
+ <div>
+ <h3 className="mb-2 text-xl text-scale-1200">Edge Function Secrets Management</h3>
+ <div className="text-sm text-scale-900">
+ Manage the secrets for your project's edge functions
+ </div>
+ </div>
+
+ {isLoading && <GenericSkeletonLoader />}
+
+ {isError && <AlertError error={error} subject="Failed to retrieve project secrets" />}
+
+ {isSuccess && (
+ <Table
+ head={[
+ <Table.th key="secret-name" className="w-[250px]">
+ Name
+ </Table.th>,
+ <Table.th key="secret-value">Value</Table.th>,
+ <Table.th key="add-secret" className="w-[100px]">
+ <div className="flex items-center justify-end">
+ <Button onClick={() => setShowCreateSecret(true)}>Add new secret</Button>
+ </div>
+ </Table.th>,
+ ]}
+ body={secrets?.map((secret) => (
+ <EdgeFunctionSecret
+ key={secret.name}
+ secret={secret}
+ onSelectDelete={() => setSelectedSecret(secret)}
+ />
+ ))}
+ />
+ )}
+
+ <AddNewSecretModal visible={showCreateSecret} onClose={() => setShowCreateSecret(false)} />
+
+ <ConfirmationModal
+ loading={isDeleting}
+ visible={selectedSecret !== undefined}
+ buttonLabel="Delete secret"
+ buttonLoadingLabel="Deleting secret"
+ header={`Confirm to delete secret "${selectedSecret?.name}"`}
+ onSelectCancel={() => setSelectedSecret(undefined)}
+ onSelectConfirm={() => {
+ if (selectedSecret !== undefined) {
+ deleteSecret({ projectRef, secrets: [selectedSecret.name] })
+ }
+ }}
+ >
+ <Modal.Content className="py-4">
+ <p className="text-sm">
+ Before removing this secret, do ensure that none of your edge functions are currently
+ actively using this secret. This action cannot be undone.
+ </p>
+ </Modal.Content>
+ </ConfirmationModal>
+ </>
+ )
+}
+
+export default EdgeFunctionSecrets
diff --git a/studio/components/layouts/ProjectSettingsLayout/SettingsMenu.utils.ts b/studio/components/layouts/ProjectSettingsLayout/SettingsMenu.utils.ts
index dfc1e53df03..aa1601f5f4a 100644
--- a/studio/components/layouts/ProjectSettingsLayout/SettingsMenu.utils.ts
+++ b/studio/components/layouts/ProjectSettingsLayout/SettingsMenu.utils.ts
@@ -72,7 +72,7 @@ export const generateSettingsMenu = (
...(IS_PLATFORM
? [
{
- name: 'Auth',
+ name: 'Authentication',
key: 'auth',
url: isProjectBuilding ? buildingUrl : `/project/${ref}/settings/auth`,
items: [],
@@ -89,6 +89,16 @@ export const generateSettingsMenu = (
},
]
: []),
+ ...(IS_PLATFORM
+ ? [
+ {
+ name: 'Edge Functions',
+ key: 'edge-functions',
+ url: `/project/${ref}/settings/functions`,
+ items: [],
+ },
+ ]
+ : []),
],
},
diff --git a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
index b5321e00cf3..b0a07b8cac7 100644
--- a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
+++ b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
@@ -108,7 +108,7 @@ const StorageConfig = ({ config, projectRef }: any) => {
return (
<>
<div className="mb-6">
- <h3 className="mb-2 text-xl text-scale-1200">Storage settings</h3>
+ <h3 className="mb-2 text-xl text-scale-1200">Storage Settings</h3>
<div className="text-sm text-scale-900">
Configure your project's storage settings
</div>
diff --git a/studio/data/secrets/secrets-create-mutation.ts b/studio/data/secrets/secrets-create-mutation.ts
index 8500df98377..7b97c4275c2 100644
--- a/studio/data/secrets/secrets-create-mutation.ts
+++ b/studio/data/secrets/secrets-create-mutation.ts
@@ -6,11 +6,13 @@ import { ResponseError } from 'types'
import { secretsKeys } from './keys'
export type SecretsCreateVariables = {
- projectRef: string
+ projectRef?: string
secrets: { name: string; value: string }[]
}
export async function createSecrets({ projectRef, secrets }: SecretsCreateVariables) {
+ if (!projectRef) throw new Error('Project ref is required')
+
const { data, error } = await post('/v1/projects/{ref}/secrets', {
params: { path: { ref: projectRef } },
body: secrets,
diff --git a/studio/data/secrets/secrets-delete-mutation.ts b/studio/data/secrets/secrets-delete-mutation.ts
index c3a57580da1..26768ac5ef8 100644
--- a/studio/data/secrets/secrets-delete-mutation.ts
+++ b/studio/data/secrets/secrets-delete-mutation.ts
@@ -6,14 +6,16 @@ import { ResponseError } from 'types'
import { secretsKeys } from './keys'
export type SecretsDeleteVariables = {
- projectRef: string
- ids: string[]
+ projectRef?: string
+ secrets: string[]
}
-export async function deleteSecrets({ projectRef, ids }: SecretsDeleteVariables) {
+export async function deleteSecrets({ projectRef, secrets }: SecretsDeleteVariables) {
+ if (!projectRef) throw new Error('Project ref is required')
+
const { data, error } = await del('/v1/projects/{ref}/secrets', {
params: { path: { ref: projectRef } },
- body: ids,
+ body: secrets,
})
if (error) throw error
diff --git a/studio/data/secrets/secrets-query.ts b/studio/data/secrets/secrets-query.ts
index d8aaa5d31b5..0ffabb9a5dd 100644
--- a/studio/data/secrets/secrets-query.ts
+++ b/studio/data/secrets/secrets-query.ts
@@ -2,12 +2,17 @@ import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'data/fetchers'
import { secretsKeys } from './keys'
import { ResponseError } from 'types'
+import { components } from 'data/api'
export type SecretsVariables = {
- projectRef: string
+ projectRef?: string
}
+export type ProjectSecret = components['schemas']['SecretResponse']
+
export async function getSecrets({ projectRef }: SecretsVariables, signal?: AbortSignal) {
+ if (!projectRef) throw new Error('Project ref is required')
+
const { data, error } = await get(`/v1/projects/{ref}/secrets`, {
params: { path: { ref: projectRef } },
signal,
diff --git a/studio/pages/project/[ref]/settings/functions.tsx b/studio/pages/project/[ref]/settings/functions.tsx
new file mode 100644
index 00000000000..cffa8f66673
--- /dev/null
+++ b/studio/pages/project/[ref]/settings/functions.tsx
@@ -0,0 +1,15 @@
+import EdgeFunctionSecrets from 'components/interfaces/Functions/EdgeFunctionSecrets/EdgeFunctionSecrets'
+import { SettingsLayout } from 'components/layouts'
+import { NextPageWithLayout } from 'types'
+
+const PageLayout: NextPageWithLayout = () => {
+ return (
+ <div className="1xl:px-28 mx-auto flex flex-col gap-8 px-5 py-6 lg:px-16 xl:px-24 2xl:px-32">
+ <EdgeFunctionSecrets />
+ </div>
+ )
+}
+
+PageLayout.getLayout = (page) => <SettingsLayout title="Settings">{page}</SettingsLayout>
+
+export default PageLayout