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

[BUG] Fix permission check failed with empty workspace for find method #6527

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6527.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Permission check failed with empty workspace for find method ([#6527](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6527))
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,24 @@ describe('WorkspaceIdConsumerWrapper', () => {
workspacesSearchOperator: 'AND',
});
});

it(`Should not pass a empty workspace array`, async () => {
const workspaceIdConsumerWrapper = new WorkspaceIdConsumerWrapper(true);
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
updateWorkspaceState(mockRequest, {});
const mockedWrapperClient = workspaceIdConsumerWrapper.wrapperFactory({
client: mockedClient,
typeRegistry: requestHandlerContext.savedObjects.typeRegistry,
request: mockRequest,
});
await mockedWrapperClient.find({
type: ['dashboard', 'visualization'],
});
// empty workspace array will get deleted
expect(mockedClient.find).toBeCalledWith({
type: ['dashboard', 'visualization'],
workspacesSearchOperator: 'OR',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export class WorkspaceIdConsumerWrapper {
findOptions.workspaces.splice(index, 1);
}
}
if (findOptions.workspaces && findOptions.workspaces.length === 0) {
delete findOptions.workspaces;
}
return wrapperOptions.client.find(findOptions);
},
bulkGet: wrapperOptions.client.bulkGet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,55 @@ describe('WorkspaceSavedObjectsClientWrapper', () => {
},
});
});
it('should call client.find with ACLSearchParams if no workspaces is provided', async () => {
const { wrapper, clientMock } = generateWorkspaceSavedObjectsClientWrapper();
// no workspaces
await wrapper.find({
type: 'dashboards',
});
expect(clientMock.find).toHaveBeenCalledWith({
Hailong-am marked this conversation as resolved.
Show resolved Hide resolved
type: 'dashboards',
ACLSearchParams: {
principals: {
users: ['user-1'],
},
permissionModes: ['read', 'write'],
},
});

// workspaces parameter is undefined
clientMock.find.mockReset();
await wrapper.find({
type: 'dashboards',
workspaces: undefined,
});
expect(clientMock.find).toHaveBeenLastCalledWith({
type: 'dashboards',
ACLSearchParams: {
principals: {
users: ['user-1'],
},
permissionModes: ['read', 'write'],
},
});

// empty workspaces array
clientMock.find.mockReset();
await wrapper.find({
type: 'dashboards',
workspaces: [],
});
expect(clientMock.find).toHaveBeenLastCalledWith({
type: 'dashboards',
workspaces: [],
ACLSearchParams: {
principals: {
users: ['user-1'],
},
permissionModes: ['read', 'write'],
},
});
});
it('should call client.find with only read permission if find workspace and permissionModes provided', async () => {
const { wrapper, clientMock } = generateWorkspaceSavedObjectsClientWrapper();
await wrapper.find({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class WorkspaceSavedObjectsClientWrapper {
})
).saved_objects.map((item) => item.id);

if (options.workspaces) {
if (options.workspaces && options.workspaces.length > 0) {
Hailong-am marked this conversation as resolved.
Show resolved Hide resolved
const permittedWorkspaces = options.workspaces.filter((item) =>
permittedWorkspaceIds.includes(item)
);
Expand Down
Loading