From 57ddd07e0d98754413f190e81e8978750b6b6cfa Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Wed, 10 Apr 2024 15:45:54 +0800 Subject: [PATCH] feat: throw error if the type is not allowed Signed-off-by: SuZhou-Joe --- src/core/server/index.ts | 1 + src/core/server/ui_settings/index.ts | 2 + .../ui_settings/saved_objects/ui_settings.ts | 4 +- .../server/ui_settings/ui_settings_service.ts | 3 +- .../workspace_id_consumer_wrapper.ts | 42 ++++++++++++++++--- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/core/server/index.ts b/src/core/server/index.ts index f497bed22755..b0e1179fbc3b 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -341,6 +341,7 @@ export { StringValidation, StringValidationRegex, StringValidationRegexString, + UI_SETTINGS_SAVED_OBJECTS_TYPE, } from './ui_settings'; export { diff --git a/src/core/server/ui_settings/index.ts b/src/core/server/ui_settings/index.ts index 7912c0af84af..3738afef95b1 100644 --- a/src/core/server/ui_settings/index.ts +++ b/src/core/server/ui_settings/index.ts @@ -49,3 +49,5 @@ export { StringValidationRegex, StringValidationRegexString, } from './types'; + +export { UI_SETTINGS_SAVED_OBJECTS_TYPE } from './saved_objects/ui_settings'; diff --git a/src/core/server/ui_settings/saved_objects/ui_settings.ts b/src/core/server/ui_settings/saved_objects/ui_settings.ts index a56b12ed2063..968c3ceb5691 100644 --- a/src/core/server/ui_settings/saved_objects/ui_settings.ts +++ b/src/core/server/ui_settings/saved_objects/ui_settings.ts @@ -31,8 +31,10 @@ import { SavedObjectsType } from '../../saved_objects'; import { migrations } from './migrations'; +export const UI_SETTINGS_SAVED_OBJECTS_TYPE = 'config'; + export const uiSettingsType: SavedObjectsType = { - name: 'config', + name: UI_SETTINGS_SAVED_OBJECTS_TYPE, hidden: false, namespaceType: 'single', mappings: { diff --git a/src/core/server/ui_settings/ui_settings_service.ts b/src/core/server/ui_settings/ui_settings_service.ts index d20b1e964e5b..514cdca96b36 100644 --- a/src/core/server/ui_settings/ui_settings_service.ts +++ b/src/core/server/ui_settings/ui_settings_service.ts @@ -48,6 +48,7 @@ import { import { uiSettingsType } from './saved_objects'; import { registerRoutes } from './routes'; import { getCoreSettings } from './settings'; +import { UI_SETTINGS_SAVED_OBJECTS_TYPE } from './saved_objects/ui_settings'; export interface SetupDeps { http: InternalHttpServiceSetup; @@ -102,7 +103,7 @@ export class UiSettingsService const { version, buildNum } = this.coreContext.env.packageInfo; return (savedObjectsClient: SavedObjectsClientContract) => new UiSettingsClient({ - type: 'config', + type: UI_SETTINGS_SAVED_OBJECTS_TYPE, id: version, buildNum, savedObjectsClient, diff --git a/src/plugins/workspace/server/saved_objects/workspace_id_consumer_wrapper.ts b/src/plugins/workspace/server/saved_objects/workspace_id_consumer_wrapper.ts index 9e94e8945d62..72bd6305b61d 100644 --- a/src/plugins/workspace/server/saved_objects/workspace_id_consumer_wrapper.ts +++ b/src/plugins/workspace/server/saved_objects/workspace_id_consumer_wrapper.ts @@ -12,6 +12,8 @@ import { SavedObjectsCheckConflictsObject, OpenSearchDashboardsRequest, SavedObjectsFindOptions, + UI_SETTINGS_SAVED_OBJECTS_TYPE, + SavedObjectsErrorHelpers, } from '../../../../core/server'; import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../../../plugins/data_source/common'; @@ -45,6 +47,13 @@ export class WorkspaceIdConsumerWrapper { return type === DATA_SOURCE_SAVED_OBJECT_TYPE; } + private isConfigType(type: SavedObjectsFindOptions['type']): boolean { + if (Array.isArray(type)) { + return type.every((item) => item === UI_SETTINGS_SAVED_OBJECTS_TYPE); + } + + return type === UI_SETTINGS_SAVED_OBJECTS_TYPE; + } private formatFindParams(options: SavedObjectsFindOptions): SavedObjectsFindOptions { const isListingDataSource = this.isDataSourceType(options.type); return isListingDataSource ? { ...options, workspaces: null } : options; @@ -61,11 +70,34 @@ export class WorkspaceIdConsumerWrapper { bulkCreate: ( objects: Array>, options: SavedObjectsCreateOptions = {} - ) => - wrapperOptions.client.bulkCreate( - objects, - this.formatWorkspaceIdParams(wrapperOptions.request, options) - ), + ) => { + const { workspaces } = this.formatWorkspaceIdParams(wrapperOptions.request, options); + const disallowedSavedObjects = objects.filter((item) => { + // If create out of workspace, allow the operation + if (!workspaces?.length && !item.workspaces?.length) { + return false; + } + + // config and data-sources can not be created inside a workspace + return this.isConfigType(item.type) || this.isDataSourceType(item.type); + }); + + if (!disallowedSavedObjects?.length) { + return wrapperOptions.client.bulkCreate( + objects, + this.formatWorkspaceIdParams(wrapperOptions.request, options) + ); + } + + const disallowedTypes = [...new Set(disallowedSavedObjects.map((item) => item.type))]; + + throw SavedObjectsErrorHelpers.decorateBadRequestError( + new Error(''), + `${disallowedTypes.map((item) => `type: ${item}`).join(', ')} ${ + disallowedTypes.length > 1 ? 'are' : 'is' + } not allowed to create within a workspace.` + ); + }, checkConflicts: ( objects: SavedObjectsCheckConflictsObject[] = [], options: SavedObjectsBaseOptions = {}