[FEATURE] Cairo Auth Integration

This commit is contained in:
Dunemask 2024-02-04 17:02:15 -07:00
parent 184f1fa631
commit cdea22c08a
16 changed files with 89 additions and 45 deletions

View file

@ -6,11 +6,14 @@ import {
uploadServerItem,
} from "../k8s/server-files.js";
import { sendError } from "../util/ExpressClientError.js";
import { checkAuthorization } from "../database/queries/server-queries.js";
export async function listFiles(req, res) {
const serverSpec = req.body;
if (!serverSpec) return res.sendStatus(400);
if (!serverSpec.id) return res.status(400).send("Server id missing!");
const authorized = await checkAuthorization(serverSpec.id, req.cairoId);
if (!authorized) return res.sendStatus(403);
listServerFiles(serverSpec)
.then((f) => {
const fileData = f.map((fi, i) => ({
@ -31,6 +34,8 @@ export async function createFolder(req, res) {
if (!serverSpec) return res.sendStatus(400);
if (!serverSpec.id) return res.status(400).send("Server id missing!");
if (!serverSpec.path) return res.status(400).send("Path required!");
const authorized = await checkAuthorization(serverSpec.id, req.cairoId);
if (!authorized) return res.sendStatus(403);
createServerFolder(serverSpec)
.then(() => res.sendStatus(200))
.catch(sendError(res));
@ -43,6 +48,8 @@ export async function deleteItem(req, res) {
if (!serverSpec.path) return res.status(400).send("Path required!");
if (serverSpec.isDir === undefined || serverSpec.isDir === null)
return res.status(400).send("IsDIr required!");
const authorized = await checkAuthorization(serverSpec.id, req.cairoId);
if (!authorized) return res.sendStatus(403);
removeServerItem(serverSpec)
.then(() => res.sendStatus(200))
.catch(sendError(res));
@ -52,6 +59,8 @@ export async function uploadItem(req, res) {
const serverSpec = req.body;
if (!serverSpec.id) return res.status(400).send("Server id missing!");
if (!serverSpec.path) return res.status(400).send("Path required!");
const authorized = await checkAuthorization(serverSpec.id, req.cairoId);
if (!authorized) return res.sendStatus(403);
uploadServerItem(serverSpec, req.file)
.then(() => res.sendStatus(200))
.catch(sendError(res));
@ -61,6 +70,8 @@ export async function getItem(req, res) {
const serverSpec = req.body;
if (!serverSpec.id) return res.status(400).send("Server id missing!");
if (!serverSpec.path) return res.status(400).send("Path required!");
const authorized = await checkAuthorization(serverSpec.id, req.cairoId);
if (!authorized) return res.sendStatus(403);
getServerItem(serverSpec, res)
.then(({ ds, ftpTransfer }) => {
ds.pipe(res).on("error", sendError(res));

View file

@ -8,6 +8,7 @@ import {
} from "../database/queries/server-queries.js";
import ExpressClientError, { sendError } from "../util/ExpressClientError.js";
import { toggleServer } from "../k8s/k8s-server-control.js";
import { checkAuthorization } from "../database/queries/server-queries.js";
const dnsRegex = new RegExp(
`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`,
@ -71,10 +72,13 @@ function payloadFilter(req, res) {
return "filtered";
}
function checkServerId(serverSpec) {
async function checkServerId(cairoId, serverSpec) {
if (!serverSpec) throw new ExpressClientError({ c: 400 });
if (!serverSpec.id)
throw new ExpressClientError({ c: 400, m: "Server id missing!" });
const authorized = await checkAuthorization(serverSpec.id, cairoId);
if (!authorized)
throw new ExpressClientError({ c: 403, m: "Access forbidden!" });
}
export async function createServer(req, res) {
@ -82,7 +86,7 @@ export async function createServer(req, res) {
if (backupPayloadFilter(req, res) !== "filtered") return;
const serverSpec = req.body;
try {
const serverEntry = await createServerEntry(serverSpec);
const serverEntry = await createServerEntry(req.cairoId, serverSpec);
await createServerResources(serverEntry);
res.json(serverEntry);
} catch (e) {
@ -94,7 +98,7 @@ export async function deleteServer(req, res) {
// Ensure spec is safe
const serverSpec = req.body;
try {
checkServerId(serverSpec);
await checkServerId(req.cairoId, serverSpec);
} catch (e) {
return sendError(res)(e);
}
@ -109,7 +113,7 @@ export async function startServer(req, res) {
// Ensure spec is safe
const serverSpec = req.body;
try {
checkServerId(serverSpec);
await checkServerId(req.cairoId, serverSpec);
} catch (e) {
return sendError(res)(e);
}
@ -123,7 +127,7 @@ export async function stopServer(req, res) {
// Ensure spec is safe
const serverSpec = req.body;
try {
checkServerId(serverSpec);
await checkServerId(req.cairoId, serverSpec);
} catch (e) {
return sendError(res)(e);
}
@ -137,7 +141,7 @@ export async function getServer(req, res) {
// Ensure spec is safe
const serverSpec = req.body;
try {
checkServerId(serverSpec);
await checkServerId(req.cairoId, serverSpec);
} catch (e) {
return sendError(res)(e);
}
@ -155,7 +159,7 @@ export async function modifyServer(req, res) {
if (payloadFilter(req, res) !== "filtered") return;
const serverSpec = req.body;
try {
checkServerId(serverSpec);
await checkServerId(req.cairoId, serverSpec);
const serverEntry = await modifyServerEntry(serverSpec);
// await createServerResources(serverEntry);
res.sendStatus(200);

View file

@ -1,9 +1,9 @@
import { getDeployments } from "../k8s/k8s-server-control.js";
import { getUserDeployments } from "../k8s/k8s-server-control.js";
import { getInstances } from "../k8s/server-status.js";
import { sendError } from "../util/ExpressClientError.js";
export function serverList(req, res) {
getDeployments()
getUserDeployments(req.cairoId)
.then((sd) => res.json(sd.map((s) => s.metadata.name.substring(4))))
.catch((e) => {
ERR("STATUS CONTROLLER", e);
@ -12,7 +12,7 @@ export function serverList(req, res) {
}
export function serverInstances(req, res) {
getInstances()
getInstances(req.cairoId)
.then((i) => res.json(i))
.catch(sendError(res));
}