simplify requestPVE args

This commit is contained in:
Arthur Lu 2023-09-12 19:16:19 +00:00
parent 8912ae5f3c
commit 40f36270aa
10 changed files with 66 additions and 60 deletions

View File

@ -1,4 +1,11 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { exit } from "process";
export default (path) => { export default (path) => {
return JSON.parse(readFileSync(path)); try {
return JSON.parse(readFileSync(path));
}
catch (e) {
console.log(`Error: ${path} was not found.`);
exit(1);
}
}; };

View File

@ -4,12 +4,11 @@ import axios from "axios";
* Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation. * Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation.
* @param {string} path HTTP path, prepended with the proxmox API base path. * @param {string} path HTTP path, prepended with the proxmox API base path.
* @param {string} method HTTP method. * @param {string} method HTTP method.
* @param {Object} cookies user cookies for authorization if an API token is not used. Safest option for authentication. * @param {Object} auth authentication method. Set auth.cookies with user cookies or auth.token with PVE API Token. Optional.
* @param {string} body body parameters and data to be sent. Optional. * @param {string} body body parameters and data to be sent. Optional.
* @param {string} token proxmox API token to be used for controled priviledge elevation, allows user requests to perform admin actions safely. Optional * @returns {Object} HTTP response object or HTTP error object.
* @returns {Object} HTTP response object or HTTP error object
*/ */
export async function requestPVE (path, method, cookies, body = null, token = null) { export async function requestPVE (path, method, auth = null, body = null) {
const pveAPI = global.db.pveAPI; const pveAPI = global.db.pveAPI;
const url = `${pveAPI}${path}`; const url = `${pveAPI}${path}`;
const content = { const content = {
@ -21,12 +20,12 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
} }
}; };
if (token) { if (auth && auth.cookies) {
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`; content.headers.CSRFPreventionToken = auth.cookies.CSRFPreventionToken;
content.headers.Cookie = `PVEAuthCookie=${auth.cookies.PVEAuthCookie}; CSRFPreventionToken=${auth.cookies.CSRFPreventionToken}`;
} }
else if (cookies) { else if (auth && auth.token) {
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken; content.headers.Authorization = `PVEAPIToken=${auth.token.user}@${auth.token.realm}!${auth.token.id}=${auth.token.uuid}`;
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
} }
if (body) { if (body) {
@ -55,21 +54,21 @@ export async function handleResponse (node, result, res) {
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) { if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) {
const upid = result.data.data; const upid = result.data.data;
let taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", null, null, pveAPIToken); let taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", { token: pveAPIToken });
while (taskStatus.data.data.status !== "stopped") { while (taskStatus.data.data.status !== "stopped") {
await waitFor(1000); await waitFor(1000);
taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", null, null, pveAPIToken); taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", { token: pveAPIToken });
} }
if (taskStatus.data.data.exitstatus === "OK") { if (taskStatus.data.data.exitstatus === "OK") {
const result = taskStatus.data.data; const result = taskStatus.data.data;
const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken); const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", { token: pveAPIToken });
result.log = taskLog.data.data; result.log = taskLog.data.data;
res.status(200).send(result); res.status(200).send(result);
res.end(); res.end();
} }
else { else {
const result = taskStatus.data.data; const result = taskStatus.data.data;
const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", null, null, pveAPIToken); const taskLog = await requestPVE(`/nodes/${node}/tasks/${upid}/log`, "GET", { token: pveAPIToken });
result.log = taskLog.data.data; result.log = taskLog.data.data;
res.status(500).send(result); res.status(500).send(result);
res.end(); res.end();
@ -88,7 +87,7 @@ export async function handleResponse (node, result, res) {
* @returns {Object} k-v pairs of resource name and used amounts * @returns {Object} k-v pairs of resource name and used amounts
*/ */
export async function getUsedResources (req, resourceMeta) { export async function getUsedResources (req, resourceMeta) {
const response = await requestPVE("/cluster/resources", "GET", req.cookies); const response = await requestPVE("/cluster/resources", "GET", { cookies: req.cookies });
const used = {}; const used = {};
const diskprefixes = []; const diskprefixes = [];
for (const resourceName of Object.keys(resourceMeta)) { for (const resourceName of Object.keys(resourceMeta)) {
@ -107,7 +106,7 @@ export async function getUsedResources (req, resourceMeta) {
} }
for (const instance of response.data.data) { for (const instance of response.data.data) {
if (instance.type === "lxc" || instance.type === "qemu") { if (instance.type === "lxc" || instance.type === "qemu") {
let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies); let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", { cookies: req.cookies });
config = config.data.data; config = config.data.data;
for (const key of Object.keys(config)) { for (const key of Object.keys(config)) {
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") { if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
@ -145,10 +144,10 @@ export async function getUsedResources (req, resourceMeta) {
export async function getDiskInfo (node, type, vmid, disk) { export async function getDiskInfo (node, type, vmid, disk) {
const pveAPIToken = global.db.pveAPIToken; const pveAPIToken = global.db.pveAPIToken;
try { try {
const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken); const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", { token: pveAPIToken });
const storageID = config.data.data[disk].split(":")[0]; const storageID = config.data.data[disk].split(":")[0];
const volID = config.data.data[disk].split(",")[0]; const volID = config.data.data[disk].split(",")[0];
const volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken); const volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", { token: pveAPIToken });
volInfo.data.data.storage = storageID; volInfo.data.data.storage = storageID;
return volInfo.data.data; return volInfo.data.data;
} }
@ -168,7 +167,7 @@ export async function getDiskInfo (node, type, vmid, disk) {
export async function getDeviceInfo (node, type, vmid, qid) { export async function getDeviceInfo (node, type, vmid, qid) {
const pveAPIToken = global.db.pveAPIToken; const pveAPIToken = global.db.pveAPIToken;
try { try {
const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", null, null, pveAPIToken)).data.data; const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", { token: pveAPIToken })).data.data;
const deviceData = []; const deviceData = [];
result.forEach((element) => { result.forEach((element) => {
if (element.id.startsWith(qid)) { if (element.id.startsWith(qid)) {
@ -196,11 +195,11 @@ export async function getDeviceInfo (node, type, vmid, qid) {
export async function getNodeAvailDevices (node, cookies) { export async function getNodeAvailDevices (node, cookies) {
const pveAPIToken = global.db.pveAPIToken; const pveAPIToken = global.db.pveAPIToken;
// get node pci devices // get node pci devices
let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", cookies, null, pveAPIToken)).data.data; let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", { token: pveAPIToken })).data.data;
// for each node container, get its config and remove devices which are already used // for each node container, get its config and remove devices which are already used
const vms = (await requestPVE(`/nodes/${node}/qemu`, "GET", cookies, null, pveAPIToken)).data.data; const vms = (await requestPVE(`/nodes/${node}/qemu`, "GET", { token: pveAPIToken })).data.data;
for (const vm of vms) { for (const vm of vms) {
const config = (await requestPVE(`/nodes/${node}/qemu/${vm.vmid}/config`, "GET", cookies, null, pveAPIToken)).data.data; const config = (await requestPVE(`/nodes/${node}/qemu/${vm.vmid}/config`, "GET", { token: pveAPIToken })).data.data;
Object.keys(config).forEach((key) => { Object.keys(config).forEach((key) => {
if (key.startsWith("hostpci")) { if (key.startsWith("hostpci")) {
const deviceID = config[key].split(",")[0]; const deviceID = config[key].split(",")[0];

View File

@ -88,7 +88,7 @@ router.post(`${basePath}/resources`, async (req, res) => {
return; return;
} }
// get current config // get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken); const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
const request = { const request = {
cores: Number(params.cores) - Number(currentConfig.data.data.cores), cores: Number(params.cores) - Number(currentConfig.data.data.cores),
memory: Number(params.memory) - Number(currentConfig.data.data.memory) memory: Number(params.memory) - Number(currentConfig.data.data.memory)
@ -117,7 +117,7 @@ router.post(`${basePath}/resources`, async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -229,7 +229,7 @@ router.post(`${basePath}/create`, async (req, res) => {
} }
action = JSON.stringify(action); action = JSON.stringify(action);
// commit action // commit action
const result = await requestPVE(`/nodes/${params.node}/${params.type}`, "POST", req.cookies, action, pveAPIToken); const result = await requestPVE(`/nodes/${params.node}/${params.type}`, "POST", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -257,6 +257,6 @@ router.delete(`${basePath}/delete`, async (req, res) => {
return; return;
} }
// commit action // commit action
const result = await requestPVE(vmpath, "DELETE", req.cookies, null, pveAPIToken); const result = await requestPVE(vmpath, "DELETE", { token: pveAPIToken });
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });

View File

@ -37,7 +37,7 @@ router.post("/:disk/detach", async (req, res) => {
return; return;
} }
// get current config // get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist // disk must exist
if (!config[params.disk]) { if (!config[params.disk]) {
res.status(500).send({ error: `Disk ${params.disk} does not exist.` }); res.status(500).send({ error: `Disk ${params.disk} does not exist.` });
@ -52,7 +52,7 @@ router.post("/:disk/detach", async (req, res) => {
} }
const action = JSON.stringify({ delete: params.disk }); const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -86,7 +86,7 @@ router.post("/:disk/attach", async (req, res) => {
return; return;
} }
// get current config // get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist // disk must exist
if (!config[`unused${params.source}`]) { if (!config[`unused${params.source}`]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` }); res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@ -107,7 +107,7 @@ router.post("/:disk/attach", async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -160,7 +160,7 @@ router.post("/:disk/resize", async (req, res) => {
} }
// action approved, commit to action // action approved, commit to action
const action = JSON.stringify({ disk: params.disk, size: `+${params.size}G` }); const action = JSON.stringify({ disk: params.disk, size: `+${params.size}G` });
const result = await requestPVE(`${vmpath}/resize`, "PUT", req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/resize`, "PUT", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -227,7 +227,7 @@ router.post("/:disk/move", async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const route = params.type === "qemu" ? "move_disk" : "move_volume"; const route = params.type === "qemu" ? "move_disk" : "move_volume";
// commit action // commit action
const result = await requestPVE(`${vmpath}/${route}`, "POST", req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/${route}`, "POST", { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -259,7 +259,7 @@ router.delete("/:disk/delete", async (req, res) => {
return; return;
} }
// get current config // get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must exist // disk must exist
if (!config[params.disk]) { if (!config[params.disk]) {
res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` }); res.status(403).send({ error: `Requested disk unused${params.source} does not exist.` });
@ -276,7 +276,7 @@ router.delete("/:disk/delete", async (req, res) => {
const action = JSON.stringify({ delete: params.disk }); const action = JSON.stringify({ delete: params.disk });
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -314,7 +314,7 @@ router.post("/:disk/create", async (req, res) => {
return; return;
} }
// get current config // get current config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies, null, null)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
// disk must not exist // disk must not exist
if (config[params.disk]) { if (config[params.disk]) {
res.status(403).send({ error: `Requested disk ${params.disk} already exists.` }); res.status(403).send({ error: `Requested disk ${params.disk} already exists.` });
@ -354,6 +354,6 @@ router.post("/:disk/create", async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });

View File

@ -41,7 +41,7 @@ router.post("/:netid/create", async (req, res) => {
return; return;
} }
// get current config // get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken); const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must not exist // net interface must not exist
if (currentConfig.data.data[`net${params.netid}`]) { if (currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} already exists.` }); res.status(500).send({ error: `Network interface net${params.netid} already exists.` });
@ -74,7 +74,7 @@ router.post("/:netid/create", async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -109,7 +109,7 @@ router.post("/:netid/modify", async (req, res) => {
return; return;
} }
// get current config // get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken); const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must already exist // net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) { if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` }); res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@ -133,7 +133,7 @@ router.post("/:netid/modify", async (req, res) => {
action = JSON.stringify(action); action = JSON.stringify(action);
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -165,7 +165,7 @@ router.delete("/:netid/delete", async (req, res) => {
return; return;
} }
// get current config // get current config
const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", null, null, pveAPIToken); const currentConfig = await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken });
// net interface must already exist // net interface must already exist
if (!currentConfig.data.data[`net${params.netid}`]) { if (!currentConfig.data.data[`net${params.netid}`]) {
res.status(500).send({ error: `Network interface net${params.netid} does not exist.` }); res.status(500).send({ error: `Network interface net${params.netid} does not exist.` });
@ -176,6 +176,6 @@ router.delete("/:netid/delete", async (req, res) => {
const action = JSON.stringify({ delete: `net${params.netid}` }); const action = JSON.stringify({ delete: `net${params.netid}` });
const method = params.type === "qemu" ? "POST" : "PUT"; const method = params.type === "qemu" ? "POST" : "PUT";
// commit action // commit action
const result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken); const result = await requestPVE(`${vmpath}/config`, method, { token: pveAPIToken }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });

View File

@ -37,7 +37,7 @@ router.get("/:hostpci", async (req, res) => {
return; return;
} }
// check device is in instance config // check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) { if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` }); res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end(); res.end();
@ -95,7 +95,7 @@ router.post("/:hostpci/modify", async (req, res) => {
// force all functions // force all functions
params.device = params.device.split(".")[0]; params.device = params.device.split(".")[0];
// get instance config to check if device has not changed // get instance config to check if device has not changed
const config = (await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", params.cookies, null, pveAPIToken)).data.data; const config = (await requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { token: pveAPIToken })).data.data;
const currentDeviceData = await getDeviceInfo(params.node, params.type, params.vmid, config[`hostpci${params.hostpci}`].split(",")[0]); const currentDeviceData = await getDeviceInfo(params.node, params.type, params.vmid, config[`hostpci${params.hostpci}`].split(",")[0]);
if (!currentDeviceData) { if (!currentDeviceData) {
res.status(500).send({ error: `No device in hostpci${params.hostpci}.` }); res.status(500).send({ error: `No device in hostpci${params.hostpci}.` });
@ -126,7 +126,7 @@ router.post("/:hostpci/modify", async (req, res) => {
action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`; action[`hostpci${params.hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action); action = JSON.stringify(action);
// commit action // commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot), null); const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) { if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end(); res.end();
@ -136,7 +136,7 @@ router.post("/:hostpci/modify", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket, PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
}; };
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null); const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -178,7 +178,7 @@ router.post("/create", async (req, res) => {
// force all functions // force all functions
params.device = params.device.split(".")[0]; params.device = params.device.split(".")[0];
// get instance config to find next available hostpci slot // get instance config to find next available hostpci slot
const config = requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", params.cookies, null, null); const config = requestPVE(`/nodes/${params.node}/${params.type}/${params.vmid}/config`, "GET", { cookies: params.cookies });
let hostpci = 0; let hostpci = 0;
while (config[`hostpci${hostpci}`]) { while (config[`hostpci${hostpci}`]) {
hostpci++; hostpci++;
@ -206,7 +206,7 @@ router.post("/create", async (req, res) => {
action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`; action[`hostpci${hostpci}`] = `${params.device},pcie=${params.pcie}`;
action = JSON.stringify(action); action = JSON.stringify(action);
// commit action // commit action
const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot), null); const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) { if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end(); res.end();
@ -216,7 +216,7 @@ router.post("/create", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket, PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
}; };
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null); const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });
@ -254,7 +254,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
return; return;
} }
// check device is in instance config // check device is in instance config
const config = (await requestPVE(`${vmpath}/config`, "GET", req.cookies)).data.data; const config = (await requestPVE(`${vmpath}/config`, "GET", { cookies: req.cookies })).data.data;
if (!config[`hostpci${params.hostpci}`]) { if (!config[`hostpci${params.hostpci}`]) {
res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` }); res.status(500).send({ error: `Could not find hostpci${params.hostpci} in ${params.vmid}.` });
res.end(); res.end();
@ -263,7 +263,7 @@ router.delete("/:hostpci/delete", async (req, res) => {
// setup action // setup action
const action = JSON.stringify({ delete: `hostpci${params.hostpci}` }); 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 // 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.getGlobalConfig().application.pveroot), null); const rootauth = await requestPVE("/access/ticket", "POST", null, JSON.stringify(db.getGlobalConfig().application.pveroot));
if (!(rootauth.status === 200)) { if (!(rootauth.status === 200)) {
res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." }); res.status(rootauth.status).send({ auth: false, error: "API could not authenticate as root user." });
res.end(); res.end();
@ -273,6 +273,6 @@ router.delete("/:hostpci/delete", async (req, res) => {
PVEAuthCookie: rootauth.data.data.ticket, PVEAuthCookie: rootauth.data.data.ticket,
CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken CSRFPreventionToken: rootauth.data.data.CSRFPreventionToken
}; };
const result = await requestPVE(`${vmpath}/config`, "POST", rootcookies, action, null); const result = await requestPVE(`${vmpath}/config`, "POST", { cookies: rootcookies }, action);
await handleResponse(params.node, result, res); await handleResponse(params.node, result, res);
}); });

View File

@ -10,7 +10,7 @@ const requestPVE = global.pve.requestPVE;
router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token
console.log(req.url); console.log(req.url);
const path = req.url.replace("/api/proxmox", ""); const path = req.url.replace("/api/proxmox", "");
const result = await requestPVE(path, "GET", req.cookies); const result = await requestPVE(path, "GET", { cookies: req.cookies });
res.status(result.status).send(result.data); res.status(result.status).send(result.data);
}); });
@ -20,6 +20,6 @@ router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api wit
*/ */
router.post("/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token router.post("/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token
const path = req.url.replace("/api/proxmox", ""); const path = req.url.replace("/api/proxmox", "");
const result = await requestPVE(path, "POST", req.cookies, JSON.stringify(req.body)); // need to stringify body because of other issues const result = await requestPVE(path, "POST", { cookies: req.cookies }, JSON.stringify(req.body)); // need to stringify body because of other issues
res.status(result.status).send(result.data); res.status(result.status).send(result.data);
}); });

View File

@ -55,7 +55,7 @@ if (schemes.hash.enabled) {
return; return;
} }
// get current cluster resources // get current cluster resources
const status = (await requestPVE("/cluster/resources", "GET", req.cookies)).data.data; const status = (await requestPVE("/cluster/resources", "GET", { cookies: req.cookies })).data.data;
// filter out just state information of resources that are needed // filter out just state information of resources that are needed
const state = extractClusterState(status, resourceTypes); const state = extractClusterState(status, resourceTypes);
res.status(200).send(getObjectHash(state)); res.status(200).send(getObjectHash(state));
@ -158,7 +158,7 @@ if (schemes.interrupt.enabled) {
// handle the wss upgrade request // handle the wss upgrade request
global.server.on("upgrade", async (req, socket, head) => { global.server.on("upgrade", async (req, socket, head) => {
const cookies = cookie.parse(req.headers.cookie || ""); const cookies = cookie.parse(req.headers.cookie || "");
const auth = (await requestPVE("/version", "GET", cookies)).status === 200; const auth = (await requestPVE("/version", "GET", { cookies })).status === 200;
if (!auth) { if (!auth) {
socket.destroy(); socket.destroy();
} }
@ -185,7 +185,7 @@ if (schemes.interrupt.enabled) {
return; return;
} }
// get current cluster resources // get current cluster resources
const status = (await requestPVE("/cluster/resources", "GET", null, null, pveAPIToken)).data.data; const status = (await requestPVE("/cluster/resources", "GET", { token: pveAPIToken })).data.data;
// filter out just state information of resources that are needed, and hash each one // filter out just state information of resources that are needed, and hash each one
const currState = extractClusterState(status, resourceTypes, true); const currState = extractClusterState(status, resourceTypes, true);
// get a map of users to send sync notifications // get a map of users to send sync notifications

View File

@ -66,7 +66,7 @@ router.get("/iso", async (req, res) => {
// get user iso config // get user iso config
const userIsoConfig = db.getGlobalConfig().useriso; const userIsoConfig = db.getGlobalConfig().useriso;
// get all isos // get all isos
const isos = (await requestPVE(`/nodes/${userIsoConfig.node}/storage/${userIsoConfig.storage}/content?content=iso`, "GET", null, null, pveAPIToken)).data.data; const isos = (await requestPVE(`/nodes/${userIsoConfig.node}/storage/${userIsoConfig.storage}/content?content=iso`, "GET", { token: pveAPIToken })).data.data;
const userIsos = []; const userIsos = [];
isos.forEach((iso) => { isos.forEach((iso) => {
iso.name = iso.volid.replace(`${userIsoConfig.storage}:iso/`, ""); iso.name = iso.volid.replace(`${userIsoConfig.storage}:iso/`, "");

View File

@ -24,11 +24,11 @@ export async function checkAuth (cookies, res, vmpath = null) {
} }
if (vmpath) { if (vmpath) {
const result = await requestPVE(`/${vmpath}/config`, "GET", cookies); const result = await requestPVE(`/${vmpath}/config`, "GET", { cookies });
auth = result.status === 200; auth = result.status === 200;
} }
else { // if no path is specified, then do a simple authentication else { // if no path is specified, then do a simple authentication
const result = await requestPVE("/version", "GET", cookies); const result = await requestPVE("/version", "GET", { cookies });
auth = result.status === 200; auth = result.status === 200;
} }