Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi Data Source] Render credential form registered from AuthMethod #6002

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [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))
- [Workspace] Optional workspaces params in repository ([#5949](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5949))
- [Multiple Datasource] Refactoring create and edit form to use authentication registry ([#6002](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6002))

### 🐛 Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { EuiSuperSelectOption } from '@elastic/eui';
export interface AuthenticationMethod {
name: string;
credentialSourceOption: EuiSuperSelectOption<string>;
credentialForm?: React.JSX.Element;
credentialForm?: (
state: { [key: string]: any },
setState: React.Dispatch<React.SetStateAction<any>>
) => React.JSX.Element;
crendentialFormField?: { [key: string]: string };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
xinruiba marked this conversation as resolved.
Show resolved Hide resolved

const titleIdentifier = '[data-test-subj="createDataSourceFormTitleField"]';
const descriptionIdentifier = `[data-test-subj="createDataSourceFormDescriptionField"]`;
Expand Down Expand Up @@ -363,3 +366,46 @@ describe('Datasource Management: Create Datasource form with different authType
});
});
});

describe('Datasource Management: Create Datasource form with registered Auth Type', () => {
let component: ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;
const mockSubmitHandler = jest.fn();
const mockTestConnectionHandler = jest.fn();
const mockCancelHandler = jest.fn();
const mockCredentialForm = jest.fn();

test('should call registered crendential form', () => {
const authTypeToBeTested = 'Some Auth Type';
const authMethodToBeTest = {
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
name: authTypeToBeTested,
credentialSourceOption: {
value: authTypeToBeTested,
inputDisplay: 'some input',
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
},
credentialForm: mockCredentialForm,
} as AuthenticationMethod;

const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery();
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTest);

component = mount(
wrapWithIntl(
<CreateDataSourceForm
handleTestConnection={mockTestConnectionHandler}
handleSubmit={mockSubmitHandler}
handleCancel={mockCancelHandler}
existingDatasourceNamesList={['dup20']}
/>
),
{
wrappingComponent: OpenSearchDashboardsContextProvider,
wrappingComponentProps: {
services: mockedContext,
},
}
);

expect(mockCredentialForm).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
} 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,
Expand Down Expand Up @@ -68,16 +69,17 @@

authOptions: Array<EuiSuperSelectOption<string>> = [];
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;
Expand Down Expand Up @@ -322,6 +324,23 @@
};
};

handleStateChange = (state: any) => {
this.setState(state);

Check warning on line 328 in src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx#L328

Added line #L328 was not covered by tests
};

getCredentialFormFromRegistry = (authType: string) => {
const registeredAuthMethod = this.authenticationMethodRegistery.getAuthenticationMethod(
authType
);
const authCredentialForm = registeredAuthMethod?.credentialForm;

if (authCredentialForm !== undefined) {
return authCredentialForm(this.state, this.handleStateChange);
}
xinruiba marked this conversation as resolved.
Show resolved Hide resolved

return null;

Check warning on line 341 in src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx#L341

Added line #L341 was not covered by tests
};

/* Render methods */

/* Render header*/
Expand Down Expand Up @@ -362,6 +381,8 @@
/* Render create new credentials*/
renderCreateNewCredentialsForm = (type: AuthType) => {
switch (type) {
case AuthType.NoAuth:
return null;
case AuthType.UsernamePasswordType:
return (
<>
Expand Down Expand Up @@ -498,7 +519,7 @@
);

default:
break;
return this.getCredentialFormFromRegistry(type);
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down Expand Up @@ -632,13 +653,7 @@
</EuiFormRow>

{/* 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)}

<EuiSpacer size="xl" />
<EuiFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]';
Expand Down Expand Up @@ -340,3 +344,45 @@ describe('Datasource Management: Edit Datasource Form', () => {
});
});
});

describe('With Registered Authentication', () => {
let component: ReactWrapper<any, Readonly<{}>, 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(
<EditDataSourceForm
existingDataSource={mockDataSourceAttributesWithNoAuth}
existingDatasourceNamesList={existingDatasourceNamesList}
onDeleteDataSource={jest.fn()}
handleSubmit={jest.fn()}
handleTestConnection={jest.fn()}
displayToastMessage={jest.fn()}
/>
),
{
wrappingComponent: OpenSearchDashboardsContextProvider,
wrappingComponentProps: {
services: mockedContext,
},
}
);

expect(mockCredentialForm).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
} 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 {
Expand All @@ -43,6 +44,7 @@
} from '../../../validation';
import { UpdatePasswordModal } from '../update_password_modal';
import { UpdateAwsCredentialModal } from '../update_aws_credential_modal';
import { getDefaultAuthMethod } from '../../../utils';

export interface EditDataSourceProps {
existingDataSource: DataSourceAttributes;
Expand Down Expand Up @@ -72,23 +74,27 @@
public readonly context!: DataSourceManagementContextValue;
maskedPassword: string = '********';
authOptions: Array<EuiSuperSelectOption<string>> = [];
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: '',
Expand Down Expand Up @@ -518,6 +524,23 @@
}
};

handleStateChange = (state: any) => {
this.setState(state);

Check warning on line 528 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx#L528

Added line #L528 was not covered by tests
};

getCredentialFormFromRegistry = (authType: string) => {
const registeredAuthMethod = this.authenticationMethodRegistery.getAuthenticationMethod(
authType
);
const authCredentialForm = registeredAuthMethod?.credentialForm;

if (authCredentialForm !== undefined) {
return authCredentialForm(this.state, this.handleStateChange);
}
xinruiba marked this conversation as resolved.
Show resolved Hide resolved

return null;

Check warning on line 541 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx#L541

Added line #L541 was not covered by tests
};

/* Render methods */

/* Render modal for new credential */
Expand Down Expand Up @@ -796,12 +819,14 @@

renderSelectedAuthType = (type: AuthType) => {
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);
}
};

Expand Down
Loading