2025-02-21 14:39:10 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import {
|
|
|
|
roles,
|
|
|
|
Client,
|
|
|
|
clients,
|
|
|
|
roleClients,
|
|
|
|
userClients,
|
2025-03-31 16:21:01 -04:00
|
|
|
olms,
|
2025-04-01 21:52:03 -04:00
|
|
|
clientSites,
|
2025-04-16 22:07:07 -04:00
|
|
|
exitNodes,
|
|
|
|
orgs
|
2025-02-21 14:39:10 -05:00
|
|
|
} from "@server/db/schema";
|
|
|
|
import response from "@server/lib/response";
|
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { eq, and } from "drizzle-orm";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
import moment from "moment";
|
|
|
|
import { hashPassword } from "@server/auth/password";
|
2025-04-16 22:07:07 -04:00
|
|
|
import { isValidCIDR, isValidIP } from "@server/lib/validators";
|
|
|
|
import { isIpInCidr } from "@server/lib/ip";
|
2025-02-21 14:39:10 -05:00
|
|
|
|
|
|
|
const createClientParamsSchema = z
|
|
|
|
.object({
|
2025-03-31 16:21:01 -04:00
|
|
|
orgId: z.string()
|
2025-02-21 14:39:10 -05:00
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
const createClientSchema = z
|
|
|
|
.object({
|
|
|
|
name: z.string().min(1).max(255),
|
2025-04-01 10:13:20 -04:00
|
|
|
siteIds: z.array(z.number().int().positive()),
|
2025-02-21 17:22:05 -05:00
|
|
|
olmId: z.string(),
|
|
|
|
secret: z.string(),
|
2025-04-16 22:00:24 -04:00
|
|
|
subnet: z.string(),
|
2025-02-21 14:39:10 -05:00
|
|
|
type: z.enum(["olm"])
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
export type CreateClientBody = z.infer<typeof createClientSchema>;
|
|
|
|
|
|
|
|
export type CreateClientResponse = Client;
|
|
|
|
|
|
|
|
export async function createClient(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
|
|
|
try {
|
|
|
|
const parsedBody = createClientSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedBody.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-04-16 22:00:24 -04:00
|
|
|
const { name, type, siteIds, olmId, secret, subnet } = parsedBody.data;
|
2025-02-21 14:39:10 -05:00
|
|
|
|
|
|
|
const parsedParams = createClientParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedParams.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-31 16:21:01 -04:00
|
|
|
const { orgId } = parsedParams.data;
|
2025-02-21 14:39:10 -05:00
|
|
|
|
|
|
|
if (!req.userOrgRoleId) {
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-04-16 22:07:07 -04:00
|
|
|
if (subnet && !isValidIP(subnet)) {
|
2025-04-16 22:00:24 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Invalid subnet format. Please provide a valid CIDR notation."
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2025-02-21 14:39:10 -05:00
|
|
|
|
2025-04-16 22:07:07 -04:00
|
|
|
const [org] = await db
|
|
|
|
.select()
|
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId));
|
|
|
|
|
|
|
|
if (!org) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Organization with ID ${orgId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subnet && !isIpInCidr(subnet, org.subnet)) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"IP is not in the CIDR range of the subnet."
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedSubnet = `${subnet}/${org.subnet.split("/")[1]}`; // we want the block size of the whole org
|
|
|
|
|
2025-02-21 14:39:10 -05:00
|
|
|
await db.transaction(async (trx) => {
|
2025-04-01 21:52:03 -04:00
|
|
|
// TODO: more intelligent way to pick the exit node
|
|
|
|
|
|
|
|
// make sure there is an exit node by counting the exit nodes table
|
|
|
|
const nodes = await db.select().from(exitNodes);
|
|
|
|
if (nodes.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
"No exit nodes available"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the first exit node
|
|
|
|
const exitNode = nodes[0];
|
|
|
|
|
2025-02-21 14:39:10 -05:00
|
|
|
const adminRole = await trx
|
|
|
|
.select()
|
|
|
|
.from(roles)
|
2025-04-01 21:52:03 -04:00
|
|
|
.where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
|
2025-02-21 14:39:10 -05:00
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (adminRole.length === 0) {
|
|
|
|
trx.rollback();
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.NOT_FOUND, `Admin role not found`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [newClient] = await trx
|
|
|
|
.insert(clients)
|
|
|
|
.values({
|
2025-04-01 21:52:03 -04:00
|
|
|
exitNodeId: exitNode.exitNodeId,
|
2025-03-31 16:21:01 -04:00
|
|
|
orgId,
|
2025-02-21 14:39:10 -05:00
|
|
|
name,
|
2025-04-16 22:07:07 -04:00
|
|
|
subnet: updatedSubnet,
|
2025-02-21 14:39:10 -05:00
|
|
|
type
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
await trx.insert(roleClients).values({
|
|
|
|
roleId: adminRole[0].roleId,
|
|
|
|
clientId: newClient.clientId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (req.userOrgRoleId != adminRole[0].roleId) {
|
|
|
|
// make sure the user can access the site
|
|
|
|
trx.insert(userClients).values({
|
|
|
|
userId: req.user?.userId!,
|
|
|
|
clientId: newClient.clientId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-31 16:21:01 -04:00
|
|
|
// Create site to client associations
|
|
|
|
if (siteIds && siteIds.length > 0) {
|
|
|
|
await trx.insert(clientSites).values(
|
2025-04-01 21:52:03 -04:00
|
|
|
siteIds.map((siteId) => ({
|
2025-03-31 16:21:01 -04:00
|
|
|
clientId: newClient.clientId,
|
|
|
|
siteId
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-21 14:39:10 -05:00
|
|
|
const secretHash = await hashPassword(secret);
|
|
|
|
|
|
|
|
await trx.insert(olms).values({
|
|
|
|
olmId,
|
|
|
|
secretHash,
|
|
|
|
clientId: newClient.clientId,
|
|
|
|
dateCreated: moment().toISOString()
|
|
|
|
});
|
|
|
|
|
|
|
|
return response<CreateClientResponse>(res, {
|
|
|
|
data: newClient,
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Site created successfully",
|
|
|
|
status: HttpCode.CREATED
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
|
|
);
|
|
|
|
}
|
2025-04-01 21:52:03 -04:00
|
|
|
}
|