Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Capabilities service add dashboard admin flag #7103

Merged
merged 10 commits into from
Jul 2, 2024
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;
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading