Skip to content

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

Usage of keys to support new api updates for read-only perms

Joshen Limc564c961a8ee
diff --git a/studio/components/interfaces/Settings/API/JWTSettings.tsx b/studio/components/interfaces/Settings/API/JWTSettings.tsx
index 231505492fb..d595dd1b8b7 100644
--- a/studio/components/interfaces/Settings/API/JWTSettings.tsx
+++ b/studio/components/interfaces/Settings/API/JWTSettings.tsx
@@ -53,7 +53,7 @@ const JWTSettings: FC<Props> = ({}) => {
const [isSubmittingJwtSecretUpdateRequest, setIsSubmittingJwtSecretUpdateRequest] =
useState<boolean>(false)
- const canReadJWTSecret = checkPermissions(PermissionAction.READ, 'postgrest_config')
+ const canReadJWTSecret = checkPermissions(PermissionAction.READ, 'field.jwt_secret')
const canGenerateNewJWTSecret = checkPermissions(
PermissionAction.INFRA_EXECUTE,
'queue_job.projects.update_jwt'
diff --git a/studio/lib/pingPostgrest.ts b/studio/lib/pingPostgrest.ts
index 0716abe56a3..623775327d8 100644
--- a/studio/lib/pingPostgrest.ts
+++ b/studio/lib/pingPostgrest.ts
@@ -23,8 +23,8 @@ async function pingPostgrest(
}
) {
if (projectRef === undefined) return false
- const serviceApiKey = await getServiceApiKey(projectRef, options?.timeout)
- if (serviceApiKey === undefined) return false
+ const apiKey = await getApiKey(projectRef, options?.timeout)
+ if (apiKey === undefined) return false
const { kpsVersion, timeout } = options ?? {}
const healthCheckApiEnable = semver.gte(
@@ -34,20 +34,20 @@ async function pingPostgrest(
)
if (healthCheckApiEnable) {
const healthCheckUrl = `${restUrl.replace('/rest/', '/rest-admin/')}live`
- return pingHealthCheckApi(healthCheckUrl, serviceApiKey, timeout)
+ return pingHealthCheckApi(healthCheckUrl, apiKey, timeout)
}
- return pingOpenApi(restUrl, serviceApiKey, timeout)
+ return pingOpenApi(restUrl, apiKey, timeout)
}
export default pingPostgrest
-const getServiceApiKey = async (
+const getApiKey = async (
projectRef: string,
timeout = DEFAULT_TIMEOUT_MILLISECONDS
): Promise<string | undefined> => {
const response = await getWithTimeout(`${API_URL}/props/project/${projectRef}/api`, { timeout })
if (response.error || response.autoApiService.service_api_keys.length === 0) return undefined
- return response.autoApiService.serviceApiKey
+ return response.autoApiService.defaultApiKey
}
const DEFAULT_TIMEOUT_MILLISECONDS = 2000
diff --git a/studio/pages/project/[ref]/api/index.tsx b/studio/pages/project/[ref]/api/index.tsx
index ff725ed7dc9..c9d4ebb3382 100644
--- a/studio/pages/project/[ref]/api/index.tsx
+++ b/studio/pages/project/[ref]/api/index.tsx
@@ -6,11 +6,12 @@ import { observer, useLocalObservable } from 'mobx-react-lite'
import { NextPageWithLayout } from 'types'
import { API_URL, IS_PLATFORM } from 'lib/constants'
-import { useStore } from 'hooks'
+import { checkPermissions, useStore } from 'hooks'
import { get } from 'lib/common/fetch'
import { snakeToCamel } from 'lib/helpers'
import { DocsLayout } from 'components/layouts'
import { GeneralContent, ResourceContent, RpcContent } from 'components/interfaces/Docs'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
const PageContext = createContext(null)
@@ -70,10 +71,16 @@ const DocView: FC<any> = observer(({}) => {
const [showApiKey, setShowApiKey] = useState<any>(DEFAULT_KEY)
const { data, error }: any = useSWR(`${API_URL}/props/project/${PageState.projectRef}/api`, get)
- const API_KEY = data?.autoApiService?.serviceApiKey
+ const API_KEY = data?.autoApiService?.defaultApiKey
const swaggerUrl = data?.autoApiService?.restUrl
const headers: any = { apikey: API_KEY }
+ const canReadServiceKey = checkPermissions(
+ PermissionAction.READ,
+ 'service_api_keys.service_role_key'
+ )
+ console.log('canReadServiceKey', canReadServiceKey)
+
if (API_KEY?.length > 40) headers['Authorization'] = `Bearer ${API_KEY}`
const { data: jsonSchema, error: jsonSchemaError } = useSWR(
@@ -161,10 +168,11 @@ const DocView: FC<any> = observer(({}) => {
className="text-scale-900 cursor-pointer border-none bg-transparent p-0 pl-2 pr-8 text-sm"
overlay={
<>
- <Dropdown.Item onClick={() => setShowApiKey(DEFAULT_KEY)}>
+ <Dropdown.Item key="hide" onClick={() => setShowApiKey(DEFAULT_KEY)}>
hide
</Dropdown.Item>
<Dropdown.Item
+ key="anon"
onClick={() =>
setShowApiKey({
key: autoApiService?.defaultApiKey,
@@ -174,16 +182,19 @@ const DocView: FC<any> = observer(({}) => {
>
anon (public)
</Dropdown.Item>
- <Dropdown.Item
- onClick={() =>
- setShowApiKey({
- key: autoApiService?.serviceApiKey,
- name: 'service_role (secret)',
- })
- }
- >
- service_role (secret)
- </Dropdown.Item>
+ {canReadServiceKey && (
+ <Dropdown.Item
+ key="service"
+ onClick={() =>
+ setShowApiKey({
+ key: autoApiService?.serviceApiKey,
+ name: 'service_role (secret)',
+ })
+ }
+ >
+ service_role (secret)
+ </Dropdown.Item>
+ )}
</>
}
>
diff --git a/studio/stores/pgmeta/OpenApiStore.ts b/studio/stores/pgmeta/OpenApiStore.ts
index 277e0619d24..aa1cbb90fab 100644
--- a/studio/stores/pgmeta/OpenApiStore.ts
+++ b/studio/stores/pgmeta/OpenApiStore.ts
@@ -53,7 +53,7 @@ export default class OpenApiStore implements IOpenApiStore {
const projectConfig = await get(this.url, { headers })
if (projectConfig.error) throw projectConfig.error
- const apiKey = projectConfig.autoApiService?.serviceApiKey
+ const apiKey = projectConfig.autoApiService?.defaultApiKey
const restApiUrl = projectConfig.autoApiService?.restUrl
const response = await get<OpenAPIV2.Document>(`${restApiUrl}?apikey=${apiKey}`, {