import { FC, useCallback, useMemo, useRef, useState } from 'react'; import { Grid } from '@mui/material'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; import handleAPIError from '../../lib/handleAPIError'; import MessageGroup, { MessageGroupForwardedRefContent } from '../MessageGroup'; import OutlinedInputWithLabel from '../OutlinedInputWithLabel'; import UncontrolledInput from '../UncontrolledInput'; import useFormikUtils from '../../hooks/useFormikUtils'; import Spinner from '../Spinner'; import schema from './testAccessSchema'; import { BodyText } from '../Text'; const TestAccessForm: FC = (props) => { const { setResponse, tools } = props; const messageGroupRef = useRef(null); const [loadingInquiry, setLoadingInquiry] = useState(false); const [moreActions, setMoreActions] = useState([]); const setApiMessage = useCallback( (message?: Message) => messageGroupRef.current?.setMessage?.call(null, 'api', message), [], ); const { disabledSubmit, formik, formikErrors, handleChange } = useFormikUtils({ initialValues: { ip: '', password: '', }, onSubmit: (values, { setSubmitting }) => { setApiMessage(); setLoadingInquiry(true); setMoreActions([]); setResponse(undefined); const { ip, password } = values; api .put('/command/inquire-host', { ipAddress: ip, password, }) .then(({ data }) => { const { badSshKeys, isConnected } = data; if (badSshKeys) { setApiMessage({ children: ( <> Host identification at {ip} changed. If this is valid, please delete the conflicting SSH host key. ), type: 'warning', }); setMoreActions([ { background: 'red', children: 'Delete keys', onClick: () => { tools.confirm.prepare({ actionProceedText: 'Delete', content: ( There's a different host key on {ip}, which could mean a MITM attack. But if this change is expected, you can delete the known host key(s) to resolve the conflict. ), onProceedAppend: () => { tools.confirm.loading(true); api .delete('/ssh-key/conflict', { data: badSshKeys, }) .then(() => { tools.confirm.finish('Success', { children: ( <>Started job to delete host key(s) for {ip}. ), }); setMoreActions([]); }) .catch((error) => { const emsg = handleAPIError(error); emsg.children = ( <>Failed to delete host key(s). {emsg.children} ); tools.confirm.finish('Error', emsg); }); }, proceedColour: 'red', titleText: `Delete all known SSH host key(s) for ${ip}?`, }); tools.confirm.open(true); }, type: 'button', }, ]); return; } if (!isConnected) { setApiMessage({ children: ( <> Failed to connect. Please make sure the credentials are correct, and the host is reachable from this striker. ), type: 'warning', }); return; } setResponse({ ...data, hostIpAddress: ip, hostPassword: password, }); setApiMessage(); }) .catch((error) => { const emsg = handleAPIError(error); emsg.children = ( <> Failed to access {ip}. {emsg.children} ); setApiMessage(emsg); }) .finally(() => { setSubmitting(false); setLoadingInquiry(false); }); }, validationSchema: schema, }); const ipChain = useMemo(() => 'ip', []); const passwordChain = useMemo(() => 'password', []); return ( { event.preventDefault(); formik.submitForm(); }} spacing="1em" > } /> } /> {loadingInquiry ? ( ) : ( )} ); }; export default TestAccessForm;