From 9d3b5f82a0e3e28cb8a594b511dbaa5f8d919a22 Mon Sep 17 00:00:00 2001 From: greysoh Date: Mon, 22 Apr 2024 11:51:37 -0400 Subject: [PATCH] feature: Adds lookup API endpoint. --- .../Backend API/Lookup Backend.bru | 18 ++++ .../Forward API/Lookup Forward.bru | 18 ++++ src/index.ts | 4 + src/routes/backends/lookup.ts | 72 ++++++++++++++ src/routes/forward/create.ts | 6 +- src/routes/forward/lookup.ts | 96 +++++++++++++++++++ src/routes/user/lookup.ts | 3 + 7 files changed, 214 insertions(+), 3 deletions(-) create mode 100644 routes/NextNet API/Backend API/Lookup Backend.bru create mode 100644 routes/NextNet API/Forward API/Lookup Forward.bru create mode 100644 src/routes/backends/lookup.ts create mode 100644 src/routes/forward/lookup.ts diff --git a/routes/NextNet API/Backend API/Lookup Backend.bru b/routes/NextNet API/Backend API/Lookup Backend.bru new file mode 100644 index 0000000..48b8eee --- /dev/null +++ b/routes/NextNet API/Backend API/Lookup Backend.bru @@ -0,0 +1,18 @@ +meta { + name: Lookup Backend + type: http + seq: 3 +} + +post { + url: http://127.0.0.1:3000/api/v1/backends/remove + body: json + auth: none +} + +body:json { + { + "token": "f1b89cc337073476289ade17ffbe7a6419b4bd52aa7ede26114bffd76fa263b5cb1bcaf389462e1d9e7acb7f4b6a7c28152a9cc9af83e3ec862f1892b1", + "id": "2" + } +} diff --git a/routes/NextNet API/Forward API/Lookup Forward.bru b/routes/NextNet API/Forward API/Lookup Forward.bru new file mode 100644 index 0000000..93db53c --- /dev/null +++ b/routes/NextNet API/Forward API/Lookup Forward.bru @@ -0,0 +1,18 @@ +meta { + name: Lookup Forward + type: http + seq: 3 +} + +post { + url: http://127.0.0.1:3000/api/v1/forward/remove + body: json + auth: none +} + +body:json { + { + "token": "f1b89cc337073476289ade17ffbe7a6419b4bd52aa7ede26114bffd76fa263b5cb1bcaf389462e1d9e7acb7f4b6a7c28152a9cc9af83e3ec862f1892b1", + "id": "2" + } +} diff --git a/src/index.ts b/src/index.ts index fddae23..67d1d98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,9 +9,11 @@ import { route as getPermissions } from "./routes/getPermissions.js"; import { route as backendCreate } from "./routes/backends/create.js"; import { route as backendRemove } from "./routes/backends/remove.js"; +import { route as backendLookup } from "./routes/backends/lookup.js"; import { route as forwardCreate } from "./routes/forward/create.js"; import { route as forwardRemove } from "./routes/forward/remove.js"; +import { route as forwardLookup } from "./routes/forward/lookup.js"; import { route as userCreate } from "./routes/user/create.js"; import { route as userRemove } from "./routes/user/remove.js"; @@ -46,9 +48,11 @@ getPermissions(fastify, prisma, sessionTokens, serverOptions); backendCreate(fastify, prisma, sessionTokens, serverOptions); backendRemove(fastify, prisma, sessionTokens, serverOptions); +backendLookup(fastify, prisma, sessionTokens, serverOptions); forwardCreate(fastify, prisma, sessionTokens, serverOptions); forwardRemove(fastify, prisma, sessionTokens, serverOptions); +forwardLookup(fastify, prisma, sessionTokens, serverOptions); userCreate(fastify, prisma, sessionTokens, serverOptions); userRemove(fastify, prisma, sessionTokens, serverOptions); diff --git a/src/routes/backends/lookup.ts b/src/routes/backends/lookup.ts new file mode 100644 index 0000000..721791a --- /dev/null +++ b/src/routes/backends/lookup.ts @@ -0,0 +1,72 @@ +import type { PrismaClient } from "@prisma/client"; +import type { FastifyInstance } from "fastify"; + +import { ServerOptions, SessionToken } from "../../libs/types.js"; +import { hasPermissionByToken } from "../../libs/permissions.js"; + +export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record, options: ServerOptions) { + function hasPermission(token: string, permissionList: string[]): Promise { + return hasPermissionByToken(permissionList, token, tokens, prisma); + }; + + /** + * Creates a new route to use + */ + fastify.post("/api/v1/backends/lookup", { + schema: { + body: { + type: "object", + required: ["token"], + + properties: { + token: { type: "string" }, + id: { type: "number" }, + name: { type: "string" }, + description: { type: "string" }, + backend: { type: "string" } + } + } + } + }, async(req, res) => { + // @ts-ignore + const body: { + token: string, + id?: number, + name?: string, + description?: string, + backend?: string + } = req.body; + + if (!await hasPermission(body.token, [ + "backends.visible" // wtf? + ])) { + return res.status(403).send({ + error: "Unauthorized" + }); + }; + + const canSeeSecrets = await hasPermission(body.token, [ + "backends.secretVis" + ]); + + const backends = await prisma.desinationProvider.findMany({ + where: { + id: body.id, + name: body.name, + description: body.description, + backend: body.backend + } + }); + + return { + success: true, + data: backends.map((i) => ({ + name: i.name, + description: i.description, + + backend: i.backend, + connectionDetails: canSeeSecrets ? i.connectionDetails : "" + })) + } + }); +} \ No newline at end of file diff --git a/src/routes/forward/create.ts b/src/routes/forward/create.ts index 3301251..edd036f 100644 --- a/src/routes/forward/create.ts +++ b/src/routes/forward/create.ts @@ -30,7 +30,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re destinationPort: { type: "number" }, providerID: { type: "number" }, - enabled: { type: "boolean"} + autoStart: { type: "boolean" } } } } @@ -49,7 +49,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re providerID: number, - enabled?: boolean + autoStart?: boolean } = req.body; if (!await hasPermission(body.token, [ @@ -82,7 +82,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re destProviderID: body.providerID, - enabled: Boolean(body.enabled) + enabled: Boolean(body.autoStart) } }); diff --git a/src/routes/forward/lookup.ts b/src/routes/forward/lookup.ts new file mode 100644 index 0000000..daddf12 --- /dev/null +++ b/src/routes/forward/lookup.ts @@ -0,0 +1,96 @@ +import type { PrismaClient } from "@prisma/client"; +import type { FastifyInstance } from "fastify"; + +import { ServerOptions, SessionToken } from "../../libs/types.js"; +import { hasPermissionByToken } from "../../libs/permissions.js"; + +export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record, options: ServerOptions) { + function hasPermission(token: string, permissionList: string[]): Promise { + return hasPermissionByToken(permissionList, token, tokens, prisma); + }; + + /** + * Creates a new route to use + */ + fastify.post("/api/v1/forward/lookup", { + schema: { + body: { + type: "object", + required: ["token"], + + properties: { + token: { type: "string" }, + id: { type: "number" }, + + name: { type: "string" }, + description: { type: "string" }, + + sourceIP: { type: "string" }, + sourcePort: { type: "number" }, + destPort: { type: "number" }, + + providerID: { type: "number" }, + autoStart: { type: "boolean" } + } + } + } + }, async(req, res) => { + // @ts-ignore + const body: { + token: string, + + id?: number, + name?: string, + description?: string, + + sourceIP?: string, + sourcePort?: number, + + destinationPort?: number, + + providerID?: number, + autoStart?: boolean + } = req.body; + + if (!await hasPermission(body.token, [ + "routes.visible" // wtf? + ])) { + return res.status(403).send({ + error: "Unauthorized" + }); + }; + + const forwardRules = await prisma.forwardRule.findMany({ + where: { + id: body.id, + name: body.name, + description: body.description, + + sourceIP: body.sourceIP, + sourcePort: body.sourcePort, + + destPort: body.destinationPort, + + destProviderID: body.providerID, + enabled: body.autoStart + } + }); + + return { + success: true, + data: forwardRules.map((i) => ({ + id: i.id, + name: i.name, + description: i.description, + + sourceIP: i.sourceIP, + sourcePort: i.sourcePort, + + destPort: i.destPort, + + providerID: i.destProviderID, + autoStart: i.enabled // TODO: Add enabled flag in here to see if we're running or not + })) + }; + }); +} \ No newline at end of file diff --git a/src/routes/user/lookup.ts b/src/routes/user/lookup.ts index e1909d1..cd97a89 100644 --- a/src/routes/user/lookup.ts +++ b/src/routes/user/lookup.ts @@ -17,6 +17,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re properties: { token: { type: "string" }, + id: { type: "number" }, name: { type: "string" }, email: { type: "string" }, isServiceAccount: { type: "boolean" } @@ -27,6 +28,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re // @ts-ignore const body: { token: string, + id?: number, name?: string, email?: string, isServiceAccount?: boolean @@ -42,6 +44,7 @@ export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Re const users = await prisma.user.findMany({ where: { + id: body.id, name: body.name, email: body.email, isRootServiceAccount: body.isServiceAccount