-
-
Notifications
You must be signed in to change notification settings - Fork 18
cedar permissions #1627
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
Open
Artuomka
wants to merge
6
commits into
main
Choose a base branch
from
backend_permissions_reworking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
cedar permissions #1627
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e02a474
cedar permissions
Artuomka 7a9d492
Merge branch 'main' into backend_permissions_reworking
gugu 319fb51
refactor: rework Cedar authorization service to use unified validatio…
Artuomka 68a11a9
Merge branch 'main' into backend_permissions_reworking
Artuomka 7af686b
Merge branch 'main' into backend_permissions_reworking
Artuomka 0be6ac0
Refactor test cases to ensure exceptions are thrown correctly and fix…
Artuomka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/entities/cedar-authorization/cedar-action-map.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| export enum CedarAction { | ||
| ConnectionRead = 'connection:read', | ||
| ConnectionEdit = 'connection:edit', | ||
| GroupRead = 'group:read', | ||
| GroupEdit = 'group:edit', | ||
| TableRead = 'table:read', | ||
| TableAdd = 'table:add', | ||
| TableEdit = 'table:edit', | ||
| TableDelete = 'table:delete', | ||
| } | ||
|
|
||
| export enum CedarResourceType { | ||
| Connection = 'RocketAdmin::Connection', | ||
| Group = 'RocketAdmin::Group', | ||
| Table = 'RocketAdmin::Table', | ||
| } | ||
|
|
||
| export const CEDAR_ACTION_TYPE = 'RocketAdmin::Action'; | ||
| export const CEDAR_USER_TYPE = 'RocketAdmin::User'; | ||
| export const CEDAR_GROUP_TYPE = 'RocketAdmin::Group'; | ||
|
|
||
| export interface CedarValidationRequest { | ||
| userId: string; | ||
| action: CedarAction; | ||
| connectionId?: string; | ||
| groupId?: string; | ||
| tableName?: string; | ||
| } |
9 changes: 9 additions & 0 deletions
9
backend/src/entities/cedar-authorization/cedar-authorization.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Global, Module } from '@nestjs/common'; | ||
| import { CedarAuthorizationService } from './cedar-authorization.service.js'; | ||
|
|
||
| @Global() | ||
| @Module({ | ||
| providers: [CedarAuthorizationService], | ||
| exports: [CedarAuthorizationService], | ||
| }) | ||
| export class CedarAuthorizationModule {} |
7 changes: 7 additions & 0 deletions
7
backend/src/entities/cedar-authorization/cedar-authorization.service.interface.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { CedarValidationRequest } from './cedar-action-map.js'; | ||
|
|
||
| export interface ICedarAuthorizationService { | ||
| isFeatureEnabled(): boolean; | ||
| validate(request: CedarValidationRequest): Promise<boolean>; | ||
| invalidatePolicyCacheForConnection(connectionId: string): void; | ||
| } |
154 changes: 154 additions & 0 deletions
154
backend/src/entities/cedar-authorization/cedar-authorization.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { HttpException, HttpStatus, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
| import { DataSource } from 'typeorm'; | ||
| import { BaseType } from '../../common/data-injection.tokens.js'; | ||
| import { Messages } from '../../exceptions/text/messages.js'; | ||
| import { Cacher } from '../../helpers/cache/cacher.js'; | ||
| import { GroupEntity } from '../group/group.entity.js'; | ||
| import { groupCustomRepositoryExtension } from '../group/repository/group-custom-repository-extension.js'; | ||
| import { IGroupRepository } from '../group/repository/group.repository.interface.js'; | ||
| import { UserEntity } from '../user/user.entity.js'; | ||
| import { | ||
| CedarAction, | ||
| CedarResourceType, | ||
| CedarValidationRequest, | ||
| CEDAR_ACTION_TYPE, | ||
| CEDAR_USER_TYPE, | ||
| } from './cedar-action-map.js'; | ||
| import { ICedarAuthorizationService } from './cedar-authorization.service.interface.js'; | ||
| import { buildCedarEntities } from './cedar-entity-builder.js'; | ||
| import { CEDAR_SCHEMA } from './cedar-schema.js'; | ||
|
|
||
| @Injectable() | ||
| export class CedarAuthorizationService implements ICedarAuthorizationService, OnModuleInit { | ||
| private cedarModule: typeof import('@cedar-policy/cedar-wasm/nodejs'); | ||
| private schema: Record<string, unknown>; | ||
| private groupRepository: IGroupRepository; | ||
| private readonly logger = new Logger(CedarAuthorizationService.name); | ||
|
|
||
| constructor( | ||
| @Inject(BaseType.DATA_SOURCE) | ||
| private readonly dataSource: DataSource, | ||
| ) {} | ||
|
|
||
| async onModuleInit(): Promise<void> { | ||
| if (!this.isFeatureEnabled()) return; | ||
| this.cedarModule = await import('@cedar-policy/cedar-wasm/nodejs'); | ||
| this.schema = CEDAR_SCHEMA as Record<string, unknown>; | ||
| this.groupRepository = this.dataSource.getRepository(GroupEntity).extend(groupCustomRepositoryExtension); | ||
| this.logger.log('Cedar authorization service initialized'); | ||
| } | ||
|
|
||
| isFeatureEnabled(): boolean { | ||
| return process.env.CEDAR_AUTHORIZATION_ENABLED === 'true'; | ||
| } | ||
|
|
||
| async validate(request: CedarValidationRequest): Promise<boolean> { | ||
| const { userId, action, groupId, tableName } = request; | ||
| let { connectionId } = request; | ||
|
|
||
| const actionPrefix = action.split(':')[0]; | ||
| let resourceType: CedarResourceType; | ||
| let resourceId: string; | ||
|
|
||
| switch (actionPrefix) { | ||
| case 'connection': | ||
| resourceType = CedarResourceType.Connection; | ||
| resourceId = connectionId; | ||
| break; | ||
| case 'group': | ||
| resourceType = CedarResourceType.Group; | ||
| connectionId = await this.getConnectionIdForGroup(groupId); | ||
| if (!connectionId) return false; | ||
| resourceId = groupId; | ||
| break; | ||
| case 'table': | ||
| resourceType = CedarResourceType.Table; | ||
| resourceId = `${connectionId}/${tableName}`; | ||
| break; | ||
| default: | ||
| return false; | ||
| } | ||
|
|
||
| return this.evaluate(userId, connectionId, action, resourceType, resourceId, tableName); | ||
| } | ||
|
|
||
| invalidatePolicyCacheForConnection(connectionId: string): void { | ||
| Cacher.invalidateCedarPolicyCache(connectionId); | ||
| } | ||
|
|
||
| private async evaluate( | ||
| userId: string, | ||
| connectionId: string, | ||
| action: CedarAction, | ||
| resourceType: CedarResourceType, | ||
| resourceId: string, | ||
| tableName?: string, | ||
| ): Promise<boolean> { | ||
| await this.assertUserNotSuspended(userId); | ||
|
|
||
| const userGroups = await this.groupRepository.findAllUserGroupsInConnection(connectionId, userId); | ||
| if (userGroups.length === 0) return false; | ||
|
|
||
| const policies = await this.loadPoliciesForConnection(connectionId); | ||
| if (!policies) return false; | ||
|
|
||
| const entities = buildCedarEntities(userId, userGroups, connectionId, tableName); | ||
|
|
||
| const call = { | ||
| principal: { type: CEDAR_USER_TYPE, id: userId }, | ||
| action: { type: CEDAR_ACTION_TYPE, id: action }, | ||
| resource: { type: resourceType as string, id: resourceId }, | ||
| context: {}, | ||
| policies: { staticPolicies: policies }, | ||
| entities: entities, | ||
| schema: this.schema, | ||
| }; | ||
|
|
||
| const result = this.cedarModule.isAuthorized(call as Parameters<typeof this.cedarModule.isAuthorized>[0]); | ||
| if (result.type === 'success') { | ||
| return result.response.decision === 'allow'; | ||
| } | ||
|
|
||
| this.logger.warn(`Cedar authorization error: ${JSON.stringify(result.errors)}`); | ||
| return false; | ||
| } | ||
|
|
||
| private async loadPoliciesForConnection(connectionId: string): Promise<string | null> { | ||
| const cached = Cacher.getCedarPolicyCache(connectionId); | ||
| if (cached !== null) return cached; | ||
|
|
||
| const groups = await this.groupRepository.findAllGroupsInConnection(connectionId); | ||
| const policyTexts = groups.map((g) => g.cedarPolicy).filter(Boolean); | ||
|
|
||
| if (policyTexts.length === 0) return null; | ||
|
|
||
| const combined = policyTexts.join('\n\n'); | ||
| Cacher.setCedarPolicyCache(connectionId, combined); | ||
| return combined; | ||
| } | ||
|
|
||
| private async assertUserNotSuspended(userId: string): Promise<void> { | ||
| const user = await this.dataSource.getRepository(UserEntity).findOne({ | ||
| where: { id: userId }, | ||
| select: ['id', 'suspended'], | ||
| }); | ||
| if (user?.suspended) { | ||
| throw new HttpException( | ||
| { | ||
| message: Messages.ACCOUNT_SUSPENDED, | ||
| }, | ||
| HttpStatus.FORBIDDEN, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private async getConnectionIdForGroup(groupId: string): Promise<string | null> { | ||
| const group = await this.dataSource | ||
| .getRepository(GroupEntity) | ||
| .createQueryBuilder('group') | ||
| .leftJoinAndSelect('group.connection', 'connection') | ||
| .where('group.id = :groupId', { groupId }) | ||
| .getOne(); | ||
| return group?.connection?.id ?? null; | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
backend/src/entities/cedar-authorization/cedar-entity-builder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { GroupEntity } from '../group/group.entity.js'; | ||
|
|
||
| export interface CedarEntityRecord { | ||
| uid: { type: string; id: string }; | ||
| attrs: Record<string, unknown>; | ||
| parents: Array<{ type: string; id: string }>; | ||
| } | ||
|
|
||
| export function buildCedarEntities( | ||
| userId: string, | ||
| userGroups: Array<GroupEntity>, | ||
| connectionId: string, | ||
| tableName?: string, | ||
| ): Array<CedarEntityRecord> { | ||
| const entities: Array<CedarEntityRecord> = []; | ||
|
|
||
| // User entity with group memberships | ||
| entities.push({ | ||
| uid: { type: 'RocketAdmin::User', id: userId }, | ||
| attrs: { suspended: false }, | ||
| parents: userGroups.map((g) => ({ type: 'RocketAdmin::Group', id: g.id })), | ||
| }); | ||
|
|
||
| // Group entities | ||
| for (const group of userGroups) { | ||
| entities.push({ | ||
| uid: { type: 'RocketAdmin::Group', id: group.id }, | ||
| attrs: { | ||
| isMain: group.isMain, | ||
| connectionId: connectionId, | ||
| }, | ||
| parents: [], | ||
| }); | ||
| } | ||
|
|
||
| // Connection entity | ||
| entities.push({ | ||
| uid: { type: 'RocketAdmin::Connection', id: connectionId }, | ||
| attrs: {}, | ||
| parents: [], | ||
| }); | ||
|
|
||
| // Table entity (if table-level check) | ||
| if (tableName) { | ||
| entities.push({ | ||
| uid: { type: 'RocketAdmin::Table', id: `${connectionId}/${tableName}` }, | ||
| attrs: { connectionId: connectionId }, | ||
| parents: [{ type: 'RocketAdmin::Connection', id: connectionId }], | ||
| }); | ||
| } | ||
|
|
||
| return entities; | ||
| } |
81 changes: 81 additions & 0 deletions
81
backend/src/entities/cedar-authorization/cedar-policy-generator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| 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}"`; | ||
|
|
||
| if (isMain) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action,\n resource\n);`, | ||
| ); | ||
| return policies.join('\n\n'); | ||
| } | ||
|
|
||
| // Connection permissions | ||
| 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);`, | ||
| ); | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\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);`, | ||
| ); | ||
| } | ||
|
|
||
| // Group permissions | ||
| const groupAccess = permissions.group.accessLevel; | ||
| 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);`, | ||
| ); | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\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);`, | ||
| ); | ||
| } | ||
|
|
||
| // Table permissions | ||
| for (const table of permissions.tables) { | ||
| const tableRef = `RocketAdmin::Table::"${connectionId}/${table.tableName}"`; | ||
| const access = table.accessLevel; | ||
|
|
||
| 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);`, | ||
| ); | ||
| } | ||
| if (access.add) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\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);`, | ||
| ); | ||
| } | ||
| if (access.delete) { | ||
| policies.push( | ||
| `permit(\n principal in ${groupRef},\n action == RocketAdmin::Action::"table:delete",\n resource == ${tableRef}\n);`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return policies.join('\n\n'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
evaluate()returnsfalsewhenloadPoliciesForConnection()returns null/empty or when Cedar returns a non-success result. In the guards, afalsereturn becomes a hard 403 and does not trigger the intended legacy fallback (fallback only happens on thrown exceptions). This can lock out users if Cedar is enabled but policies haven’t been backfilled yet, or if Cedar returns structured errors instead of throwing. Consider throwing on Cedar evaluation errors / missing policy set (or returning a tri-state like {allowed, shouldFallback}) so the guards can correctly fall back to legacy auth when Cedar can’t make a decision.