Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GS] use the request's basePath when processing server-side result urls #76747

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x-pack/plugins/global_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export interface GlobalSearchProviderResult {
icon?: string;
/**
* The url associated with this result.
* This can be either an absolute url, a path relative to the basePath, or a structure specifying if the basePath should be prepended.
* This can be either an absolute url, a path relative to the incoming request's basePath, or a structure specifying if the basePath should be prepended.
*
* @example
* `result.url = 'https://kibana-instance:8080/base-path/app/my-app/my-result-type/id';`
* `result.url = '/app/my-app/my-result-type/id';`
* `result.url = { path: '/base-path/app/my-app/my-result-type/id', prependBasePath: false };`
* `result.url = { path: '/base-path/s/my-other-space/app/my-app/my-result-type/id', prependBasePath: false };`
*/
url: GlobalSearchProviderResultUrl;
/** the score of the result, from 1 (lowest) to 100 (highest) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ describe('SearchService', () => {

beforeEach(() => {
service = new SearchService();
basePath = httpServiceMock.createBasePath();
basePath.prepend.mockImplementation((path) => `/base-path${path}`);
basePath = httpServiceMock.createBasePath('/base-path');
basePath.get.mockReturnValue('/base-path/s/space');
coreStart = coreMock.createStart();
licenseChecker = licenseCheckerMock.create();
});
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('SearchService', () => {
expect(batch.results).toHaveLength(2);
expect(batch.results[0]).toEqual({
...resultA,
url: '/base-path/foo/bar',
url: '/base-path/s/space/foo/bar',
});
expect(batch.results[1]).toEqual({
...resultB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { processProviderResult } from '../../common/process_result';
import { GlobalSearchConfigType } from '../config';
import { getContextFactory, GlobalSearchContextFactory } from './context';
import { GlobalSearchResultProvider, GlobalSearchFindOptions } from '../types';
import { getRequestBasePath } from './utils';

/** @public */
export interface SearchServiceSetup {
Expand Down Expand Up @@ -132,6 +133,7 @@ export class SearchService {
}

const context = this.contextFactory!(request);
const basePath = getRequestBasePath(request, this.basePath!);

const timeout$ = timer(this.config!.search_timeout.asMilliseconds()).pipe(map(mapToUndefined));
const aborted$ = options.aborted$ ? merge(options.aborted$, timeout$) : timeout$;
Expand All @@ -143,7 +145,7 @@ export class SearchService {
};

const processResult = (result: GlobalSearchProviderResult) =>
processProviderResult(result, this.basePath!);
processProviderResult(result, basePath);

const providersResults$ = [...this.providers.values()].map((provider) =>
provider.find(term, providerOptions, context).pipe(
Expand Down
34 changes: 34 additions & 0 deletions x-pack/plugins/global_search/server/services/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { httpServiceMock, httpServerMock } from '../../../../../src/core/server/mocks';
import { getRequestBasePath } from './utils';

describe('getRequestBasePath', () => {
let basePath: ReturnType<typeof httpServiceMock.createBasePath>;
let request: ReturnType<typeof httpServerMock.createKibanaRequest>;

beforeEach(() => {
basePath = httpServiceMock.createBasePath();
request = httpServerMock.createKibanaRequest();
});

it('return a IBasePath prepending the request basePath', () => {
basePath.get.mockReturnValue('/base-path/s/my-space');
const requestBasePath = getRequestBasePath(request, basePath);

const fullPath = requestBasePath.prepend('/app/dashboard/some-id');

expect(fullPath).toBe('/base-path/s/my-space/app/dashboard/some-id');

expect(basePath.get).toHaveBeenCalledTimes(1);
expect(basePath.get).toHaveBeenCalledWith(request);

expect(basePath.prepend).not.toHaveBeenCalled();
});
});

httpServiceMock.createBasePath();
20 changes: 20 additions & 0 deletions x-pack/plugins/global_search/server/services/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import type { IBasePath, KibanaRequest } from 'src/core/server';
import type { IBasePath as BasePathAccessor } from '../../common/utils';

export const getRequestBasePath = (
request: KibanaRequest,
basePath: IBasePath
): BasePathAccessor => {
const requestBasePath = basePath.get(request);
return {
prepend: (path) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to reuse basePath.prepend here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there is no way to only retrieve the request-specific (requestScopePath) part of the basePath via the service, as get returns the 'whole' base path:

/**
* returns `basePath` value, specific for an incoming request.
*/
public get = (request: KibanaRequest | LegacyRequest) => {
const requestScopePath = this.basePathCache.get(ensureRawRequest(request)) || '';
return `${this.serverBasePath}${requestScopePath}`;
};

and prepend only allow to preprend using the server's base path, not a request-scoped basePath:

public prepend = (path: string): string => {
if (this.serverBasePath === '') return path;
return modifyUrl(path, (parts) => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.serverBasePath}${parts.pathname}`;
}
});
};

But maybe adding a new core API is better. I was surprised to be the first to have such need on the server-side TBH.

I can add something like prependRequestPath(path: string, request: KibanaRequest) to the server-side BasePath?

return `${requestBasePath}/${path}`.replace(/\/{2,}/g, '/');
},
};
};