-
-
Notifications
You must be signed in to change notification settings - Fork 18
Backend extend cedar permissions #1658
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ae7a6f0
Add end-to-end tests for saving Cedar policies in the SaaS application
Artuomka b7cd8b8
Implement Cedar policy reference validation and corresponding error m…
Artuomka a916066
Refactor permit statement extraction to handle nested parentheses and…
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
88 changes: 88 additions & 0 deletions
88
backend/src/entities/cedar-authorization/cedar-authorization.controller.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,88 @@ | ||
| import { | ||
| Body, | ||
| Controller, | ||
| Get, | ||
| HttpException, | ||
| HttpStatus, | ||
| Injectable, | ||
| Post, | ||
| UseGuards, | ||
| UseInterceptors, | ||
| } from '@nestjs/common'; | ||
| import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
| import { SlugUuid } from '../../decorators/index.js'; | ||
| import { Timeout } from '../../decorators/timeout.decorator.js'; | ||
| import { Messages } from '../../exceptions/text/messages.js'; | ||
| import { ConnectionEditGuard } from '../../guards/connection-edit.guard.js'; | ||
| import { ConnectionReadGuard } from '../../guards/connection-read.guard.js'; | ||
| import { SentryInterceptor } from '../../interceptors/index.js'; | ||
| import { IComplexPermission } from '../permission/permission.interface.js'; | ||
| import { CedarAuthorizationService } from './cedar-authorization.service.js'; | ||
| import { SaveCedarPolicyDto } from './dto/save-cedar-policy.dto.js'; | ||
| import { ValidateCedarSchemaDto } from './dto/validate-cedar-schema.dto.js'; | ||
|
|
||
| @UseInterceptors(SentryInterceptor) | ||
| @Timeout() | ||
| @Controller() | ||
| @ApiBearerAuth() | ||
| @ApiTags('Cedar Authorization') | ||
| @Injectable() | ||
| export class CedarAuthorizationController { | ||
| constructor(private readonly cedarAuthService: CedarAuthorizationService) {} | ||
|
|
||
| @ApiOperation({ summary: 'Get the current cedar schema used for authorization' }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'Cedar schema returned.', | ||
| }) | ||
| @ApiParam({ name: 'connectionId', required: true }) | ||
| @UseGuards(ConnectionReadGuard) | ||
| @Get('/connection/cedar-schema/:connectionId') | ||
| async getCedarSchema( | ||
| @SlugUuid('connectionId') connectionId: string, | ||
| ): Promise<{ cedarSchema: Record<string, unknown> }> { | ||
| if (!connectionId) { | ||
| throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); | ||
| } | ||
| return { cedarSchema: this.cedarAuthService.getSchema() }; | ||
| } | ||
|
|
||
| @ApiOperation({ summary: 'Validate a cedar schema against the Cedar engine' }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'Cedar schema is valid.', | ||
| }) | ||
| @ApiBody({ type: ValidateCedarSchemaDto }) | ||
| @ApiParam({ name: 'connectionId', required: true }) | ||
| @UseGuards(ConnectionReadGuard) | ||
| @Post('/connection/cedar-schema/validate/:connectionId') | ||
| async validateCedarSchema( | ||
| @SlugUuid('connectionId') connectionId: string, | ||
| @Body() dto: ValidateCedarSchemaDto, | ||
| ): Promise<{ valid: boolean }> { | ||
| if (!connectionId) { | ||
| throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); | ||
| } | ||
| this.cedarAuthService.validateCedarSchema(dto.cedarSchema); | ||
| return { valid: true }; | ||
| } | ||
|
|
||
| @ApiOperation({ summary: 'Save a cedar policy for a group, generating classical permissions for backward compatibility' }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'Cedar policy saved and classical permissions generated.', | ||
| }) | ||
| @ApiBody({ type: SaveCedarPolicyDto }) | ||
| @ApiParam({ name: 'connectionId', required: true }) | ||
| @UseGuards(ConnectionEditGuard) | ||
| @Post('/connection/cedar-policy/:connectionId') | ||
| async saveCedarPolicy( | ||
| @SlugUuid('connectionId') connectionId: string, | ||
| @Body() dto: SaveCedarPolicyDto, | ||
| ): Promise<{ cedarPolicy: string; classicalPermissions: IComplexPermission }> { | ||
| if (!connectionId) { | ||
| throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); | ||
| } | ||
| return this.cedarAuthService.saveCedarPolicy(connectionId, dto.groupId, dto.cedarPolicy); | ||
| } | ||
| } | ||
31 changes: 28 additions & 3 deletions
31
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 |
|---|---|---|
| @@ -1,9 +1,34 @@ | ||
| import { Global, Module } from '@nestjs/common'; | ||
| import { Global, MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
| import { AuthMiddleware } from '../../authorization/auth.middleware.js'; | ||
| import { GlobalDatabaseContext } from '../../common/application/global-database-context.js'; | ||
| import { BaseType } from '../../common/data-injection.tokens.js'; | ||
| import { LogOutEntity } from '../log-out/log-out.entity.js'; | ||
| import { UserEntity } from '../user/user.entity.js'; | ||
| import { CedarAuthorizationController } from './cedar-authorization.controller.js'; | ||
| import { CedarAuthorizationService } from './cedar-authorization.service.js'; | ||
|
|
||
| @Global() | ||
| @Module({ | ||
| providers: [CedarAuthorizationService], | ||
| imports: [TypeOrmModule.forFeature([UserEntity, LogOutEntity])], | ||
| providers: [ | ||
| { | ||
| provide: BaseType.GLOBAL_DB_CONTEXT, | ||
| useClass: GlobalDatabaseContext, | ||
| }, | ||
| CedarAuthorizationService, | ||
| ], | ||
| controllers: [CedarAuthorizationController], | ||
| exports: [CedarAuthorizationService], | ||
| }) | ||
| export class CedarAuthorizationModule {} | ||
| export class CedarAuthorizationModule implements NestModule { | ||
| public configure(consumer: MiddlewareConsumer): void { | ||
| consumer | ||
| .apply(AuthMiddleware) | ||
| .forRoutes( | ||
| { path: '/connection/cedar-schema/:connectionId', method: RequestMethod.GET }, | ||
| { path: '/connection/cedar-schema/validate/:connectionId', method: RequestMethod.POST }, | ||
| { path: '/connection/cedar-policy/:connectionId', method: RequestMethod.POST }, | ||
| ); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
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 |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| import { IComplexPermission } from '../permission/permission.interface.js'; | ||
| import { CedarValidationRequest } from './cedar-action-map.js'; | ||
|
|
||
| export interface ICedarAuthorizationService { | ||
| isFeatureEnabled(): boolean; | ||
| validate(request: CedarValidationRequest): Promise<boolean>; | ||
| invalidatePolicyCacheForConnection(connectionId: string): void; | ||
| getSchema(): Record<string, unknown>; | ||
| validateCedarSchema(schema: Record<string, unknown>): void; | ||
| saveCedarPolicy( | ||
| connectionId: string, | ||
| groupId: string, | ||
| cedarPolicy: string, | ||
| ): Promise<{ cedarPolicy: string; classicalPermissions: IComplexPermission }>; | ||
| } |
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.
Swagger decorators declare
@ApiResponse({ status: 200 })for POST endpoints (/connection/cedar-schema/validate/:connectionIdand/connection/cedar-policy/:connectionId), but Nest will respond with 201 by default for@Post()unless@HttpCode(200)is set. Update the documented status to 201 or set an explicit HttpCode to keep the OpenAPI spec consistent with actual responses (the e2e tests assert 201 for save policy).