Skip to content

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

Add abac for project api keys, jwt secret, and custom postgrest config

Joshen Lim4865d6f4ab31
diff --git a/studio/components/interfaces/Settings/API/PostgrestConfig.tsx b/studio/components/interfaces/Settings/API/PostgrestConfig.tsx
index f6cc1ba8e52..5df3e8f9a85 100644
--- a/studio/components/interfaces/Settings/API/PostgrestConfig.tsx
+++ b/studio/components/interfaces/Settings/API/PostgrestConfig.tsx
@@ -4,12 +4,15 @@ import { AutoField } from 'uniforms-bootstrap4'
import { observer } from 'mobx-react-lite'
import { IconAlertCircle } from '@supabase/ui'
-import { useStore } from 'hooks'
+import { checkPermissions, useStore } from 'hooks'
import { API_URL } from 'lib/constants'
import { patch } from 'lib/common/fetch'
import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
import MultiSelect from 'components/ui/MultiSelect'
import { PageContext } from 'pages/project/[ref]/settings/api'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+// [Joshen TODO] Refactor to use supabase form component and FormPanel component
const PostgrestConfig = observer(({ config, projectRef }: any) => {
const PageState: any = useContext(PageContext)
@@ -22,6 +25,11 @@ const PostgrestConfig = observer(({ config, projectRef }: any) => {
db_extra_search_path: config.db_extra_search_path || '',
})
+ const canUpdatePostgrestConfig = checkPermissions(
+ PermissionAction.UPDATE,
+ 'custom_config_postgrest'
+ )
+
const updateConfig = async (updatedConfig: any) => {
try {
const response = await patch(
@@ -91,12 +99,18 @@ const PostgrestConfig = observer(({ config, projectRef }: any) => {
type: 'object',
}}
model={updates}
+ message={
+ !canUpdatePostgrestConfig
+ ? "You need additional permissions to update your project's API settings"
+ : undefined
+ }
onSubmit={(model: any) => updateConfig(model)}
onReset={() => setUpdates(config)}
>
<div className="space-y-6 py-4">
{schema.length >= 1 && (
<MultiSelect
+ disabled={!canUpdatePostgrestConfig}
options={schema}
// value must be passed as array of strings
value={updates.db_schema.replace(/ /g, '').split(',')}
@@ -126,8 +140,20 @@ const PostgrestConfig = observer(({ config, projectRef }: any) => {
}
/>
)}
- <AutoField name="db_extra_search_path" showInlineError errorMessage="Must be a string." />
- <AutoField name="max_rows" showInlineError errorMessage="Must be a number." />
+ <AutoField
+ disabled={!canUpdatePostgrestConfig}
+ name="db_extra_search_path"
+ showInlineError
+ errorMessage="Must be a string."
+ className={`${!canUpdatePostgrestConfig ? 'opacity-50' : ''}`}
+ />
+ <AutoField
+ disabled={!canUpdatePostgrestConfig}
+ name="max_rows"
+ showInlineError
+ errorMessage="Must be a number."
+ className={`${!canUpdatePostgrestConfig ? 'opacity-50' : ''}`}
+ />
</div>
</SchemaFormPanel>
</>
diff --git a/studio/components/interfaces/Settings/API/ServiceList.tsx b/studio/components/interfaces/Settings/API/ServiceList.tsx
index eeb0471f7d8..70b292d74e8 100644
--- a/studio/components/interfaces/Settings/API/ServiceList.tsx
+++ b/studio/components/interfaces/Settings/API/ServiceList.tsx
@@ -1,6 +1,7 @@
import useSWR from 'swr'
import { Dispatch, FC, SetStateAction, useEffect, useRef, useState } from 'react'
import { useRouter } from 'next/router'
+import * as Tooltip from '@radix-ui/react-tooltip'
import {
JwtSecretUpdateError,
JwtSecretUpdateProgress,
@@ -25,7 +26,7 @@ import {
import { API_URL } from 'lib/constants'
import { uuidv4 } from 'lib/helpers'
import { patch, get } from 'lib/common/fetch'
-import { useStore, useJwtSecretUpdateStatus } from 'hooks'
+import { useStore, useJwtSecretUpdateStatus, checkPermissions } from 'hooks'
import Panel from 'components/ui/Panel'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
@@ -35,6 +36,7 @@ import {
JWT_SECRET_UPDATE_ERROR_MESSAGES,
JWT_SECRET_UPDATE_PROGRESS_MESSAGES,
} from './API.constants'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
interface Props {
projectRef: string
@@ -84,6 +86,12 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
const previousJwtSecretUpdateStatus = useRef()
+ const canReadJWTSecret = checkPermissions(PermissionAction.READ, 'postgrest_config')
+ const canGenerateNewJWTSecret = checkPermissions(
+ PermissionAction.INFRA_EXECUTE,
+ 'queue_job.projects.update_jwt'
+ )
+
useEffect(() => {
if (previousJwtSecretUpdateStatus.current === Updating) {
switch (jwtSecretUpdateStatus) {
@@ -194,11 +202,13 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
<Input
label="JWT Secret"
readOnly
- copy={isNotUpdatingJwtSecret}
- reveal={isNotUpdatingJwtSecret}
+ copy={canReadJWTSecret && isNotUpdatingJwtSecret}
+ reveal={canReadJWTSecret && isNotUpdatingJwtSecret}
disabled
value={
- isJwtSecretUpdateFailed
+ !canReadJWTSecret
+ ? 'You need additional permissions to view the JWT secret'
+ : isJwtSecretUpdateFailed
? 'JWT secret update failed'
: isUpdatingJwtSecret
? 'Updating JWT secret...'
@@ -223,7 +233,7 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
<div className="w-full space-y-2">
<div className="flex w-full items-center justify-between">
<div className="flex flex-col space-y-1">
- <Typography.Text>Generate a new JWT secret</Typography.Text>
+ <p>Generate a new JWT secret</p>
<p className="text-sm opacity-50">
A random secret will be created, or you can create your own.
</p>
@@ -233,6 +243,32 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
<Button loading type="secondary">
Updating JWT secret...
</Button>
+ ) : !canGenerateNewJWTSecret ? (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ disabled
+ as="span"
+ type="default"
+ iconRight={<IconChevronDown />}
+ >
+ Generate a new secret
+ </Button>
+ </Tooltip.Trigger>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'bg-scale-100 rounded py-1 px-2 leading-none shadow',
+ 'border-scale-200 border',
+ ].join(' ')}
+ >
+ <span className="text-scale-1200 text-xs">
+ You need additional permissions to generate a new JWT secret
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
) : (
<Dropdown
align="end"
@@ -272,7 +308,7 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
Change tracking ID: {changeTrackingId} <br />
Error message: {jwtSecretUpdateErrorMessage}
</Alert>
- ) : (
+ ) : canGenerateNewJWTSecret ? (
<Alert
withIcon
variant="warning"
@@ -283,6 +319,8 @@ const ServiceList: FC<Props> = ({ projectRef }) => {
project will also be restarted during this process, which will terminate any
existing connections.
</Alert>
+ ) : (
+ <></>
)}
</div>
</Panel.Content>
diff --git a/studio/components/to-be-cleaned/forms/SchemaFormPanel.js b/studio/components/to-be-cleaned/forms/SchemaFormPanel.js
index 23301d11ff0..c6298df20aa 100644
--- a/studio/components/to-be-cleaned/forms/SchemaFormPanel.js
+++ b/studio/components/to-be-cleaned/forms/SchemaFormPanel.js
@@ -12,6 +12,7 @@ export default function SchemaFormPanel({
onSubmit,
submitLabel = 'Save',
cancelLabel = 'Cancel',
+ message = '',
}) {
let formRef
const [submitButtonLoading, setSubmitButtonLoading] = useState(false)
@@ -65,6 +66,7 @@ export default function SchemaFormPanel({
{submitLabel || 'Save'}
</Button>
</div>
+ <span className="text-sm text-scale-900">{message}</span>
</div>
<div className="Form section-block--body px-6 py-3">
diff --git a/studio/components/ui/MultiSelect/index.tsx b/studio/components/ui/MultiSelect/index.tsx
index a464b9a6698..9294ee716dc 100644
--- a/studio/components/ui/MultiSelect/index.tsx
+++ b/studio/components/ui/MultiSelect/index.tsx
@@ -19,6 +19,7 @@ interface Props {
searchPlaceholder?: string
descriptionText?: string | ReactNode
emptyMessage?: string | ReactNode
+ disabled?: boolean
onChange?(x: string[]): void
}
@@ -35,6 +36,7 @@ export default function MultiSelect({
placeholder,
searchPlaceholder = 'Search for option',
emptyMessage,
+ disabled,
onChange = () => {},
}: Props) {
const ref = useRef(null)
@@ -94,7 +96,7 @@ export default function MultiSelect({
}
return (
- <div className="form-group">
+ <div className={`form-group ${disabled ? 'pointer-events-none opacity-50' : ''}`}>
{label && <label>{label}</label>}
<div
className={[
diff --git a/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx b/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx
index c7aa65e11ae..140f21f907f 100644
--- a/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx
+++ b/studio/components/ui/ProjectSettings/DisplayApiSettings.tsx
@@ -84,7 +84,7 @@ const DisplayApiSettings = () => {
reveal={x.tags !== 'anon' && canReadAPIKeys && isNotUpdatingJwtSecret}
value={
!canReadAPIKeys
- ? 'You need additional permissions to read API keys'
+ ? 'You need additional permissions to view API keys'
: jwtSecretUpdateStatus === JwtSecretUpdateStatus.Failed
? 'JWT secret update failed, new API key may have issues'
: jwtSecretUpdateStatus === JwtSecretUpdateStatus.Updating