From c33a7e27b6eedb2eb273d8cd0eaa70125eb70434 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 21 Jun 2023 05:04:29 -0400 Subject: [PATCH] fix(striker-ui-api): make required endpoints public when striker is not configured --- .../lib/request_handlers/host/createHost.ts | 9 ++-- .../src/lib/request_handlers/host/index.ts | 1 + .../lib/request_handlers/host/updateHost.ts | 4 +- striker-ui-api/src/routes/index.ts | 2 + striker-ui-api/src/routes/init.ts | 50 +++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 striker-ui-api/src/routes/init.ts diff --git a/striker-ui-api/src/lib/request_handlers/host/createHost.ts b/striker-ui-api/src/lib/request_handlers/host/createHost.ts index 9aae36bd..39275e42 100644 --- a/striker-ui-api/src/lib/request_handlers/host/createHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/createHost.ts @@ -1,8 +1,5 @@ import { RequestHandler } from 'express'; -import { buildBranchRequestHandler } from '../buildBranchRequestHandler'; -import { configStriker } from './configStriker'; - -export const createHost: RequestHandler = buildBranchRequestHandler({ - striker: configStriker, -}); +export const createHost: RequestHandler = (request, response) => { + return response.status(204).send(); +}; diff --git a/striker-ui-api/src/lib/request_handlers/host/index.ts b/striker-ui-api/src/lib/request_handlers/host/index.ts index df94e1bb..88c06c1c 100644 --- a/striker-ui-api/src/lib/request_handlers/host/index.ts +++ b/striker-ui-api/src/lib/request_handlers/host/index.ts @@ -1,3 +1,4 @@ +export * from './configStriker'; export * from './createHost'; export * from './createHostConnection'; export * from './deleteHostConnection'; diff --git a/striker-ui-api/src/lib/request_handlers/host/updateHost.ts b/striker-ui-api/src/lib/request_handlers/host/updateHost.ts index c17e4795..31948025 100644 --- a/striker-ui-api/src/lib/request_handlers/host/updateHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/updateHost.ts @@ -1,8 +1,10 @@ import { RequestHandler } from 'express'; import { buildBranchRequestHandler } from '../buildBranchRequestHandler'; +import { configStriker } from './configStriker'; import { setHostInstallTarget } from './setHostInstallTarget'; export const updateHost: RequestHandler = buildBranchRequestHandler({ - 'install-target': setHostInstallTarget, + 'install-target': setHostInstallTarget as RequestHandler, + striker: configStriker, }); diff --git a/striker-ui-api/src/routes/index.ts b/striker-ui-api/src/routes/index.ts index 1fb9f91c..ea99b35d 100644 --- a/striker-ui-api/src/routes/index.ts +++ b/striker-ui-api/src/routes/index.ts @@ -5,6 +5,7 @@ import echoRouter from './echo'; import fenceRouter from './fence'; import fileRouter from './file'; import hostRouter from './host'; +import initRouter from './init'; import jobRouter from './job'; import manifestRouter from './manifest'; import networkInterfaceRouter from './network-interface'; @@ -32,6 +33,7 @@ const routes = { public: { auth: authRouter, echo: echoRouter, + init: initRouter, }, static: staticRouter, }; diff --git a/striker-ui-api/src/routes/init.ts b/striker-ui-api/src/routes/init.ts new file mode 100644 index 00000000..58ba8578 --- /dev/null +++ b/striker-ui-api/src/routes/init.ts @@ -0,0 +1,50 @@ +import express from 'express'; + +import { getLocalHostUUID, query } from '../lib/accessModule'; +import { stderr, stdoutVar } from '../lib/shell'; + +import { setMapNetwork } from '../lib/request_handlers/command'; +import { configStriker } from '../lib/request_handlers/host'; +import { getNetworkInterface } from '../lib/request_handlers/network-interface'; + +const router = express.Router(); + +router.use(async (request, response, next) => { + const localHostUuid = getLocalHostUUID(); + + let rows: [[string]]; + + try { + rows = await query( + `SELECT variable_value + FROM variables + WHERE variable_name = 'system::configured' + AND variable_source_table = 'hosts' + AND variable_source_uuid = '${localHostUuid}' + LIMIT 1;`, + ); + } catch (error) { + stderr(`Failed to get system configured flag; CAUSE: ${error}`); + + return response.status(500).send(); + } + + stdoutVar(rows, `system::configured=`); + + if (rows.length === 1 && rows[0][0] === '1') { + stderr( + `The init endpoints cannot be used after initializing the local striker`, + ); + + return response.status(401).send(); + } + + return next(); +}); + +router + .get('/network-interface', getNetworkInterface) + .post('/', configStriker) + .put('/set-map-network', setMapNetwork); + +export default router;