2024-12-21 21:01:12 -05:00
|
|
|
import { SqliteError } from "better-sqlite3";
|
2024-11-05 22:38:57 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import {
|
|
|
|
orgs,
|
2024-11-09 00:08:17 -05:00
|
|
|
Resource,
|
2024-11-05 22:38:57 -05:00
|
|
|
resources,
|
|
|
|
roleResources,
|
|
|
|
roles,
|
2024-12-21 21:01:12 -05:00
|
|
|
userResources
|
2024-11-05 22:38:57 -05:00
|
|
|
} from "@server/db/schema";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-11-05 22:38:57 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { eq, and } from "drizzle-orm";
|
2025-01-01 21:41:31 -05:00
|
|
|
import stoi from "@server/lib/stoi";
|
2024-11-05 22:38:57 -05:00
|
|
|
import { fromError } from "zod-validation-error";
|
2024-12-21 21:01:12 -05:00
|
|
|
import logger from "@server/logger";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const createResourceParamsSchema = z
|
|
|
|
.object({
|
|
|
|
siteId: z.string().transform(stoi).pipe(z.number().int().positive()),
|
|
|
|
orgId: z.string()
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-11-14 00:00:17 -05:00
|
|
|
const createResourceSchema = z
|
|
|
|
.object({
|
2025-01-28 22:26:45 -05:00
|
|
|
subdomain: z
|
|
|
|
.union([
|
|
|
|
z
|
|
|
|
.string()
|
|
|
|
.regex(
|
|
|
|
/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9-_]+$/,
|
|
|
|
"Invalid subdomain format"
|
|
|
|
)
|
|
|
|
.min(1, "Subdomain must be at least 1 character long")
|
|
|
|
.transform((val) => val.toLowerCase()),
|
|
|
|
z.string().optional()
|
|
|
|
])
|
|
|
|
.optional(),
|
2024-11-14 00:00:17 -05:00
|
|
|
name: z.string().min(1).max(255),
|
2025-01-28 22:26:45 -05:00
|
|
|
http: z.boolean(),
|
|
|
|
protocol: z.string(),
|
|
|
|
proxyPort: z.number().int().min(1).max(65535).optional(),
|
|
|
|
}).refine(
|
|
|
|
(data) => {
|
|
|
|
if (data.http === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!data.proxyPort;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
message: "Port number is required for non-HTTP resources",
|
|
|
|
path: ["proxyPort"]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
export type CreateResourceResponse = Resource;
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
export async function createResource(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedBody = createResourceSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedBody.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2025-01-28 22:26:45 -05:00
|
|
|
let { name, subdomain, protocol, proxyPort, http } = parsedBody.data;
|
2024-12-26 15:13:49 -05:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Validate request params
|
|
|
|
const parsedParams = createResourceParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { siteId, orgId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (!req.userOrgRoleId) {
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-19 16:19:47 -04:00
|
|
|
// get the org
|
2024-11-05 22:38:57 -05:00
|
|
|
const org = await db
|
|
|
|
.select()
|
2024-10-19 16:19:47 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId))
|
|
|
|
.limit(1);
|
2024-10-26 12:15:03 -04:00
|
|
|
|
2024-10-19 16:19:47 -04:00
|
|
|
if (org.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Organization with ID ${orgId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-16 22:41:43 -05:00
|
|
|
const fullDomain = `${subdomain}.${org[0].domain}`;
|
2025-01-28 22:26:45 -05:00
|
|
|
// if http is false check to see if there is already a resource with the same port and protocol
|
|
|
|
if (!http) {
|
|
|
|
const existingResource = await db
|
|
|
|
.select()
|
|
|
|
.from(resources)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(resources.protocol, protocol),
|
|
|
|
eq(resources.proxyPort, proxyPort!)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (existingResource.length > 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.CONFLICT,
|
|
|
|
"Resource with that protocol and port already exists"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (proxyPort === 443 || proxyPort === 80) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Port 80 and 443 are reserved for https resources"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the full domain is unique
|
|
|
|
const existingResource = await db
|
|
|
|
.select()
|
|
|
|
.from(resources)
|
|
|
|
.where(eq(resources.fullDomain, fullDomain));
|
|
|
|
|
|
|
|
if (existingResource.length > 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.CONFLICT,
|
|
|
|
"Resource with that domain already exists"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
await db.transaction(async (trx) => {
|
|
|
|
const newResource = await trx
|
|
|
|
.insert(resources)
|
|
|
|
.values({
|
|
|
|
siteId,
|
2025-01-28 22:26:45 -05:00
|
|
|
fullDomain: http? fullDomain : null,
|
2024-12-24 16:00:02 -05:00
|
|
|
orgId,
|
|
|
|
name,
|
|
|
|
subdomain,
|
2025-01-28 22:26:45 -05:00
|
|
|
http,
|
|
|
|
protocol,
|
|
|
|
proxyPort,
|
2024-12-24 16:00:02 -05:00
|
|
|
ssl: true
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
const adminRole = await db
|
|
|
|
.select()
|
|
|
|
.from(roles)
|
|
|
|
.where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (adminRole.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.NOT_FOUND, `Admin role not found`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await trx.insert(roleResources).values({
|
|
|
|
roleId: adminRole[0].roleId,
|
2024-12-21 21:01:12 -05:00
|
|
|
resourceId: newResource[0].resourceId
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
if (req.userOrgRoleId != adminRole[0].roleId) {
|
|
|
|
// make sure the user can access the resource
|
|
|
|
await trx.insert(userResources).values({
|
|
|
|
userId: req.user?.userId!,
|
|
|
|
resourceId: newResource[0].resourceId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
response<CreateResourceResponse>(res, {
|
|
|
|
data: newResource[0],
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Resource created successfully",
|
|
|
|
status: HttpCode.CREATED
|
|
|
|
});
|
2024-10-06 18:05:20 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-12-21 21:01:12 -05:00
|
|
|
logger.error(error);
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
2024-11-09 00:08:17 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-05 22:38:57 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|