|
|
@ -7,13 +7,13 @@ import { |
|
|
|
REP_PEACEFUL_STRING, |
|
|
|
REP_PEACEFUL_STRING, |
|
|
|
} from '../../consts'; |
|
|
|
} from '../../consts'; |
|
|
|
|
|
|
|
|
|
|
|
import { getLocalHostUUID, getPeerData, query } from '../../accessModule'; |
|
|
|
import { getPeerData, query } from '../../accessModule'; |
|
|
|
import { sanitize } from '../../sanitize'; |
|
|
|
import { sanitize } from '../../sanitize'; |
|
|
|
import { perr } from '../../shell'; |
|
|
|
import { perr } from '../../shell'; |
|
|
|
|
|
|
|
|
|
|
|
export const getHostSSH: RequestHandler< |
|
|
|
export const getHostSSH: RequestHandler< |
|
|
|
unknown, |
|
|
|
unknown, |
|
|
|
GetHostSshResponseBody, |
|
|
|
GetHostSshResponseBody | ErrorResponseBody, |
|
|
|
GetHostSshRequestBody |
|
|
|
GetHostSshRequestBody |
|
|
|
> = async (request, response) => { |
|
|
|
> = async (request, response) => { |
|
|
|
const { |
|
|
|
const { |
|
|
@ -45,36 +45,61 @@ export const getHostSSH: RequestHandler< |
|
|
|
return response.status(400).send(); |
|
|
|
return response.status(400).send(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const localHostUUID = getLocalHostUUID(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rsbody: GetHostSshResponseBody; |
|
|
|
let rsbody: GetHostSshResponseBody; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
rsbody = await getPeerData(target, { password, port }); |
|
|
|
rsbody = await getPeerData(target, { password, port }); |
|
|
|
} catch (subError) { |
|
|
|
} catch (error) { |
|
|
|
perr(`Failed to get peer data; CAUSE: ${subError}`); |
|
|
|
const emsg = `Failed to get peer data; CAUSE: ${error}`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
perr(emsg); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rserror: ErrorResponseBody = { |
|
|
|
|
|
|
|
code: 'fe14fb1', |
|
|
|
|
|
|
|
message: emsg, |
|
|
|
|
|
|
|
name: 'AccessError', |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.status(500).send(rserror); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return response.status(500).send(); |
|
|
|
let states: [string, string][]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
states = await query<[stateUuid: string, hostUuid: string][]>(` |
|
|
|
|
|
|
|
SELECT a.state_uuid, a.state_host_uuid |
|
|
|
|
|
|
|
FROM states AS a |
|
|
|
|
|
|
|
WHERE a.state_name = '${HOST_KEY_CHANGED_PREFIX}${target}';`);
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
|
|
const emsg = `Failed to list SSH key conflicts; CAUSE: ${error}`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
perr(emsg); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rserror: ErrorResponseBody = { |
|
|
|
|
|
|
|
code: 'd5a2acf', |
|
|
|
|
|
|
|
message: emsg, |
|
|
|
|
|
|
|
name: 'AccessError', |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.status(500).send(rserror); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!rsbody.isConnected) { |
|
|
|
if (states.length > 0) { |
|
|
|
const rows: [stateNote: string, stateUUID: string][] = await query(` |
|
|
|
rsbody.badSshKeys = states.reduce<DeleteSshKeyConflictRequestBody>( |
|
|
|
SELECT sta.state_note, sta.state_uuid |
|
|
|
(previous, state) => { |
|
|
|
FROM states AS sta |
|
|
|
const [stateUuid, hostUuid] = state; |
|
|
|
WHERE sta.state_host_uuid = '${localHostUUID}' |
|
|
|
|
|
|
|
AND sta.state_name = '${HOST_KEY_CHANGED_PREFIX}${target}';`);
|
|
|
|
const { [hostUuid]: list = [] } = previous; |
|
|
|
|
|
|
|
|
|
|
|
if (rows.length > 0) { |
|
|
|
list.push(stateUuid); |
|
|
|
rsbody.badSSHKeys = rows.reduce<DeleteSshKeyConflictRequestBody>( |
|
|
|
|
|
|
|
(previous, [, stateUUID]) => { |
|
|
|
previous[hostUuid] = list; |
|
|
|
previous[localHostUUID].push(stateUUID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return previous; |
|
|
|
return previous; |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ [localHostUUID]: [] }, |
|
|
|
{}, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.status(200).send(rsbody); |
|
|
|
response.status(200).send(rsbody); |
|
|
|
}; |
|
|
|
}; |
|
|
|