Skip to content

Commit 00c856e

Browse files
authored
chore - RHINENG-22573 added cost.plugin RBAC permission to view Openshift page (#1981)
1 parent ea3cbe4 commit 00c856e

File tree

7 files changed

+92
-14
lines changed

7 files changed

+92
-14
lines changed

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-backend/src/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export const resourceOptimizationPlugin = createBackendPlugin({
7070
path: '/access',
7171
allow: 'user-cookie',
7272
});
73+
httpRouter.addAuthPolicy({
74+
path: '/access/cost-management',
75+
allow: 'user-cookie',
76+
});
7377
},
7478
});
7579
},
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type { RequestHandler } from 'express';
18+
import type { RouterOptions } from '../models/RouterOptions';
19+
import { authorize } from '../util/checkPermissions';
20+
import { costPluginPermissions } from '@red-hat-developer-hub/plugin-redhat-resource-optimization-common/permissions';
21+
import { AuthorizeResult } from '@backstage/plugin-permission-common';
22+
23+
export const getCostManagementAccess: (
24+
options: RouterOptions,
25+
) => RequestHandler = options => async (_, response) => {
26+
const { logger, permissions, httpAuth } = options;
27+
let finalDecision = AuthorizeResult.DENY;
28+
29+
// Check for cost.plugin permisssion
30+
// if user has ros.plugin permission, allow access to all the data
31+
const costPluginDecision = await authorize(
32+
_,
33+
costPluginPermissions,
34+
permissions,
35+
httpAuth,
36+
);
37+
38+
logger.info(`Checking decision:`, costPluginDecision);
39+
40+
if (costPluginDecision.result === AuthorizeResult.ALLOW) {
41+
finalDecision = AuthorizeResult.ALLOW;
42+
43+
const body = {
44+
decision: finalDecision,
45+
authorizeClusterIds: [],
46+
authorizeProjects: [],
47+
};
48+
return response.json(body);
49+
}
50+
51+
const body = {
52+
decision: finalDecision,
53+
authorizeClusterIds: [],
54+
authorizeProjects: [],
55+
};
56+
57+
return response.json(body);
58+
};

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-backend/src/service/router.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { getToken } from '../routes/token';
2121
import { createPermissionIntegrationRouter } from '@backstage/plugin-permission-node';
2222
import { rosPluginPermissions } from '@red-hat-developer-hub/plugin-redhat-resource-optimization-common/permissions';
2323
import { getAccess } from '../routes/access';
24+
import { getCostManagementAccess } from '../routes/costManagementAccess';
2425

2526
/** @public */
2627
export async function createRouter(
@@ -41,5 +42,7 @@ export async function createRouter(
4142

4243
router.get('/access', getAccess(options));
4344

45+
router.get('/access/cost-management', getCostManagementAccess(options));
46+
4447
return router;
4548
}

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/report-permissions.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
```ts
66
import { BasicPermission } from '@backstage/plugin-permission-common';
77

8+
// @public (undocumented)
9+
export const costPluginPermissions: BasicPermission[];
10+
11+
// @public (undocumented)
12+
export const costPluginReadPermission: BasicPermission;
13+
814
// @public (undocumented)
915
export const rosClusterProjectPermission: (
1016
clusterName: string,

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/src/clients/cost-management/CostManagementSlimClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ export class CostManagementSlimClient implements CostManagementSlimApi {
577577

578578
private async getAccess(): Promise<GetAccessResponse> {
579579
const baseUrl = await this.discoveryApi.getBaseUrl(`${pluginId}`);
580-
const response = await this.fetchApi.fetch(`${baseUrl}/access`);
580+
const response = await this.fetchApi.fetch(
581+
`${baseUrl}/access/cost-management`,
582+
);
581583
const data = (await response.json()) as GetAccessResponse;
582584
return data;
583585
}

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization-common/src/permissions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,14 @@ export const rosClusterProjectPermission = (
3939
attributes: { action: 'read' },
4040
});
4141

42+
/** @public */
43+
export const costPluginReadPermission = createPermission({
44+
name: 'cost.plugin',
45+
attributes: { action: 'read' },
46+
});
47+
48+
/** @public */
49+
export const costPluginPermissions = [costPluginReadPermission];
50+
4251
/** @public */
4352
export const rosPluginPermissions = [rosPluginReadPermission];

workspaces/redhat-resource-optimization/plugins/redhat-resource-optimization/src/pages/openshift/OpenShiftPage.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,16 @@ export function OpenShiftPage() {
296296
return null;
297297
}
298298

299-
try {
300-
const offset = currentPage * pageSize;
301-
const queryParams = buildCostManagementQueryParams(queryParamsConfig, {
302-
limit: pageSize,
303-
offset,
304-
});
299+
const offset = currentPage * pageSize;
300+
const queryParams = buildCostManagementQueryParams(queryParamsConfig, {
301+
limit: pageSize,
302+
offset,
303+
});
305304

306-
const response = await api.getCostManagementReport({
307-
query: queryParams,
308-
});
309-
return response.json();
310-
} catch {
311-
return null;
312-
}
305+
const response = await api.getCostManagementReport({
306+
query: queryParams,
307+
});
308+
return response.json();
313309
}, [
314310
currency,
315311
overheadDistribution,

0 commit comments

Comments
 (0)