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

Hide/Show authentication method in multi data source plugin based on configuration #5916

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
333987f
[AuthType Config] Add EnabledAuthType configuration in yml file and p…
xinruiba Feb 20, 2024
5327710
[AuthType Config] provide default auth types when types array is empty
xinruiba Feb 20, 2024
cc91319
[AuthRegistry Onboard - DataSourceCreationForm] Integrate auth cofig …
xinruiba Feb 22, 2024
0339047
[AuthRegistry Onboard] Support default selected auth type in datasour…
xinruiba Feb 23, 2024
9b138fe
[AuthRegistry Onboard] SDisable auth option selector when auth option…
xinruiba Feb 23, 2024
f0b4c88
[AuthRegistry Onboard] clear credential attribute every time use sele…
xinruiba Feb 23, 2024
c6bb34c
Merge branch 'opensearch-project:main' into xinrui_AuthMethod_Config
xinruiba Feb 23, 2024
1ebd2e2
Update yml file to disable config
xinruiba Feb 23, 2024
91e734e
Update change.md file
xinruiba Feb 23, 2024
fc0369e
[UT] Fix broken test cases
xinruiba Feb 23, 2024
2bf8abe
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 23, 2024
f0201ef
[UT] Add more unit tests
xinruiba Feb 23, 2024
ae1b8c8
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 23, 2024
246b1d3
Merge branch 'opensearch-project:main' into xinrui_AuthMethod_Config
xinruiba Feb 26, 2024
551f87a
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 27, 2024
c2da4e2
[UT] update unit test and handle scenario when no options enabled
xinruiba Feb 27, 2024
8f2c75a
[UT] update description of unit test case
xinruiba Feb 27, 2024
6d7463e
[UT] update unit test cases to resolve comment
xinruiba Feb 27, 2024
f11cebf
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 27, 2024
b0b1918
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 27, 2024
9d39598
[UT] Snapshot auth option super selector for unit test. Also upate th…
xinruiba Feb 27, 2024
070195d
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 27, 2024
39f77b9
Merge branch 'main' into xinrui_AuthMethod_Config
xinruiba Feb 27, 2024
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 @@ -69,6 +69,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Add api registry and allow it to be added into client config in data source plugin ([#5895](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5895))
- [Multiple Datasource] Concatenate data source name with index pattern name and change delimiter to double colon ([#5907](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5907))
- [Multiple Datasource] Refactor client and legacy client to use authentication registry ([#5881](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5881))
- [Multiple Datasource] Hide/Show authentication method in multi data source plugin based on configuration ([#5916](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5916))

### 🐛 Bug Fixes

Expand Down
11 changes: 11 additions & 0 deletions config/opensearch_dashboards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@
# 'ff00::/8',
# ]

# 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:
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
# 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"

Expand Down
11 changes: 11 additions & 0 deletions src/plugins/data_source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ export const configSchema = schema.object({
appender: fileAppenderSchema,
}),
endpointDeniedIPs: schema.maybe(schema.arrayOf(schema.string())),
authTypes: schema.object({
NoAuthentication: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
UsernamePassword: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
AWSSigV4: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
}),
});

export type DataSourcePluginConfigType = TypeOf<typeof configSchema>;
6 changes: 6 additions & 0 deletions src/plugins/data_source/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
return {
dataSourceEnabled: config.enabled,
hideLocalCluster: config.hideLocalCluster,
noAuthenticationTypeEnabled: config.authTypes.NoAuthentication.enabled,
usernamePasswordAuthEnabled: config.authTypes.UsernamePassword.enabled,
awsSigV4AuthEnabled: config.authTypes.AWSSigV4.enabled,
};
}

Expand All @@ -30,6 +33,9 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
return {
dataSourceEnabled: config.enabled,
hideLocalCluster: config.hideLocalCluster,
noAuthenticationTypeEnabled: config.authTypes.NoAuthentication.enabled,
usernamePasswordAuthEnabled: config.authTypes.UsernamePassword.enabled,
awsSigV4AuthEnabled: config.authTypes.AWSSigV4.enabled,
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data_source/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
export interface DataSourcePluginSetup {
dataSourceEnabled: boolean;
hideLocalCluster: boolean;
noAuthenticationTypeEnabled: boolean;
usernamePasswordAuthEnabled: boolean;
awsSigV4AuthEnabled: boolean;
}

export interface DataSourcePluginStart {
dataSourceEnabled: boolean;
hideLocalCluster: boolean;
noAuthenticationTypeEnabled: boolean;
usernamePasswordAuthEnabled: boolean;
awsSigV4AuthEnabled: boolean;
}
1 change: 1 addition & 0 deletions src/plugins/data_source/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const config: PluginConfigDescriptor<DataSourcePluginConfigType> = {
exposeToBrowser: {
enabled: true,
hideLocalCluster: true,
authTypes: true,
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
},
schema: configSchema,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { EuiSuperSelectOption } from '@elastic/eui';

export interface AuthenticationMethod {
name: string;
credentialForm: React.JSX.Element;
credentialSourceOption: EuiSuperSelectOption<string>;
credentialForm?: React.JSX.Element;
crendentialFormField?: { [key: string]: string };
}

export type IAuthenticationMethodRegistery = Omit<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { OpenSearchDashboardsContextProvider } from '../../../../../../opensearc
import { CreateDataSourceForm } from './create_data_source_form';
// @ts-ignore
import { findTestSubject } from '@elastic/eui/lib/test';
import { AuthType } from '../../../../types';
import {
AuthType,
noAuthCredentialAuthMethod,
sigV4AuthMethod,
usernamePasswordAuthMethod,
} from '../../../../types';
import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry';

const titleIdentifier = '[data-test-subj="createDataSourceFormTitleField"]';
const descriptionIdentifier = `[data-test-subj="createDataSourceFormDescriptionField"]`;
Expand All @@ -21,9 +27,18 @@ const usernameIdentifier = '[data-test-subj="createDataSourceFormUsernameField"]
const passwordIdentifier = '[data-test-subj="createDataSourceFormPasswordField"]';
const createButtonIdentifier = '[data-test-subj="createDataSourceButton"]';
const testConnectionButtonIdentifier = '[data-test-subj="createDataSourceTestConnectionButton"]';
const authOptionSelectorIdentifier = '[data-test-subj="createDataSourceFormAuthTypeSelect"]';

describe('Datasource Management: Create Datasource form', () => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(
noAuthCredentialAuthMethod
);
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(
usernamePasswordAuthMethod
);
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(sigV4AuthMethod);

let component: ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;
const mockSubmitHandler = jest.fn();
const mockTestConnectionHandler = jest.fn();
Expand Down Expand Up @@ -222,3 +237,129 @@ describe('Datasource Management: Create Datasource form', () => {
expect(component.find(passwordIdentifier).first().props().isInvalid).toBe(false);
});
});

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

/* Scenario 1: Should render the page normally with all authMethod combinations */
test('should render normally with all authMethod combinations', () => {
const authMethodCombinationsToBeTested = [
[noAuthCredentialAuthMethod],
[usernamePasswordAuthMethod],
[sigV4AuthMethod],
[noAuthCredentialAuthMethod, usernamePasswordAuthMethod],
[noAuthCredentialAuthMethod, sigV4AuthMethod],
[usernamePasswordAuthMethod, sigV4AuthMethod],
[noAuthCredentialAuthMethod, usernamePasswordAuthMethod, sigV4AuthMethod],
];

authMethodCombinationsToBeTested.forEach((authMethodCombination) => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery();

authMethodCombination.forEach((authMethod) => {
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod);
});

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

const testConnBtn = component.find(testConnectionButtonIdentifier).last();
expect(testConnBtn.prop('disabled')).toBe(true);
});
bandinib-amzn marked this conversation as resolved.
Show resolved Hide resolved
});

/* Scenario 2: options selector should be disabled when only one authMethod supported */
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
test('options selector should be disabled when only one authMethod supported', () => {
const authMethodCombinationsToBeTested = [
[noAuthCredentialAuthMethod],
[usernamePasswordAuthMethod],
[sigV4AuthMethod],
];

authMethodCombinationsToBeTested.forEach((authMethodCombination) => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery();

authMethodCombination.forEach((authMethod) => {
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod);
});

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

const authOptionSelector = component.find(authOptionSelectorIdentifier).last();
expect(authOptionSelector.prop('disabled')).toBe(true);
});
});

/* Scenario 3: options selector should not be disabled when more than one authMethod supported */
test('options selector should not be disabled when more than one authMethod supported', () => {
const authMethodCombinationsToBeTested = [
[sigV4AuthMethod, usernamePasswordAuthMethod],
[noAuthCredentialAuthMethod, sigV4AuthMethod],
[noAuthCredentialAuthMethod, usernamePasswordAuthMethod],
[noAuthCredentialAuthMethod, sigV4AuthMethod, usernamePasswordAuthMethod],
];

authMethodCombinationsToBeTested.forEach((authMethodCombination) => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery();

authMethodCombination.forEach((authMethod) => {
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod);
});

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

const authOptionSelector = component.find(authOptionSelectorIdentifier).last();
expect(authOptionSelector.prop('disabled')).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
EuiSuperSelect,
EuiSpacer,
EuiText,
EuiSuperSelectOption,
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';
import { SigV4Content, SigV4ServiceName } from '../../../../../../data_source/common/data_sources';
import {
AuthType,
credentialSourceOptions,
DataSourceAttributes,
DataSourceManagementContextValue,
UsernamePasswordTypedContent,
Expand All @@ -38,7 +38,7 @@ import {
isTitleValid,
performDataSourceFormValidation,
} from '../../../validation';
import { isValidUrl } from '../../../utils';
import { getDefaultAuthMethod, isValidUrl } from '../../../utils';

export interface CreateDataSourceProps {
existingDatasourceNamesList: string[];
Expand All @@ -55,7 +55,7 @@ export interface CreateDataSourceState {
endpoint: string;
auth: {
type: AuthType;
credentials: UsernamePasswordTypedContent | SigV4Content;
credentials: UsernamePasswordTypedContent | SigV4Content | undefined;
};
}

Expand All @@ -66,19 +66,28 @@ export class CreateDataSourceForm extends React.Component<
static contextType = contextType;
public readonly context!: DataSourceManagementContextValue;

authOptions: Array<EuiSuperSelectOption<string>> = [];

constructor(props: CreateDataSourceProps, context: DataSourceManagementContextValue) {
super(props, context);

const authenticationMethodRegistery = context.services.authenticationMethodRegistery;
const registeredAuthMethods = authenticationMethodRegistery.getAllAuthenticationMethods();
const initialSelectedAuthMethod = getDefaultAuthMethod(authenticationMethodRegistery);

this.authOptions = registeredAuthMethods.map((authMethod) => {
return authMethod.credentialSourceOption;
});

this.state = {
formErrorsByField: { ...defaultValidation },
title: '',
description: '',
endpoint: '',
auth: {
type: AuthType.UsernamePasswordType,
type: initialSelectedAuthMethod?.name,
credentials: {
username: '',
password: '',
...initialSelectedAuthMethod?.crendentialFormField,
},
},
};
Expand Down Expand Up @@ -297,6 +306,9 @@ export class CreateDataSourceForm extends React.Component<
service: this.state.auth.credentials.service || SigV4ServiceName.OpenSearch,
} as SigV4Content;
}
if (this.state.auth.type === AuthType.NoAuth) {
credentials = {};
}

return {
title: this.state.title,
Expand Down Expand Up @@ -598,9 +610,10 @@ export class CreateDataSourceForm extends React.Component<
<EuiSpacer size="l" />
<EuiFormRow>
<EuiSuperSelect
options={credentialSourceOptions}
options={this.authOptions}
valueOfSelected={this.state.auth.type}
onChange={(value) => this.onChangeAuthType(value)}
disabled={this.authOptions.length <= 1}
name="Credential"
data-test-subj="createDataSourceFormAuthTypeSelect"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {
import { OpenSearchDashboardsContextProvider } from '../../../../../../opensearch_dashboards_react/public';
import { EditDataSourceForm } from './edit_data_source_form';
import { act } from 'react-dom/test-utils';
import { AuthType } from '../../../../types';
import {
AuthType,
noAuthCredentialAuthMethod,
sigV4AuthMethod,
usernamePasswordAuthMethod,
} from '../../../../types';

const titleFieldIdentifier = 'dataSourceTitle';
const titleFormRowIdentifier = '[data-test-subj="editDataSourceTitleFormRow"]';
Expand All @@ -29,6 +34,14 @@ const passwordFieldIdentifier = '[data-test-subj="updateDataSourceFormPasswordFi
const updatePasswordBtnIdentifier = '[data-test-subj="editDatasourceUpdatePasswordBtn"]';
describe('Datasource Management: Edit Datasource Form', () => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(
noAuthCredentialAuthMethod
);
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(
usernamePasswordAuthMethod
);
mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(sigV4AuthMethod);

let component: ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;
const mockFn = jest.fn();

Expand Down
Loading
Loading