Skip to content

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

Ensure that policy roles works in storage policy editor

Joshen Lim0a3f4ffe9a53
diff --git a/studio/components/to-be-cleaned/Storage/Storage.utils.ts b/studio/components/to-be-cleaned/Storage/Storage.utils.ts
index 3b375ce4697..2257fd6a192 100644
--- a/studio/components/to-be-cleaned/Storage/Storage.utils.ts
+++ b/studio/components/to-be-cleaned/Storage/Storage.utils.ts
@@ -87,32 +87,12 @@ export const groupPoliciesByBucket = (policies: any[]) => {
/* End: Internal methods to support formatPoliciesForStorage but exported for tests to cover */
-export const createPayloadForNewPolicy = (
- idx: number,
- bucketName: string,
- policyName: string,
- definition: string,
- operation: string,
- addSuffixToPolicyName: boolean
-) => {
- const hashedBucketName = shortHash(bucketName)
- return {
- name: addSuffixToPolicyName ? `${policyName} ${hashedBucketName}_${idx}` : policyName,
- definition: operation === 'INSERT' ? undefined : `(${definition})`,
- action: 'PERMISSIVE',
- check: operation === 'INSERT' ? `(${definition})` : undefined,
- command: operation,
- schema: 'storage',
- table: 'objects',
- }
-}
-
export const createPayloadsForAddPolicy = (
bucketName = '',
policyFormFields: any = {},
addSuffixToPolicyName = true
) => {
- const { name: policyName, definition, allowedOperations } = policyFormFields
+ const { name: policyName, definition, allowedOperations, roles } = policyFormFields
const formattedDefinition = definition.replace(/\s+/g, ' ').trim()
return allowedOperations.map((operation: any, idx: number) => {
@@ -122,11 +102,34 @@ export const createPayloadsForAddPolicy = (
policyName,
formattedDefinition,
operation,
+ roles,
addSuffixToPolicyName
)
})
}
+const createPayloadForNewPolicy = (
+ idx: number,
+ bucketName: string,
+ policyName: string,
+ definition: string,
+ operation: string,
+ roles: string[],
+ addSuffixToPolicyName: boolean
+) => {
+ const hashedBucketName = shortHash(bucketName)
+ return {
+ name: addSuffixToPolicyName ? `${policyName} ${hashedBucketName}_${idx}` : policyName,
+ definition: operation === 'INSERT' ? undefined : `(${definition})`,
+ action: 'PERMISSIVE',
+ check: operation === 'INSERT' ? `(${definition})` : undefined,
+ command: operation,
+ schema: 'storage',
+ table: 'objects',
+ roles: roles.length > 0 ? roles : undefined,
+ }
+}
+
// Used in the policy editor to highlight which library methods are allowed depending on which operations are allowed
export const deriveAllowedClientLibraryMethods = (allowedOperations = []) => {
return Object.keys(STORAGE_CLIENT_LIBRARY_MAPPINGS).filter((method) => {
@@ -145,6 +148,7 @@ export const createSQLStatementForCreatePolicy = (
policyName: string,
definition: string,
operation: string,
+ selectedRoles: string[],
addSuffixToPolicyName: boolean
) => {
const hashedBucketName = shortHash(bucketName)
@@ -152,9 +156,14 @@ export const createSQLStatementForCreatePolicy = (
? `${policyName} ${hashedBucketName}_${idx}`
: policyName
const description = `Add policy for the ${operation} operation under the policy "${policyName}"`
+ const roles = selectedRoles.length === 0 ? ['public'] : selectedRoles
+
const statement = `
- CREATE POLICY "${formattedPolicyName}" ON storage.objects FOR ${operation}
- ${operation === 'INSERT' ? 'WITH CHECK' : 'USING'} (${definition});
+ CREATE POLICY "${formattedPolicyName}"
+ ON storage.objects
+ FOR ${operation}
+ TO ${roles.join(', ')}
+ ${operation === 'INSERT' ? 'WITH CHECK' : 'USING'} (${definition});
`
.replace(/\s+/g, ' ')
.trim()
@@ -166,7 +175,7 @@ export const createSQLPolicies = (
policyFormFields: any,
addSuffixToPolicyName = true
) => {
- const { name: policyName, definition, allowedOperations } = policyFormFields
+ const { name: policyName, definition, allowedOperations, roles } = policyFormFields
const policies = allowedOperations.map((operation: any, idx: number) =>
createSQLStatementForCreatePolicy(
idx,
@@ -174,6 +183,7 @@ export const createSQLPolicies = (
policyName,
definition,
operation,
+ roles,
addSuffixToPolicyName
)
)
diff --git a/studio/components/to-be-cleaned/Storage/StoragePolicies/StoragePoliciesEditPolicyModal.js b/studio/components/to-be-cleaned/Storage/StoragePolicies/StoragePoliciesEditPolicyModal.js
index 946c01b579c..edb3db8576d 100644
--- a/studio/components/to-be-cleaned/Storage/StoragePolicies/StoragePoliciesEditPolicyModal.js
+++ b/studio/components/to-be-cleaned/Storage/StoragePolicies/StoragePoliciesEditPolicyModal.js
@@ -130,8 +130,7 @@ const StoragePoliciesEditPolicyModal = ({
message: 'You will need to allow at least one operation in your policy',
})
}
- console.log('Validate', policyFormFields)
- // [TODO] Joshen left off here to implement roles
+
const policySQLStatements = createSQLPolicies(bucketName, policyFormFields)
setPolicyStatementsForReview(policySQLStatements)
onReviewPolicy()