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

feat: add workspace into includeHiddenTypes #249

Merged
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
8 changes: 7 additions & 1 deletion src/plugins/workspace/server/permission_control/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { i18n } from '@osd/i18n';
import { OpenSearchDashboardsRequest, Principals, SavedObject } from '../../../../core/server';
import {
OpenSearchDashboardsRequest,
Principals,
SavedObject,
WORKSPACE_TYPE,
} from '../../../../core/server';
import {
ACL,
TransformedPermission,
Expand All @@ -27,6 +32,7 @@ export class SavedObjectsPermissionControl {
private getScopedClient(request: OpenSearchDashboardsRequest) {
return this._getScopedClient?.(request, {
excludedWrappers: [WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID],
includedHiddenTypes: [WORKSPACE_TYPE],
});
}

Expand Down
7 changes: 5 additions & 2 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Plugin,
Logger,
SavedObjectsClient,
WORKSPACE_TYPE,
} from '../../../core/server';
import { IWorkspaceClientImpl } from './types';
import { WorkspaceClientWithSavedObject } from './workspace_client';
Expand All @@ -30,6 +31,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
private client?: IWorkspaceClientImpl;
private permissionControl?: SavedObjectsPermissionControlContract;
private readonly config$: Observable<ConfigSchema>;
private workspaceSavedObjectsClientWrapper?: WorkspaceSavedObjectsClientWrapper;

private proxyWorkspaceTrafficToRealHandler(setupDeps: CoreSetup) {
/**
Expand Down Expand Up @@ -71,14 +73,14 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
permissionControl: this.permissionControl,
});

const workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper(
this.workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper(
this.permissionControl
);

core.savedObjects.addClientWrapper(
0,
WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
workspaceSavedObjectsClientWrapper.wrapperFactory
this.workspaceSavedObjectsClientWrapper.wrapperFactory
);
}

Expand Down Expand Up @@ -111,6 +113,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
this.logger.debug('Starting SavedObjects service');
this.permissionControl?.setup(core.savedObjects.getScopedClient);
this.client?.setSavedObjects(core.savedObjects);
this.workspaceSavedObjectsClientWrapper?.setScopedClient(core.savedObjects.getScopedClient);

return {
client: this.client as IWorkspaceClientImpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ import {
WorkspacePermissionMode,
SavedObjectsDeleteByWorkspaceOptions,
SavedObjectsErrorHelpers,
SavedObjectsServiceStart,
SavedObjectsClientContract,
} from '../../../../core/server';
import { SavedObjectsPermissionControlContract } from '../permission_control/client';
import { getPrincipalsFromRequest } from '../utils';
import { WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID } from '../../common/constants';

// Can't throw unauthorized for now, the page will be refreshed if unauthorized
const generateWorkspacePermissionError = () =>
Expand All @@ -52,6 +55,7 @@ const generateSavedObjectsPermissionError = () =>
);

export class WorkspaceSavedObjectsClientWrapper {
private getScopedClient?: SavedObjectsServiceStart['getScopedClient'];
private formatWorkspacePermissionModeToStringArray(
permission: WorkspacePermissionMode | WorkspacePermissionMode[]
): string[] {
Expand Down Expand Up @@ -175,6 +179,17 @@ export class WorkspaceSavedObjectsClientWrapper {
return hasPermission;
}

private getWorkspaceTypeEnabledClient(request: OpenSearchDashboardsRequest) {
return this.getScopedClient?.(request, {
includedHiddenTypes: [WORKSPACE_TYPE],
excludedWrappers: [WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID],
}) as SavedObjectsClientContract;
}

public setScopedClient(getScopedClient: SavedObjectsServiceStart['getScopedClient']) {
this.getScopedClient = getScopedClient;
}

public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
const deleteWithWorkspacePermissionControl = async (
type: string,
Expand Down Expand Up @@ -398,8 +413,12 @@ export class WorkspaceSavedObjectsClientWrapper {
];
options.ACLSearchParams.principals = principals;
} else {
/**
* Workspace is a hidden type so that we need to
* initialize a new saved objects client with workspace enabled to retrieve all the workspaces with permission.
*/
const permittedWorkspaceIds = (
await wrapperOptions.client.find({
await this.getWorkspaceTypeEnabledClient(wrapperOptions.request).find({
type: WORKSPACE_TYPE,
perPage: 999,
ACLSearchParams: {
Expand Down
Loading