From 69706871cc2640687d6ec2bc516d863d6407ad76 Mon Sep 17 00:00:00 2001 From: Nikhil Shahi Date: Sat, 30 Jul 2022 13:20:52 -0500 Subject: [PATCH] add get endpoints --- backend/src/api/get-endpoints/index.ts | 13 ++++++ backend/src/api/log-request/index.ts | 18 +++++--- backend/src/services/get-endpoints/index.ts | 46 +++++++++++++++++++++ backend/src/services/log-request/index.ts | 12 +----- backend/src/types.ts | 8 ++++ 5 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 backend/src/api/get-endpoints/index.ts create mode 100644 backend/src/services/get-endpoints/index.ts diff --git a/backend/src/api/get-endpoints/index.ts b/backend/src/api/get-endpoints/index.ts new file mode 100644 index 00000000..0cd42a04 --- /dev/null +++ b/backend/src/api/get-endpoints/index.ts @@ -0,0 +1,13 @@ +import { Request, Response } from "express"; +import { GetEndpointsService } from "../../services/get-endpoints"; +import { GetEndpointParams } from "../../types"; + +export const getEndpointsHandler = async (req: Request, res: Response) => { + const getEndpointParams: GetEndpointParams = req.query; + try { + const endpoints = await GetEndpointsService.getEndpoints(getEndpointParams); + res.send(200).send(endpoints) + } catch { + res.sendStatus(500) + } +} diff --git a/backend/src/api/log-request/index.ts b/backend/src/api/log-request/index.ts index 450068c8..a2bd6ec0 100644 --- a/backend/src/api/log-request/index.ts +++ b/backend/src/api/log-request/index.ts @@ -1,15 +1,23 @@ import { Request, Response } from "express"; import { LogRequestService } from "../../services/log-request"; -import { TraceParams } from "types"; +import { TraceParams } from "../../types"; export const logRequestSingleHandler = async (req: Request, res: Response) => { const traceParams: TraceParams = req.body; - await LogRequestService.logRequest(traceParams); - res.sendStatus(200); + try { + await LogRequestService.logRequest(traceParams); + res.sendStatus(200) + } catch { + res.sendStatus(500) + } } export const logRequestBatchHandler = async (req: Request, res: Response) => { const traceParamsBatch: TraceParams[] = req.body; - await LogRequestService.logRequestBatch(traceParamsBatch); - res.sendStatus(200); + try { + await LogRequestService.logRequestBatch(traceParamsBatch); + res.sendStatus(200); + } catch { + res.sendStatus(500); + } } \ No newline at end of file diff --git a/backend/src/services/get-endpoints/index.ts b/backend/src/services/get-endpoints/index.ts new file mode 100644 index 00000000..9bf25e3a --- /dev/null +++ b/backend/src/services/get-endpoints/index.ts @@ -0,0 +1,46 @@ +import { FindManyOptions, FindOptionsWhere } from "typeorm"; +import { GetEndpointParams } from "../../types"; +import { AppDataSource } from "../../data-source" +import { ApiEndpoint } from "../../../models"; + +export class GetEndpointsService { + static async getEndpoints(getEndpointParams: GetEndpointParams): Promise { + try { + const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint); + let whereConditions: FindOptionsWhere = {} + let paginationParams: FindManyOptions = {} + if (getEndpointParams?.environment) { + whereConditions = { + ...whereConditions, + environment: getEndpointParams.environment + } + } + if (getEndpointParams?.host) { + whereConditions = { + ...whereConditions, + host: getEndpointParams.host + } + } + if (getEndpointParams?.offset) { + paginationParams = { + ...paginationParams, + skip: getEndpointParams.offset + } + } + if (getEndpointParams?.limit) { + paginationParams = { + ...paginationParams, + take: getEndpointParams.limit + } + } + + const endpoints = await apiEndpointRepository.find({ where: whereConditions, ...paginationParams }) + + // TODO: Calculate risk score for endpoints and if risk score param present, only return those that meet + + return endpoints + } catch (err) { + console.error(`Error in Get Endpoints service: ${err}`) + } + } +} \ No newline at end of file diff --git a/backend/src/services/log-request/index.ts b/backend/src/services/log-request/index.ts index 162581af..33dc6107 100644 --- a/backend/src/services/log-request/index.ts +++ b/backend/src/services/log-request/index.ts @@ -1,23 +1,13 @@ -import { Repository } from "typeorm"; import { TraceParams } from "../../types"; import { ApiTrace } from "../../../models"; import { AppDataSource } from "../../data-source"; import { ScannerService } from "../scanner/scan"; -let API_TRACE_REPOSITORY = AppDataSource.getRepository(ApiTrace); - export class LogRequestService { - static getApiTraceRepository(): Repository { - if (!API_TRACE_REPOSITORY) { - API_TRACE_REPOSITORY = AppDataSource.getRepository(ApiTrace); - } - return API_TRACE_REPOSITORY; - } - static async logRequest(traceParams: TraceParams) { try { /** Log Request in ApiTrace table */ - const apiTraceRepository = this.getApiTraceRepository(); + const apiTraceRepository = AppDataSource.getRepository(ApiTrace); const path = traceParams?.request?.url?.path; const method = traceParams?.request?.method; const environment = traceParams?.meta?.environment; diff --git a/backend/src/types.ts b/backend/src/types.ts index bea08592..fab39169 100644 --- a/backend/src/types.ts +++ b/backend/src/types.ts @@ -43,3 +43,11 @@ export interface TraceParams { response: Response meta: Meta } + +export interface GetEndpointParams { + environment?: string + host?: string + riskScore?: string + offset?: number + limit?: number +}