The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add ABAC for edge functions, hide UI if no read permissions
Joshen Lim3ceee5f4bda2
diff --git a/studio/components/interfaces/Functions/FunctionsListItem.tsx b/studio/components/interfaces/Functions/FunctionsListItem.tsxindex db721e96c4a..91e871ce560 100644--- a/studio/components/interfaces/Functions/FunctionsListItem.tsx+++ b/studio/components/interfaces/Functions/FunctionsListItem.tsx@@ -1,12 +1,10 @@import dayjs from 'dayjs'import { FC } from 'react'+import { useRouter } from 'next/router'import { observer } from 'mobx-react-lite'-import { IconClipboard, IconGlobe } from '@supabase/ui'-import Table from 'components/to-be-cleaned/Table'-import { useRouter } from 'next/router'import { useStore } from 'hooks'-// import _functionDropdown from './_functionDropdown'+import Table from 'components/to-be-cleaned/Table'interface Props {function: any@@ -32,10 +30,6 @@ const FunctionsListItem: FC<Props> = ({ function: item }) => {</div></Table.td><Table.td className="">- {/* <span className="text-sm text-scale-1100 flex items-center gap-1">- <IconGlobe size={14} />- HTTP Request- </span> */}<div className="text-xs text-scale-900 flex gap-2 items-center truncate"><span className="font-mono truncate hidden md:inline">{`https://${ref}.functions.supabase.co/${item.slug}`}</span><span className="font-mono truncate md:hidden">{`/${item.name}`}</span>diff --git a/studio/components/interfaces/Functions/index.ts b/studio/components/interfaces/Functions/index.tsnew file mode 100644index 00000000000..b096ff5e604--- /dev/null+++ b/studio/components/interfaces/Functions/index.ts@@ -0,0 +1,5 @@+import FunctionsListItem from './FunctionsListItem'+import FunctionsEmptyState from './FunctionsEmptyState'+import TerminalInstructions from './TerminalInstructions'++export { FunctionsListItem, FunctionsEmptyState, TerminalInstructions }diff --git a/studio/components/layouts/FunctionsLayout.tsx b/studio/components/layouts/FunctionsLayout.tsxindex f6efbe32ec9..2b30a084cc4 100644--- a/studio/components/layouts/FunctionsLayout.tsx+++ b/studio/components/layouts/FunctionsLayout.tsx@@ -1,25 +1,41 @@import Link from 'next/link'import { useRouter } from 'next/router'-import React, { useEffect } from 'react'+import { FC, useEffect, ReactNode } from 'react'import { observer } from 'mobx-react-lite'import { IconCode } from '@supabase/ui'-import { useStore, withAuth } from 'hooks'+import { checkPermissions, useStore, withAuth } from 'hooks'import FunctionsNav from '../interfaces/Functions/FunctionsNav'import BaseLayout from 'components/layouts'+import NoPermission from 'components/ui/NoPermission'+import { PermissionAction } from '@supabase/shared-types/out/constants'-const FunctionsLayout = ({ children }: { children?: React.ReactNode }) => {+interface Props {+ title?: string+ children?: ReactNode+}++const FunctionsLayout: FC<Props> = ({ title, children }) => {const { functions, ui } = useStore()const router = useRouter()const { id, ref } = router.queryuseEffect(() => {- if (ui.selectedProjectRef) {- functions.load()- }+ if (ui.selectedProjectRef) functions.load()}, [ui.selectedProjectRef])+ const canReadFunctions = checkPermissions(PermissionAction.FUNCTIONS_READ, '*')+ if (!canReadFunctions) {+ return (+ <BaseLayout title={title || 'Edge Functions'} product="Edge Functions">+ <main style={{ maxHeight: '100vh' }} className="flex-1 overflow-y-auto">+ <NoPermission isFullPage resourceText="access your project's edge functions" />+ </main>+ </BaseLayout>+ )+ }+const item = id ? functions.byId(id) : nullconst name = item?.name || ''@@ -27,7 +43,11 @@ const FunctionsLayout = ({ children }: { children?: React.ReactNode }) => {const centered = !hasFunctionsreturn (- <BaseLayout isLoading={!functions.isLoaded}>+ <BaseLayout+ isLoading={!functions.isLoaded}+ title={title || 'Edge Functions'}+ product="Edge Functions"+ >{centered ? (<><div className="mx-auto max-w-5xl py-24 px-5">diff --git a/studio/components/ui/NoPermission.tsx b/studio/components/ui/NoPermission.tsxindex 436112b4307..8e1870eb465 100644--- a/studio/components/ui/NoPermission.tsx+++ b/studio/components/ui/NoPermission.tsx@@ -36,7 +36,7 @@ const NoPermission: FC<Props> = ({ resourceText, isFullPage = false }) => {if (isFullPage) {return (<div className="h-full flex items-center justify-center">- <div className="w-[500px]">+ <div className="w-[550px]"><NoPermissionMessage /></div></div>diff --git a/studio/pages/project/[ref]/functions/[id]/details.tsx b/studio/pages/project/[ref]/functions/[id]/details.tsxindex 9c41cbc5035..455358bb6b8 100644--- a/studio/pages/project/[ref]/functions/[id]/details.tsx+++ b/studio/pages/project/[ref]/functions/[id]/details.tsx@@ -1,13 +1,15 @@+import dayjs from 'dayjs'+import { useEffect, useState } from 'react'+import { useRouter } from 'next/router'import { observer } from 'mobx-react-lite'-import { useProjectSettings, useStore } from 'hooks'+import { IconGlobe, IconTerminal } from '@supabase/ui'+import { PermissionAction } from '@supabase/shared-types/out/constants'+import { NextPageWithLayout } from 'types'+import { checkPermissions, useProjectSettings, useStore } from 'hooks'import FunctionsLayout from 'components/layouts/FunctionsLayout'import CommandRender from 'components/interfaces/Functions/CommandRender'-import { useRouter } from 'next/router'-import { useEffect, useState } from 'react'-import { IconGlobe, IconTerminal } from '@supabase/ui'-import dayjs from 'dayjs'-import { NextPageWithLayout } from 'types'+import NoPermission from 'components/ui/NoPermission'const PageLayout: NextPageWithLayout = () => {const router = useRouter()@@ -125,6 +127,11 @@ const PageLayout: NextPageWithLayout = () => {},]+ const canReadFunction = checkPermissions(PermissionAction.FUNCTIONS_READ, id as string)+ if (!canReadFunction) {+ return <NoPermission isFullPage resourceText="access this edge function's details" />+ }+return (<div className="grid gap-y-4 lg:grid-cols-2 lg:gap-x-8"><div>diff --git a/studio/pages/project/[ref]/functions/[id]/index.tsx b/studio/pages/project/[ref]/functions/[id]/index.tsxindex ca9b57173d4..d5facfd36f9 100644--- a/studio/pages/project/[ref]/functions/[id]/index.tsx+++ b/studio/pages/project/[ref]/functions/[id]/index.tsx@@ -1,20 +1,19 @@-import { observer } from 'mobx-react-lite'-import useSWR from 'swr'import dayjs from 'dayjs'-import { useState } from 'react'import { useRouter } from 'next/router'import { Button } from '@supabase/ui'+import { observer } from 'mobx-react-lite'+import { PermissionAction } from '@supabase/shared-types/out/constants'-import Panel from 'components/ui/Panel'-import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler'-+import { checkPermissions } from 'hooks'+import { ChartIntervals, NextPageWithLayout } from 'types'import { get } from 'lib/common/fetch'import { API_URL, DATE_FORMAT } from 'lib/constants'-+import Panel from 'components/ui/Panel'+import NoPermission from 'components/ui/NoPermission'+import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler'import FunctionsLayout from 'components/layouts/FunctionsLayout'-import { ChartIntervals, NextPageWithLayout } from 'types'const CHART_INTERVALS: ChartIntervals[] = [{@@ -33,8 +32,8 @@ const CHART_INTERVALS: ChartIntervals[] = [function calculateHighlightedValue(array: any, attribute: string, options?: { sum: boolean }) {if (!array) return ''- var total = 0- var count = 0+ let total = 0+ let count = 0array.forEach(function (item: any, index: number) {const _item = item[attribute]@@ -50,12 +49,12 @@ function calculateHighlightedValue(array: any, attribute: string, options?: { su}const PageLayout: NextPageWithLayout = () => {- const [interval, setInterval] = useState<string>('15min')const router = useRouter()const { ref, id } = router.query- const url = `${API_URL}/projects/${ref}/analytics/endpoints/functions.inv-stats`+ const [interval, setInterval] = useState<string>('15min')+ const url = `${API_URL}/projects/${ref}/analytics/endpoints/functions.inv-stats`const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]const { data, error }: any = useSWR(@@ -66,11 +65,9 @@ const PageLayout: NextPageWithLayout = () => {const startDate = dayjs().subtract(selectedInterval.startValue, selectedInterval.startUnit).format(DATE_FORMAT)-const endDate = dayjs().format(DATE_FORMAT)const charts: any = {}-charts.data = data?.resultconst datetimeFormat = selectedInterval.format || 'MMM D, ha'@@ -90,6 +87,11 @@ const PageLayout: NextPageWithLayout = () => {}}+ const canReadFunction = checkPermissions(PermissionAction.FUNCTIONS_READ, id as string)+ if (!canReadFunction) {+ return <NoPermission isFullPage resourceText="access this edge function" />+ }+return (<div className="space-y-6 py-2"><div className="flex flex-row items-center gap-2">diff --git a/studio/pages/project/[ref]/functions/[id]/invocations.tsx b/studio/pages/project/[ref]/functions/[id]/invocations.tsxindex f207dfc5fd2..8e45b17b1de 100644--- a/studio/pages/project/[ref]/functions/[id]/invocations.tsx+++ b/studio/pages/project/[ref]/functions/[id]/invocations.tsx@@ -1,9 +1,13 @@import React from 'react'import { useRouter } from 'next/router'import { observer } from 'mobx-react-lite'+import { PermissionAction } from '@supabase/shared-types/out/constants'++import { NextPageWithLayout } from 'types'+import { checkPermissions } from 'hooks'+import NoPermission from 'components/ui/NoPermission'import LogsPreviewer from 'components/interfaces/Settings/Logs/LogsPreviewer'import FunctionsLayout from 'components/layouts/FunctionsLayout'-import { NextPageWithLayout } from 'types'/*** Placeholder page for logs previewers until we figure out where to slot them@@ -12,6 +16,11 @@ export const LogPage: NextPageWithLayout = () => {const router = useRouter()const { ref, id } = router.query+ const canReadFunction = checkPermissions(PermissionAction.FUNCTIONS_READ, id as string)+ if (!canReadFunction) {+ return <NoPermission isFullPage resourceText="access this edge function's invocations" />+ }+return (<LogsPreviewerprojectRef={ref as string}diff --git a/studio/pages/project/[ref]/functions/[id]/logs-old.tsx b/studio/pages/project/[ref]/functions/[id]/logs-old.tsxdeleted file mode 100644index d8b0a8d1b12..00000000000--- a/studio/pages/project/[ref]/functions/[id]/logs-old.tsx+++ /dev/null@@ -1,206 +0,0 @@-import dayjs from 'dayjs'-import { useRouter } from 'next/router'-import { useState } from 'react'-import { observer } from 'mobx-react-lite'--import {- Button,- Checkbox,- IconCalendar,- IconExternalLink,- IconSearch,- Input,- Popover,- SidePanel,-} from '@supabase/ui'--import { withAuth } from 'hooks'-import Table from 'components/to-be-cleaned/Table'-import FunctionsLayout from 'components/layouts/FunctionsLayout'-import FunctionLogsItem from 'components/interfaces/Functions/FunctionLogsItem'-import { LOGS_DATA } from 'components/interfaces/Functions/Functions.data'--// [Joshen] Is this supposed to be deprecated?--const FunctionsList = () => {- const [detailVisible, setDetailVisible] = useState(false)-- const router = useRouter()- const { ref } = router.query-- const logDetails = (- <div className="grid grid-cols-12 gap-2">- <span className="col-span-3 text-sm text-scale-900 w-32">Status</span>- <div className="col-span-7">- <div className="flex">- <div className="text-sm bg-scale-500 text-scale-1200 rounded px-2">200 OK</div>- </div>- </div>-- <div className="col-span-3 text-sm text-scale-900 w-32">ID</div>- <div className="col-span-7 text-sm text-scale-1200">- <span className="">{LOGS_DATA[0].id}</span>- </div>-- <div className="col-span-3 text-sm text-scale-900 w-32">Time</div>- <div className="col-span-7 text-sm text-scale-1200">- <span className="">{dayjs(LOGS_DATA[0].created_at).format('DD MMM, YYYY HH:mm')}</span>- </div>-- <div className="col-span-3 text-sm text-scale-900 w-32">IP Address</div>- <div className="col-span-7 text-sm text-scale-1200">- {/*- // @ts-ignore */}- <span className="">{LOGS_DATA[0].ip_address}</span>- </div>-- <div className="col-span-3 text-sm text-scale-900 w-32">API Version</div>- <div className="col-span-7 text-sm text-scale-1200">- <span className="">v0.1</span>- </div>-- <div className="col-span-3 text-sm text-scale-900 w-32">Source</div>- <div className="col-span-7 text-sm text-scale-1200">- <span className="">Something</span>- </div>- </div>- )-- return (- <div className="flex flex-col gap-3">- <div className="flex items-center justify-between gap-3">- <div className="flex items-center gap-3">- <Input icon={<IconSearch size={14} />} placeholder="Filter by anything.." size="tiny" />- <div className="flex -space-x-px">- <Button- type="secondary"- style={{ borderTopRightRadius: 0, borderBottomRightRadius: 0 }}- >- Last Hour- </Button>- <Button- type="default"- icon={<IconCalendar />}- style={{ borderTopLeftRadius: 0, borderBottomLeftRadius: 0 }}- >- Custom- </Button>- </div>- <Popover- size="medium"- align="end"- portalled- showClose- side="bottom"- header={- <div className="flex justify-between items-center">- <Button size="tiny" type="default">- Clear- </Button>- <h5 className="text-sm text-scale-1200">Status</h5>- <Button type="primary">Save</Button>- </div>- }- onOpenChange={function noRefCheck() {}}- overlay={[- <>- <div className="py-6 space-y-4">- <Checkbox- className="px-3"- defaultValue="100%"- description="Show all 200 responses"- label="Succeeded"- size="medium"- />- <Popover.Seperator />- <Checkbox- className="px-3"- defaultValue="100%"- description="Show all 5XX responses"- label="Failed"- size="medium"- />- </div>- </>,- ]}- >- <Button as="span" type="default">- Status- </Button>- </Popover>- </div>- <Button- type="default"- iconRight={<IconExternalLink size={14} />}- onClick={() =>- router.push(- `http://localhost:8082/project/${ref}/logs/database?q=timestamp+%3C+TIMESTAMP_SUB%28CURRENT_TIMESTAMP%28%29%2C+INTERVAL+10+MINUTE%29&s=&te=`- )- }- >- Open in logs explorer- </Button>- </div>- <div className="grid grid-cols-2 h-full">- <div>- <Table- head={- <>- <Table.th className="w-16">Response</Table.th>- <Table.th className="w-16">Method</Table.th>- <Table.th>URL</Table.th>- <Table.th>Timestamp</Table.th>- </>- }- body={- <>- {LOGS_DATA.length > 0 &&- LOGS_DATA.map((x: any) => (- <FunctionLogsItem- key={x.id}- log={x}- onClick={() => {- setDetailVisible(true)- }}- />- ))}- </>- }- />- </div>- <div className="border-t border-l border-r px-6 py-4 space-y-6">- <h3 className="text-xl font-medium text-scale-1200 mb-4">- POST /readededeaded/functions/hello-world- </h3>- {logDetails}- <SidePanel.Seperator />- <h3 className="text-base text-scale-1200">Response body</h3>- </div>- </div>- <SidePanel- size="large"- header={'POST /readededeaded/functions/hello-world'}- visible={detailVisible}- onCancel={() => setDetailVisible(false)}- >- <div className="py-4 flex flex-col space-y-8">- <SidePanel.Content>{logDetails}</SidePanel.Content>- <SidePanel.Seperator />- <SidePanel.Content>- <h3 className="text-base text-scale-1200">Response body</h3>- </SidePanel.Content>- </div>- </SidePanel>- </div>- )-}--const PageLayout = () => {- return (- <FunctionsLayout>- <FunctionsList />- </FunctionsLayout>- )-}--export default withAuth(observer(PageLayout))diff --git a/studio/pages/project/[ref]/functions/[id]/logs.tsx b/studio/pages/project/[ref]/functions/[id]/logs.tsxindex 2e1ca4fe80b..2913da2c2d2 100644--- a/studio/pages/project/[ref]/functions/[id]/logs.tsx+++ b/studio/pages/project/[ref]/functions/[id]/logs.tsx@@ -1,17 +1,22 @@-import React from 'react'import { useRouter } from 'next/router'import { observer } from 'mobx-react-lite'+import { PermissionAction } from '@supabase/shared-types/out/constants'++import { NextPageWithLayout } from 'types'+import { checkPermissions } from 'hooks'+import NoPermission from 'components/ui/NoPermission'import LogsPreviewer from 'components/interfaces/Settings/Logs/LogsPreviewer'import FunctionsLayout from 'components/layouts/FunctionsLayout'-import { NextPageWithLayout } from 'types'-/**- * Placeholder page for logs previewers until we figure out where to slot them- */export const LogPage: NextPageWithLayout = () => {const router = useRouter()const { ref, id } = router.query+ const canReadFunction = checkPermissions(PermissionAction.FUNCTIONS_READ, id as string)+ if (!canReadFunction) {+ return <NoPermission isFullPage resourceText="access this edge function's logs" />+ }+return (<LogsPreviewerprojectRef={ref as string}diff --git a/studio/pages/project/[ref]/functions/[id]/triggers.tsx b/studio/pages/project/[ref]/functions/[id]/triggers.tsxdeleted file mode 100644index a6ef881515a..00000000000--- a/studio/pages/project/[ref]/functions/[id]/triggers.tsx+++ /dev/null@@ -1,11 +0,0 @@-import FunctionsLayout from 'components/layouts/FunctionsLayout'-import { observer } from 'mobx-react-lite'-import { NextPageWithLayout } from 'types'--const PageLayout: NextPageWithLayout = () => {- return <div>Triggers</div>-}--PageLayout.getLayout = (page) => <FunctionsLayout>{page}</FunctionsLayout>--export default observer(PageLayout)diff --git a/studio/pages/project/[ref]/functions/index.tsx b/studio/pages/project/[ref]/functions/index.tsxindex 43c89b08d8d..f8778eb09f5 100644--- a/studio/pages/project/[ref]/functions/index.tsx+++ b/studio/pages/project/[ref]/functions/index.tsx@@ -2,12 +2,15 @@ import { observer } from 'mobx-react-lite'import { NextPageWithLayout } from 'types'import { useStore } from 'hooks'-import Table from 'components/to-be-cleaned/Table'import FunctionsLayout from 'components/layouts/FunctionsLayout'-import FunctionsListItem from 'components/interfaces/Functions/FunctionsListItem'++import Table from 'components/to-be-cleaned/Table'import { Function } from 'components/interfaces/Functions/Functions.types'-import FunctionsEmptyState from 'components/interfaces/Functions/FunctionsEmptyState'-import TerminalInstructions from 'components/interfaces/Functions/TerminalInstructions'+import {+ FunctionsListItem,+ FunctionsEmptyState,+ TerminalInstructions,+} from 'components/interfaces/Functions'const FunctionsList = ({ functions }: { functions: Function[] }) => {return (