forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Public URL support for Elastic Cloud (#21)
* Add server-side public URL route - Per feedback from Kibana platform team, it's not possible to pass info from server/ to public/ without a HTTP call :[ * Update MockRouter for routes without any payload/params * Add client-side helper for calling the new public URL API + API seems to return a URL a trailing slash, which we need to omit * Update public/plugin.ts to check and set a public URL - relies on this.hasCheckedPublicUrl to only make the call once per page load instead of on every page nav
- Loading branch information
Showing
8 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...search/public/applications/shared/enterprise_search_url/get_enterprise_search_url.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 { getPublicUrl } from './'; | ||
|
||
describe('Enterprise Search URL helper', () => { | ||
const httpMock = { get: jest.fn() } as any; | ||
|
||
it('calls and returns the public URL API endpoint', async () => { | ||
httpMock.get.mockImplementationOnce(() => ({ publicUrl: 'http://some.vanity.url' })); | ||
|
||
expect(await getPublicUrl(httpMock)).toEqual('http://some.vanity.url'); | ||
}); | ||
|
||
it('strips trailing slashes', async () => { | ||
httpMock.get.mockImplementationOnce(() => ({ publicUrl: 'http://trailing.slash/' })); | ||
|
||
expect(await getPublicUrl(httpMock)).toEqual('http://trailing.slash'); | ||
}); | ||
|
||
// For the most part, error logging/handling is done on the server side. | ||
// On the front-end, we should simply gracefully fall back to config.host | ||
// if we can't fetch a public URL | ||
it('falls back to an empty string', async () => { | ||
expect(await getPublicUrl(httpMock)).toEqual(''); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...rise_search/public/applications/shared/enterprise_search_url/get_enterprise_search_url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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 { HttpSetup } from 'src/core/public'; | ||
|
||
/** | ||
* On Elastic Cloud, the host URL set in kibana.yml is not necessarily the same | ||
* URL we want to send users to in the front-end (e.g. if a vanity URL is set). | ||
* | ||
* This helper checks a Kibana API endpoint (which has checks an Enterprise | ||
* Search internal API endpoint) for the correct public-facing URL to use. | ||
*/ | ||
export const getPublicUrl = async (http: HttpSetup): Promise<string> => { | ||
try { | ||
const { publicUrl } = await http.get('/api/enterprise_search/public_url'); | ||
return stripTrailingSlash(publicUrl); | ||
} catch { | ||
return ''; | ||
} | ||
}; | ||
|
||
const stripTrailingSlash = (url: string): string => { | ||
return url.endsWith('/') ? url.slice(0, -1) : url; | ||
}; |
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/enterprise_search/public/applications/shared/enterprise_search_url/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { getPublicUrl } from './get_enterprise_search_url'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/enterprise_search/server/routes/enterprise_search/public_url.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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 { MockRouter } from '../__mocks__/router.mock'; | ||
|
||
jest.mock('../../lib/enterprise_search_config_api', () => ({ | ||
callEnterpriseSearchConfigAPI: jest.fn(), | ||
})); | ||
import { callEnterpriseSearchConfigAPI } from '../../lib/enterprise_search_config_api'; | ||
|
||
import { registerPublicUrlRoute } from './public_url'; | ||
|
||
describe('Enterprise Search Public URL API', () => { | ||
const mockRouter = new MockRouter({ method: 'get' }); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockRouter.createRouter(); | ||
|
||
registerPublicUrlRoute({ | ||
router: mockRouter.router, | ||
config: {}, | ||
log: {}, | ||
} as any); | ||
}); | ||
|
||
describe('GET /api/enterprise_search/public_url', () => { | ||
it('returns a publicUrl', async () => { | ||
(callEnterpriseSearchConfigAPI as jest.Mock).mockImplementationOnce(() => { | ||
return Promise.resolve({ publicUrl: 'http://some.vanity.url' }); | ||
}); | ||
|
||
await mockRouter.callRoute({}); | ||
|
||
expect(mockRouter.response.ok).toHaveBeenCalledWith({ | ||
body: { publicUrl: 'http://some.vanity.url' }, | ||
headers: { 'content-type': 'application/json' }, | ||
}); | ||
}); | ||
|
||
// For the most part, all error logging is handled by callEnterpriseSearchConfigAPI. | ||
// This endpoint should mostly just fall back gracefully to an empty string | ||
it('falls back to an empty string', async () => { | ||
await mockRouter.callRoute({}); | ||
expect(mockRouter.response.ok).toHaveBeenCalledWith({ | ||
body: { publicUrl: '' }, | ||
headers: { 'content-type': 'application/json' }, | ||
}); | ||
}); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/enterprise_search/server/routes/enterprise_search/public_url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { IRouteDependencies } from '../../plugin'; | ||
import { callEnterpriseSearchConfigAPI } from '../../lib/enterprise_search_config_api'; | ||
|
||
export function registerPublicUrlRoute({ router, config, log }: IRouteDependencies) { | ||
router.get( | ||
{ | ||
path: '/api/enterprise_search/public_url', | ||
validate: false, | ||
}, | ||
async (context, request, response) => { | ||
const { publicUrl = '' } = | ||
(await callEnterpriseSearchConfigAPI({ request, config, log })) || {}; | ||
|
||
return response.ok({ | ||
body: { publicUrl }, | ||
headers: { 'content-type': 'application/json' }, | ||
}); | ||
} | ||
); | ||
} |