-
-
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
Changes from all commits
5ed31bc
02c4786
15390c4
e4d2057
f86d75d
53a4c53
5e059ff
e5808d1
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 |
|---|---|---|
| @@ -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>; | ||
| } |
| 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> } }>; | ||
| }; | ||
| }; |
| 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); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -148,14 +148,14 @@ export class DateTimeFilterComponent extends BaseFilterFieldComponent implements | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private _restoreBetween(value: string[]): void { | ||||||||||||||||||||||||||||||||||||||||||||
| if (value[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
| const lower = new Date(value[0]); | ||||||||||||||||||||||||||||||||||||||||||||
| this.lowerDate = format(lower, 'yyyy-MM-dd'); | ||||||||||||||||||||||||||||||||||||||||||||
| this.lowerTime = format(lower, 'HH:mm:ss'); | ||||||||||||||||||||||||||||||||||||||||||||
| const iso = new Date(value[0]).toISOString(); | ||||||||||||||||||||||||||||||||||||||||||||
| this.lowerDate = iso.slice(0, 10); | ||||||||||||||||||||||||||||||||||||||||||||
| this.lowerTime = iso.slice(11, 19); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (value[1]) { | ||||||||||||||||||||||||||||||||||||||||||||
| const upper = new Date(value[1]); | ||||||||||||||||||||||||||||||||||||||||||||
| this.upperDate = format(upper, 'yyyy-MM-dd'); | ||||||||||||||||||||||||||||||||||||||||||||
| this.upperTime = format(upper, 'HH:mm:ss'); | ||||||||||||||||||||||||||||||||||||||||||||
| const iso = new Date(value[1]).toISOString(); | ||||||||||||||||||||||||||||||||||||||||||||
| this.upperDate = iso.slice(0, 10); | ||||||||||||||||||||||||||||||||||||||||||||
| this.upperTime = iso.slice(11, 19); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+151
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: Yes—Date.prototype.toISOString always serializes using UTC (it appends the “Z” UTC designator, per the documented return format). [1] For date-fns v4: date-fns/format formats using the system time zone by default (i.e., it relies on the underlying JavaScript Date, which uses the environment’s local time zone). date-fns only lets you control time zone explicitly via its v4 time zone support (e.g., the “in” context option with Citations:
Fix timezone inconsistency between BETWEEN restore and single-value restore Suggested local fix if (value[0]) {
- const iso = new Date(value[0]).toISOString();
- this.lowerDate = iso.slice(0, 10);
- this.lowerTime = iso.slice(11, 19);
+ const lower = new Date(value[0]);
+ this.lowerDate = format(lower, 'yyyy-MM-dd');
+ this.lowerTime = format(lower, 'HH:mm:ss');
}
if (value[1]) {
- const iso = new Date(value[1]).toISOString();
- this.upperDate = iso.slice(0, 10);
- this.upperTime = iso.slice(11, 19);
+ const upper = new Date(value[1]);
+ this.upperDate = format(upper, 'yyyy-MM-dd');
+ this.upperTime = format(upper, 'HH:mm:ss');
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
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