parent
14a5b178dd
commit
e67e732fd8
6 changed files with 115 additions and 93 deletions
@ -0,0 +1,39 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import SERVER_PATHS from '../../consts/SERVER_PATHS'; |
||||
|
||||
import { job } from '../../accessModule'; |
||||
import { stderr } from '../../shell'; |
||||
|
||||
export const deleteSSHKeyConflict: RequestHandler< |
||||
unknown, |
||||
undefined, |
||||
{ [hostUUID: string]: string[] } |
||||
> = (request, response) => { |
||||
const { body } = request; |
||||
const hostUUIDs = Object.keys(body); |
||||
|
||||
hostUUIDs.forEach((hostUUID) => { |
||||
const stateUUIDs = body[hostUUID]; |
||||
|
||||
try { |
||||
job({ |
||||
file: __filename, |
||||
job_command: SERVER_PATHS.usr.sbin['anvil-manage-keys'].self, |
||||
job_data: stateUUIDs.join(','), |
||||
job_description: 'job_0057', |
||||
job_host_uuid: hostUUID, |
||||
job_name: 'manage::broken_keys', |
||||
job_title: 'job_0056', |
||||
}); |
||||
} catch (subError) { |
||||
stderr(`Failed to delete bad SSH keys; CAUSE: ${subError}`); |
||||
|
||||
response.status(500).send(); |
||||
|
||||
return; |
||||
} |
||||
}); |
||||
|
||||
response.status(204).send(); |
||||
}; |
@ -1,90 +0,0 @@ |
||||
import { getLocalHostUUID } from '../../accessModule'; |
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import { buildQueryResultReducer } from '../../buildQueryResultModifier'; |
||||
import { toLocal } from '../../convertHostUUID'; |
||||
import { match } from '../../match'; |
||||
import { sanitizeQS } from '../../sanitizeQS'; |
||||
|
||||
type BuildQuerySubFunction = (result: Parameters<BuildQueryFunction>[0]) => { |
||||
afterQueryReturn?: QueryResultModifierFunction; |
||||
query: string; |
||||
}; |
||||
|
||||
const HOST_KEY_CHANGED_PREFIX = 'host_key_changed::'; |
||||
|
||||
const MAP_TO_HANDLER: Record<string, BuildQuerySubFunction> = { |
||||
conflict: () => { |
||||
const localHostUUID: string = getLocalHostUUID(); |
||||
|
||||
return { |
||||
afterQueryReturn: buildQueryResultReducer<{ |
||||
[hostUUID: string]: { |
||||
[stateUUID: string]: { |
||||
badFile: string; |
||||
badLine: number; |
||||
hostName: string; |
||||
hostUUID: string; |
||||
ipAddress: string; |
||||
stateUUID: string; |
||||
}; |
||||
}; |
||||
}>((previous, [hostName, hostUUID, stateName, stateNote, stateUUID]) => { |
||||
const hostUUIDKey = toLocal(hostUUID, localHostUUID); |
||||
|
||||
if (previous[hostUUIDKey] === undefined) { |
||||
previous[hostUUIDKey] = {}; |
||||
} |
||||
|
||||
const ipAddress = stateName.slice(HOST_KEY_CHANGED_PREFIX.length); |
||||
const [, badFile, badLine = '0'] = match( |
||||
stateNote, |
||||
/file=([^\s]+),line=(\d+)/, |
||||
); |
||||
|
||||
previous[hostUUIDKey][stateUUID] = { |
||||
badFile, |
||||
badLine: parseInt(badLine), |
||||
hostName, |
||||
hostUUID, |
||||
ipAddress, |
||||
stateUUID, |
||||
}; |
||||
|
||||
return previous; |
||||
}, {}), |
||||
query: ` |
||||
SELECT |
||||
hos.host_name, |
||||
hos.host_uuid, |
||||
sta.state_name, |
||||
sta.state_note, |
||||
sta.state_uuid |
||||
FROM states AS sta |
||||
JOIN hosts AS hos |
||||
ON sta.state_host_uuid = hos.host_uuid |
||||
WHERE sta.state_name LIKE '${HOST_KEY_CHANGED_PREFIX}%';`,
|
||||
}; |
||||
}, |
||||
}; |
||||
|
||||
export const getSSHKey = buildGetRequestHandler( |
||||
(request, buildQueryOptions) => { |
||||
const { type: rawType } = request.query; |
||||
|
||||
const type = sanitizeQS(rawType, { |
||||
modifierType: 'sql', |
||||
returnType: 'string', |
||||
}); |
||||
|
||||
const { afterQueryReturn, query } = MAP_TO_HANDLER[type]?.call( |
||||
null, |
||||
request, |
||||
) ?? { query: '' }; |
||||
|
||||
if (buildQueryOptions) { |
||||
buildQueryOptions.afterQueryReturn = afterQueryReturn; |
||||
} |
||||
|
||||
return query; |
||||
}, |
||||
); |
@ -0,0 +1,66 @@ |
||||
import { getLocalHostUUID } from '../../accessModule'; |
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import { buildQueryResultReducer } from '../../buildQueryResultModifier'; |
||||
import { toLocal } from '../../convertHostUUID'; |
||||
import { match } from '../../match'; |
||||
|
||||
const HOST_KEY_CHANGED_PREFIX = 'host_key_changed::'; |
||||
|
||||
export const getSSHKeyConflict = buildGetRequestHandler( |
||||
(request, buildQueryOptions) => { |
||||
const localHostUUID: string = getLocalHostUUID(); |
||||
|
||||
const query = ` |
||||
SELECT |
||||
hos.host_name, |
||||
hos.host_uuid, |
||||
sta.state_name, |
||||
sta.state_note, |
||||
sta.state_uuid |
||||
FROM states AS sta |
||||
JOIN hosts AS hos |
||||
ON sta.state_host_uuid = hos.host_uuid |
||||
WHERE sta.state_name LIKE '${HOST_KEY_CHANGED_PREFIX}%';`;
|
||||
const afterQueryReturn = buildQueryResultReducer<{ |
||||
[hostUUID: string]: { |
||||
[stateUUID: string]: { |
||||
badFile: string; |
||||
badLine: number; |
||||
hostName: string; |
||||
hostUUID: string; |
||||
ipAddress: string; |
||||
stateUUID: string; |
||||
}; |
||||
}; |
||||
}>((previous, [hostName, hostUUID, stateName, stateNote, stateUUID]) => { |
||||
const hostUUIDKey = toLocal(hostUUID, localHostUUID); |
||||
|
||||
if (previous[hostUUIDKey] === undefined) { |
||||
previous[hostUUIDKey] = {}; |
||||
} |
||||
|
||||
const ipAddress = stateName.slice(HOST_KEY_CHANGED_PREFIX.length); |
||||
const [, badFile, badLine = '0'] = match( |
||||
stateNote, |
||||
/file=([^\s]+),line=(\d+)/, |
||||
); |
||||
|
||||
previous[hostUUIDKey][stateUUID] = { |
||||
badFile, |
||||
badLine: parseInt(badLine), |
||||
hostName, |
||||
hostUUID, |
||||
ipAddress, |
||||
stateUUID, |
||||
}; |
||||
|
||||
return previous; |
||||
}, {}); |
||||
|
||||
if (buildQueryOptions) { |
||||
buildQueryOptions.afterQueryReturn = afterQueryReturn; |
||||
} |
||||
|
||||
return query; |
||||
}, |
||||
); |
@ -1 +1,2 @@ |
||||
export * from './getSSHKey'; |
||||
export * from './deleteSSHKeyConflict'; |
||||
export * from './getSSHKeyConflict'; |
||||
|
@ -1,9 +1,14 @@ |
||||
import express from 'express'; |
||||
|
||||
import { getSSHKey } from '../lib/request_handlers/ssh-key'; |
||||
import { |
||||
deleteSSHKeyConflict, |
||||
getSSHKeyConflict, |
||||
} from '../lib/request_handlers/ssh-key'; |
||||
|
||||
const router = express.Router(); |
||||
|
||||
router.get('/', getSSHKey); |
||||
router |
||||
.get('/conflict', getSSHKeyConflict) |
||||
.delete('/conflict', deleteSSHKeyConflict); |
||||
|
||||
export default router; |
||||
|
Loading…
Reference in new issue