Skip to content

Commit

Permalink
[Workspace] Capabilities service add dashboard admin flag (#7103) (#7151
Browse files Browse the repository at this point in the history
)

* Capabilities service add dashboard admin flag



* Capabilities service add dashboard admin flag



* Changeset file for PR #7103 created/updated

* optimize the code



* optimize the code



* if groups/users are not configured or [], login defaults to OSD Admin



* optimize the code



---------



(cherry picked from commit bb9eeef)

Signed-off-by: yubonluo <yubonluo@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent fe361e9 commit 6a871ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7103.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Workspace] Capabilities service add dashboard admin flag ([#7103](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7103))
29 changes: 28 additions & 1 deletion src/plugins/workspace/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,55 @@
import { OnPostAuthHandler, OnPreRoutingHandler } from 'src/core/server';
import { coreMock, httpServerMock } from '../../../core/server/mocks';
import { WorkspacePlugin } from './plugin';
import { getWorkspaceState } from '../../../core/server/utils';
import { getWorkspaceState, updateWorkspaceState } from '../../../core/server/utils';
import * as utilsExports from './utils';

describe('Workspace server plugin', () => {
it('#setup', async () => {
let value;
const capabilities = {} as any;
const setupMock = coreMock.createSetup();
const initializerContextConfigMock = coreMock.createPluginInitializerContext({
enabled: true,
});
const request = httpServerMock.createOpenSearchDashboardsRequest();

setupMock.capabilities.registerProvider.mockImplementationOnce((fn) => (value = fn()));
setupMock.capabilities.registerSwitcher.mockImplementationOnce((fn) => {
return fn(request, capabilities);
});

const workspacePlugin = new WorkspacePlugin(initializerContextConfigMock);
await workspacePlugin.setup(setupMock);
expect(value).toMatchInlineSnapshot(`
Object {
"dashboards": Object {
"isDashboardAdmin": false,
},
"workspaces": Object {
"enabled": true,
"permissionEnabled": true,
},
}
`);
expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(4);

let registerSwitcher;
let result;
updateWorkspaceState(request, { isDashboardAdmin: false });
registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0];
result = registerSwitcher(request, capabilities);
expect(result).toEqual({ dashboards: { isDashboardAdmin: false } });

updateWorkspaceState(request, { isDashboardAdmin: true });
registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0];
result = registerSwitcher(request, capabilities);
expect(result).toEqual({ dashboards: { isDashboardAdmin: true } });

updateWorkspaceState(request, { isDashboardAdmin: undefined });
registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0];
result = registerSwitcher(request, capabilities);
expect(result).toEqual({ dashboards: { isDashboardAdmin: true } });
});

it('#proxyWorkspaceTrafficToRealHandler', async () => {
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { WorkspaceSavedObjectsClientWrapper } from './saved_objects';
import {
cleanWorkspaceId,
getWorkspaceIdFromUrl,
getWorkspaceState,
updateWorkspaceState,
} from '../../../core/server/utils';
import { WorkspaceConflictSavedObjectsClientWrapper } from './saved_objects/saved_objects_wrapper_for_check_workspace_conflict';
Expand Down Expand Up @@ -157,7 +158,14 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
enabled: true,
permissionEnabled: isPermissionControlEnabled,
},
dashboards: { isDashboardAdmin: false },
}));
// Dynamically update capabilities based on the auth information from request.
core.capabilities.registerSwitcher((request) => {
// If the value is undefined/true, the user is dashboard admin.
const isDashboardAdmin = getWorkspaceState(request).isDashboardAdmin !== false;
return { dashboards: { isDashboardAdmin } };
});

return {
client: this.client,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('workspace utils', () => {
const configGroups: string[] = [];
const configUsers: string[] = [];
updateDashboardAdminStateForRequest(mockRequest, groups, users, configGroups, configUsers);
expect(getWorkspaceState(mockRequest)?.isDashboardAdmin).toBe(false);
expect(getWorkspaceState(mockRequest)?.isDashboardAdmin).toBe(true);
});

it('should get correct admin config when admin config is enabled ', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export const updateDashboardAdminStateForRequest = (
updateWorkspaceState(request, { isDashboardAdmin: true });
return;
}

// If groups/users are not configured or [], login defaults to OSD Admin
if (!configGroups.length && !configUsers.length) {
updateWorkspaceState(request, { isDashboardAdmin: false });
updateWorkspaceState(request, { isDashboardAdmin: true });
return;
}
const groupMatchAny = groups.some((group) => configGroups.includes(group));
Expand Down

0 comments on commit 6a871ce

Please sign in to comment.