Skip to content

Commit

Permalink
fix(rbac): correct plugin ID matching to permission policy (#1795)
Browse files Browse the repository at this point in the history
* fix(rbac): correct plugin ID matching to permission policy

Signed-off-by: Oleksandr Andriienko <[email protected]>

* fix(rbac): refactor types for plugin policies list

Signed-off-by: Oleksandr Andriienko <[email protected]>

* fix(rbac): fix broken getAllPolicies method

Signed-off-by: Oleksandr Andriienko <[email protected]>

* fix(rbac): update apis.md file and fix import

Signed-off-by: Oleksandr Andriienko <[email protected]>

---------

Signed-off-by: Oleksandr Andriienko <[email protected]>
  • Loading branch information
AndrienkoAleksandr authored Jun 28, 2024
1 parent 23a143d commit 6dc4b1c
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 127 deletions.
32 changes: 14 additions & 18 deletions plugins/rbac-backend/docs/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,40 +461,36 @@ Returns:
[
{
"pluginId": "catalog",
"policies": [
"policies": [
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "read"
"name": "catalog.entity.read",
"policy": "read",
"resourceType": "catalog-entity"
},
{
"isResourced": false,
"permission": "catalog.entity.create",
"name": "catalog.entity.create",
"policy": "create"
},
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "delete"
"name": "catalog.entity.delete",
"policy": "delete",
"resourceType": "catalog-entity"
},
{
"isResourced": true,
"permission": "catalog-entity",
"policy": "update"
"name": "catalog.entity.refresh",
"policy": "update",
"resourceType": "catalog-entity"
},
{
"isResourced": false,
"permission": "catalog.location.read",
"name": "catalog.location.read",
"policy": "read"
},
{
"isResourced": false,
"permission": "catalog.location.create",
"name": "catalog.location.create",
"policy": "create"
},
{
"isResourced": false,
"permission": "catalog.location.delete",
"name": "catalog.location.delete",
"policy": "delete"
}
]
Expand Down
1 change: 0 additions & 1 deletion plugins/rbac-backend/src/audit-log/audit-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const ConditionEvents = {
};

export type ConditionAuditInfo = {
conditionId?: number;
condition: RoleConditionalPolicyDecision<PermissionAction>;
};

Expand Down
19 changes: 9 additions & 10 deletions plugins/rbac-backend/src/service/plugin-endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: true,
permission: 'policy-entity',
name: 'policy.entity.read',
resourceType: 'policy-entity',
policy: 'read',
},
]);
Expand All @@ -118,8 +118,7 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: false,
permission: 'catalog.entity.create',
name: 'catalog.entity.create',
policy: 'create',
},
]);
Expand Down Expand Up @@ -158,8 +157,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: true,
permission: 'policy-entity',
name: 'policy.entity.read',
resourceType: 'policy-entity',
policy: 'read',
},
]);
Expand Down Expand Up @@ -200,8 +199,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: true,
permission: 'policy-entity',
name: 'policy.entity.read',
resourceType: 'policy-entity',
policy: 'read',
},
]);
Expand Down Expand Up @@ -242,8 +241,8 @@ describe('plugin-endpoint', () => {
expect(policiesMetadata[0].pluginId).toEqual('permission');
expect(policiesMetadata[0].policies).toEqual([
{
isResourced: true,
permission: 'policy-entity',
name: 'policy.entity.read',
resourceType: 'policy-entity',
policy: 'read',
},
]);
Expand Down
23 changes: 11 additions & 12 deletions plugins/rbac-backend/src/service/plugin-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
MetadataResponseSerializedRule,
} from '@backstage/plugin-permission-node';

import { Policy } from '@janus-idp/backstage-plugin-rbac-common';
import {
PluginPermissionMetaData,
PolicyDetails,
} from '@janus-idp/backstage-plugin-rbac-common';
import { PluginIdProvider } from '@janus-idp/backstage-plugin-rbac-node';

type PluginMetadataResponse = {
Expand All @@ -33,11 +36,6 @@ export type PluginMetadataResponseSerializedRule = {
rules: MetadataResponseSerializedRule[];
};

export type PluginPermissionMetaData = {
pluginId: string;
policies: Policy[];
};

export class PluginPermissionMetadataCollector {
private readonly pluginIds: string[];
private urlReader: UrlReaderService;
Expand Down Expand Up @@ -147,20 +145,21 @@ export class PluginPermissionMetadataCollector {
}
}

function permissionsToCasbinPolicies(permissions: Permission[]): Policy[] {
const policies: Policy[] = [];
function permissionsToCasbinPolicies(
permissions: Permission[],
): PolicyDetails[] {
const policies: PolicyDetails[] = [];
for (const permission of permissions) {
if (isResourcePermission(permission)) {
policies.push({
permission: permission.resourceType,
resourceType: permission.resourceType,
name: permission.name,
policy: permission.attributes.action || 'use',
isResourced: true,
});
} else {
policies.push({
permission: permission.name,
name: permission.name,
policy: permission.attributes.action || 'use',
isResourced: false,
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions plugins/rbac-backend/src/service/policies-rest-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PermissionAction,
PermissionInfo,
PermissionPolicyMetadata,
PluginPermissionMetaData,
policyEntityCreatePermission,
policyEntityDeletePermission,
policyEntityReadPermission,
Expand All @@ -32,7 +33,6 @@ import { EnforcerDelegate } from './enforcer-delegate';
import { RBACPermissionPolicy } from './permission-policy';
import {
PluginMetadataResponseSerializedRule,
PluginPermissionMetaData,
PluginPermissionMetadataCollector,
} from './plugin-endpoints';
import { PoliciesServer } from './policies-rest-api';
Expand Down Expand Up @@ -3440,7 +3440,8 @@ describe('REST policies api', () => {
pluginId: 'permissions',
policies: [
{
permission: 'policy-entity',
name: 'catalog.entity.read',
resourceType: 'policy-entity',
policy: 'read',
},
],
Expand Down
29 changes: 23 additions & 6 deletions plugins/rbac-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ export type RoleMetadata = {

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

export type RoleBasedPolicy = Policy & {
entityReference?: string;
effect?: string;
metadata?: PermissionPolicyMetadata;
};

export type Role = {
Expand All @@ -46,9 +45,27 @@ export type UpdatePolicy = {
newPolicy: Policy;
};

export type PermissionPolicy = {
pluginId?: string;
policies?: Policy[];
export type NamedPolicy = {
name: string;

policy: string;
};

export type ResourcedPolicy = NamedPolicy & {
resourceType: string;
};

export type PolicyDetails = NamedPolicy | ResourcedPolicy;

export function isResourcedPolicy(
policy: PolicyDetails,
): policy is ResourcedPolicy {
return 'resourceType' in policy;
}

export type PluginPermissionMetaData = {
pluginId: string;
policies: PolicyDetails[];
};

export type NonEmptyArray<T> = [T, ...T[]];
Expand Down
4 changes: 2 additions & 2 deletions plugins/rbac/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createDevAppThemes } from '@redhat-developer/red-hat-developer-hub-them

import {
PermissionAction,
PermissionPolicy,
PluginPermissionMetaData,
Role,
RoleBasedPolicy,
RoleConditionalPolicyDecision,
Expand Down Expand Up @@ -82,7 +82,7 @@ class MockRBACApi implements RBACAPI {
return mockMembers;
}

async listPermissions(): Promise<PermissionPolicy[]> {
async listPermissions(): Promise<PluginPermissionMetaData[]> {
return mockPermissionPolicies;
}

Expand Down
44 changes: 22 additions & 22 deletions plugins/rbac/src/__fixtures__/mockPermissionPolicies.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { PermissionPolicy } from '@janus-idp/backstage-plugin-rbac-common';
import { PluginPermissionMetaData } from '@janus-idp/backstage-plugin-rbac-common';

export const mockPermissionPolicies: PermissionPolicy[] = [
export const mockPermissionPolicies: PluginPermissionMetaData[] = [
{
pluginId: 'catalog',
policies: [
{
permission: 'catalog-entity',
resourceType: 'catalog-entity',
name: 'catalog.entity.read',
policy: 'read',
isResourced: true,
},
{
permission: 'catalog.entity.create',
name: 'catalog.entity.create',
policy: 'create',
},
{
permission: 'catalog-entity',
resourceType: 'catalog-entity',
name: 'catalog.entity.delete',
policy: 'delete',
isResourced: true,
},
{
permission: 'catalog-entity',
resourceType: 'catalog-entity',
name: 'catalog.entity.update',
policy: 'update',
isResourced: true,
},
{
permission: 'catalog.location.read',
name: 'catalog.location.read',
policy: 'read',
},
{
permission: 'catalog.location.create',
name: 'catalog.location.create',
policy: 'create',
},
{
permission: 'catalog.location.delete',
name: 'catalog.location.delete',
policy: 'delete',
},
],
Expand All @@ -41,39 +41,39 @@ export const mockPermissionPolicies: PermissionPolicy[] = [
pluginId: 'scaffolder',
policies: [
{
permission: 'scaffolder-template',
resourceType: 'scaffolder-template',
name: 'scaffolder.template.read',
policy: 'read',
isResourced: true,
},
{
permission: 'scaffolder-template',
resourceType: 'scaffolder-template',
name: 'scaffolder.template.read',
policy: 'read',
isResourced: true,
},
{
permission: 'scaffolder-action',
resourceType: 'scaffolder-action',
name: 'scaffolder.action.use',
policy: 'use',
isResourced: true,
},
],
},
{
pluginId: 'permission',
policies: [
{
permission: 'policy-entity',
name: 'policy-entity',
policy: 'read',
},
{
permission: 'policy-entity',
name: 'policy-entity',
policy: 'create',
},
{
permission: 'policy-entity',
name: 'policy-entity',
policy: 'delete',
},
{
permission: 'policy-entity',
name: 'policy-entity',
policy: 'update',
},
],
Expand Down
4 changes: 2 additions & 2 deletions plugins/rbac/src/api/RBACBackendClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

import {
PermissionAction,
PermissionPolicy,
PluginPermissionMetaData,
Role,
RoleBasedPolicy,
RoleConditionalPolicyDecision,
Expand All @@ -31,7 +31,7 @@ export type RBACAPI = {
deleteRole: (role: string) => Promise<Response>;
getRole: (role: string) => Promise<Role[] | Response>;
getMembers: () => Promise<MemberEntity[] | Response>;
listPermissions: () => Promise<PermissionPolicy[] | Response>;
listPermissions: () => Promise<PluginPermissionMetaData[] | Response>;
createRole: (role: Role) => Promise<RoleError | Response>;
updateRole: (oldRole: Role, newRole: Role) => Promise<RoleError | Response>;
updatePolicies: (
Expand Down
Loading

0 comments on commit 6dc4b1c

Please sign in to comment.