From a51bab5f99d7e2ca32ce2a2691e22cd813fbfafc Mon Sep 17 00:00:00 2001 From: Afreen Date: Tue, 6 Feb 2024 15:13:58 +0530 Subject: [PATCH] mgr/dashboard: fix error while accessing roles tab when policy attached Fixes https://tracker.ceph.com/issues/64270 Issue: ====== Accessing Object->Users-Roles tab causing 500 internal servor error. This is due to the "PermissionPolicies" which are attached to role and backend was not handling this field for rgw roles. Fix: ==== Added "PermissionPolicies" as the valid field in backend and updated frontend to render the attached policy in formatted JSON Signed-off-by: Afreen (cherry picked from commit 5247d7d19455a5ab7d1bb56b9586c6b08843b15d) Conflicts: src/pybind/mgr/dashboard/frontend/src/app/shared/models/crud-table-metadata.ts --- src/pybind/mgr/dashboard/controllers/rgw.py | 6 ++-- .../crud-table/crud-table.component.html | 2 +- .../crud-table/crud-table.component.ts | 34 +++++++++++++++++-- .../app/shared/models/crud-table-metadata.ts | 1 + 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/rgw.py b/src/pybind/mgr/dashboard/controllers/rgw.py index 9bc1b489edee0..fc4c2f48eb1ca 100644 --- a/src/pybind/mgr/dashboard/controllers/rgw.py +++ b/src/pybind/mgr/dashboard/controllers/rgw.py @@ -762,9 +762,10 @@ create_role_form = Form(path='/rgw/roles/create', "CreateDate": {'cellTemplate': 'date'}, "MaxSessionDuration": {'cellTemplate': 'duration'}, "RoleId": {'isHidden': True}, - "AssumeRolePolicyDocument": {'isHidden': True} + "AssumeRolePolicyDocument": {'isHidden': True}, + "PermissionPolicies": {'isHidden': True} }, - detail_columns=['RoleId', 'AssumeRolePolicyDocument'], + detail_columns=['RoleId', 'AssumeRolePolicyDocument', 'PermissionPolicies'], meta=CRUDMeta() ) class RgwUserRole(NamedTuple): @@ -775,6 +776,7 @@ class RgwUserRole(NamedTuple): CreateDate: str MaxSessionDuration: int AssumeRolePolicyDocument: str + PermissionPolicies: List @APIRouter('/rgw/realm', Scope.RGW) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.html b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.html index 7e1a7f2b34186..a1edf253c0125 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.html @@ -36,7 +36,7 @@ {{ column }} - {{ expandedRow[column] }} +
{{ expandedRow[column] }}
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.ts index 750152161c246..fa3a9d76a0b15 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/crud-table/crud-table.component.ts @@ -39,7 +39,7 @@ export class CRUDTableComponent implements OnInit { permissions: Permissions; permission: Permission; selection = new CdTableSelection(); - expandedRow: any = null; + expandedRow: { [key: string]: any } = {}; modalRef: NgbModalRef; tabs = {}; resource: string; @@ -145,7 +145,11 @@ export class CRUDTableComponent implements OnInit { } setExpandedRow(event: any) { - this.expandedRow = event; + for (let i = 0; i < this.meta.detail_columns.length; i++) { + let column = this.meta.detail_columns[i]; + let columnDetail = event[column]; + this.expandedRow[column] = this.formatColumnDetails(columnDetail); + } } edit() { @@ -174,4 +178,30 @@ export class CRUDTableComponent implements OnInit { this.modalRef = this.modalService.show(ConfirmationModalComponent, modalVariables); }); } + + /** + * Custom string replacer function for JSON.stringify + * + * This is specifically for objects inside an array. + * The custom replacer recursively stringifies deep nested objects + **/ + stringReplacer(_key: string, value: any) { + try { + const parsedValue = JSON.parse(value); + return parsedValue; + } catch (e) { + return value; + } + } + + /** + * returns a json string for arrays and string + * returns the same value for the rest + **/ + formatColumnDetails(details: any) { + if (Array.isArray(details) || typeof details === 'string') { + return JSON.stringify(details, this.stringReplacer, 2); + } + return details; + } } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/crud-table-metadata.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/crud-table-metadata.ts index 140fa5b5f8ea4..5ff77031c4e08 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/crud-table-metadata.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/crud-table-metadata.ts @@ -14,4 +14,5 @@ export class CrudMetadata { actions: CdTableAction[]; forms: any; columnKey: string; + detail_columns: string[]; } -- 2.39.5