feat(servers): cru added in to the server and dynamically updates vms036

This commit is contained in:
2025-10-07 06:46:02 -05:00
parent d49c8807d0
commit 90610c4ce2
11 changed files with 239 additions and 23 deletions

View File

@@ -0,0 +1,15 @@
meta {
name: Active notifications
type: http
seq: 1
}
get {
url: {{urlv2}}/api/notify/activenotifications
body: none
auth: inherit
}
settings {
encodeUrl: true
}

View File

@@ -0,0 +1,8 @@
meta {
name: Notifcations
seq: 3
}
auth {
mode: inherit
}

View File

@@ -10,12 +10,16 @@ post {
auth: inherit auth: inherit
} }
headers {
Cookie: {{session_cookie}}
}
body:json { body:json {
{ {
"name": "Test Server", "name": "Bethlehem",
"serverDNS": "USMCD1VMS036", "serverDNS": "USBET1VMS006",
"plantToken": "test3", "plantToken": "usbet1",
"ipAddress": "10.193.0.56", "ipAddress": "10.204.0.26",
"greatPlainsPlantCode": 0, "greatPlainsPlantCode": 0,
"lstServerPort": 4000, "lstServerPort": 4000,
"serverLoc": "E$\\LST" "serverLoc": "E$\\LST"

View File

@@ -5,11 +5,22 @@ meta {
} }
patch { patch {
url: {{url}}/lst/api/admin/server url: {{url}}/lst/api/admin/server/:token
body: none body: json
auth: inherit auth: inherit
} }
params:path {
token: usbet1
}
body:json {
{
"zipcode": 45247
}
}
settings { settings {
encodeUrl: true encodeUrl: true
} }

View File

@@ -1,7 +1,7 @@
vars { vars {
url: http://localhost:4200 url: http://localhost:4200
session_cookie: session_cookie:
urlv2: http://localhost:3000 urlv2: http://usksc1vms006:3000
jwtV2: jwtV2:
} }
vars:secret [ vars:secret [

View File

@@ -1,3 +1,4 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
import express from "express"; import express from "express";
import morgan from "morgan"; import morgan from "morgan";
import { createServer } from "http"; import { createServer } from "http";

View File

@@ -5,9 +5,13 @@ import { requireAuth } from "../../pkg/middleware/authMiddleware.js";
import users from "./routes/getUserRoles.js"; import users from "./routes/getUserRoles.js";
import grantRoles from "./routes/grantRole.js"; import grantRoles from "./routes/grantRole.js";
import servers from "./routes/servers/serverRoutes.js"; import servers from "./routes/servers/serverRoutes.js";
import { restrictToHosts } from "../../pkg/middleware/restrictToHosts.js";
export const setupAdminRoutes = (app: Express, basePath: string) => { export const setupAdminRoutes = (app: Express, basePath: string) => {
app.use(
basePath + "/api/admin/server", // will pass bc system admin but this is just telling us we need this
servers
);
app.use( app.use(
basePath + "/api/admin/users", basePath + "/api/admin/users",
requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this
@@ -18,11 +22,4 @@ export const setupAdminRoutes = (app: Express, basePath: string) => {
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
grantRoles grantRoles
); );
app.use(
basePath + "/api/admin/server",
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
restrictToHosts(["usmcd1vms036", "USMCD1VMS036"]), // what servers are allowed to see the server section
servers
);
}; };

View File

@@ -9,6 +9,7 @@ import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
import type { DrizzleError } from "drizzle-orm"; import type { DrizzleError } from "drizzle-orm";
import axios from "axios"; import axios from "axios";
import { createLogger } from "../../../../pkg/logger/logger.js"; import { createLogger } from "../../../../pkg/logger/logger.js";
import https from "https";
const router = Router(); const router = Router();
@@ -43,6 +44,28 @@ router.post("/", async (req: Request, res: Response) => {
if (req.hostname === "localhost" && process.env.MAIN_SERVER) { if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
log.info({}, "Running in dev server about to add in a new server"); log.info({}, "Running in dev server about to add in a new server");
const axiosInstance = axios.create({
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
baseURL: process.env.MAIN_SERVER, // e.g. "https://example.com"
withCredentials: true,
});
const loginRes = await axiosInstance.post(
`${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`,
{
username: process.env.MAIN_SERVER_USERNAME,
password: process.env.MAIN_SERVER_PASSWORD,
},
{
headers: { "Content-Type": "application/json" },
}
);
const setCookie = loginRes.headers["set-cookie"];
if (!setCookie) {
throw new Error("Did not receive a Set-Cookie header from login");
}
const { data, error } = await tryCatch( const { data, error } = await tryCatch(
axios.post( axios.post(
`${process.env.MAIN_SERVER}/lst/api/admin/server`, `${process.env.MAIN_SERVER}/lst/api/admin/server`,
@@ -50,7 +73,7 @@ router.post("/", async (req: Request, res: Response) => {
{ {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Cookie: req.headers.cookie ?? "", Cookie: setCookie.join("; "),
}, },
withCredentials: true, withCredentials: true,
} }
@@ -63,7 +86,10 @@ router.post("/", async (req: Request, res: Response) => {
"There was an error adding the server to Main Server" "There was an error adding the server to Main Server"
); );
} }
log.info({ stack: data }, "A new Server was just added to the server."); log.info(
{ stack: data?.data },
"A new Server was just added to the server."
);
} }
return res return res

View File

@@ -8,11 +8,21 @@ import { and, asc, eq } from "drizzle-orm";
const router = Router(); const router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const token = req.query.token;
const conditions = [];
if (token !== undefined) {
conditions.push(eq(serverData.plantToken, `${token}`));
}
conditions.push(eq(serverData.active, true));
const { data, error } = await tryCatch( const { data, error } = await tryCatch(
db db
.select() .select()
.from(serverData) .from(serverData)
.where(eq(serverData.active, true)) .where(and(...conditions))
.orderBy(asc(serverData.name)) .orderBy(asc(serverData.name))
); );

View File

@@ -2,11 +2,31 @@ import { Router } from "express";
import addServer from "./addServer.js"; import addServer from "./addServer.js";
import getServers from "./getServers.js"; import getServers from "./getServers.js";
import updateServer from "./updateServer.js"; import updateServer from "./updateServer.js";
import { restrictToHosts } from "../../../../pkg/middleware/restrictToHosts.js";
import { requireAuth } from "../../../../pkg/middleware/authMiddleware.js";
const router = Router(); const router = Router();
router.get("/", getServers); router.use("/", getServers);
router.post("/", addServer); router.use(
router.patch("/", updateServer); "/",
requireAuth("user", ["systemAdmin", "admin"]),
restrictToHosts([
"usmcd1vms036",
"USMCD1VMS036",
"https://usmcd1vms036.alpla.net",
]),
addServer
);
router.use(
"/",
requireAuth("user", ["systemAdmin", "admin"]),
restrictToHosts([
"usmcd1vms036",
"USMCD1VMS036",
"https://usmcd1vms036.alpla.net",
]),
updateServer
);
export default router; export default router;

View File

@@ -1,11 +1,135 @@
import { Router } from "express"; import { Router } from "express";
import type { Request, Response } from "express"; import type { Request, Response } from "express";
import { db } from "../../../../pkg/db/db.js";
import { serverData } from "../../../../pkg/db/schema/servers.js";
import { eq } from "drizzle-orm";
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
import axios from "axios";
import { createLogger } from "../../../../pkg/logger/logger.js";
import https from "https";
const router = Router(); const router = Router();
router.patch("/", async (req: Request, res: Response) => { router.patch("/:token", async (req: Request, res: Response) => {
const log = createLogger({ module: "admin", subModule: "update server" });
// when a server is updated and is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there, we want to insert with update on conflict. // when a server is updated and is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there, we want to insert with update on conflict.
res.status(200).json({ message: "Server added" }); const token = req.params.token;
const updates: Record<string, any> = {};
if (req.body?.name !== undefined) {
updates.name = req.body.name;
}
if (req.body?.serverDNS !== undefined) {
updates.serverDNS = req.body.serverDNS;
}
if (req.body?.ipAddress !== undefined) {
updates.ipAddress = req.body.ipAddress;
}
if (req.body?.greatPlainsPlantCode !== undefined) {
updates.greatPlainsPlantCode = req.body.greatPlainsPlantCode;
}
if (req.body?.lstServerPort !== undefined) {
updates.lstServerPort = req.body.lstServerPort;
}
if (req.body?.serverLoc !== undefined) {
updates.serverLoc = req.body.serverLoc;
}
if (req.body?.streetAddress !== undefined) {
updates.streetAddress = req.body.streetAddress;
}
if (req.body?.cityState !== undefined) {
updates.cityState = req.body.cityState;
}
if (req.body?.zipcode !== undefined) {
updates.zipcode = req.body.zipcode;
}
if (req.body?.contactEmail !== undefined) {
updates.contactEmail = req.body.contactEmail;
}
if (req.body?.contactPhone !== undefined) {
updates.contactPhone = req.body.contactPhone;
}
if (req.body?.customerTiAcc !== undefined) {
updates.customerTiAcc = req.body.customerTiAcc;
}
if (req.body?.active !== undefined) {
updates.active = req.body.active;
}
try {
if (Object.keys(updates).length > 0) {
await db
.update(serverData)
.set(updates)
.where(eq(serverData.plantToken, token));
}
if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
log.info({}, "Running in dev server about to add in a new server");
const axiosInstance = axios.create({
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
baseURL: process.env.MAIN_SERVER,
withCredentials: true,
});
const loginRes = await axiosInstance.post(
`${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`,
{
username: process.env.MAIN_SERVER_USERNAME,
password: process.env.MAIN_SERVER_PASSWORD,
},
{
headers: { "Content-Type": "application/json" },
}
);
const setCookie = loginRes.headers["set-cookie"];
if (!setCookie) {
throw new Error(
"Did not receive a Set-Cookie header from login"
);
}
const { data, error } = await tryCatch(
axios.post(
`${process.env.MAIN_SERVER}/lst/api/admin/server/${token}`,
updates,
{
headers: {
"Content-Type": "application/json",
Cookie: setCookie.join("; "),
},
withCredentials: true,
}
)
);
if (error) {
log.error(
{ stack: error },
"There was an error adding the server to Main Server"
);
}
log.info(
{ stack: data?.data },
"A new Server was just added to the server."
);
}
res.status(200).json({ message: `${token} Server was just updated` });
} catch (error) {
console.log(error);
res.status(400).json({ message: "Error Server updated", error });
}
}); });
export default router; export default router;