From 1224a050ad133196a3a2aab329fab26ad49ce41f Mon Sep 17 00:00:00 2001 From: Tim Sullivan Date: Thu, 27 Jun 2024 14:28:48 -0700 Subject: [PATCH] [DataViews] Use client-side authc.getCurrentUser from core.security (#186856) ## Summary Part of https://github.com/elastic/kibana/issues/186574 Background: This PR serves as an example of a plugin migrating away from depending on the Security plugin, which is a high priority effort for the last release before 9.0. The Data Views plugin uses the `authc.getCurrentUser` method as a means to use the user's profile_uid for creating a user-hash which is sent as a field in the headers of a particular request. What makes this example interesting: Data Views was not able to have a direct dependency on the Security plugin due to issues with a circular dependency. As a workaround, it used the `coreSetup.plugins.onStart` Promise to use Security services in a non-contractual way. The change in this PR does away with the workaround and allows Data Views to consume the Security service in a more typical way. ### Checklist Delete any items that are not applicable to this PR. - [X] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Lukas Olson --- src/plugins/data_views/public/index.ts | 1 - src/plugins/data_views/public/plugin.ts | 20 ++++---------------- src/plugins/data_views/public/types.ts | 2 -- src/plugins/data_views/tsconfig.json | 1 - 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/plugins/data_views/public/index.ts b/src/plugins/data_views/public/index.ts index 96380665548dd..f690552b5a147 100644 --- a/src/plugins/data_views/public/index.ts +++ b/src/plugins/data_views/public/index.ts @@ -67,7 +67,6 @@ export type { DataViewsContract, HasDataViewsResponse, IndicesViaSearchResponse, - UserIdGetter, } from './types'; // Export plugin after all other imports diff --git a/src/plugins/data_views/public/plugin.ts b/src/plugins/data_views/public/plugin.ts index 8ff545d8585e1..3a847ce939981 100644 --- a/src/plugins/data_views/public/plugin.ts +++ b/src/plugins/data_views/public/plugin.ts @@ -8,7 +8,6 @@ import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '@kbn/core/public'; import { i18n } from '@kbn/i18n'; -import type { SecurityPluginStart } from '@kbn/security-plugin-types-public'; import { getIndexPatternLoad } from './expressions'; import type { ClientConfigType } from '../common/types'; import { @@ -16,7 +15,6 @@ import { DataViewsPublicPluginStart, DataViewsPublicSetupDependencies, DataViewsPublicStartDependencies, - UserIdGetter, } from './types'; import { DataViewsApiClient } from '.'; @@ -43,7 +41,6 @@ export class DataViewsPublicPlugin { private readonly hasData = new HasData(); private rollupsEnabled: boolean = false; - private userIdGetter: UserIdGetter = async () => undefined; constructor(private readonly initializerContext: PluginInitializerContext) {} @@ -63,18 +60,6 @@ export class DataViewsPublicPlugin }), }); - core.plugins.onStart<{ security: SecurityPluginStart }>('security').then(({ security }) => { - if (security.found) { - const getUserId = async function getUserId(): Promise { - const currentUser = await security.contract.authc.getCurrentUser(); - return currentUser?.profile_uid; - }; - this.userIdGetter = getUserId; - } else { - throw new Error('Security plugin is not available, but is required for Data Views plugin'); - } - }); - return { enableRollups: () => (this.rollupsEnabled = true), }; @@ -101,7 +86,10 @@ export class DataViewsPublicPlugin hasData: this.hasData.start(core), uiSettings: new UiSettingsPublicToCommon(uiSettings), savedObjectsClient: new ContentMagementWrapper(contentManagement.client), - apiClient: new DataViewsApiClient(http, () => this.userIdGetter()), + apiClient: new DataViewsApiClient(http, async () => { + const currentUser = await core.security.authc.getCurrentUser(); + return currentUser?.profile_uid; + }), fieldFormats, http, onNotification: (toastInputFields, key) => { diff --git a/src/plugins/data_views/public/types.ts b/src/plugins/data_views/public/types.ts index 98db0382f641f..7d6cbbc6cf533 100644 --- a/src/plugins/data_views/public/types.ts +++ b/src/plugins/data_views/public/types.ts @@ -106,8 +106,6 @@ export interface DataViewsPublicStartDependencies { contentManagement: ContentManagementPublicStart; } -export type UserIdGetter = () => Promise; - /** * Data plugin public Setup contract */ diff --git a/src/plugins/data_views/tsconfig.json b/src/plugins/data_views/tsconfig.json index 13fc03be53996..312de968d6408 100644 --- a/src/plugins/data_views/tsconfig.json +++ b/src/plugins/data_views/tsconfig.json @@ -33,7 +33,6 @@ "@kbn/object-versioning", "@kbn/core-saved-objects-server", "@kbn/logging", - "@kbn/security-plugin-types-public", "@kbn/crypto-browser", ], "exclude": [