Skip to content
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

fix(rbac): reduce the number of permissions returned, add isResourced flag #1474

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions plugins/rbac-backend/docs/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,37 +415,44 @@ Returns:
[
{
"pluginId": "catalog",
"policies": [
{
"permission": "catalog-entity",
"policy": "read"
},
{
"permission": "catalog.entity.create",
"policy": "create"
},
{
"permission": "catalog-entity",
"policy": "delete"
},
{
"permission": "catalog-entity",
"policy": "update"
},
{
"permission": "catalog.location.read",
"policy": "read"
},
{
"permission": "catalog.location.create",
"policy": "create"
},
{
"permission": "catalog.location.delete",
"policy": "delete"
}
]
},
"policies": [
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "read"
},
{
"isResourced": false,
"permission": "catalog.entity.create",
"policy": "create"
},
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "delete"
},
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "update"
},
{
"isResourced": false,
"permission": "catalog.location.read",
"policy": "read"
},
{
"isResourced": false,
"permission": "catalog.location.create",
"policy": "create"
},
{
"isResourced": false,
"permission": "catalog.location.delete",
"policy": "delete"
}
]
},
...
]
```
Expand Down
31 changes: 16 additions & 15 deletions plugins/rbac-backend/src/service/plugin-endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata.length).toEqual(0);
});

it('should return non empty plugin policies list', async () => {
it('should return non empty plugin policies list with resourced permission', async () => {
backendPluginIDsProviderMock.getPluginIds.mockReturnValue(['permission']);

mockUrlReaderService.readUrl.mockReturnValue(mockReadUrlResponse);
Expand All @@ -89,22 +89,19 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: true,
permission: 'policy-entity',
policy: 'read',
},
{
permission: 'policy.entity.read',
policy: 'read',
},
]);
});

it('should return plugin policies list without resource type permissions', async () => {
it('should return non empty plugin policies list with non resourced permission', async () => {
backendPluginIDsProviderMock.getPluginIds.mockReturnValue(['permission']);

mockUrlReaderService.readUrl.mockReturnValue(mockReadUrlResponse);
bufferMock.toString.mockReturnValueOnce(
'{"permissions":[{"type":"resource","name":"policy.entity.read","attributes":{"action":"read"}}]}',
'{"permissions":[{"type":"basic","name":"catalog.entity.create","attributes":{"action":"create"}}]}',
);

const collector = new PluginPermissionMetadataCollector(
Expand All @@ -119,8 +116,9 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
permission: 'policy.entity.read',
policy: 'read',
isResourced: false,
permission: 'catalog.entity.create',
policy: 'create',
},
]);
});
Expand All @@ -143,7 +141,7 @@ describe('plugin-endpoint', () => {
throw new NotFoundError();
});
bufferMock.toString.mockReturnValueOnce(
'{"permissions":[{"type":"resource","name":"policy.entity.read","attributes":{"action":"read"}}]}',
'{"permissions":[{"type":"resource","resourceType":"policy-entity","name":"policy.entity.read","attributes":{"action":"read"}}]}',
);

const collector = new PluginPermissionMetadataCollector(
Expand All @@ -158,7 +156,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
permission: 'policy.entity.read',
isResourced: true,
permission: 'policy-entity',
policy: 'read',
},
]);
Expand All @@ -182,7 +181,7 @@ describe('plugin-endpoint', () => {
throw new Error('Unexpected error');
});
bufferMock.toString.mockReturnValueOnce(
'{"permissions":[{"type":"resource","name":"policy.entity.read","attributes":{"action":"read"}}]}',
'{"permissions":[{"type":"resource","resourceType":"policy-entity","name":"policy.entity.read","attributes":{"action":"read"}}]}',
);

const errorSpy = jest.spyOn(logger, 'error').mockClear();
Expand All @@ -199,7 +198,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
permission: 'policy.entity.read',
isResourced: true,
permission: 'policy-entity',
policy: 'read',
},
]);
Expand All @@ -222,7 +222,7 @@ describe('plugin-endpoint', () => {
});
bufferMock.toString
.mockReturnValueOnce(
'{"permissions":[{"type":"resource","name":"policy.entity.read","attributes":{"action":"read"}}]}',
'{"permissions":[{"type":"resource","resourceType":"policy-entity","name":"policy.entity.read","attributes":{"action":"read"}}]}',
)
.mockReturnValueOnce('non json data');

Expand All @@ -240,7 +240,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
permission: 'policy.entity.read',
isResourced: true,
permission: 'policy-entity',
policy: 'read',
},
]);
Expand Down
14 changes: 9 additions & 5 deletions plugins/rbac-backend/src/service/plugin-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,22 @@ export class PluginPermissionMetadataCollector {
}

function permissionsToCasbinPolicies(permissions: Permission[]): Policy[] {
const policies = [];
const policies: Policy[] = [];
for (const permission of permissions) {
if (isResourcePermission(permission)) {
policies.push({
permission: permission.resourceType,
policy: permission.attributes.action || 'use',
isResourced: true,
});
} else {
policies.push({
permission: permission.name,
policy: permission.attributes.action || 'use',
isResourced: false,
});
}
policies.push({
permission: permission.name,
policy: permission.attributes.action || 'use',
});
}

return policies;
}
1 change: 1 addition & 0 deletions plugins/rbac-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type RoleMetadata = {

export type Policy = {
permission?: string;
isResourced?: boolean;
policy?: string;
effect?: string;
metadata?: PermissionPolicyMetadata;
Expand Down
Loading