From b128a6758ab3b46decf0f5a66638e9947cd85721 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 00:08:12 +0000 Subject: [PATCH 01/10] [TokenExchange] Render credential form registered from AuthMethod Signed-off-by: Xinrui Bai --- config/opensearch_dashboards.yml | 20 ++++++----- .../create_form/create_data_source_form.tsx | 35 ++++++++++++------- .../edit_form/edit_data_source_form.tsx | 29 +++++++++++++-- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/config/opensearch_dashboards.yml b/config/opensearch_dashboards.yml index 4f86a8729a3e..715f9b9dbdec 100644 --- a/config/opensearch_dashboards.yml +++ b/config/opensearch_dashboards.yml @@ -236,7 +236,7 @@ # vis_builder.enabled: false # Set the value of this setting to true to enable multiple data source feature. -#data_source.enabled: false +data_source.enabled: true # Set the value of this setting to true to hide local cluster in data source feature. #data_source.hideLocalCluster: false # Set the value of these settings to customize crypto materials to encryption saved credentials @@ -273,16 +273,18 @@ # Set enabled false to hide authentication method in OpenSearch Dashboards. # If this setting is commented then all 3 options will be available in OpenSearch Dashboards. # Default value will be considered to True. -#data_source.authTypes: -# NoAuthentication: -# enabled: true -# UsernamePassword: -# enabled: true -# AWSSigV4: -# enabled: true +data_source.authTypes: + NoAuthentication: + enabled: true + UsernamePassword: + enabled: true + AWSSigV4: + enabled: true # Set the value of this setting to false to hide the help menu link to the OpenSearch Dashboards user survey # opensearchDashboards.survey.url: "https://survey.opensearch.org" # Set the value of this setting to true to enable plugin augmentation on Dashboard -# vis_augmenter.pluginAugmentationEnabled: true \ No newline at end of file +# vis_augmenter.pluginAugmentationEnabled: true + +dynamoDBStorage.enabled: true \ No newline at end of file diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx index e178ea1bcffa..254bf123587c 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx @@ -22,6 +22,7 @@ import { } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; +import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources'; import { AuthType, @@ -68,16 +69,17 @@ export class CreateDataSourceForm extends React.Component< authOptions: Array> = []; isNoAuthOptionEnabled: boolean; + authenticationMethodRegistery: AuthenticationMethodRegistery; constructor(props: CreateDataSourceProps, context: DataSourceManagementContextValue) { super(props, context); - const authenticationMethodRegistery = context.services.authenticationMethodRegistery; - const registeredAuthMethods = authenticationMethodRegistery.getAllAuthenticationMethods(); - const initialSelectedAuthMethod = getDefaultAuthMethod(authenticationMethodRegistery); + this.authenticationMethodRegistery = context.services.authenticationMethodRegistery; + const registeredAuthMethods = this.authenticationMethodRegistery.getAllAuthenticationMethods(); + const initialSelectedAuthMethod = getDefaultAuthMethod(this.authenticationMethodRegistery); this.isNoAuthOptionEnabled = - authenticationMethodRegistery.getAuthenticationMethod(AuthType.NoAuth) !== undefined; + this.authenticationMethodRegistery.getAuthenticationMethod(AuthType.NoAuth) !== undefined; this.authOptions = registeredAuthMethods.map((authMethod) => { return authMethod.credentialSourceOption; @@ -322,6 +324,21 @@ export class CreateDataSourceForm extends React.Component< }; }; + handleStateChange = (state: any) => { + this.setState(state); + }; + + getCredentialFormFromRegistry = (authType: string) => { + const registeredAuthMethod = this.authenticationMethodRegistery.getAuthenticationMethod( + authType + ); + const authCredentialForm = registeredAuthMethod?.credentialForm; + + if (authCredentialForm !== undefined) { + return authCredentialForm(this.state, this.handleStateChange); + } + }; + /* Render methods */ /* Render header*/ @@ -498,7 +515,7 @@ export class CreateDataSourceForm extends React.Component< ); default: - break; + return this.getCredentialFormFromRegistry(this.state.auth.type); } }; @@ -632,13 +649,7 @@ export class CreateDataSourceForm extends React.Component< {/* Create New credentials */} - {this.state.auth.type === AuthType.UsernamePasswordType - ? this.renderCreateNewCredentialsForm(this.state.auth.type) - : null} - - {this.state.auth.type === AuthType.SigV4 - ? this.renderCreateNewCredentialsForm(this.state.auth.type) - : null} + {this.renderCreateNewCredentialsForm(this.state.auth.type)} diff --git a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx index c3d7daa7db48..a2feaff3ae28 100644 --- a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx @@ -24,6 +24,7 @@ import { } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; +import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources'; import { Header } from '../header'; import { @@ -43,6 +44,7 @@ import { } from '../../../validation'; import { UpdatePasswordModal } from '../update_password_modal'; import { UpdateAwsCredentialModal } from '../update_aws_credential_modal'; +import { getDefaultAuthMethod } from '../../../utils'; export interface EditDataSourceProps { existingDataSource: DataSourceAttributes; @@ -72,23 +74,27 @@ export class EditDataSourceForm extends React.Component> = []; + authenticationMethodRegistery: AuthenticationMethodRegistery; constructor(props: EditDataSourceProps, context: DataSourceManagementContextValue) { super(props, context); - this.authOptions = context.services.authenticationMethodRegistery + this.authenticationMethodRegistery = context.services.authenticationMethodRegistery; + this.authOptions = this.authenticationMethodRegistery .getAllAuthenticationMethods() .map((authMethod) => { return authMethod.credentialSourceOption; }); + const initialSelectedAuthMethod = getDefaultAuthMethod(this.authenticationMethodRegistery); + this.state = { formErrorsByField: { ...defaultValidation }, title: '', description: '', endpoint: '', auth: { - type: AuthType.NoAuth, + type: initialSelectedAuthMethod?.name, credentials: { username: '', password: '', @@ -518,6 +524,21 @@ export class EditDataSourceForm extends React.Component { + this.setState(state); + }; + + getCredentialFormFromRegistry = (authType: string) => { + const registeredAuthMethod = this.authenticationMethodRegistery.getAuthenticationMethod( + authType + ); + const authCredentialForm = registeredAuthMethod?.credentialForm; + + if (authCredentialForm !== undefined) { + return authCredentialForm(this.state, this.handleStateChange); + } + }; + /* Render methods */ /* Render modal for new credential */ @@ -796,12 +817,14 @@ export class EditDataSourceForm extends React.Component { switch (type) { + case AuthType.NoAuth: + return null; case AuthType.UsernamePasswordType: return this.renderUsernamePasswordFields(); case AuthType.SigV4: return this.renderSigV4ContentFields(); default: - return null; + return this.getCredentialFormFromRegistry(type); } }; From 1a620b7b35d0c231373d9fb9f738ca865a7f9a41 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 01:37:37 +0000 Subject: [PATCH 02/10] [UT] Add unittest to test registered credential form get rendered in create datasource page Signed-off-by: Xinrui Bai --- .../authentication_methods_registry.ts | 2 +- .../create_data_source_form.test.tsx | 49 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts b/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts index 00c3b0dbf0ee..5593b82f7bf6 100644 --- a/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts +++ b/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts @@ -9,7 +9,7 @@ import { EuiSuperSelectOption } from '@elastic/eui'; export interface AuthenticationMethod { name: string; credentialSourceOption: EuiSuperSelectOption; - credentialForm?: React.JSX.Element; + credentialForm?: (state?: any, setState?: any) => React.JSX.Element; crendentialFormField?: { [key: string]: string }; } diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx index 4f30ba9da5e4..38b3cd61c3ad 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx @@ -17,7 +17,10 @@ import { sigV4AuthMethod, usernamePasswordAuthMethod, } from '../../../../types'; -import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; +import { + AuthenticationMethod, + AuthenticationMethodRegistery, +} from 'src/plugins/data_source_management/public/auth_registry'; const titleIdentifier = '[data-test-subj="createDataSourceFormTitleField"]'; const descriptionIdentifier = `[data-test-subj="createDataSourceFormDescriptionField"]`; @@ -363,3 +366,47 @@ describe('Datasource Management: Create Datasource form with different authType }); }); }); + +describe('Datasource Management: Create Datasource form with registered Auth Type', () => { + let component: ReactWrapper, React.Component<{}, {}, any>>; + const mockSubmitHandler = jest.fn(); + const mockTestConnectionHandler = jest.fn(); + const mockCancelHandler = jest.fn(); + const mockCredentialForm = jest.fn(); + + /* Scenario 1: should render the page normally when only one registered Auth Type supported */ + test('should render the page normally when only one registered Auth Type supported', () => { + const authTypeToBeTested = 'Some Auth Type'; + const authMethodToBeTest = { + name: authTypeToBeTested, + credentialSourceOption: { + value: authTypeToBeTested, + inputDisplay: 'some input', + }, + credentialForm: mockCredentialForm, + } as AuthenticationMethod; + + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTest); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + expect(mockCredentialForm).toHaveBeenCalled(); + }); +}); From e3edb61fca963d86aeaf10ba88691a4379a81693 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 01:40:12 +0000 Subject: [PATCH 03/10] [UT] Update test case descriptions Signed-off-by: Xinrui Bai --- .../components/create_form/create_data_source_form.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx index 38b3cd61c3ad..352d79a67afc 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx @@ -374,8 +374,8 @@ describe('Datasource Management: Create Datasource form with registered Auth Typ const mockCancelHandler = jest.fn(); const mockCredentialForm = jest.fn(); - /* Scenario 1: should render the page normally when only one registered Auth Type supported */ - test('should render the page normally when only one registered Auth Type supported', () => { + /* Scenario 1: should call registered crendential form */ + test('should call registered crendential form', () => { const authTypeToBeTested = 'Some Auth Type'; const authMethodToBeTest = { name: authTypeToBeTested, From d2de84954265c3cee1a192973bc291c87056e0c3 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 01:47:20 +0000 Subject: [PATCH 04/10] [Token Exchange] improve code format in create datasource page Signed-off-by: Xinrui Bai --- .../components/create_form/create_data_source_form.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx index 254bf123587c..3818a1fe48fa 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx @@ -379,6 +379,8 @@ export class CreateDataSourceForm extends React.Component< /* Render create new credentials*/ renderCreateNewCredentialsForm = (type: AuthType) => { switch (type) { + case AuthType.NoAuth: + return null; case AuthType.UsernamePasswordType: return ( <> @@ -515,7 +517,7 @@ export class CreateDataSourceForm extends React.Component< ); default: - return this.getCredentialFormFromRegistry(this.state.auth.type); + return this.getCredentialFormFromRegistry(type); } }; From ba71c8abec2fc88b75fcff185f783af5657b544d Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 19:59:55 +0000 Subject: [PATCH 05/10] [UT] Add unit test for edit datasource page Signed-off-by: Xinrui Bai --- config/opensearch_dashboards.yml | 18 ++++---- .../create_data_source_form.test.tsx | 1 - .../edit_form/edit_data_source_form.test.tsx | 46 +++++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/config/opensearch_dashboards.yml b/config/opensearch_dashboards.yml index 715f9b9dbdec..c2234704a4ac 100644 --- a/config/opensearch_dashboards.yml +++ b/config/opensearch_dashboards.yml @@ -273,18 +273,16 @@ data_source.enabled: true # Set enabled false to hide authentication method in OpenSearch Dashboards. # If this setting is commented then all 3 options will be available in OpenSearch Dashboards. # Default value will be considered to True. -data_source.authTypes: - NoAuthentication: - enabled: true - UsernamePassword: - enabled: true - AWSSigV4: - enabled: true +#data_source.authTypes: +# NoAuthentication: +# enabled: true +# UsernamePassword: +# enabled: true +# AWSSigV4: +# enabled: true # Set the value of this setting to false to hide the help menu link to the OpenSearch Dashboards user survey # opensearchDashboards.survey.url: "https://survey.opensearch.org" # Set the value of this setting to true to enable plugin augmentation on Dashboard -# vis_augmenter.pluginAugmentationEnabled: true - -dynamoDBStorage.enabled: true \ No newline at end of file +# vis_augmenter.pluginAugmentationEnabled: true \ No newline at end of file diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx index 352d79a67afc..8dc1223cb399 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx @@ -374,7 +374,6 @@ describe('Datasource Management: Create Datasource form with registered Auth Typ const mockCancelHandler = jest.fn(); const mockCredentialForm = jest.fn(); - /* Scenario 1: should call registered crendential form */ test('should call registered crendential form', () => { const authTypeToBeTested = 'Some Auth Type'; const authMethodToBeTest = { diff --git a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx index 4326d6e6832d..289b1d1bcd80 100644 --- a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx @@ -21,6 +21,10 @@ import { sigV4AuthMethod, usernamePasswordAuthMethod, } from '../../../../types'; +import { + AuthenticationMethod, + AuthenticationMethodRegistery, +} from 'src/plugins/data_source_management/public/auth_registry'; const titleFieldIdentifier = 'dataSourceTitle'; const titleFormRowIdentifier = '[data-test-subj="editDataSourceTitleFormRow"]'; @@ -340,3 +344,45 @@ describe('Datasource Management: Edit Datasource Form', () => { }); }); }); + +describe('With Registered Authentication', () => { + let component: ReactWrapper, React.Component<{}, {}, any>>; + const mockCredentialForm = jest.fn(); + + test('should call registered crendential form', () => { + const authTypeToBeTested = 'Some Auth Type'; + const authMethodToBeTest = { + name: authTypeToBeTested, + credentialSourceOption: { + value: authTypeToBeTested, + inputDisplay: 'some input', + }, + credentialForm: mockCredentialForm, + } as AuthenticationMethod; + + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTest); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + expect(mockCredentialForm).toHaveBeenCalled(); + }); +}); From 3570c97c4406a5a111b905dd5e2452417f9147bc Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 20:04:40 +0000 Subject: [PATCH 06/10] Update changelog file Signed-off-by: Xinrui Bai --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e67c86790f9..50e3330f7f84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Multiple Datasource] Improved error handling for the search API when a null value is passed for the dataSourceId ([#5882](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5882)) - [Multiple Datasource] Hide/Show authentication method in multi data source plugin based on configuration ([#5916](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5916)) - [[Dynamic Configurations] Add support for dynamic application configurations ([#5855](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5855)) +- [Multiple Datasource] Refactoring create and edit form to use authentication registry ([#6002](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6002)) ### 🐛 Bug Fixes From 249e175d3ff53340ce6b6c2708e7b801b72d7ca3 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Sat, 2 Mar 2024 20:06:22 +0000 Subject: [PATCH 07/10] update yml config file to original status Signed-off-by: Xinrui Bai --- config/opensearch_dashboards.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/opensearch_dashboards.yml b/config/opensearch_dashboards.yml index b3b29589c1a1..f1858ac35dde 100644 --- a/config/opensearch_dashboards.yml +++ b/config/opensearch_dashboards.yml @@ -243,7 +243,7 @@ # vis_builder.enabled: false # Set the value of this setting to true to enable multiple data source feature. -data_source.enabled: true +#data_source.enabled: false # Set the value of this setting to true to hide local cluster in data source feature. #data_source.hideLocalCluster: false # Set the value of these settings to customize crypto materials to encryption saved credentials From c9070710d4eddfa0657d131866276440bc480dd9 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Mon, 4 Mar 2024 19:11:57 +0000 Subject: [PATCH 08/10] Resolving comments Signed-off-by: Xinrui Bai --- .../public/auth_registry/authentication_methods_registry.ts | 5 ++++- .../components/create_form/create_data_source_form.tsx | 2 ++ .../components/edit_form/edit_data_source_form.tsx | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts b/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts index 5593b82f7bf6..a4152cb0627a 100644 --- a/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts +++ b/src/plugins/data_source_management/public/auth_registry/authentication_methods_registry.ts @@ -9,7 +9,10 @@ import { EuiSuperSelectOption } from '@elastic/eui'; export interface AuthenticationMethod { name: string; credentialSourceOption: EuiSuperSelectOption; - credentialForm?: (state?: any, setState?: any) => React.JSX.Element; + credentialForm?: ( + state: { [key: string]: any }, + setState: React.Dispatch> + ) => React.JSX.Element; crendentialFormField?: { [key: string]: string }; } diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx index 3818a1fe48fa..82827b20d048 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx @@ -337,6 +337,8 @@ export class CreateDataSourceForm extends React.Component< if (authCredentialForm !== undefined) { return authCredentialForm(this.state, this.handleStateChange); } + + return null; }; /* Render methods */ diff --git a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx index a2feaff3ae28..b03e070ce554 100644 --- a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx @@ -537,6 +537,8 @@ export class EditDataSourceForm extends React.Component Date: Mon, 4 Mar 2024 22:42:52 +0000 Subject: [PATCH 09/10] [UT] Add more unit test to cover existing auth type and plugin registered Auth type scenario Signed-off-by: Xinrui Bai --- .../create_data_source_form.test.tsx | 163 +++++++++++++++--- 1 file changed, 136 insertions(+), 27 deletions(-) diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx index 8dc1223cb399..1040a17584a0 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx @@ -17,10 +17,7 @@ import { sigV4AuthMethod, usernamePasswordAuthMethod, } from '../../../../types'; -import { - AuthenticationMethod, - AuthenticationMethodRegistery, -} from 'src/plugins/data_source_management/public/auth_registry'; +import { AuthenticationMethod, AuthenticationMethodRegistery } from '../../../../auth_registry'; const titleIdentifier = '[data-test-subj="createDataSourceFormTitleField"]'; const descriptionIdentifier = `[data-test-subj="createDataSourceFormDescriptionField"]`; @@ -372,11 +369,11 @@ describe('Datasource Management: Create Datasource form with registered Auth Typ const mockSubmitHandler = jest.fn(); const mockTestConnectionHandler = jest.fn(); const mockCancelHandler = jest.fn(); - const mockCredentialForm = jest.fn(); - test('should call registered crendential form', () => { + test('should call registered crendential form at the first round when registered method is at the first place and username & password disabled', () => { + const mockCredentialForm = jest.fn(); const authTypeToBeTested = 'Some Auth Type'; - const authMethodToBeTest = { + const authMethodToBeTested = { name: authTypeToBeTested, credentialSourceOption: { value: authTypeToBeTested, @@ -385,27 +382,139 @@ describe('Datasource Management: Create Datasource form with registered Auth Typ credentialForm: mockCredentialForm, } as AuthenticationMethod; - const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); - mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); - mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTest); + const authMethodCombinationsToBeTested = [ + [authMethodToBeTested], + [authMethodToBeTested, sigV4AuthMethod], + [authMethodToBeTested, noAuthCredentialAuthMethod], + [authMethodToBeTested, noAuthCredentialAuthMethod, sigV4AuthMethod], + ]; - component = mount( - wrapWithIntl( - - ), - { - wrappingComponent: OpenSearchDashboardsContextProvider, - wrappingComponentProps: { - services: mockedContext, - }, - } - ); + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + expect(mockCredentialForm).toHaveBeenCalled(); + }); + }); + + test('should not call registered crendential form at the first round when registered method is at the first place and username & password enabled', () => { + const mockCredentialForm = jest.fn(); + const authTypeToBeTested = 'Some Auth Type'; + const authMethodToBeTested = { + name: authTypeToBeTested, + credentialSourceOption: { + value: authTypeToBeTested, + inputDisplay: 'some input', + }, + credentialForm: mockCredentialForm, + } as AuthenticationMethod; + + const authMethodCombinationsToBeTested = [ + [authMethodToBeTested, usernamePasswordAuthMethod], + [authMethodToBeTested, usernamePasswordAuthMethod, sigV4AuthMethod], + [authMethodToBeTested, usernamePasswordAuthMethod, noAuthCredentialAuthMethod], + [ + authMethodToBeTested, + usernamePasswordAuthMethod, + noAuthCredentialAuthMethod, + sigV4AuthMethod, + ], + ]; + + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); - expect(mockCredentialForm).toHaveBeenCalled(); + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + expect(mockCredentialForm).not.toHaveBeenCalled(); + }); + }); + + test('should not call registered crendential form at the first round when registered method is not at the first place', () => { + const mockCredentialForm = jest.fn(); + const authTypeToBeTested = 'Some Auth Type'; + const authMethodToBeTested = { + name: authTypeToBeTested, + credentialSourceOption: { + value: authTypeToBeTested, + inputDisplay: 'some input', + }, + credentialForm: mockCredentialForm, + } as AuthenticationMethod; + + const authMethodCombinationsToBeTested = [ + [sigV4AuthMethod, authMethodToBeTested], + [noAuthCredentialAuthMethod, authMethodToBeTested], + [noAuthCredentialAuthMethod, authMethodToBeTested, sigV4AuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + expect(mockCredentialForm).not.toHaveBeenCalled(); + }); }); }); From b55ddb3b409de95885006f00721b892206966637 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Mon, 4 Mar 2024 23:03:53 +0000 Subject: [PATCH 10/10] Resolving comments, update pmport path Signed-off-by: Xinrui Bai --- .../components/create_form/create_data_source_form.tsx | 2 +- .../components/edit_form/edit_data_source_form.test.tsx | 5 +---- .../components/edit_form/edit_data_source_form.tsx | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx index 82827b20d048..25b082b8c6a1 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx @@ -22,7 +22,7 @@ import { } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; -import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; +import { AuthenticationMethodRegistery } from '../../../../auth_registry'; import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources'; import { AuthType, diff --git a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx index 289b1d1bcd80..89d5c54cbc2a 100644 --- a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.test.tsx @@ -21,10 +21,7 @@ import { sigV4AuthMethod, usernamePasswordAuthMethod, } from '../../../../types'; -import { - AuthenticationMethod, - AuthenticationMethodRegistery, -} from 'src/plugins/data_source_management/public/auth_registry'; +import { AuthenticationMethod, AuthenticationMethodRegistery } from '../../../../auth_registry'; const titleFieldIdentifier = 'dataSourceTitle'; const titleFormRowIdentifier = '[data-test-subj="editDataSourceTitleFormRow"]'; diff --git a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx index b03e070ce554..ee46af355312 100644 --- a/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx @@ -24,7 +24,7 @@ import { } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; -import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; +import { AuthenticationMethodRegistery } from '../../../../auth_registry'; import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources'; import { Header } from '../header'; import {