diff --git a/src/plugins/management/common/contants.ts b/src/plugins/management/common/contants.ts index 47e418fc620..037df0ad7c5 100644 --- a/src/plugins/management/common/contants.ts +++ b/src/plugins/management/common/contants.ts @@ -29,3 +29,17 @@ */ export const MANAGEMENT_APP_ID = 'management'; +export const DEFAULT_MANAGEMENT_CAPABILITIES = { + management: { + /* + * Management settings correspond to management section/link ids, and should not be changed + * without also updating those definitions. + */ + opensearchDashboards: { + settings: true, + indexPatterns: true, + objects: true, + dataSources: true, + }, + }, +}; diff --git a/src/plugins/management/public/management_sections_service.test.ts b/src/plugins/management/public/management_sections_service.test.ts index 8f969bce899..e0358fac99a 100644 --- a/src/plugins/management/public/management_sections_service.test.ts +++ b/src/plugins/management/public/management_sections_service.test.ts @@ -28,6 +28,7 @@ * under the License. */ +import { DEFAULT_MANAGEMENT_CAPABILITIES } from '../common/contants'; import { ManagementSectionsService, getSectionsServiceStartPrivate, @@ -105,4 +106,31 @@ describe('ManagementService', () => { ] `); }); + + it('should disable apps register in predefined opensearchDashboards section', () => { + // The management capabilities has `opensearchDashboards` as the key + const originalDataSourcesCapability = + DEFAULT_MANAGEMENT_CAPABILITIES.management.opensearchDashboards.dataSources; + + const setup = managementService.setup(); + + // The predefined opensearchDashboards section has id `opensearch-dashboards` which + // doesn't match the capability id `opensearchDashboards` + setup.section.opensearchDashboards.registerApp({ + id: 'dataSources', + title: 'Data source', + mount: jest.fn(), + }); + + // Now set dataSources to capability to false should disable + // the dataSources app registered in opensearchDashboards section + DEFAULT_MANAGEMENT_CAPABILITIES.management.opensearchDashboards.dataSources = false; + + managementService.start({ capabilities: DEFAULT_MANAGEMENT_CAPABILITIES }); + expect( + setup.section.opensearchDashboards.apps.find((app) => app.id === 'dataSources')?.enabled + ).toBe(false); + + DEFAULT_MANAGEMENT_CAPABILITIES.management.opensearchDashboards.dataSources = originalDataSourcesCapability; + }); }); diff --git a/src/plugins/management/public/management_sections_service.ts b/src/plugins/management/public/management_sections_service.ts index 81b8c7ac24a..4a65388d22e 100644 --- a/src/plugins/management/public/management_sections_service.ts +++ b/src/plugins/management/public/management_sections_service.ts @@ -51,6 +51,22 @@ const [getSectionsServiceStartPrivate, setSectionsServiceStartPrivate] = createG ManagementSectionsStartPrivate >('SectionsServiceStartPrivate'); +/** + * The management capabilities has `opensearchDashboards` as the key + * While when registering the opensearchDashboards section, the section id is `opensearch-dashboards` + * as defined in {@link ManagementSectionId.OpenSearchDashboards} and section id is used as the capability + * id. Here we have a mapping so that the section id opensearch-dashboards can mapping correctly back to the + * capability id: opensearchDashboards + * + * Why not directly change the capability id to opensearch-dashboards? + * The issue was introduced in https://github.com/opensearch-project/OpenSearch-Dashboards/pull/260 + * Since then, the capability id `opensearchDashboards` has been used by plugins, having a mapping here + * is for backward compatibility + */ +const MANAGEMENT_ID_TO_CAPABILITIES: Record = { + 'opensearch-dashboards': 'opensearchDashboards', +}; + export { getSectionsServiceStartPrivate }; export class ManagementSectionsService { @@ -94,8 +110,9 @@ export class ManagementSectionsService { start({ capabilities }: SectionsServiceStartDeps) { this.getAllSections().forEach((section) => { - if (capabilities.management.hasOwnProperty(section.id)) { - const sectionCapabilities = capabilities.management[section.id]; + const capabilityId = MANAGEMENT_ID_TO_CAPABILITIES[section.id] || section.id; + if (capabilities.management.hasOwnProperty(capabilityId)) { + const sectionCapabilities = capabilities.management[capabilityId]; section.apps.forEach((app) => { if (sectionCapabilities.hasOwnProperty(app.id) && sectionCapabilities[app.id] !== true) { app.disable(); diff --git a/src/plugins/management/server/capabilities_provider.ts b/src/plugins/management/server/capabilities_provider.ts index 2786378c982..9d67c752f52 100644 --- a/src/plugins/management/server/capabilities_provider.ts +++ b/src/plugins/management/server/capabilities_provider.ts @@ -28,17 +28,6 @@ * under the License. */ -export const capabilitiesProvider = () => ({ - management: { - /* - * Management settings correspond to management section/link ids, and should not be changed - * without also updating those definitions. - */ - opensearchDashboards: { - settings: true, - indexPatterns: true, - objects: true, - dataSources: true, - }, - }, -}); +import { DEFAULT_MANAGEMENT_CAPABILITIES } from '../common/contants'; + +export const capabilitiesProvider = () => DEFAULT_MANAGEMENT_CAPABILITIES;