Skip to content

Commit

Permalink
feat: change savedObjectsClient in runtime
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Aug 25, 2023
1 parent e8560b4 commit 7bac360
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
private coreStart?: CoreStart;
private config$: Observable<ConfigSchema>;
private enabled$: BehaviorSubject<boolean> = new BehaviorSubject(false);
private workspaceSavedObjectsClientWrapper?: WorkspaceSavedObjectsClientWrapper;

private get isEnabled() {
return this.enabled$.getValue();
Expand Down Expand Up @@ -70,7 +71,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
this.client = new WorkspaceClientWithSavedObject(core);

await this.client.setup(core);
const workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper(
this.workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper(
core.savedObjects.permissionControl,
{
config$: this.config$,
Expand All @@ -81,7 +82,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
core.savedObjects.addClientWrapper(
0,
'workspace',
workspaceSavedObjectsClientWrapper.wrapperFactory
this.workspaceSavedObjectsClientWrapper.wrapperFactory
);

this.proxyWorkspaceTrafficToRealHandler(core);
Expand All @@ -94,10 +95,6 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
config$: this.config$,
});

core.savedObjects.setClientFactoryProvider((repositoryFactory) => () =>
new SavedObjectsClient(repositoryFactory.createInternalRepository())
);

return {
client: this.client,
enabled$: this.enabled$,
Expand Down Expand Up @@ -202,6 +199,10 @@ export class WorkspacePlugin implements Plugin<{}, {}> {

this.coreStart = core;

this.workspaceSavedObjectsClientWrapper?.setInternalRepositoryFactory(
core.savedObjects.createInternalRepository
);

this.setupWorkspaceFeatureFlag();

this.enabled$.subscribe((enabled) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
WORKSPACE_TYPE,
ACL,
WorkspacePermissionMode,
SavedObjectsClient,
SavedObjectsRepositoryFactory,
} from '../../../../core/server';
import { ConfigSchema } from '../../config';

Expand Down Expand Up @@ -60,6 +62,7 @@ const isWorkspacesLikeAttributes = (attributes: unknown): attributes is Attribut

export class WorkspaceSavedObjectsClientWrapper {
private config?: ConfigSchema;
private internalRepositoryFactory?: SavedObjectsRepositoryFactory['createInternalRepository'];
private formatWorkspacePermissionModeToStringArray(
permission: WorkspacePermissionMode | WorkspacePermissionMode[]
): string[] {
Expand Down Expand Up @@ -160,6 +163,23 @@ export class WorkspaceSavedObjectsClientWrapper {
}

public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
/**
* The client here is scopedSavedObjectsClient by default
*/
let client = wrapperOptions.client;
const featureFlagEnabled = this.options.enabled$.getValue();

if (!featureFlagEnabled) {
return client;
}

/**
* If featureFlag is open and we have internalRepositoryFactory
* Use internal repository as access control will be provided by ACL.
*/
if (featureFlagEnabled && this.internalRepositoryFactory) {
client = new SavedObjectsClient(this.internalRepositoryFactory());
}
const deleteWithWorkspacePermissionControl = async (
type: string,
id: string,
Expand All @@ -171,13 +191,13 @@ export class WorkspaceSavedObjectsClientWrapper {
]);
}

const objectToDeleted = await wrapperOptions.client.get(type, id, options);
const objectToDeleted = await client.get(type, id, options);
await this.validateMultiWorkspacesPermissions(
objectToDeleted.workspaces,
wrapperOptions.request,
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Management]
);
return await wrapperOptions.client.delete(type, id, options);
return await client.delete(type, id, options);
};

const updateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -191,7 +211,7 @@ export class WorkspaceSavedObjectsClientWrapper {
WorkspacePermissionMode.Management,
]);
}
return await wrapperOptions.client.update(type, id, attributes, options);
return await client.update(type, id, attributes, options);
};

const bulkUpdateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -213,7 +233,7 @@ export class WorkspaceSavedObjectsClientWrapper {
throw generateWorkspacePermissionError();
}

return await wrapperOptions.client.bulkUpdate(objects, options);
return await client.bulkUpdate(objects, options);
};

const bulkCreateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -226,7 +246,7 @@ export class WorkspaceSavedObjectsClientWrapper {
WorkspacePermissionMode.Management,
]);
}
return await wrapperOptions.client.bulkCreate(objects, options);
return await client.bulkCreate(objects, options);
};

const createWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -241,15 +261,15 @@ export class WorkspaceSavedObjectsClientWrapper {
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Management]
);
}
return await wrapperOptions.client.create(type, attributes, options);
return await client.create(type, attributes, options);
};

const getWithWorkspacePermissionControl = async <T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> => {
const objectToGet = await wrapperOptions.client.get<T>(type, id, options);
const objectToGet = await client.get<T>(type, id, options);
await this.validateAtLeastOnePermittedWorkspaces(
objectToGet.workspaces,
wrapperOptions.request,
Expand All @@ -266,7 +286,7 @@ export class WorkspaceSavedObjectsClientWrapper {
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
const objectToBulkGet = await wrapperOptions.client.bulkGet<T>(objects, options);
const objectToBulkGet = await client.bulkGet<T>(objects, options);
for (const object of objectToBulkGet.saved_objects) {
await this.validateAtLeastOnePermittedWorkspaces(
object.workspaces,
Expand Down Expand Up @@ -375,7 +395,7 @@ export class WorkspaceSavedObjectsClientWrapper {
}
}

return await wrapperOptions.client.find<T>(options);
return await client.find<T>(options);
};

const addToWorkspacesWithPermissionControl = async (
Expand All @@ -402,24 +422,24 @@ export class WorkspaceSavedObjectsClientWrapper {
throw generateSavedObjectsPermissionError();
}

return await wrapperOptions.client.addToWorkspaces(objects, targetWorkspaces, options);
return await client.addToWorkspaces(objects, targetWorkspaces, options);
};

const isDashboardAdmin = this.isDashboardAdmin(wrapperOptions.request);

if (isDashboardAdmin || !this.options.enabled$.getValue()) {
return wrapperOptions.client;
if (isDashboardAdmin) {
return client;
}

return {
...wrapperOptions.client,
...client,
get: getWithWorkspacePermissionControl,
checkConflicts: wrapperOptions.client.checkConflicts,
checkConflicts: client.checkConflicts,
find: findWithWorkspacePermissionControl,
bulkGet: bulkGetWithWorkspacePermissionControl,
errors: wrapperOptions.client.errors,
addToNamespaces: wrapperOptions.client.addToNamespaces,
deleteFromNamespaces: wrapperOptions.client.deleteFromNamespaces,
errors: client.errors,
addToNamespaces: client.addToNamespaces,
deleteFromNamespaces: client.deleteFromNamespaces,
create: createWithWorkspacePermissionControl,
bulkCreate: bulkCreateWithWorkspacePermissionControl,
delete: deleteWithWorkspacePermissionControl,
Expand All @@ -429,6 +449,12 @@ export class WorkspaceSavedObjectsClientWrapper {
};
};

public setInternalRepositoryFactory(
internalRepositoryFactory: SavedObjectsRepositoryFactory['createInternalRepository']
) {
this.internalRepositoryFactory = internalRepositoryFactory;
}

constructor(
private readonly permissionControl: SavedObjectsPermissionControlContract,
private readonly options: {
Expand Down

0 comments on commit 7bac360

Please sign in to comment.