-
-
Notifications
You must be signed in to change notification settings - Fork 18
permissions: backend-driven permission catalog endpoint #1799
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
gugu
wants to merge
5
commits into
main
Choose a base branch
from
feature/permissions-catalog-endpoint
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ed31bc
permissions: backend-driven permission catalog endpoint
gugu 02c4786
permissions: simplify catalog endpoint to pure CEDAR_SCHEMA passthrough
gugu 15390c4
permissions: flatten catalog response and derive groups on frontend
gugu e4d2057
Merge branch 'main' into feature/permissions-catalog-endpoint
gugu f86d75d
app.component.spec: isolate session-restoration setTimeout from cross…
gugu 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
14 changes: 14 additions & 0 deletions
14
backend/src/entities/permission/application/data-structures/available-permissions.ds.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,14 @@ | ||
| import { ApiProperty } from '@nestjs/swagger'; | ||
|
|
||
| export class AvailablePermissionDs { | ||
| @ApiProperty() | ||
| value: string; | ||
|
|
||
| @ApiProperty({ required: false }) | ||
| resource?: string; | ||
| } | ||
|
|
||
| export class AvailablePermissionsResponseDs { | ||
| @ApiProperty({ isArray: true, type: AvailablePermissionDs }) | ||
| actions: Array<AvailablePermissionDs>; | ||
| } |
30 changes: 30 additions & 0 deletions
30
backend/src/entities/permission/permission-catalog.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,30 @@ | ||
| import { CEDAR_SCHEMA } from '../cedar-authorization/cedar-schema.js'; | ||
| import { AvailablePermissionDs } from './application/data-structures/available-permissions.ds.js'; | ||
|
|
||
| export function buildPermissionCatalog(): Array<AvailablePermissionDs> { | ||
| const schemaActions = (CEDAR_SCHEMA as SchemaShape).RocketAdmin.actions; | ||
| return Object.entries(schemaActions).map(([value, definition]) => | ||
| buildAction(value, definition.appliesTo.resourceTypes), | ||
| ); | ||
| } | ||
|
|
||
| function buildAction(value: string, resourceTypes: Array<string>): AvailablePermissionDs { | ||
| const action: AvailablePermissionDs = { value }; | ||
| const resource = deriveResource(resourceTypes); | ||
| if (resource) { | ||
| action.resource = resource; | ||
| } | ||
| return action; | ||
| } | ||
|
|
||
| function deriveResource(resourceTypes: Array<string>): string | undefined { | ||
| const first = resourceTypes[0]; | ||
| if (!first) return undefined; | ||
| return first.charAt(0).toLowerCase() + first.slice(1); | ||
| } | ||
|
|
||
| type SchemaShape = { | ||
| RocketAdmin: { | ||
| actions: Record<string, { appliesTo: { principalTypes: Array<string>; resourceTypes: Array<string> } }>; | ||
| }; | ||
| }; |
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
97 changes: 97 additions & 0 deletions
97
backend/test/ava-tests/non-saas-tests/non-saas-permission-catalog-e2e.test.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,97 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
|
||
| import { INestApplication, ValidationPipe } from '@nestjs/common'; | ||
| import { Test } from '@nestjs/testing'; | ||
| import test from 'ava'; | ||
| import { ValidationError } from 'class-validator'; | ||
| import cookieParser from 'cookie-parser'; | ||
| import request from 'supertest'; | ||
| import { ApplicationModule } from '../../../src/app.module.js'; | ||
| import { CedarAction } from '../../../src/entities/cedar-authorization/cedar-action-map.js'; | ||
| import { WinstonLogger } from '../../../src/entities/logging/winston-logger.js'; | ||
| import { AllExceptionsFilter } from '../../../src/exceptions/all-exceptions.filter.js'; | ||
| import { ValidationException } from '../../../src/exceptions/custom-exceptions/validation-exception.js'; | ||
| import { Cacher } from '../../../src/helpers/cache/cacher.js'; | ||
| import { DatabaseModule } from '../../../src/shared/database/database.module.js'; | ||
| import { DatabaseService } from '../../../src/shared/database/database.service.js'; | ||
| import { registerUserAndReturnUserInfo } from '../../utils/register-user-and-return-user-info.js'; | ||
| import { setSaasEnvVariable } from '../../utils/set-saas-env-variable.js'; | ||
| import { TestUtils } from '../../utils/test.utils.js'; | ||
|
|
||
| let app: INestApplication; | ||
|
|
||
| test.before(async () => { | ||
| setSaasEnvVariable(); | ||
| const moduleFixture = await Test.createTestingModule({ | ||
| imports: [ApplicationModule, DatabaseModule], | ||
| providers: [DatabaseService, TestUtils], | ||
| }).compile(); | ||
| app = moduleFixture.createNestApplication(); | ||
|
|
||
| app.use(cookieParser()); | ||
| app.useGlobalFilters(new AllExceptionsFilter(app.get(WinstonLogger))); | ||
| app.useGlobalPipes( | ||
| new ValidationPipe({ | ||
| exceptionFactory(validationErrors: ValidationError[] = []) { | ||
| return new ValidationException(validationErrors); | ||
| }, | ||
| }), | ||
| ); | ||
| await app.init(); | ||
| app.getHttpServer().listen(0); | ||
| }); | ||
|
|
||
| test.after(async () => { | ||
| await Cacher.clearAllCache(); | ||
| await app.close(); | ||
| }); | ||
|
|
||
| test.serial('GET /permissions/available returns catalog covering every CedarAction', async (t) => { | ||
| const token = (await registerUserAndReturnUserInfo(app)).token; | ||
|
|
||
| const response = await request(app.getHttpServer()) | ||
| .get('/permissions/available') | ||
| .set('Cookie', token) | ||
| .set('Accept', 'application/json'); | ||
|
|
||
| t.is(response.status, 200); | ||
|
|
||
| const body = response.body as { | ||
| actions: Array<{ value: string; resource?: string }>; | ||
| }; | ||
|
|
||
| t.true(Array.isArray(body.actions)); | ||
| t.true(body.actions.length > 0); | ||
|
|
||
| const values = new Set(body.actions.map((a) => a.value)); | ||
|
|
||
| for (const cedarValue of Object.values(CedarAction)) { | ||
| t.true(values.has(cedarValue), `catalog missing CedarAction ${cedarValue}`); | ||
| } | ||
|
|
||
| t.false(values.has('*'), 'catalog must NOT include synthesized wildcards'); | ||
| t.false(values.has('table:*'), 'catalog must NOT include synthesized wildcards'); | ||
| t.false(values.has('dashboard:*'), 'catalog must NOT include synthesized wildcards'); | ||
|
|
||
| const byValue = new Map(body.actions.map((a) => [a.value, a])); | ||
|
|
||
| t.is(byValue.get('connection:read')!.resource, 'connection'); | ||
| t.is(byValue.get('group:edit')!.resource, 'group'); | ||
| t.is(byValue.get('table:read')!.resource, 'table'); | ||
| t.is(byValue.get('actionEvent:trigger')!.resource, 'actionEvent'); | ||
| t.is(byValue.get('dashboard:read')!.resource, 'dashboard'); | ||
| t.is(byValue.get('dashboard:create')!.resource, 'dashboard'); | ||
| t.is(byValue.get('panel:read')!.resource, 'panel'); | ||
|
|
||
| for (const action of body.actions) { | ||
| t.is(Object.hasOwn(action, 'label'), false, `action ${action.value} should not have label`); | ||
| t.is(Object.hasOwn(action, 'shortLabel'), false, `action ${action.value} should not have shortLabel`); | ||
| t.is(Object.hasOwn(action, 'icon'), false, `action ${action.value} should not have icon`); | ||
| } | ||
| }); | ||
|
|
||
| test.serial('GET /permissions/available requires authentication', async (t) => { | ||
| const response = await request(app.getHttpServer()).get('/permissions/available').set('Accept', 'application/json'); | ||
|
|
||
| t.is(response.status, 401); | ||
| }); | ||
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
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.
Remove the explicit server listen in this E2E setup.
supertestusesapp.getHttpServer()directly;listen(0)here is redundant and may create teardown flakiness because it isn’t awaited.Suggested fix
- app.getHttpServer().listen(0);📝 Committable suggestion
🤖 Prompt for AI Agents