-
-
Notifications
You must be signed in to change notification settings - Fork 18
Refactor Cedar policy tests to remove group principal references #1660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,16 @@ import { AccessLevelEnum } from '../../enums/index.js'; | |
| import { IComplexPermission } from '../permission/permission.interface.js'; | ||
|
|
||
| export function generateCedarPolicyForGroup( | ||
| groupId: string, | ||
| connectionId: string, | ||
| isMain: boolean, | ||
| permissions: IComplexPermission, | ||
| ): string { | ||
| const policies: Array<string> = []; | ||
| const groupRef = `RocketAdmin::Group::"${groupId}"`; | ||
| const connectionRef = `RocketAdmin::Connection::"${connectionId}"`; | ||
|
Comment on lines
9
to
10
|
||
|
|
||
| if (isMain) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action,\n resource\n);`, | ||
| `permit(\n principal,\n action,\n resource\n);`, | ||
| ); | ||
| return policies.join('\n\n'); | ||
| } | ||
|
|
@@ -22,14 +20,14 @@ export function generateCedarPolicyForGroup( | |
| const connAccess = permissions.connection.accessLevel; | ||
| if (connAccess === AccessLevelEnum.edit) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"connection:read",\n resource == ${connectionRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"connection:read",\n resource == ${connectionRef}\n);`, | ||
| ); | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"connection:edit",\n resource == ${connectionRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"connection:edit",\n resource == ${connectionRef}\n);`, | ||
| ); | ||
| } else if (connAccess === AccessLevelEnum.readonly) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"connection:read",\n resource == ${connectionRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"connection:read",\n resource == ${connectionRef}\n);`, | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -38,14 +36,14 @@ export function generateCedarPolicyForGroup( | |
| const groupResourceRef = `RocketAdmin::Group::"${permissions.group.groupId}"`; | ||
| if (groupAccess === AccessLevelEnum.edit) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"group:read",\n resource == ${groupResourceRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"group:read",\n resource == ${groupResourceRef}\n);`, | ||
| ); | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"group:edit",\n resource == ${groupResourceRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"group:edit",\n resource == ${groupResourceRef}\n);`, | ||
| ); | ||
| } else if (groupAccess === AccessLevelEnum.readonly) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"group:read",\n resource == ${groupResourceRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"group:read",\n resource == ${groupResourceRef}\n);`, | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -59,32 +57,32 @@ export function generateCedarPolicyForGroup( | |
| if (access.read) { | ||
| hasReadPermission = true; | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"dashboard:read",\n resource == ${dashboardRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"dashboard:read",\n resource == ${dashboardRef}\n);`, | ||
| ); | ||
| } | ||
| if (access.create) { | ||
| hasCreatePermission = true; | ||
| } | ||
| if (access.edit) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"dashboard:edit",\n resource == ${dashboardRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"dashboard:edit",\n resource == ${dashboardRef}\n);`, | ||
| ); | ||
| } | ||
| if (access.delete) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"dashboard:delete",\n resource == ${dashboardRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"dashboard:delete",\n resource == ${dashboardRef}\n);`, | ||
| ); | ||
| } | ||
| } | ||
| const newDashboardRef = `RocketAdmin::Dashboard::"${connectionId}/__new__"`; | ||
| if (hasReadPermission) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"dashboard:read",\n resource == ${newDashboardRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"dashboard:read",\n resource == ${newDashboardRef}\n);`, | ||
| ); | ||
| } | ||
| if (hasCreatePermission) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"dashboard:create",\n resource == ${newDashboardRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"dashboard:create",\n resource == ${newDashboardRef}\n);`, | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -96,22 +94,22 @@ export function generateCedarPolicyForGroup( | |
| const hasAnyAccess = access.visibility || access.add || access.delete || access.edit; | ||
| if (hasAnyAccess) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"table:read",\n resource == ${tableRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"table:read",\n resource == ${tableRef}\n);`, | ||
| ); | ||
| } | ||
| if (access.add) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"table:add",\n resource == ${tableRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"table:add",\n resource == ${tableRef}\n);`, | ||
| ); | ||
| } | ||
| if (access.edit) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"table:edit",\n resource == ${tableRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"table:edit",\n resource == ${tableRef}\n);`, | ||
| ); | ||
| } | ||
| if (access.delete) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"table:delete",\n resource == ${tableRef}\n);`, | ||
| `permit(\n principal,\n action == RocketAdmin::Action::"table:delete",\n resource == ${tableRef}\n);`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
loadPoliciesForUsermethod fetches all groups in the connection viafindAllGroupsInConnectionand then filters in-memory by user group IDs. Since you already have theuserGroupIds, it would be more efficient to query only the user's groups directly (e.g., using theuserGroupsalready fetched in theevaluatemethod and accessing theircedarPolicyfield) instead of fetching all groups and filtering. This avoids loading unnecessary data, especially for connections with many groups.