Skip to content

Commit

Permalink
[AuthRegistry Onboard - DataSourceCreationForm] Integrate auth cofig …
Browse files Browse the repository at this point in the history
…with AuthRegistray and providing data source options from AuthRegistry

Signed-off-by: Xinrui Bai <xinruiba@amazon.com>
  • Loading branch information
xinruiba committed Feb 22, 2024
1 parent 5327710 commit cc91319
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 52 deletions.
20 changes: 11 additions & 9 deletions config/opensearch_dashboards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -270,14 +270,16 @@
# 'ff00::/8',
# ]

# Full AuthType list: ['NoAuth', 'UsernamePasswordType', 'SigV4'].
# Add / Remove elements in this list to Enable / Diasble auth types.
# If this setting is commented then all options will be available.
data_source.enabledAuthTypes: [
'NoAuth',
'UsernamePasswordType',
'SigV4',
]
# 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

# 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
12 changes: 10 additions & 2 deletions src/plugins/data_source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ export const configSchema = schema.object({
appender: fileAppenderSchema,
}),
endpointDeniedIPs: schema.maybe(schema.arrayOf(schema.string())),
enabledAuthTypes: schema.arrayOf(schema.string(), {
defaultValue: ['NoAuth', 'UsernamePasswordType', 'SigV4'],
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 }),
}),
}),
});

Expand Down
8 changes: 6 additions & 2 deletions src/plugins/data_source/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
return {
dataSourceEnabled: config.enabled,
hideLocalCluster: config.hideLocalCluster,
enabledAuthTypes: config.enabledAuthTypes,
noAuthenticationTypeEnabled: config.authTypes.NoAuthentication.enabled,
usernamePasswordAuthEnabled: config.authTypes.UsernamePassword.enabled,
awsSigV4AuthEnabled: config.authTypes.AWSSigV4.enabled,
};
}

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

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

export interface DataSourcePluginStart {
dataSourceEnabled: boolean;
hideLocalCluster: boolean;
enabledAuthTypes: string[];
noAuthenticationTypeEnabled: boolean;
usernamePasswordAuthEnabled: boolean;
awsSigV4AuthEnabled: boolean;
}
2 changes: 1 addition & 1 deletion src/plugins/data_source/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const config: PluginConfigDescriptor<DataSourcePluginConfigType> = {
exposeToBrowser: {
enabled: true,
hideLocalCluster: true,
enabledAuthTypes: true,
authTypes: true,
},
schema: configSchema,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EuiSuperSelectOption } from '@elastic/eui';

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

Expand Down
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 Down Expand Up @@ -55,7 +55,7 @@ export interface CreateDataSourceState {
endpoint: string;
auth: {
type: AuthType;
credentials: UsernamePasswordTypedContent | SigV4Content;
credentials: UsernamePasswordTypedContent | SigV4Content | undefined;
};
}

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

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

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

this.enabledAuthTypes = context.services.enabledAuthTypes;
this.authOptions = context.services.authenticationMethodRegistery
.getAllAuthenticationMethods()
.map((authMethod) => {
return authMethod.credentialSourceOption;
});

this.state = {
formErrorsByField: { ...defaultValidation },
Expand Down Expand Up @@ -602,7 +606,7 @@ 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)}
name="Credential"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Router, Switch } from 'react-router-dom';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { DataSourcePluginSetup } from 'src/plugins/data_source/public';
import { ManagementAppMountParams } from '../../../management/public';

import { OpenSearchDashboardsContextProvider } from '../../../opensearch_dashboards_react/public';
import { CreateDataSourceWizardWithRouter } from '../components/create_data_source_wizard';
import { DataSourceTableWithRouter } from '../components/data_source_table';
import { AuthType, DataSourceManagementContext } from '../types';
import { EditDataSourceWithRouter } from '../components/edit_data_source';
import { AuthenticationMethodRegistery } from '../auth_registry';

export interface DataSourceManagementStartDependencies {
data: DataPublicPluginStart;
Expand All @@ -26,14 +26,12 @@ export interface DataSourceManagementStartDependencies {
export async function mountManagementSection(
getStartServices: StartServicesAccessor<DataSourceManagementStartDependencies>,
params: ManagementAppMountParams,
dataSource: DataSourcePluginSetup
authMethodsRegistry: AuthenticationMethodRegistery
) {
const [
{ chrome, application, savedObjects, uiSettings, notifications, overlays, http, docLinks },
] = await getStartServices();

const allSupportedAuthTypes = Object.keys(AuthType);

const deps: DataSourceManagementContext = {
chrome,
application,
Expand All @@ -44,10 +42,7 @@ export async function mountManagementSection(
http,
docLinks,
setBreadcrumbs: params.setBreadcrumbs,
enabledAuthTypes:
dataSource.enabledAuthTypes.length === 0
? allSupportedAuthTypes
: dataSource.enabledAuthTypes,
authenticationMethodRegistery: authMethodsRegistry,
};

ReactDOM.render(
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/data_source_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IAuthenticationMethodRegistery,
AuthenticationMethodRegistery,
} from './auth_registry';
import { noAuthCredentialAuthMethod, sigV4AuthMethod, usernamePasswordAuthMethod } from './types';

export interface DataSourceManagementSetupDependencies {
management: ManagementSetup;
Expand Down Expand Up @@ -67,7 +68,7 @@ export class DataSourceManagementPlugin
mount: async (params) => {
const { mountManagementSection } = await import('./management_app');

return mountManagementSection(core.getStartServices, params, dataSource);
return mountManagementSection(core.getStartServices, params, this.authMethodsRegistry);
},
});

Expand All @@ -80,6 +81,16 @@ export class DataSourceManagementPlugin
this.authMethodsRegistry.registerAuthenticationMethod(authMethod);
};

if (dataSource.noAuthenticationTypeEnabled) {
registerAuthenticationMethod(noAuthCredentialAuthMethod);
}
if (dataSource.usernamePasswordAuthEnabled) {
registerAuthenticationMethod(usernamePasswordAuthMethod);
}
if (dataSource.awsSigV4AuthEnabled) {
registerAuthenticationMethod(sigV4AuthMethod);
}

return { registerAuthenticationMethod };
}

Expand Down
64 changes: 43 additions & 21 deletions src/plugins/data_source_management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SavedObjectAttributes } from 'src/core/types';
import { i18n } from '@osd/i18n';
import { SigV4ServiceName } from '../../data_source/common/data_sources';
import { OpenSearchDashboardsReactContextValue } from '../../opensearch_dashboards_react/public';
import { AuthenticationMethodRegistery } from './auth_registry';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DataSourceManagementPluginStart {}
Expand All @@ -32,7 +33,7 @@ export interface DataSourceManagementContext {
http: HttpSetup;
docLinks: DocLinksStart;
setBreadcrumbs: ManagementAppMountParams['setBreadcrumbs'];
enabledAuthTypes: string[];
authenticationMethodRegistery: AuthenticationMethodRegistery;
}

export interface DataSourceTableItem {
Expand All @@ -59,26 +60,26 @@ export enum AuthType {
SigV4 = 'sigv4',
}

export const credentialSourceOptions = [
{
value: AuthType.NoAuth,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.NoAuthentication', {
defaultMessage: 'No authentication',
}),
},
{
value: AuthType.UsernamePasswordType,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.UsernamePassword', {
defaultMessage: 'Username & Password',
}),
},
{
value: AuthType.SigV4,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.AwsSigV4', {
defaultMessage: 'AWS SigV4',
}),
},
];
export const noAuthCredentialOption = {
value: AuthType.NoAuth,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.NoAuthentication', {
defaultMessage: 'No authentication',
}),
};

export const usernamePasswordCredentialOption = {
value: AuthType.UsernamePasswordType,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.UsernamePassword', {
defaultMessage: 'Username & Password',
}),
};

export const sigV4CredentialOption = {
value: AuthType.SigV4,
inputDisplay: i18n.translate('dataSourceManagement.credentialSourceOptions.AwsSigV4', {
defaultMessage: 'AWS SigV4',
}),
};

export const sigV4ServiceOptions = [
{
Expand All @@ -95,6 +96,27 @@ export const sigV4ServiceOptions = [
},
];

export const noAuthCredentialAuthMethod = {
name: AuthType.NoAuth,
credentialSourceOption: noAuthCredentialOption,
};

export const usernamePasswordAuthMethod = {
name: AuthType.UsernamePasswordType,
credentialSourceOption: usernamePasswordCredentialOption,
};

export const sigV4AuthMethod = {
name: AuthType.SigV4,
credentialSourceOption: sigV4CredentialOption,
};

export const credentialSourceOptions = [
noAuthCredentialOption,
usernamePasswordCredentialOption,
sigV4CredentialOption,
];

export interface DataSourceAttributes extends SavedObjectAttributes {
title: string;
description?: string;
Expand Down

0 comments on commit cc91319

Please sign in to comment.