From 95760b536af98b554014d0d85453ee4a6636280b Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Mon, 28 Oct 2024 23:52:35 +0800 Subject: [PATCH] fix: generate short url with workspace info (#8719) * fix: generate short url with workspace info Signed-off-by: SuZhou-Joe * Changeset file for PR #8719 created/updated --------- Signed-off-by: SuZhou-Joe Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8719.yml | 2 ++ src/core/server/index.ts | 7 ++++++- src/plugins/share/public/services/share_menu_manager.tsx | 2 +- .../server/routes/lib/short_url_assert_valid.test.ts | 2 ++ .../share/server/routes/lib/short_url_assert_valid.ts | 9 ++++++++- 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/8719.yml diff --git a/changelogs/fragments/8719.yml b/changelogs/fragments/8719.yml new file mode 100644 index 000000000000..e0e6d17e26a4 --- /dev/null +++ b/changelogs/fragments/8719.yml @@ -0,0 +1,2 @@ +fix: +- Generate short url with workspace info ([#8719](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8719)) \ No newline at end of file diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 984318556b4e..7f2abc96cc40 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -373,7 +373,12 @@ export { } from './metrics'; export { AppCategory, WorkspaceAttribute, PermissionModeId } from '../types'; -export { DEFAULT_APP_CATEGORIES, WORKSPACE_TYPE, DEFAULT_NAV_GROUPS } from '../utils'; +export { + DEFAULT_APP_CATEGORIES, + WORKSPACE_TYPE, + DEFAULT_NAV_GROUPS, + WORKSPACE_PATH_PREFIX, +} from '../utils'; export { SavedObject, diff --git a/src/plugins/share/public/services/share_menu_manager.tsx b/src/plugins/share/public/services/share_menu_manager.tsx index 5d8210b48c5b..da76f8f3897c 100644 --- a/src/plugins/share/public/services/share_menu_manager.tsx +++ b/src/plugins/share/public/services/share_menu_manager.tsx @@ -56,7 +56,7 @@ export class ShareMenuManager { ...options, menuItems, post: core.http.post, - basePath: core.http.basePath.get(), + basePath: core.http.basePath.getBasePath(), }); }, }; diff --git a/src/plugins/share/server/routes/lib/short_url_assert_valid.test.ts b/src/plugins/share/server/routes/lib/short_url_assert_valid.test.ts index 7f9744119c73..c272185fed59 100644 --- a/src/plugins/share/server/routes/lib/short_url_assert_valid.test.ts +++ b/src/plugins/share/server/routes/lib/short_url_assert_valid.test.ts @@ -49,6 +49,7 @@ describe('shortUrlAssertValid()', () => { ['path with an extra leading slash', '///app/opensearch-dashboards', HOSTNAME_ERROR], // parser detects '' as the hostname ['path without app', '/foo/opensearch-dashboards', PATH_ERROR], // fails because first path part is not 'app' ['path without appId', '/app/', PATH_ERROR], // fails because there is only one path part (leading and trailing slashes are trimmed) + '/w/app/dashboards', // fails because it is not a standard workspace path ]; invalid.forEach(([desc, url, error]) => { @@ -67,6 +68,7 @@ describe('shortUrlAssertValid()', () => { '/app/some?with=query', '/app/some?with=query#and-a-hash', '/app/some/deeper?with=query#and-a-hash', + '/w/workspaceid/app/foo', ]; valid.forEach((url) => { diff --git a/src/plugins/share/server/routes/lib/short_url_assert_valid.ts b/src/plugins/share/server/routes/lib/short_url_assert_valid.ts index b6514a68e6f6..248249e33225 100644 --- a/src/plugins/share/server/routes/lib/short_url_assert_valid.ts +++ b/src/plugins/share/server/routes/lib/short_url_assert_valid.ts @@ -31,6 +31,7 @@ import { parse } from 'url'; import { trim } from 'lodash'; import Boom from '@hapi/boom'; +import { WORKSPACE_PATH_PREFIX } from '../../../../../core/server'; export function shortUrlAssertValid(url: string) { const { protocol, hostname, pathname } = parse( @@ -47,7 +48,13 @@ export function shortUrlAssertValid(url: string) { throw Boom.notAcceptable(`Short url targets cannot have a hostname, found "${hostname}"`); } - const pathnameParts = trim(pathname === null ? undefined : pathname, '/').split('/'); + let pathnameParts = trim(pathname === null ? undefined : pathname, '/').split('/'); + + // Workspace introduced paths like `/w/${workspaceId}/app` + // ignore the first 2 elements if it starts with /w + if (`/${pathnameParts[0]}` === WORKSPACE_PATH_PREFIX) { + pathnameParts = pathnameParts.slice(2); + } if (pathnameParts[0] !== 'app' || !pathnameParts[1]) { throw Boom.notAcceptable( `Short url target path must be in the format "/app/{{appId}}", found "${pathname}"`