From 9198475715c1ec0f02a5a0db40f3e3d7168acf08 Mon Sep 17 00:00:00 2001 From: Sander Philipse <94373878+sphilipse@users.noreply.github.com> Date: Tue, 26 Sep 2023 13:10:22 +0200 Subject: [PATCH] [Search] Fix broken Search plugin on unauthenticated user (#167171) ## Summary This fixes an issue where the Search plugin was inaccessible for unauthenticated user, eg. for Kibana in read-only demo setups. --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../__mocks__/kea_logic/kibana_logic.mock.ts | 1 - .../product_selector/product_selector.tsx | 22 +++++++++++++++++-- .../public/applications/index.tsx | 3 +-- .../shared/kibana/kibana_logic.ts | 3 --- .../enterprise_search/public/plugin.ts | 5 +---- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/kibana_logic.mock.ts b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/kibana_logic.mock.ts index ac0656e11182b..f374fe8b60666 100644 --- a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/kibana_logic.mock.ts +++ b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/kibana_logic.mock.ts @@ -61,7 +61,6 @@ export const mockKibanaValues = { setDocTitle: jest.fn(), share: sharePluginMock.createStartContract(), uiSettings: uiSettingsServiceMock.createStartContract(), - user: {}, }; jest.mock('../../shared/kibana', () => ({ diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_overview/components/product_selector/product_selector.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_overview/components/product_selector/product_selector.tsx index 15e8dd00abf36..ecfd109077399 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_overview/components/product_selector/product_selector.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_overview/components/product_selector/product_selector.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { useValues } from 'kea'; @@ -21,6 +21,8 @@ import { Chat } from '@kbn/cloud-chat-plugin/public'; import { i18n } from '@kbn/i18n'; import { WelcomeBanner } from '@kbn/search-api-panels'; +import { AuthenticatedUser } from '@kbn/security-plugin/common'; + import { ErrorStateCallout } from '../../../shared/error_state'; import { HttpLogic } from '../../../shared/http'; import { KibanaLogic } from '../../../shared/kibana'; @@ -40,8 +42,24 @@ import { IngestionSelector } from './ingestion_selector'; import './product_selector.scss'; export const ProductSelector: React.FC = () => { - const { config, user } = useValues(KibanaLogic); + const { config } = useValues(KibanaLogic); const { errorConnectingMessage } = useValues(HttpLogic); + const { security } = useValues(KibanaLogic); + + const [user, setUser] = useState(null); + + useEffect(() => { + try { + security.authc + .getCurrentUser() + .then(setUser) + .catch(() => { + setUser(null); + }); + } catch { + setUser(null); + } + }, [security.authc]); const showErrorConnecting = !!(config.host && errorConnectingMessage); // The create index flow does not work without ent-search, when content is updated diff --git a/x-pack/plugins/enterprise_search/public/applications/index.tsx b/x-pack/plugins/enterprise_search/public/applications/index.tsx index f5fac8af4cd43..def858dfb6040 100644 --- a/x-pack/plugins/enterprise_search/public/applications/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/index.tsx @@ -66,7 +66,7 @@ export const renderApp = ( const { history } = params; const { application, chrome, http, uiSettings } = core; const { capabilities, navigateToUrl } = application; - const { charts, cloud, guidedOnboarding, lens, security, share, user } = plugins; + const { charts, cloud, guidedOnboarding, lens, security, share } = plugins; const entCloudHost = getCloudEnterpriseSearchHost(plugins.cloud); externalUrl.enterpriseSearchUrl = publicUrl || entCloudHost || config.host || ''; @@ -108,7 +108,6 @@ export const renderApp = ( setDocTitle: chrome.docTitle.change, share, uiSettings, - user, }); const unmountLicensingLogic = mountLicensingLogic({ canManageLicense: core.application.capabilities.management?.stack?.license_management, diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts index 3f72242f87a10..d816c747e5027 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts @@ -21,7 +21,6 @@ import { import { DataPublicPluginStart } from '@kbn/data-plugin/public'; import { GuidedOnboardingPluginStart } from '@kbn/guided-onboarding-plugin/public'; import { LensPublicStart } from '@kbn/lens-plugin/public'; -import { AuthenticatedUser } from '@kbn/security-plugin/common'; import { SecurityPluginStart } from '@kbn/security-plugin/public'; import { SharePluginStart } from '@kbn/share-plugin/public'; @@ -54,7 +53,6 @@ interface KibanaLogicProps { setDocTitle(title: string): void; share: SharePluginStart; uiSettings: IUiSettingsClient; - user: AuthenticatedUser | null; } export interface KibanaValues extends Omit { @@ -95,7 +93,6 @@ export const KibanaLogic = kea>({ setDocTitle: [props.setDocTitle, {}], share: [props.share, {}], uiSettings: [props.uiSettings, {}], - user: [props.user, {}], }), selectors: ({ selectors }) => ({ isCloud: [() => [selectors.cloud], (cloud?: Partial) => !!cloud?.isCloudEnabled], diff --git a/x-pack/plugins/enterprise_search/public/plugin.ts b/x-pack/plugins/enterprise_search/public/plugin.ts index 40fde678715a3..5fde4b3c41d85 100644 --- a/x-pack/plugins/enterprise_search/public/plugin.ts +++ b/x-pack/plugins/enterprise_search/public/plugin.ts @@ -22,7 +22,6 @@ import { GuidedOnboardingPluginStart } from '@kbn/guided-onboarding-plugin/publi import type { HomePublicPluginSetup } from '@kbn/home-plugin/public'; import { LensPublicStart } from '@kbn/lens-plugin/public'; import { LicensingPluginStart } from '@kbn/licensing-plugin/public'; -import { AuthenticatedUser } from '@kbn/security-plugin/common'; import { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/public'; import { SharePluginStart } from '@kbn/share-plugin/public'; @@ -66,7 +65,6 @@ export interface PluginsStart { licensing: LicensingPluginStart; security: SecurityPluginStart; share: SharePluginStart; - user: AuthenticatedUser; } export class EnterpriseSearchPlugin implements Plugin { @@ -102,8 +100,7 @@ export class EnterpriseSearchPlugin implements Plugin { cloudSetup && (pluginsStart as PluginsStart).cloud ? { ...cloudSetup, ...(pluginsStart as PluginsStart).cloud } : undefined; - const user = (await (pluginsStart as PluginsStart).security.authc.getCurrentUser()) || null; - const plugins = { ...pluginsStart, cloud, user } as PluginsStart; + const plugins = { ...pluginsStart, cloud } as PluginsStart; coreStart.chrome .getChromeStyle$()