|
| 1 | +import { useFormContext } from 'react-hook-form'; |
| 2 | +import { APISIX, type APISIXType } from '@/types/schema/apisix'; |
| 3 | +import { FormItemSwitch } from '@/components/form/Switch'; |
| 4 | +import { FormItemTextInput } from '@/components/form/TextInput'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { FormSection } from './FormSection'; |
| 7 | +import { FormItemSelect } from '@/components/form/Select'; |
| 8 | +import { Divider, InputWrapper } from '@mantine/core'; |
| 9 | +import { FormItemTagsInput } from '../form/TagInput'; |
| 10 | + |
| 11 | +const VaultSecretForm = () => { |
| 12 | + const { t } = useTranslation(); |
| 13 | + const { control } = useFormContext<APISIXType['Secret']>(); |
| 14 | + |
| 15 | + return ( |
| 16 | + <> |
| 17 | + <FormItemTextInput |
| 18 | + control={control} |
| 19 | + name="uri" |
| 20 | + label={t('form.secrets.vault.uri')} |
| 21 | + /> |
| 22 | + <FormItemTextInput |
| 23 | + control={control} |
| 24 | + name="prefix" |
| 25 | + label={t('form.secrets.vault.prefix')} |
| 26 | + /> |
| 27 | + <FormItemTextInput |
| 28 | + control={control} |
| 29 | + name="token" |
| 30 | + label={t('form.secrets.vault.token')} |
| 31 | + /> |
| 32 | + <FormItemTextInput |
| 33 | + control={control} |
| 34 | + name="namespace" |
| 35 | + label={t('form.secrets.vault.namespace')} |
| 36 | + /> |
| 37 | + </> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +const AWSSecretForm = () => { |
| 42 | + const { t } = useTranslation(); |
| 43 | + const { control } = useFormContext<APISIXType['Secret']>(); |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <FormItemTextInput |
| 48 | + control={control} |
| 49 | + name="access_key_id" |
| 50 | + label={t('form.secrets.aws.access_key_id')} |
| 51 | + /> |
| 52 | + <FormItemTextInput |
| 53 | + control={control} |
| 54 | + name="secret_access_key" |
| 55 | + label={t('form.secrets.aws.secret_access_key')} |
| 56 | + /> |
| 57 | + <FormItemTextInput |
| 58 | + control={control} |
| 59 | + name="session_token" |
| 60 | + label={t('form.secrets.aws.session_token')} |
| 61 | + /> |
| 62 | + |
| 63 | + <FormItemTextInput |
| 64 | + control={control} |
| 65 | + name="region" |
| 66 | + label={t('form.secrets.aws.region')} |
| 67 | + /> |
| 68 | + <FormItemTextInput |
| 69 | + control={control} |
| 70 | + name="endpoint_url" |
| 71 | + label={t('form.secrets.aws.endpoint_url')} |
| 72 | + /> |
| 73 | + </> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const GCPSecretForm = () => { |
| 78 | + const { t } = useTranslation(); |
| 79 | + const { control } = useFormContext<APISIXType['Secret']>(); |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <InputWrapper label={t('form.secrets.gcp.ssl_verify')}> |
| 84 | + <FormItemSwitch control={control} name="ssl_verify" /> |
| 85 | + </InputWrapper> |
| 86 | + <FormSection legend={t('form.secrets.gcp.auth')}> |
| 87 | + <FormItemTextInput |
| 88 | + control={control} |
| 89 | + name="auth_file" |
| 90 | + label={t('form.secrets.gcp.auth_file')} |
| 91 | + /> |
| 92 | + <Divider my="xs" label={t('or')} /> |
| 93 | + <FormSection legend={t('form.secrets.gcp.auth_config')}> |
| 94 | + <FormItemTextInput |
| 95 | + control={control} |
| 96 | + name="auth_config.client_email" |
| 97 | + label={t('form.secrets.gcp.client_email')} |
| 98 | + /> |
| 99 | + <FormItemTextInput |
| 100 | + control={control} |
| 101 | + name="auth_config.private_key" |
| 102 | + label={t('form.secrets.gcp.private_key')} |
| 103 | + /> |
| 104 | + <FormItemTextInput |
| 105 | + control={control} |
| 106 | + name="auth_config.project_id" |
| 107 | + label={t('form.secrets.gcp.project_id')} |
| 108 | + /> |
| 109 | + <FormItemTextInput |
| 110 | + control={control} |
| 111 | + name="auth_config.token_uri" |
| 112 | + label={t('form.secrets.gcp.token_uri')} |
| 113 | + /> |
| 114 | + <FormItemTagsInput |
| 115 | + control={control} |
| 116 | + name="auth_config.scope" |
| 117 | + label={t('form.secrets.gcp.scope')} |
| 118 | + /> |
| 119 | + <FormItemTextInput |
| 120 | + control={control} |
| 121 | + name="auth_config.entries_uri" |
| 122 | + label={t('form.secrets.gcp.entries_uri')} |
| 123 | + /> |
| 124 | + </FormSection> |
| 125 | + </FormSection> |
| 126 | + </> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +type FormSectionManagerProps = { readOnlyManager?: boolean }; |
| 131 | +const FormSectionManager = (props: FormSectionManagerProps) => { |
| 132 | + const { readOnlyManager } = props; |
| 133 | + const { t } = useTranslation(); |
| 134 | + const { control } = useFormContext<APISIXType['Secret']>(); |
| 135 | + return ( |
| 136 | + <FormSection legend={t('form.secrets.manager')} disabled={readOnlyManager}> |
| 137 | + <FormItemSelect |
| 138 | + control={control} |
| 139 | + name="manager" |
| 140 | + defaultValue={APISIX.Secret.options[0].shape.manager.value} |
| 141 | + data={APISIX.Secret.options.map((v) => v.shape.manager.value)} |
| 142 | + /> |
| 143 | + </FormSection> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +const FormSectionManagerConfig = () => { |
| 148 | + const { t } = useTranslation(); |
| 149 | + const { watch } = useFormContext<APISIXType['Secret']>(); |
| 150 | + // useWatch not working here |
| 151 | + const manager = watch('manager'); |
| 152 | + return ( |
| 153 | + <FormSection legend={t('form.secrets.managerConfig')}> |
| 154 | + {manager === 'vault' && <VaultSecretForm />} |
| 155 | + {manager === 'aws' && <AWSSecretForm />} |
| 156 | + {manager === 'gcp' && <GCPSecretForm />} |
| 157 | + </FormSection> |
| 158 | + ); |
| 159 | +}; |
| 160 | + |
| 161 | +/** |
| 162 | + * id and manager cannot be changed when editing |
| 163 | + */ |
| 164 | +export const FormPartSecret = (props: FormSectionManagerProps) => { |
| 165 | + const { readOnlyManager } = props; |
| 166 | + return ( |
| 167 | + <> |
| 168 | + <FormSectionManager readOnlyManager={readOnlyManager} /> |
| 169 | + <FormSectionManagerConfig /> |
| 170 | + </> |
| 171 | + ); |
| 172 | +}; |
0 commit comments