Skip to content

Commit

Permalink
feat: throw error if the type is not allowed
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Apr 10, 2024
1 parent f65ffcf commit 57ddd07
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export {
StringValidation,
StringValidationRegex,
StringValidationRegexString,
UI_SETTINGS_SAVED_OBJECTS_TYPE,
} from './ui_settings';

export {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/ui_settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ export {
StringValidationRegex,
StringValidationRegexString,
} from './types';

export { UI_SETTINGS_SAVED_OBJECTS_TYPE } from './saved_objects/ui_settings';
4 changes: 3 additions & 1 deletion src/core/server/ui_settings/saved_objects/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/core/server/ui_settings/ui_settings_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -61,11 +70,34 @@ export class WorkspaceIdConsumerWrapper {
bulkCreate: <T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
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 = {}
Expand Down

0 comments on commit 57ddd07

Please sign in to comment.