From a1aebee7e10084225a010302d8740dc3e0577072 Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Mon, 24 Jun 2024 11:17:20 -0700 Subject: [PATCH 1/4] [DataViews] remove reference to security.contract.authc --- src/plugins/data_views/public/plugin.test.ts | 68 ++++++++++++++++++++ src/plugins/data_views/public/plugin.ts | 19 ++---- 2 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 src/plugins/data_views/public/plugin.test.ts diff --git a/src/plugins/data_views/public/plugin.test.ts b/src/plugins/data_views/public/plugin.test.ts new file mode 100644 index 0000000000000..4a9025ef1ab2b --- /dev/null +++ b/src/plugins/data_views/public/plugin.test.ts @@ -0,0 +1,68 @@ +/* + * 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 { coreMock } from '@kbn/core/public/mocks'; +import { expressionsPluginMock } from '@kbn/expressions-plugin/public/mocks'; +import { DataViewsPublicPlugin } from './plugin'; +import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks'; +import { contentManagementMock } from '@kbn/content-management-plugin/public/mocks'; + +const mockInitializerContext = coreMock.createPluginInitializerContext(); +const coreSetup = coreMock.createSetup(); +const coreStart = coreMock.createStart(); + +const setupDeps = { + expressions: expressionsPluginMock.createSetupContract(), + fieldFormats: fieldFormatsServiceMock.createSetupContract(), + contentManagement: contentManagementMock.createSetupContract(), +}; +const startDeps = { + fieldFormats: fieldFormatsServiceMock.createStartContract(), + contentManagement: contentManagementMock.createStartContract(), +}; + +describe('DataViewsPublicPlugin', () => { + test('setup contract', () => { + const setup = new DataViewsPublicPlugin(mockInitializerContext).setup(coreSetup, setupDeps); + expect(setup).toHaveProperty('enableRollups'); + }); + + describe('userIdGetter', () => { + class TestDataViewsPublicPlugin extends DataViewsPublicPlugin { + constructor(...args: ConstructorParameters) { + super(...args); + } + + public testUserIdGetter() { + return this.userIdGetter(); + } + } + + const mockUserProfileUid = '321-contact'; + let getCurrentUserMock: jest.SpyInstance; + let plugin: TestDataViewsPublicPlugin; + + beforeEach(async () => { + jest.spyOn(coreSetup, 'getStartServices').mockResolvedValue([coreStart, {}, {}]); + getCurrentUserMock = jest + .spyOn(coreStart.security.authc, 'getCurrentUser') + .mockResolvedValueOnce({ profile_uid: mockUserProfileUid }); + + plugin = new TestDataViewsPublicPlugin(mockInitializerContext); + plugin.setup(coreSetup, setupDeps); + plugin.start(coreStart, startDeps); + await coreSetup.plugins.onStart(); + }); + + test('uses core.security.authc API', async () => { + const result = await plugin.testUserIdGetter(); + expect(result).toBe(mockUserProfileUid); + expect(getCurrentUserMock).toBeCalledTimes(1); + }); + }); +}); diff --git a/src/plugins/data_views/public/plugin.ts b/src/plugins/data_views/public/plugin.ts index 8ff545d8585e1..954643d9372df 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 { @@ -43,7 +42,7 @@ export class DataViewsPublicPlugin { private readonly hasData = new HasData(); private rollupsEnabled: boolean = false; - private userIdGetter: UserIdGetter = async () => undefined; + protected userIdGetter: UserIdGetter = async () => undefined; constructor(private readonly initializerContext: PluginInitializerContext) {} @@ -63,16 +62,12 @@ 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'); - } + core.getStartServices().then(([coreStart]) => { + const getUserId = async function getUserId(): Promise { + const currentUser = await coreStart.security.authc.getCurrentUser(); + return currentUser?.profile_uid; + }; + this.userIdGetter = getUserId; }); return { From 4b65fc217143263fe8bcbdbab380d52fb5e17da8 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:14:16 +0000 Subject: [PATCH 2/4] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- src/plugins/data_views/tsconfig.json | 1 - 1 file changed, 1 deletion(-) 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": [ From 62a009d3d41ab7f037423e17222cd1d241c80162 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 25 Jun 2024 15:56:24 -0700 Subject: [PATCH 3/4] Simplify data views getCurrentUser --- src/plugins/data_views/public/index.ts | 1 - src/plugins/data_views/public/plugin.ts | 15 ++++----------- src/plugins/data_views/public/types.ts | 2 -- 3 files changed, 4 insertions(+), 14 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 954643d9372df..3a847ce939981 100644 --- a/src/plugins/data_views/public/plugin.ts +++ b/src/plugins/data_views/public/plugin.ts @@ -15,7 +15,6 @@ import { DataViewsPublicPluginStart, DataViewsPublicSetupDependencies, DataViewsPublicStartDependencies, - UserIdGetter, } from './types'; import { DataViewsApiClient } from '.'; @@ -42,7 +41,6 @@ export class DataViewsPublicPlugin { private readonly hasData = new HasData(); private rollupsEnabled: boolean = false; - protected userIdGetter: UserIdGetter = async () => undefined; constructor(private readonly initializerContext: PluginInitializerContext) {} @@ -62,14 +60,6 @@ export class DataViewsPublicPlugin }), }); - core.getStartServices().then(([coreStart]) => { - const getUserId = async function getUserId(): Promise { - const currentUser = await coreStart.security.authc.getCurrentUser(); - return currentUser?.profile_uid; - }; - this.userIdGetter = getUserId; - }); - return { enableRollups: () => (this.rollupsEnabled = true), }; @@ -96,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 */ From c73e2200e2d5596e8c3a6b4a8775f85da215f38e Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Wed, 26 Jun 2024 10:47:42 -0700 Subject: [PATCH 4/4] cleanup --- src/plugins/data_views/public/plugin.test.ts | 68 -------------------- 1 file changed, 68 deletions(-) delete mode 100644 src/plugins/data_views/public/plugin.test.ts diff --git a/src/plugins/data_views/public/plugin.test.ts b/src/plugins/data_views/public/plugin.test.ts deleted file mode 100644 index 4a9025ef1ab2b..0000000000000 --- a/src/plugins/data_views/public/plugin.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 { coreMock } from '@kbn/core/public/mocks'; -import { expressionsPluginMock } from '@kbn/expressions-plugin/public/mocks'; -import { DataViewsPublicPlugin } from './plugin'; -import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks'; -import { contentManagementMock } from '@kbn/content-management-plugin/public/mocks'; - -const mockInitializerContext = coreMock.createPluginInitializerContext(); -const coreSetup = coreMock.createSetup(); -const coreStart = coreMock.createStart(); - -const setupDeps = { - expressions: expressionsPluginMock.createSetupContract(), - fieldFormats: fieldFormatsServiceMock.createSetupContract(), - contentManagement: contentManagementMock.createSetupContract(), -}; -const startDeps = { - fieldFormats: fieldFormatsServiceMock.createStartContract(), - contentManagement: contentManagementMock.createStartContract(), -}; - -describe('DataViewsPublicPlugin', () => { - test('setup contract', () => { - const setup = new DataViewsPublicPlugin(mockInitializerContext).setup(coreSetup, setupDeps); - expect(setup).toHaveProperty('enableRollups'); - }); - - describe('userIdGetter', () => { - class TestDataViewsPublicPlugin extends DataViewsPublicPlugin { - constructor(...args: ConstructorParameters) { - super(...args); - } - - public testUserIdGetter() { - return this.userIdGetter(); - } - } - - const mockUserProfileUid = '321-contact'; - let getCurrentUserMock: jest.SpyInstance; - let plugin: TestDataViewsPublicPlugin; - - beforeEach(async () => { - jest.spyOn(coreSetup, 'getStartServices').mockResolvedValue([coreStart, {}, {}]); - getCurrentUserMock = jest - .spyOn(coreStart.security.authc, 'getCurrentUser') - .mockResolvedValueOnce({ profile_uid: mockUserProfileUid }); - - plugin = new TestDataViewsPublicPlugin(mockInitializerContext); - plugin.setup(coreSetup, setupDeps); - plugin.start(coreStart, startDeps); - await coreSetup.plugins.onStart(); - }); - - test('uses core.security.authc API', async () => { - const result = await plugin.testUserIdGetter(); - expect(result).toBe(mockUserProfileUid); - expect(getCurrentUserMock).toBeCalledTimes(1); - }); - }); -});