Skip to content

Commit

Permalink
feat: adding SSL Toggle to Create Database Modal (#14976)
Browse files Browse the repository at this point in the history
* first draft of SSL Toggle

* added payload data

* Update superset-frontend/src/views/CRUD/data/database/DatabaseModal/DatabaseConnectionForm.tsx

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>

* changed tooltips based on stephen advice

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
Co-authored-by: Hugh A. Miles II <hughmil3s@gmail.com>
  • Loading branch information
3 people authored Jun 5, 2021
1 parent 298f660 commit 860efef
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { FormEvent, useState } from 'react';
import { SupersetTheme, JsonObject } from '@superset-ui/core';
import React, { FormEvent, useEvent } from 'react';
import { SupersetTheme, JsonObject, t } from '@superset-ui/core';
import { InputProps } from 'antd/lib/input';
import { Select, Button } from 'src/common/components';
import { Switch, Select, Button } from 'src/common/components';
import InfoTooltip from 'src/components/InfoTooltip';
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput';
import { DeleteFilled } from '@ant-design/icons';
import {
formScrollableStyles,
validatedFormStyles,
CredentialInfoForm,
StyledFormHeader,
toggleStyle,
infoTooltip,
} from './styles';
import { DatabaseForm, DatabaseObject } from '../types';

Expand All @@ -42,6 +45,7 @@ export const FormFieldOrder = [
'username',
'password',
'database_name',
'encryption',
'credentials_info',
];

Expand All @@ -53,6 +57,8 @@ interface FieldPropTypes {
validationErrors: JsonObject | null;
getValidation: () => void;
db?: DatabaseObject;
isEditMode?: boolean;
sslForced?: boolean;
}

const credentialsInfo = ({
Expand Down Expand Up @@ -259,6 +265,34 @@ const displayField = ({
helpText="Pick a nickname for this database to display as in Superset."
/>
);
const forceSSLField = ({
isEditMode,
changeMethods,
db,
sslForced,
}: FieldPropTypes) => (
<div css={(theme: SupersetTheme) => infoTooltip(theme)}>
<Switch
disabled={sslForced && !isEditMode}
checked={db?.parameters?.encryption || sslForced}
onChange={changed => {
changeMethods.onParametersChange({
target: {
type: 'toggle',
name: 'encryption',
checked: true,
value: changed,
},
});
}}
/>
<span css={toggleStyle}>SSL</span>
<InfoTooltip
tooltip={t('SSL will be enabled in the database connection')}
placement="bottomRight"
/>
</div>
);

const FORM_FIELD_MAP = {
host: hostField,
Expand All @@ -267,6 +301,7 @@ const FORM_FIELD_MAP = {
username: usernameField,
password: passwordField,
database_name: displayField,
encryption: forceSSLField,
credentials_info: credentialsInfo,
};

Expand All @@ -279,8 +314,10 @@ const DatabaseConnectionForm = ({
getValidation,
db,
isEditMode = false,
sslForced,
}: {
isEditMode?: boolean;
sslForced: boolean;
dbModel: DatabaseForm;
db: Partial<DatabaseObject> | null;
onParametersChange: (
Expand All @@ -296,13 +333,14 @@ const DatabaseConnectionForm = ({
getValidation: () => void;
}) => (
<>
<StyledFormHeader>
<p className="helper"> Step 2 of 3 </p>
<h4>Enter the required {name} credentials</h4>
<p className="helper">
Need help? Learn more about connecting to {name}.
</p>
</StyledFormHeader>
{!isEditMode && (
<StyledFormHeader>
<h4>Enter the required {name} credentials</h4>
<p className="helper">
Need help? Learn more about connecting to {name}.
</p>
</StyledFormHeader>
)}
<div
// @ts-ignore
css={(theme: SupersetTheme) => [
Expand All @@ -327,6 +365,8 @@ const DatabaseConnectionForm = ({
getValidation,
db,
key: field,
isEditMode,
sslForced,
}),
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
import { t, SupersetTheme } from '@superset-ui/core';
import {
t,
SupersetTheme,
FeatureFlag,
isFeatureEnabled,
} from '@superset-ui/core';
import React, {
FunctionComponent,
useEffect,
Expand Down Expand Up @@ -44,7 +49,6 @@ import {
} from 'src/views/CRUD/data/database/types';
import ExtraOptions from './ExtraOptions';
import SqlAlchemyForm from './SqlAlchemyForm';

import DatabaseConnectionForm from './DatabaseConnectionForm';
import {
antDAlertStyles,
Expand Down Expand Up @@ -209,8 +213,10 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const [dbName, setDbName] = useState('');
const [isLoading, setLoading] = useState<boolean>(false);
const conf = useCommonConf();

const isEditMode = !!databaseId;
const sslForced = isFeatureEnabled(
FeatureFlag.FORCE_DATABASE_CONNECTIONS_SSL,
);
const useSqlAlchemyForm =
db?.configuration_method === CONFIGURATION_METHOD.SQLALCHEMY_URI;
const useTabLayout = isEditMode || useSqlAlchemyForm;
Expand Down Expand Up @@ -309,10 +315,11 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
}
};

const setDatabaseModel = engine => {
const setDatabaseModel = (engine: string) => {
const isDynamic =
availableDbs?.databases.filter(db => db.engine === engine)[0]
.parameters !== undefined;
availableDbs?.databases.filter(
(db: DatabaseObject) => db.engine === engine,
)[0].parameters !== undefined;
setDB({
type: ActionType.dbSelected,
payload: {
Expand All @@ -329,13 +336,13 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
<span className="available-label">
Or choose from a list of other databases we support{' '}
</span>
<label className="label-available-select">supported databases</label>
<Label className="label-available-select">supported databases</Label>
<Select
style={{ width: '100%' }}
onChange={setDatabaseModel}
placeholder="Choose a database..."
>
{availableDbs?.databases?.map(database => (
{availableDbs?.databases?.map((database: DatabaseForm) => (
<Select.Option value={database.engine} key={database.engine}>
{database.name}
</Select.Option>
Expand All @@ -347,10 +354,10 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const renderPreferredSelector = () => (
<div className="preferred">
{availableDbs?.databases
?.filter(db => db.preferred)
.map(database => (
?.filter((db: DatabaseForm) => db.preferred)
.map((database: DatabaseForm) => (
<IconButton
className="preferred-item"
icon="preferred-item"
onClick={() => setDatabaseModel(database.engine)}
buttonText={database.name}
/>
Expand All @@ -370,7 +377,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
Back
</Button>,
!hasConnectedDb ? ( // if hasConnectedDb show back + finish
<Button key="submit" type="primary" onClick={onSave}>
<Button key="submit" buttonStyle="primary" onClick={onSave}>
Connect
</Button>
) : (
Expand Down Expand Up @@ -490,6 +497,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
) : (
<DatabaseConnectionForm
isEditMode
sslForced={sslForced}
dbModel={dbModel}
db={db as DatabaseObject}
onParametersChange={({ target }: { target: HTMLInputElement }) =>
Expand Down Expand Up @@ -618,6 +626,8 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
{!isLoading && db && (
<>
<DatabaseConnectionForm
db={db}
sslForced={sslForced}
dbModel={dbModel}
onParametersChange={({
target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ export const antDModalNoPaddingStyles = css`
}
`;

export const infoTooltip = (theme: SupersetTheme) => css`
svg {
vertical-align: bottom;
margin-bottom: ${theme.gridUnit * 0.25}px;
}
`;

export const toggleStyle = (theme: SupersetTheme) => css`
padding-left: ${theme.gridUnit * 2}px;
`;

export const formScrollableStyles = (theme: SupersetTheme) => css`
padding-left: ${theme.gridUnit * 4}px;
padding-right: ${theme.gridUnit * 4}px;
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/views/CRUD/data/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type DatabaseObject = {
database?: string;
username?: string;
password?: string;
encryption?: boolean;
};
configuration_method: CONFIGURATION_METHOD;
engine?: string;
Expand Down

0 comments on commit 860efef

Please sign in to comment.