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 @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiSuperSelectOption } from '@elastic/eui';
export interface AuthenticationMethod {
name: string;
credentialSourceOption: EuiSuperSelectOption<string>;
credentialForm?: React.JSX.Element;
credentialForm?: (state?: any, setState?: any) => React.JSX.Element;
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
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 @@ 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,
Expand Down Expand Up @@ -68,16 +69,17 @@ export class CreateDataSourceForm extends React.Component<

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,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);
}
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
};

/* Render methods */

/* Render header*/
Expand Down Expand Up @@ -362,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 (
<>
Expand Down Expand Up @@ -498,7 +517,7 @@ export class CreateDataSourceForm extends React.Component<
);

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

Expand Down Expand Up @@ -632,13 +651,7 @@ export class CreateDataSourceForm extends React.Component<
</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 @@ 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 {
Expand All @@ -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;
Expand Down Expand Up @@ -72,23 +74,27 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi
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,21 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi
}
};

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);
}
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
};

/* Render methods */

/* Render modal for new credential */
Expand Down Expand Up @@ -796,12 +817,14 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi

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