-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning toast when server.publicBaseUrl not configured correctly (#…
- Loading branch information
Showing
10 changed files
with
225 additions
and
7 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
docs/development/core/public/kibana-plugin-core-public.doclinksstart.md
Large diffs are not rendered by default.
Oops, something went wrong.
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
114 changes: 114 additions & 0 deletions
114
src/core/public/core_app/errors/public_base_url.test.tsx
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,114 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { docLinksServiceMock } from '../../doc_links/doc_links_service.mock'; | ||
import { httpServiceMock } from '../../http/http_service.mock'; | ||
import { notificationServiceMock } from '../../notifications/notifications_service.mock'; | ||
|
||
import { setupPublicBaseUrlConfigWarning } from './public_base_url'; | ||
|
||
describe('publicBaseUrl warning', () => { | ||
const docLinks = docLinksServiceMock.createStartContract(); | ||
const notifications = notificationServiceMock.createStartContract(); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('does not show any toast on localhost', () => { | ||
const http = httpServiceMock.createStartContract(); | ||
|
||
setupPublicBaseUrlConfigWarning({ | ||
docLinks, | ||
notifications, | ||
http, | ||
location: { | ||
hostname: 'localhost', | ||
} as Location, | ||
}); | ||
|
||
expect(notifications.toasts.addWarning).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not show any toast on 127.0.0.1', () => { | ||
const http = httpServiceMock.createStartContract(); | ||
|
||
setupPublicBaseUrlConfigWarning({ | ||
docLinks, | ||
notifications, | ||
http, | ||
location: { | ||
hostname: '127.0.0.1', | ||
} as Location, | ||
}); | ||
|
||
expect(notifications.toasts.addWarning).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not show toast if configured correctly', () => { | ||
const http = httpServiceMock.createStartContract({ publicBaseUrl: 'http://myhost.com' }); | ||
|
||
setupPublicBaseUrlConfigWarning({ | ||
docLinks, | ||
notifications, | ||
http, | ||
location: { | ||
hostname: 'myhost.com', | ||
toString() { | ||
return 'http://myhost.com/'; | ||
}, | ||
} as Location, | ||
}); | ||
|
||
expect(notifications.toasts.addWarning).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe('config missing toast', () => { | ||
it('adds toast if publicBaseUrl is missing', () => { | ||
const http = httpServiceMock.createStartContract({ publicBaseUrl: undefined }); | ||
|
||
setupPublicBaseUrlConfigWarning({ | ||
docLinks, | ||
notifications, | ||
http, | ||
location: { | ||
hostname: 'myhost.com', | ||
toString() { | ||
return 'http://myhost.com/'; | ||
}, | ||
} as Location, | ||
}); | ||
|
||
expect(notifications.toasts.addWarning).toHaveBeenCalledWith({ | ||
title: 'Configuration missing', | ||
text: expect.any(Function), | ||
}); | ||
}); | ||
|
||
it('does not add toast if storage key set', () => { | ||
const http = httpServiceMock.createStartContract({ publicBaseUrl: undefined }); | ||
|
||
setupPublicBaseUrlConfigWarning({ | ||
docLinks, | ||
notifications, | ||
http, | ||
location: { | ||
hostname: 'myhost.com', | ||
toString() { | ||
return 'http://myhost.com/'; | ||
}, | ||
} as Location, | ||
storage: { | ||
getItem: (id: string) => 'true', | ||
} as Storage, | ||
}); | ||
|
||
expect(notifications.toasts.addWarning).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,88 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { EuiButton, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
|
||
import type { HttpStart, NotificationsStart } from '../..'; | ||
import type { DocLinksStart } from '../../doc_links'; | ||
import { mountReactNode } from '../../utils'; | ||
|
||
/** Only exported for tests */ | ||
export const MISSING_CONFIG_STORAGE_KEY = `core.warnings.publicBaseUrlMissingDismissed`; | ||
|
||
interface Deps { | ||
docLinks: DocLinksStart; | ||
http: HttpStart; | ||
notifications: NotificationsStart; | ||
// Exposed for easier testing | ||
storage?: Storage; | ||
location?: Location; | ||
} | ||
|
||
export const setupPublicBaseUrlConfigWarning = ({ | ||
docLinks, | ||
http, | ||
notifications, | ||
storage = window.localStorage, | ||
location = window.location, | ||
}: Deps) => { | ||
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') { | ||
return; | ||
} | ||
|
||
const missingWarningSeen = storage.getItem(MISSING_CONFIG_STORAGE_KEY) === 'true'; | ||
if (missingWarningSeen || http.basePath.publicBaseUrl) { | ||
return; | ||
} | ||
|
||
const toast = notifications.toasts.addWarning({ | ||
title: i18n.translate('core.ui.publicBaseUrlWarning.configMissingTitle', { | ||
defaultMessage: 'Configuration missing', | ||
}), | ||
text: mountReactNode( | ||
<> | ||
<p> | ||
<FormattedMessage | ||
id="core.ui.publicBaseUrlWarning.configMissingDescription" | ||
defaultMessage="{configKey} is missing and should be configured when running in a production environment. Some features may not behave correctly." | ||
values={{ | ||
configKey: <code>server.publicBaseUrl</code>, | ||
}} | ||
/>{' '} | ||
<a href={`${docLinks.links.settings}#server-publicBaseUrl`} target="_blank"> | ||
<FormattedMessage | ||
id="core.ui.publicBaseUrlWarning.seeDocumentationLinkLabel" | ||
defaultMessage="See the documentation." | ||
/> | ||
</a> | ||
</p> | ||
|
||
<EuiFlexGroup justifyContent="flexEnd" gutterSize="s"> | ||
<EuiFlexItem grow={false}> | ||
<EuiButton | ||
size="s" | ||
onClick={() => { | ||
notifications.toasts.remove(toast); | ||
storage.setItem(MISSING_CONFIG_STORAGE_KEY, 'true'); | ||
}} | ||
id="mute" | ||
> | ||
<FormattedMessage | ||
id="core.ui.publicBaseUrlWarning.muteWarningButtonLabel" | ||
defaultMessage="Mute warning" | ||
/> | ||
</EuiButton> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</> | ||
), | ||
}); | ||
}; |
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
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