major rework of backend loading and usage:

- split config from user data to config.json, add config.hson.template
- moved default user data localdb to root folder
- moved pve, localdb, ldap backend handlers to backends sub folder
- add dynamic loading of all backends
- add dynamic mapping for auth backends to support multiple auth sources
- update affected endpoints
This commit is contained in:
2024-01-06 22:36:18 +00:00
parent 7b0549e052
commit a0109d3546
21 changed files with 629 additions and 674 deletions
+23 -28
View File
@@ -1,13 +1,8 @@
import { Router } from "express";
export const router = Router({ mergeParams: true });
const db = global.db;
const requestPVE = global.pve.requestPVE;
const handleResponse = global.pve.handleResponse;
const getDiskInfo = global.pve.getDiskInfo;
const checkAuth = global.utils.checkAuth;
const approveResources = global.utils.approveResources;
const pveAPIToken = global.db.pveAPIToken;
/**
* POST - detach mounted disk from instance
@@ -37,7 +32,7 @@ router.post("/:disk/detach", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[params.disk]) {
res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
@@ -52,8 +47,8 @@ router.post("/:disk/detach", async (req, res) => {
}
const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT";
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -86,7 +81,7 @@ router.post("/:disk/attach", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[`unused${params.source}`]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@@ -94,8 +89,8 @@ router.post("/:disk/attach", async (req, res) => {
return;
}
// target disk must be allowed according to source disk's storage options
const diskConfig = await getDiskInfo(params.node, config, `unused${params.source}`); // get target disk
const resourceConfig = db.getGlobal().resources;
const diskConfig = await global.pve.getDiskInfo(params.node, config, `unused${params.source}`); // get target disk
const resourceConfig = global.config.resources;
if (!resourceConfig[diskConfig.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) {
res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[diskConfig.storage].disks}].` });
res.end();
@@ -107,8 +102,8 @@ router.post("/:disk/attach", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -142,9 +137,9 @@ router.post("/:disk/resize", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// check disk existence
const diskConfig = await getDiskInfo(params.node, config, params.disk); // get target disk
const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk
if (!diskConfig) { // exit if disk does not exist
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
res.end();
@@ -162,8 +157,8 @@ router.post("/:disk/resize", async (req, res) => {
}
// action approved, commit to action
const action = JSON.stringify({ disk: params.disk, size: `+${params.size}G` });
const result = await requestPVE(`${vmpath}/resize`, "PUT", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/resize`, "PUT", { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -199,9 +194,9 @@ router.post("/:disk/move", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// check disk existence
const diskConfig = await getDiskInfo(params.node, config, params.disk); // get target disk
const diskConfig = await global.pve.getDiskInfo(params.node, config, params.disk); // get target disk
if (!diskConfig) { // exit if disk does not exist
res.status(500).send({ error: `requested disk ${params.disk} does not exist.` });
res.end();
@@ -231,8 +226,8 @@ router.post("/:disk/move", async (req, res) => {
action = JSON.stringify(action);
const route = params.type === "qemu" ? "move_disk" : "move_volume";
// commit action
const result = await requestPVE(`${vmpath}/${route}`, "POST", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/${route}`, "POST", { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -263,7 +258,7 @@ router.delete("/:disk/delete", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist
if (!config[params.disk]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@@ -280,8 +275,8 @@ router.delete("/:disk/delete", async (req, res) => {
const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -318,7 +313,7 @@ router.post("/:disk/create", async (req, res) => {
return;
}
// get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must not exist
if (config[params.disk]) {
res.status(403).send({ error: `Requested disk ${params.disk} already exists.` });
@@ -337,7 +332,7 @@ router.post("/:disk/create", async (req, res) => {
return;
}
// target disk must be allowed according to storage options
const resourceConfig = db.getGlobal().resources;
const resourceConfig = global.config.resources;
if (!resourceConfig[params.storage].disks.some(diskPrefix => params.disk.startsWith(diskPrefix))) {
res.status(500).send({ error: `Requested target ${params.disk} is not in allowed list [${resourceConfig[params.storage].disks}].` });
res.end();
@@ -358,6 +353,6 @@ router.post("/:disk/create", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
+9 -12
View File
@@ -2,11 +2,8 @@ import { Router } from "express";
export const router = Router({ mergeParams: true }); ;
const db = global.db;
const requestPVE = global.pve.requestPVE;
const handleResponse = global.pve.handleResponse;
const checkAuth = global.utils.checkAuth;
const approveResources = global.utils.approveResources;
const pveAPIToken = global.db.pveAPIToken;
/**
* POST - create new virtual network interface
@@ -41,7 +38,7 @@ router.post("/:netid/create", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
// net interface must not exist
if (currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} already exists.` });
@@ -74,8 +71,8 @@ router.post("/:netid/create", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -109,7 +106,7 @@ router.post("/:netid/modify", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
// net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@@ -133,8 +130,8 @@ router.post("/:netid/modify", async (req, res) => {
action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -165,7 +162,7 @@ router.delete("/:netid/delete", async (req, res) => {
return;
}
// get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
const currentConfig = await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true });
// net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@@ -176,6 +173,6 @@ router.delete("/:netid/delete", async (req, res) => {
const action = JSON.stringify({ delete: `net${params.netid}` });
const method = params.type === "qemu" ? "POST" : "PUT";
// commit action
const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, method, { token: true }, action);
await global.pve.handleResponse(params.node, result, res);
});
+19 -25
View File
@@ -1,14 +1,8 @@
import { Router } from "express";
export const router = Router({ mergeParams: true }); ;
const db = global.db;
const requestPVE = global.pve.requestPVE;
const handleResponse = global.pve.handleResponse;
const getDeviceInfo = global.pve.getDeviceInfo;
const getNodeAvailDevices = global.pve.getNodeAvailDevices;
const checkAuth = global.utils.checkAuth;
const approveResources = global.utils.approveResources;
const pveAPIToken = global.db.pveAPIToken;
/**
* GET - get instance pcie device data
@@ -37,7 +31,7 @@ router.get("/:hostpci", async (req, res) => {
return;
}
// check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
@@ -45,7 +39,7 @@ router.get("/:hostpci", async (req, res) => {
}
const device = config[`hostpci${params.hostpci}`].split(",")[0];
// get node's pci devices
const deviceData = await getDeviceInfo(params.node, device);
const deviceData = await global.pve.getDeviceInfo(params.node, device);
if (!deviceData) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci}=${device} in ${params.node}.` });
res.end();
@@ -95,8 +89,8 @@ router.post("/:hostpci/modify", async (req, res) => {
// force all functions
params.device = params.device.split(".")[0];
// get instance config to check if device has not changed
const config = (await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken })).data.data;
const currentDeviceData = await getDeviceInfo(params.node, config[`hostpci${params.hostpci}`].split(",")[0]);
const config = (await global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: true })).data.data;
const currentDeviceData = await global.pve.getDeviceInfo(params.node, config[`hostpci${params.hostpci}`].split(",")[0]);
if (!currentDeviceData) {
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
res.end();
@@ -105,7 +99,7 @@ router.post("/:hostpci/modify", async (req, res) => {
// only check user and node availability if base id is different
if (currentDeviceData.id.split(".")[0] !== params.device) {
// setup request
const deviceData = await getDeviceInfo(params.node, params.device);
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
const request = { pci: deviceData.device_name };
// check resource approval
if (!await approveResources(req, req.cookies.username, request, params.node)) {
@@ -114,7 +108,7 @@ router.post("/:hostpci/modify", async (req, res) => {
return;
}
// check node availability
const nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies);
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) {
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
res.end();
@@ -126,7 +120,7 @@ router.post("/:hostpci/modify", async (req, res) => {
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action);
// commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobal().application.pveroot));
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, JSON.stringify(global.config.backends.pve.config.root));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -136,8 +130,8 @@ router.post("/:hostpci/modify", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -178,13 +172,13 @@ router.post("/create", async (req, res) => {
// force all functions
params.device = params.device.split(".")[0];
// get instance config to find next available hostpci slot
const config = requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies });
const config = global.pve.requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies });
let hostpci = 0;
while (config[`hostpci${hostpci}`]) {
hostpci++;
}
// setup request
const deviceData = await getDeviceInfo(params.node, params.device);
const deviceData = await global.pve.getDeviceInfo(params.node, params.device);
const request = {
pci: deviceData.device_name
};
@@ -195,7 +189,7 @@ router.post("/create", async (req, res) => {
return;
}
// check node availability
const nodeAvailPci = await getNodeAvailDevices(params.node, req.cookies);
const nodeAvailPci = await global.pve.getNodeAvailDevices(params.node, req.cookies);
if (!nodeAvailPci.some(element => element.id.split(".")[0] === params.device)) {
res.status(500).send({ error: `Device ${params.device} is already in use on ${params.node}.` });
res.end();
@@ -206,7 +200,7 @@ router.post("/create", async (req, res) => {
action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action);
// commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobal().application.pveroot));
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, JSON.stringify(global.config.backends.pve.config.root));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -216,8 +210,8 @@ router.post("/create", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await global.pve.handleResponse(params.node, result, res);
});
/**
@@ -254,7 +248,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
return;
}
// check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
const config = (await global.pve.requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end();
@@ -263,7 +257,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
// setup action
const action = JSON.stringify({ delete: `hostpci${params.hostpci}` });
// commit action, need to use root user here because proxmox api only allows root to modify hostpci for whatever reason
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobal().application.pveroot));
const rootauth = await global.pve.requestPVE("/access/ticket", "POST", null, JSON.stringify(global.config.backends.pve.config.root));
if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end();
@@ -273,6 +267,6 @@ router.delete("/:hostpci/delete", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
};
const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res);
const result = await global.pve.requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await global.pve.handleResponse(params.node, result, res);
});
+75
View File
@@ -0,0 +1,75 @@
import { Router } from "express";
export const router = Router({ mergeParams: true }); ;
const config = global.config;
const checkAuth = global.utils.checkAuth;
const getUserResources = global.utils.getUserResources;
/**
* GET - get db user resource information including allocated, free, and maximum resource values along with resource metadata
* responses:
* - 200: {avail: Object, max: Object, used: Object, resources: Object}
* - 401: {auth: false}
*/
router.get("/dynamic/resources", async (req, res) => {
// check auth
const auth = await checkAuth(req.cookies, res);
if (!auth) {
return;
}
const resources = await getUserResources(req, req.cookies.username);
res.status(200).send(resources);
});
/**
* GET - get db user configuration by key
* request:
* - key: string - user config key
* responses:
* - 200: Object
* - 401: {auth: false}
* - 401: {auth: false, error: string}
*/
router.get("/config/:key", async (req, res) => {
const params = {
key: req.params.key
};
// check auth
const auth = await checkAuth(req.cookies, res);
if (!auth) {
return;
}
const allowKeys = ["resources", "cluster", "nodes"];
if (allowKeys.includes(params.key)) {
const config = global.db.getUser(req.cookies.username);
res.status(200).send(config[params.key]);
}
else {
res.status(401).send({ auth: false, error: `User is not authorized to access /user/config/${params.key}.` });
}
});
/**
* GET - get user accessible iso files
* response:
* - 200: Array.<Object>
* - 401: {auth: false}
*/
router.get("/iso", async (req, res) => {
// check auth
const auth = await checkAuth(req.cookies, res);
if (!auth) {
return;
}
// get user iso config
const userIsoConfig = config.useriso;
// get all isos
const isos = (await global.pve.requestPVE(`/nodes/${userIsoConfig.node}/storage/${userIsoConfig.storage}/content?content=iso`, "GET", { token: true })).data.data;
const userIsos = [];
isos.forEach((iso) => {
iso.name = iso.volid.replace(`${userIsoConfig.storage}:iso/`, "");
userIsos.push(iso);
});
userIsos.sort();
res.status(200).send(userIsos);
});