This is an automated email from the ASF dual-hosted git repository. mintsweet pushed a commit to branch feat-5643 in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit fedb57ab3535c5ee65cdeb6b01cade69bee1b910 Author: mintsweet <[email protected]> AuthorDate: Mon Dec 11 20:06:08 2023 +1300 feat(config-ui): not returning the tokens or passwords on APIs --- config-ui/src/api/connection/index.ts | 16 ++++- config-ui/src/features/connections/slice.ts | 16 +---- config-ui/src/features/connections/utils.ts | 3 +- .../components/connection-form/fields/index.tsx | 10 +-- .../components/connection-form/fields/password.tsx | 21 ++++--- .../connection-form/fields/secret-key.tsx | 21 ++++--- .../components/connection-form/fields/token.tsx | 17 +++--- .../plugins/components/connection-form/index.tsx | 71 +++++++++++++--------- config-ui/src/plugins/register/github/config.tsx | 4 +- .../github/connection-fields/githubapp.tsx | 2 +- .../register/github/connection-fields/token.tsx | 45 ++++++++++---- .../register/jira/connection-fields/auth.tsx | 21 ++++++- config-ui/src/types/connection.ts | 19 +++++- 13 files changed, 176 insertions(+), 90 deletions(-) diff --git a/config-ui/src/api/connection/index.ts b/config-ui/src/api/connection/index.ts index d19c87cac..a546c9855 100644 --- a/config-ui/src/api/connection/index.ts +++ b/config-ui/src/api/connection/index.ts @@ -16,7 +16,7 @@ * */ -import { IConnectionAPI, IConnectionTestResult } from '@/types'; +import { IConnectionAPI, IConnectionTestResult, IConnectionOldTestResult } from '@/types'; import { request } from '@/utils'; export const list = (plugin: string): Promise<IConnectionAPI[]> => request(`/plugins/${plugin}/connections`); @@ -37,9 +37,21 @@ export const update = (plugin: string, id: ID, payload: Omit<IConnectionAPI, 'id }); export const test = ( + plugin: string, + connectionId: ID, + payload?: Partial< + Pick< + IConnectionAPI, + 'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 'secretKey' | 'proxy' | 'dbUrl' + > + >, +): Promise<IConnectionTestResult> => + request(`/plugins/${plugin}/connections/${connectionId}/test`, { method: 'post', data: payload }); + +export const testOld = ( plugin: string, payload: Pick< IConnectionAPI, 'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 'secretKey' | 'proxy' | 'dbUrl' >, -): Promise<IConnectionTestResult> => request(`/plugins/${plugin}/test`, { method: 'post', data: payload }); +): Promise<IConnectionOldTestResult> => request(`/plugins/${plugin}/test`, { method: 'post', data: payload }); diff --git a/config-ui/src/features/connections/slice.ts b/config-ui/src/features/connections/slice.ts index d8be32f64..8eb886015 100644 --- a/config-ui/src/features/connections/slice.ts +++ b/config-ui/src/features/connections/slice.ts @@ -92,21 +92,9 @@ export const removeConnection = createAsyncThunk( export const testConnection = createAsyncThunk( 'connections/testConnection', - async ( - { unique, plugin, endpoint, proxy, token, username, password, authMethod, secretKey, appId }: IConnection, - { rejectWithValue }, - ) => { + async ({ plugin, id, unique }: IConnection, { rejectWithValue }) => { try { - const res = await API.connection.test(plugin, { - endpoint, - proxy, - token, - username, - password, - authMethod, - secretKey, - appId, - }); + const res = await API.connection.test(plugin, id); return { unique, diff --git a/config-ui/src/features/connections/utils.ts b/config-ui/src/features/connections/utils.ts index a7d3a584c..3fe4fac75 100644 --- a/config-ui/src/features/connections/utils.ts +++ b/config-ui/src/features/connections/utils.ts @@ -30,13 +30,14 @@ export const transformConnection = (plugin: string, connection: IConnectionAPI): name: connection.name, status: IConnectionStatus.IDLE, endpoint: connection.endpoint, - proxy: connection.proxy, authMethod: connection.authMethod, token: connection.token, username: connection.username, password: connection.password, appId: connection.appId, secretKey: connection.secretKey, + dbUrl: connection.dbUrl, + proxy: connection.proxy, enableGraphql: connection.enableGraphql, rateLimitPerHour: connection.rateLimitPerHour, }; diff --git a/config-ui/src/plugins/components/connection-form/fields/index.tsx b/config-ui/src/plugins/components/connection-form/fields/index.tsx index 514ba3f95..f6aeb0df9 100644 --- a/config-ui/src/plugins/components/connection-form/fields/index.tsx +++ b/config-ui/src/plugins/components/connection-form/fields/index.tsx @@ -27,6 +27,7 @@ import { ConnectionAppId } from './app-id'; import { ConnectionSecretKey } from './secret-key'; interface Props { + type: 'create' | 'update'; name: string; fields: any[]; initialValues: any; @@ -36,7 +37,7 @@ interface Props { setErrors: (errors: any) => void; } -export const Form = ({ name, fields, initialValues, values, errors, setValues, setErrors }: Props) => { +export const Form = ({ type, name, fields, initialValues, values, errors, setValues, setErrors }: Props) => { const onValues = (values: any) => setValues((prev: any) => ({ ...prev, ...values })); const onErrors = (values: any) => setErrors((prev: any) => ({ ...prev, ...values })); @@ -55,6 +56,7 @@ export const Form = ({ name, fields, initialValues, values, errors, setValues, s return fields.map((field) => { if (typeof field === 'function') { return field({ + type, initialValues, values, setValues: onValues, @@ -73,13 +75,13 @@ export const Form = ({ name, fields, initialValues, values, errors, setValues, s case 'username': return <ConnectionUsername key={key} {...getProps('username')} {...field} />; case 'password': - return <ConnectionPassword key={key} {...getProps('password')} {...field} />; + return <ConnectionPassword key={key} type={type} {...getProps('password')} {...field} />; case 'token': - return <ConnectionToken key={key} {...getProps('token')} {...field} />; + return <ConnectionToken key={key} type={type} {...getProps('token')} {...field} />; case 'appId': return <ConnectionAppId key={key} {...getProps('appId')} {...field} />; case 'secretKey': - return <ConnectionSecretKey key={key} {...getProps('secretKey')} {...field} />; + return <ConnectionSecretKey key={key} type={type} {...getProps('secretKey')} {...field} />; case 'proxy': return <ConnectionProxy key={key} {...getProps('proxy')} {...field} />; case 'rateLimitPerHour': diff --git a/config-ui/src/plugins/components/connection-form/fields/password.tsx b/config-ui/src/plugins/components/connection-form/fields/password.tsx index 98dcc0680..0117d0b8e 100644 --- a/config-ui/src/plugins/components/connection-form/fields/password.tsx +++ b/config-ui/src/plugins/components/connection-form/fields/password.tsx @@ -23,6 +23,7 @@ import { FormGroup } from '@blueprintjs/core'; import * as S from './styled'; interface Props { + type: 'create' | 'update'; label?: string; subLabel?: string; placeholder?: string; @@ -30,11 +31,12 @@ interface Props { initialValue: string; value: string; error: string; - setValue: (value: string) => void; - setError: (value: string) => void; + setValue: (value?: string) => void; + setError: (value?: string) => void; } export const ConnectionPassword = ({ + type, label, subLabel, placeholder, @@ -44,12 +46,12 @@ export const ConnectionPassword = ({ setError, }: Props) => { useEffect(() => { - setValue(initialValue); - }, [initialValue]); + setValue(type === 'create' ? initialValue : undefined); + }, [type, initialValue]); useEffect(() => { - setError(value ? '' : 'password is required'); - }, [value]); + setError(type === 'create' && !value ? 'password is required' : undefined); + }, [type, value]); const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { setValue(e.target.value); @@ -61,7 +63,12 @@ export const ConnectionPassword = ({ labelInfo={<S.LabelInfo>*</S.LabelInfo>} subLabel={subLabel ? <S.LabelDescription>{subLabel}</S.LabelDescription> : null} > - <Input.Password placeholder={placeholder ?? 'Your Password'} value={value} onChange={handleChange} /> + <Input.Password + style={{ width: 386 }} + placeholder={placeholder ?? 'Your Password'} + value={value} + onChange={handleChange} + /> </FormGroup> ); }; diff --git a/config-ui/src/plugins/components/connection-form/fields/secret-key.tsx b/config-ui/src/plugins/components/connection-form/fields/secret-key.tsx index dd3fedcc5..7e9e342a8 100644 --- a/config-ui/src/plugins/components/connection-form/fields/secret-key.tsx +++ b/config-ui/src/plugins/components/connection-form/fields/secret-key.tsx @@ -40,6 +40,7 @@ import { FormGroup } from '@blueprintjs/core'; import * as S from './styled'; interface Props { + type: 'create' | 'update'; label?: string; subLabel?: string; placeholder?: string; @@ -47,11 +48,12 @@ interface Props { initialValue: string; value: string; error: string; - setValue: (value: string) => void; - setError: (value: string) => void; + setValue: (value?: string) => void; + setError: (value?: string) => void; } export const ConnectionSecretKey = ({ + type, label, subLabel, placeholder, @@ -61,12 +63,12 @@ export const ConnectionSecretKey = ({ setError, }: Props) => { useEffect(() => { - setValue(initialValue); - }, [initialValue]); + setValue(type === 'create' ? initialValue : undefined); + }, [type, initialValue]); useEffect(() => { - setError(value ? '' : 'SecretKey is required'); - }, [value]); + setError(type === 'create' && !value ? 'SecretKey is required' : undefined); + }, [type, value]); const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { setValue(e.target.value); @@ -78,7 +80,12 @@ export const ConnectionSecretKey = ({ labelInfo={<S.LabelInfo>*</S.LabelInfo>} subLabel={subLabel ? <S.LabelDescription>{subLabel}</S.LabelDescription> : null} > - <Input.Password placeholder={placeholder ?? 'Your SecretKey'} value={value} onChange={handleChange} /> + <Input.Password + style={{ width: 386 }} + placeholder={placeholder ?? 'Your SecretKey'} + value={value} + onChange={handleChange} + /> </FormGroup> ); }; diff --git a/config-ui/src/plugins/components/connection-form/fields/token.tsx b/config-ui/src/plugins/components/connection-form/fields/token.tsx index 71e736a43..8360b9e4a 100644 --- a/config-ui/src/plugins/components/connection-form/fields/token.tsx +++ b/config-ui/src/plugins/components/connection-form/fields/token.tsx @@ -40,24 +40,25 @@ import { FormGroup } from '@blueprintjs/core'; import * as S from './styled'; interface Props { + type: 'create' | 'update'; label?: string; subLabel?: string; name: string; initialValue: string; value: string; error: string; - setValue: (value: string) => void; - setError: (value: string) => void; + setValue: (value?: string) => void; + setError: (value?: string) => void; } -export const ConnectionToken = ({ label, subLabel, initialValue, value, setValue, setError }: Props) => { +export const ConnectionToken = ({ type, label, subLabel, initialValue, value, setValue, setError }: Props) => { useEffect(() => { - setValue(initialValue); - }, [initialValue]); + setValue(type === 'create' ? initialValue : undefined); + }, [type, initialValue]); useEffect(() => { - setError(value ? '' : 'token is required'); - }, [value]); + setError(type === 'create' && !value ? 'token is required' : undefined); + }, [type, value]); const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { setValue(e.target.value); @@ -69,7 +70,7 @@ export const ConnectionToken = ({ label, subLabel, initialValue, value, setValue labelInfo={<S.LabelInfo>*</S.LabelInfo>} subLabel={subLabel && <S.LabelDescription>{subLabel}</S.LabelDescription>} > - <Input.Password placeholder="Your Token" value={value} onChange={handleChange} /> + <Input.Password style={{ width: 386 }} placeholder="Your Token" value={value} onChange={handleChange} /> </FormGroup> ); }; diff --git a/config-ui/src/plugins/components/connection-form/index.tsx b/config-ui/src/plugins/components/connection-form/index.tsx index 6a4ccd2d7..fb45460ec 100644 --- a/config-ui/src/plugins/components/connection-form/index.tsx +++ b/config-ui/src/plugins/components/connection-form/index.tsx @@ -16,9 +16,9 @@ * */ -import { useMemo, useState } from 'react'; -import { Button, Intent } from '@blueprintjs/core'; -import { pick } from 'lodash'; +import { useState, useEffect, useMemo } from 'react'; +import { isEqual, pick } from 'lodash'; +import { Button } from 'antd'; import API from '@/api'; import { useAppDispatch, useAppSelector } from '@/app/hook'; @@ -38,6 +38,7 @@ interface Props { } export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => { + const [type, setType] = useState<'create' | 'update'>('create'); const [values, setValues] = useState<any>({}); const [errors, setErrors] = useState<Record<string, any>>({}); const [operating, setOperating] = useState(false); @@ -45,6 +46,10 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => { const dispatch = useAppDispatch(); const connection = useAppSelector((state) => selectConnection(state, `${plugin}-${connectionId}`)); + useEffect(() => { + setType(connectionId ? 'update' : 'create'); + }, [connectionId]); + const { name, connection: { docLink, fields, initialValues }, @@ -57,22 +62,34 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => { const handleTest = async () => { await operator( () => - API.connection.test( - plugin, - pick(values, [ - 'endpoint', - 'token', - 'username', - 'password', - 'proxy', - 'authMethod', - 'appId', - 'secretKey', - 'tenantId', - 'tenantType', - 'dbUrl', - ]), - ), + type === 'update' && connectionId + ? API.connection.test(plugin, connectionId, { + endpoint: isEqual(connection?.endpoint, values.endpoint) ? undefined : values.endpoint, + authMethod: isEqual(connection?.authMethod, values.authMethod) ? undefined : values.authMethod, + username: isEqual(connection?.username, values.username) ? undefined : values.username, + password: isEqual(connection?.password, values.password) ? undefined : values.password, + token: isEqual(connection?.token, values.token) ? undefined : values.token, + appId: isEqual(connection?.appId, values.appId) ? undefined : values.appId, + secretKey: isEqual(connection?.secretKey, values.secretKey) ? undefined : values.secretKey, + proxy: isEqual(connection?.proxy, values.proxy) ? undefined : values.proxy, + dbUrl: isEqual(connection?.dbUrl, values.dbUrl) ? undefined : values.dbUrl, + }) + : API.connection.testOld( + plugin, + pick(values, [ + 'endpoint', + 'token', + 'username', + 'password', + 'proxy', + 'authMethod', + 'appId', + 'secretKey', + 'tenantId', + 'tenantType', + 'dbUrl', + ]), + ), { setOperating, formatMessage: () => 'Test Connection Successfully.', @@ -105,6 +122,7 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => { </S.Tips> <S.Form> <Form + type={type} name={name} fields={fields} initialValues={{ ...initialValues, ...(connection ?? {}) }} @@ -114,15 +132,12 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => { setErrors={setErrors} /> <Buttons position="bottom" align="right"> - <Button loading={operating} disabled={disabled} outlined text="Test Connection" onClick={handleTest} /> - <Button - loading={operating} - disabled={disabled} - intent={Intent.PRIMARY} - outlined - text="Save Connection" - onClick={handleSave} - /> + <Button loading={operating} disabled={disabled} onClick={handleTest}> + Test Connection + </Button> + <Button type="primary" loading={operating} disabled={disabled} onClick={handleSave}> + Save Connection + </Button> </Buttons> </S.Form> </S.Wrapper> diff --git a/config-ui/src/plugins/register/github/config.tsx b/config-ui/src/plugins/register/github/config.tsx index 38383f5fe..477cf7ba0 100644 --- a/config-ui/src/plugins/register/github/config.tsx +++ b/config-ui/src/plugins/register/github/config.tsx @@ -53,10 +53,12 @@ export const GitHubConfig: IPluginConfig = { setValue={(value) => setValues({ authMethod: value })} /> ), - ({ initialValues, values, errors, setValues, setErrors }: any) => + ({ type, initialValues, values, errors, setValues, setErrors }: any) => (values.authMethod || initialValues.authMethod) === 'AccessToken' ? ( <Token key="token" + type={type} + connectionId={initialValues.id} endpoint={values.endpoint} proxy={values.proxy} initialValue={initialValues.token ?? ''} diff --git a/config-ui/src/plugins/register/github/connection-fields/githubapp.tsx b/config-ui/src/plugins/register/github/connection-fields/githubapp.tsx index 2cff1a8fd..a6f3727c8 100644 --- a/config-ui/src/plugins/register/github/connection-fields/githubapp.tsx +++ b/config-ui/src/plugins/register/github/connection-fields/githubapp.tsx @@ -87,7 +87,7 @@ export const GithubApp = ({ endpoint, proxy, initialValue, value, error, setValu } try { - const res = await API.connection.test('github', { + const res = await API.connection.testOld('github', { authMethod: 'AppKey', endpoint, proxy, diff --git a/config-ui/src/plugins/register/github/connection-fields/token.tsx b/config-ui/src/plugins/register/github/connection-fields/token.tsx index a84f5c93e..3a215ebfe 100644 --- a/config-ui/src/plugins/register/github/connection-fields/token.tsx +++ b/config-ui/src/plugins/register/github/connection-fields/token.tsx @@ -34,16 +34,28 @@ type TokenItem = { }; interface Props { + type: 'create' | 'update'; + connectionId?: ID; endpoint?: string; proxy: string; initialValue: string; value: string; error: string; - setValue: (value: string) => void; - setError: (error: string) => void; + setValue: (value?: string) => void; + setError: (error?: string) => void; } -export const Token = ({ endpoint, proxy, initialValue, value, error, setValue, setError }: Props) => { +export const Token = ({ + type, + connectionId, + endpoint, + proxy, + initialValue, + value, + error, + setValue, + setError, +}: Props) => { const [tokens, setTokens] = useState<TokenItem[]>([{ value: '' }]); const testToken = async (token: string): Promise<TokenItem> => { @@ -54,7 +66,7 @@ export const Token = ({ endpoint, proxy, initialValue, value, error, setValue, s } try { - const res = await API.connection.test('github', { + const res = await API.connection.testOld('github', { authMethod: 'AccessToken', endpoint, proxy, @@ -74,22 +86,31 @@ export const Token = ({ endpoint, proxy, initialValue, value, error, setValue, s } }; - const checkTokens = async (value: string) => { - const res = await Promise.all((value ?? '').split(',').map((it) => testToken(it))); - setTokens(res); + const checkTokens = async (connectionId: ID) => { + const res = await API.connection.test('github', connectionId); + setTokens( + res.tokens.map((it) => ({ + value: it.token, + isValid: it.success || it.warning, + from: it.login, + status: !it.success ? 'error' : it.warning ? 'warning' : 'success', + })), + ); }; useEffect(() => { - checkTokens(initialValue); - }, [initialValue, endpoint]); + if (connectionId) { + checkTokens(connectionId); + } + }, [connectionId]); useEffect(() => { - setError(value ? '' : 'token is required'); + setError(type === 'create' && !value ? 'token is required' : undefined); return () => { setError(''); }; - }, [value]); + }, [type, value]); useEffect(() => { setValue(tokens.map((it) => it.value).join(',')); @@ -128,7 +149,9 @@ export const Token = ({ endpoint, proxy, initialValue, value, error, setValue, s <S.Input key={i}> <div className="input"> <Input.Password + style={{ width: 386 }} placeholder="Token" + value={value} onChange={(e) => handleChangeToken(i, e.target.value)} onBlur={() => handleTestToken(i)} /> diff --git a/config-ui/src/plugins/register/jira/connection-fields/auth.tsx b/config-ui/src/plugins/register/jira/connection-fields/auth.tsx index c18b900a3..2073051d3 100644 --- a/config-ui/src/plugins/register/jira/connection-fields/auth.tsx +++ b/config-ui/src/plugins/register/jira/connection-fields/auth.tsx @@ -160,7 +160,12 @@ export const Auth = ({ initialValues, values, setValues, setErrors }: Props) => </S.LabelDescription> } > - <Input.Password placeholder="Your PAT" value={values.password} onChange={handleChangePassword} /> + <Input.Password + style={{ width: 386 }} + placeholder="Your PAT" + value={values.password} + onChange={handleChangePassword} + /> </FormGroup> </> )} @@ -179,7 +184,12 @@ export const Auth = ({ initialValues, values, setValues, setErrors }: Props) => <InputGroup placeholder="Your Username" value={values.username} onChange={handleChangeUsername} /> </FormGroup> <FormGroup label={<S.Label>Password</S.Label>} labelInfo={<S.LabelInfo>*</S.LabelInfo>}> - <Input.Password placeholder="Your Password" value={values.password} onChange={handleChangePassword} /> + <Input.Password + style={{ width: 386 }} + placeholder="Your Password" + value={values.password} + onChange={handleChangePassword} + /> </FormGroup> </> )} @@ -195,7 +205,12 @@ export const Auth = ({ initialValues, values, setValues, setErrors }: Props) => </S.LabelDescription> } > - <Input.Password placeholder="Your PAT" value={values.token} onChange={handleChangeToken} /> + <Input.Password + style={{ width: 386 }} + placeholder="Your PAT" + value={values.token} + onChange={handleChangeToken} + /> </FormGroup> )} </> diff --git a/config-ui/src/types/connection.ts b/config-ui/src/types/connection.ts index 12fcc5bad..09121db95 100644 --- a/config-ui/src/types/connection.ts +++ b/config-ui/src/types/connection.ts @@ -24,16 +24,28 @@ export interface IConnectionAPI { token?: string; username?: string; password?: string; - apiKey?: string; - dbUrl?: string; appId?: string; secretKey?: string; + dbUrl?: string; enableGraphql?: boolean; proxy: string; rateLimitPerHour?: number; } export interface IConnectionTestResult { + success: boolean; + message: string; + causes: null; + data: null; + tokens: Array<{ + token: string; + success: boolean; + login: string; + warning: boolean; + }>; +} + +export interface IConnectionOldTestResult { message: string; success: boolean; login?: string; @@ -61,13 +73,14 @@ export interface IConnection { name: string; status: IConnectionStatus; endpoint: string; - proxy: string; authMethod?: string; token?: string; username?: string; password?: string; appId?: string; secretKey?: string; + dbUrl?: string; enableGraphql?: boolean; + proxy: string; rateLimitPerHour?: number; }
