From 48b00910c9eded498bf97c97802f3969d573e516 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Mon, 16 Sep 2019 10:13:05 -0400 Subject: [PATCH] improve handling when navigating to the root of a non-default space --- .../on_post_auth_interceptor.test.ts | 67 +++++++++++++++++++ .../on_post_auth_interceptor.ts | 12 ++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts b/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts index eb640671814f7..677b10de8ecbf 100644 --- a/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts +++ b/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts @@ -347,6 +347,73 @@ describe('onPostAuthInterceptor', () => { ); }); + it('redirects to the space selector when accessing the root of the default space', async () => { + const spaces = [ + { + id: 'default', + type: 'space', + attributes: { + name: 'Default space', + _reserved: true, + }, + }, + { + id: 'a-space', + type: 'space', + attributes: { + name: 'a space', + }, + }, + ]; + + const { response, spacesService } = await request('/', spaces); + + expect(response.status).toEqual(302); + expect(response.header.location).toEqual(`/spaces/space_selector`); + + expect(spacesService.scopedClient).toHaveBeenCalledWith( + expect.objectContaining({ + headers: expect.objectContaining({ + authorization: headers.authorization, + }), + }) + ); + }, 30000); + + it('allows the request to continue when accessing the root of a non-default space', async () => { + const spaces = [ + { + id: 'default', + type: 'space', + attributes: { + name: 'Default space', + _reserved: true, + }, + }, + { + id: 'a-space', + type: 'space', + attributes: { + name: 'a space', + }, + }, + ]; + + const { response, spacesService } = await request('/s/a-space', spaces); + + // OSS handles this redirection for us + expect(response.status).toEqual(302); + expect(response.header.location).toEqual(`/s/a-space${defaultRoute}`); + + expect(spacesService.scopedClient).toHaveBeenCalledWith( + expect.objectContaining({ + headers: expect.objectContaining({ + authorization: headers.authorization, + }), + }) + ); + }, 30000); + describe('with a single available space', () => { it('it redirects to the defaultRoute within the context of the single Space when navigating to Kibana root', async () => { const spaces = [ diff --git a/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.ts b/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.ts index 51e587f5410e2..0fed454621e04 100644 --- a/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.ts +++ b/x-pack/legacy/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.ts @@ -6,11 +6,12 @@ import { Logger, CoreSetup } from 'src/core/server'; import { Space } from '../../../common/model/space'; import { wrapError } from '../errors'; -import { addSpaceIdToPath, getSpaceIdFromPath } from '../spaces_url_parser'; +import { addSpaceIdToPath } from '../spaces_url_parser'; import { XPackMainPlugin } from '../../../../xpack_main/xpack_main'; import { SpacesServiceSetup } from '../../new_platform/spaces_service/spaces_service'; import { LegacyAPI } from '../../new_platform/plugin'; import { getSpaceSelectorUrl } from '../get_space_selector_url'; +import { DEFAULT_SPACE_ID } from '../../../common/constants'; export interface OnPostAuthInterceptorDeps { getLegacyAPI(): LegacyAPI; @@ -32,7 +33,11 @@ export function initSpacesOnPostAuthRequestInterceptor({ http.registerOnPostAuth(async (request, response, toolkit) => { const path = request.url.pathname!; - const isRequestingKibanaRoot = path === '/'; + const spaceId = spacesService.getSpaceId(request); + + // The root of kibana is also the root of the defaut space, + // since the default space does not have a URL Identifier (i.e., `/s/foo`). + const isRequestingKibanaRoot = path === '/' && spaceId === DEFAULT_SPACE_ID; const isRequestingApplication = path.startsWith('/app'); const spacesClient = await spacesService.scopedClient(request); @@ -71,11 +76,8 @@ export function initSpacesOnPostAuthRequestInterceptor({ // This condition should only happen after selecting a space, or when transitioning from one application to another // e.g.: Navigating from Dashboard to Timelion if (isRequestingApplication) { - let spaceId: string = ''; let space: Space; try { - spaceId = getSpaceIdFromPath(http.basePath.get(request), serverBasePath); - log.debug(`Verifying access to space "${spaceId}"`); space = await spacesClient.get(spaceId);