Skip to content

Commit

Permalink
fix management section id not match the key defined in capabilities
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Jul 21, 2024
1 parent 376ead0 commit ea9f4ea
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/plugins/management/common/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
};
26 changes: 26 additions & 0 deletions src/plugins/management/public/management_sections_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* under the License.
*/

import { DEFAULT_MANAGEMENT_CAPABILITIES } from '../common/contants';
import {
ManagementSectionsService,
getSectionsServiceStartPrivate,
Expand Down Expand Up @@ -105,4 +106,29 @@ describe('ManagementService', () => {
]
`);
});

it('should disable apps register in opensearchDashboards section', () => {
const originalDataSourcesCapability =
DEFAULT_MANAGEMENT_CAPABILITIES.management.opensearchDashboards.dataSources;

const setup = managementService.setup();

// Register app with id dataSources, the app id will be capability id
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;
});
});
9 changes: 7 additions & 2 deletions src/plugins/management/public/management_sections_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const [getSectionsServiceStartPrivate, setSectionsServiceStartPrivate] = createG
ManagementSectionsStartPrivate
>('SectionsServiceStartPrivate');

const MANAGEMENT_ID_TO_CAPABILITIES: Record<string, string> = {
'opensearch-dashboards': 'opensearchDashboards',
};

export { getSectionsServiceStartPrivate };

export class ManagementSectionsService {
Expand Down Expand Up @@ -94,8 +98,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();
Expand Down
17 changes: 3 additions & 14 deletions src/plugins/management/server/capabilities_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit ea9f4ea

Please sign in to comment.