fix(striker-ui-api): split /ssh-key and /ssh-key/conflict

main
Tsu-ba-me 2 years ago
parent 14a5b178dd
commit e67e732fd8
  1. 1
      striker-ui-api/src/lib/consts/SERVER_PATHS.ts
  2. 39
      striker-ui-api/src/lib/request_handlers/ssh-key/deleteSSHKeyConflict.ts
  3. 90
      striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKey.ts
  4. 66
      striker-ui-api/src/lib/request_handlers/ssh-key/getSSHKeyConflict.ts
  5. 3
      striker-ui-api/src/lib/request_handlers/ssh-key/index.ts
  6. 9
      striker-ui-api/src/routes/ssh-key.ts

@ -18,6 +18,7 @@ const EMPTY_SERVER_PATHS: ServerPath = {
'anvil-access-module': {},
'anvil-configure-host': {},
'anvil-get-server-screenshot': {},
'anvil-manage-keys': {},
'anvil-manage-power': {},
'anvil-provision-server': {},
'anvil-sync-shared': {},

@ -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…
Cancel
Save