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

[Multiple DataSource] DataSource creation and edition page improvement to better support registered auth types #6122

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Adds a session token to AWS credentials ([#6103](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6103))
- [Multiple Datasource] Add Vega support to MDS by specifying a data source name in the Vega spec ([#5975](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5975))
- [Multiple Datasource] Test connection schema validation for registered auth types ([#6109](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6109))
- [Multiple DataSource] DataSource creation and edition page improvement to better support registered auth types ([#6122](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6122))
- [Workspace] Consume workspace id in saved object client ([#6014](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6014))
- [Multiple Datasource] Export DataSourcePluginRequestContext at top level for plugins to use ([#6108](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6108))
- [Multiple Datasource] Expose filterfn in datasource menu component to allow filter data sources before rendering in navigation bar ([#6113](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6113))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,20 @@ export class CreateDataSourceForm extends React.Component<
};

onChangeAuthType = (authType: AuthType) => {
const credentials = this.state.auth.credentials;

const registeredAuthCredentials = extractRegisteredAuthTypeCredentials(
(credentials ?? {}) as { [key: string]: string },
authType,
this.authenticationMethodRegistery
);

this.setState({
auth: {
...this.state.auth,
type: authType,
credentials: {
...this.state.auth.credentials,
service:
(this.state.auth.credentials.service as SigV4ServiceName) ||
SigV4ServiceName.OpenSearch,
...registeredAuthCredentials,
},
},
});
Expand Down
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 deepEqual from 'fast-deep-equal';
import { AuthenticationMethodRegistery } from '../../../../auth_registry';
import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources';
import { Header } from '../header';
Expand Down Expand Up @@ -100,11 +101,7 @@
auth: {
type: initialSelectedAuthMethod?.name,
credentials: {
username: '',
password: '',
region: '',
accessKey: '',
secretKey: '',
...initialSelectedAuthMethod?.crendentialFormField,
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
},
},
showUpdatePasswordModal: false,
Expand All @@ -127,10 +124,11 @@
if (this.props.existingDataSource) {
const { title, description, endpoint, auth } = this.props.existingDataSource;

const authTypeCheckResults = {
isUserNamePassword: auth.type === AuthType.UsernamePasswordType,
isSigV4: auth.type === AuthType.SigV4,
};
const registeredAuthCredentials = extractRegisteredAuthTypeCredentials(
(auth.credentials ?? {}) as { [key: string]: string },
auth.type,
this.authenticationMethodRegistery
);

this.setState({
title,
Expand All @@ -139,14 +137,7 @@
auth: {
type: auth.type,
credentials: {
username: authTypeCheckResults.isUserNamePassword ? auth.credentials?.username : '',
password: authTypeCheckResults.isUserNamePassword ? this.maskedPassword : '',
service: authTypeCheckResults.isSigV4
? auth.credentials?.service || SigV4ServiceName.OpenSearch
: '',
region: authTypeCheckResults.isSigV4 ? auth.credentials!.region : '',
accessKey: authTypeCheckResults.isSigV4 ? this.maskedPassword : '',
secretKey: authTypeCheckResults.isSigV4 ? this.maskedPassword : '',
...registeredAuthCredentials,
},
},
});
Expand Down Expand Up @@ -185,16 +176,23 @@
};

onChangeAuthType = (authType: AuthType) => {
const credentials =
this.props.existingDataSource && authType === this.props.existingDataSource.auth.type
? this.props.existingDataSource.auth.credentials
: this.state.auth.credentials;
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
const registeredAuthCredentials = extractRegisteredAuthTypeCredentials(
(credentials ?? {}) as { [key: string]: string },
authType,
this.authenticationMethodRegistery
);

this.setState(
{
auth: {
...this.state.auth,
type: authType,
credentials: {
...this.state.auth.credentials,
service:
(this.state.auth.credentials?.service as SigV4ServiceName) ||
SigV4ServiceName.OpenSearch,
...registeredAuthCredentials,
},
},
},
Expand Down Expand Up @@ -427,6 +425,14 @@
break;

default:
const currentCredentials = (this.state.auth.credentials ?? {}) as {
[key: string]: string;
};
credentials = extractRegisteredAuthTypeCredentials(

Check warning on line 431 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#L431

Added line #L431 was not covered by tests
currentCredentials,
this.state.auth.type,
this.authenticationMethodRegistery
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
);
break;
}

Expand Down Expand Up @@ -1029,21 +1035,50 @@
const isServiceNameChanged =
isAuthTypeSigV4Unchanged &&
formValues.auth.credentials?.service !== auth.credentials?.service;
const isRegisteredAuthCredentialChanged = this.isRegisteredAuthCredentialUpdated();

if (
formValues.title !== title ||
formValues.description !== description ||
formValues.auth.type !== auth.type ||
isUsernameChanged ||
isRegionChanged ||
isServiceNameChanged
isServiceNameChanged ||
isRegisteredAuthCredentialChanged
) {
this.setState({ showUpdateOptions: true });
} else {
this.setState({ showUpdateOptions: false });
}
};

isRegisteredAuthCredentialUpdated = () => {
const { auth } = this.props.existingDataSource;
const currentAuth = this.state.auth;

if (
currentAuth.type === AuthType.NoAuth ||
currentAuth.type === AuthType.UsernamePasswordType ||
currentAuth.type === AuthType.SigV4
) {
return false;
}

const existingAuthCredentials = extractRegisteredAuthTypeCredentials(
(auth?.credentials ?? {}) as { [key: string]: string },
currentAuth.type,
this.authenticationMethodRegistery
);

const registeredAuthCredentials = extractRegisteredAuthTypeCredentials(
(currentAuth?.credentials ?? {}) as { [key: string]: string },
currentAuth.type,
this.authenticationMethodRegistery
);

return !deepEqual(existingAuthCredentials, registeredAuthCredentials);
};

renderBottomBar = () => {
return (
<EuiBottomBar data-test-subj="datasource-edit-bottomBar" affordForDisplacement={true}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,71 @@ describe('DataSourceManagement: Utils.ts', () => {

expect(deepEqual(registedAuthTypeCredentials, expectExtractedAuthCredentials));
});

test('Should inherit value from registered field when credentail state not have registered field', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: credentail. Actually, there are only 8 files referenced by crendentialFormField. Can you please fix crendentialFormField in this PR, and then fix #6122 (comment) (31 files referenced)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks for the comment.
Fixing.

Copy link
Member Author

@xinruiba xinruiba Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crendentialFormField typo resolved, thanks.

const authTypeToBeTested = 'Some Auth Type';

const authMethodToBeTested = {
name: authTypeToBeTested,
credentialSourceOption: {
value: authTypeToBeTested,
inputDisplay: 'some input',
},
crendentialFormField: {
registeredField: 'some value',
},
} as AuthenticationMethod;

const mockedCredentialState = {} as { [key: string]: string };

const expectExtractedAuthCredentials = {
registeredField: 'some value',
};

const authenticationMethodRegistery = new AuthenticationMethodRegistery();
authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTested);

const registedAuthTypeCredentials = extractRegisteredAuthTypeCredentials(
mockedCredentialState,
authTypeToBeTested,
authenticationMethodRegistery
);

expect(deepEqual(registedAuthTypeCredentials, expectExtractedAuthCredentials));
});

test('Should not inherit value from registered field when credentail state have registered field', () => {
const authTypeToBeTested = 'Some Auth Type';

const authMethodToBeTested = {
name: authTypeToBeTested,
credentialSourceOption: {
value: authTypeToBeTested,
inputDisplay: 'some input',
},
crendentialFormField: {
registeredField: 'Some value',
},
} as AuthenticationMethod;

const mockedCredentialState = {
registeredField: 'some other values',
} as { [key: string]: string };

const expectExtractedAuthCredentials = {
registeredField: 'some other values',
};

const authenticationMethodRegistery = new AuthenticationMethodRegistery();
authenticationMethodRegistery.registerAuthenticationMethod(authMethodToBeTested);

const registedAuthTypeCredentials = extractRegisteredAuthTypeCredentials(
mockedCredentialState,
authTypeToBeTested,
authenticationMethodRegistery
);

expect(deepEqual(registedAuthTypeCredentials, expectExtractedAuthCredentials));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const extractRegisteredAuthTypeCredentials = (
authenticationMethodRegistery.getAuthenticationMethod(authType)?.crendentialFormField ?? {};

Object.keys(registeredCredentialField).forEach((credentialFiled) => {
registeredCredentials[credentialFiled] = currentCredentialState[credentialFiled] ?? '';
registeredCredentials[credentialFiled] =
currentCredentialState[credentialFiled] ?? registeredCredentialField[credentialFiled];
});

return registeredCredentials;
Expand Down
Loading