Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Jul 31, 2022
1 parent 6970687 commit 4a90977
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 195 deletions.
Empty file added backend/.prettierrc
Empty file.
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "tsc",
"watch": "tsc -w",
"dev": "nodemon src/index.ts",
"start": "node dist/src/index.js"
"start": "node dist/src/index.js",
"format": "prettier --write './src/**/*.{ts,tsx}'"
},
"keywords": [],
"author": "",
Expand All @@ -25,6 +26,7 @@
"@types/express": "^4.17.13",
"@types/node": "^18.6.1",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"tslib": "^2.4.0",
"typescript": "^4.7.4"
Expand Down
6 changes: 3 additions & 3 deletions backend/src/api/get-endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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)
res.status(200).send(endpoints);
} catch {
res.sendStatus(500)
res.sendStatus(500);
}
}
};
8 changes: 4 additions & 4 deletions backend/src/api/log-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export const logRequestSingleHandler = async (req: Request, res: Response) => {
const traceParams: TraceParams = req.body;
try {
await LogRequestService.logRequest(traceParams);
res.sendStatus(200)
res.sendStatus(200);
} catch {
res.sendStatus(500)
res.sendStatus(500);
}
}
};

export const logRequestBatchHandler = async (req: Request, res: Response) => {
const traceParamsBatch: TraceParams[] = req.body;
Expand All @@ -20,4 +20,4 @@ export const logRequestBatchHandler = async (req: Request, res: Response) => {
} catch {
res.sendStatus(500);
}
}
};
6 changes: 3 additions & 3 deletions backend/src/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "dotenv/config"
import "dotenv/config";
import { DataSource } from "typeorm";
import { ApiEndpoint, MatchedDataClass, ApiTrace } from "../models"
import { ApiEndpoint, MatchedDataClass, ApiTrace } from "../models";

export const AppDataSource = new DataSource({
type: "postgres",
Expand All @@ -9,4 +9,4 @@ export const AppDataSource = new DataSource({
entities: [ApiEndpoint, MatchedDataClass, ApiTrace],
migrations: [],
logging: false,
})
});
16 changes: 8 additions & 8 deletions backend/src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export enum RestMethod {
GET = "GET",
HEAD = "HEAD",
POST = "POST",
PUT = "PUT",
PATCH = "PATCH",
DELETE = "DELETE",
CONNECT = "CONNECT",
OPTIONS = "OPTIONS",
TRACE = "TRACE"
POST = "POST",
PUT = "PUT",
PATCH = "PATCH",
DELETE = "DELETE",
CONNECT = "CONNECT",
OPTIONS = "OPTIONS",
TRACE = "TRACE",
}

export enum DataClass {
Expand All @@ -20,5 +20,5 @@ export enum DataClass {
VIN = "Vehicle Identification Number",
ADDRESS = "Address",
DOB = "Date of Birth",
DL_NUMBER = "Driver License Number"
DL_NUMBER = "Driver License Number",
}
39 changes: 20 additions & 19 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import { logRequestBatchHandler, logRequestSingleHandler } from "./api/log-request"
import { AppDataSource } from './data-source';
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import bodyParser from "body-parser";
import {
logRequestBatchHandler,
logRequestSingleHandler,
} from "./api/log-request";
import { AppDataSource } from "./data-source";

dotenv.config();

const app: Express = express();
const port = process.env.PORT || 8080;

app.use(bodyParser.json())
app.use(bodyParser.json());

app.get('/', (req: Request, res: Response) => {
res.send('OK');
app.get("/", (req: Request, res: Response) => {
res.send("OK");
});

app.post('/log-request/single', logRequestSingleHandler)
app.post('/log-request/batch', logRequestBatchHandler)
app.post("/log-request/single", logRequestSingleHandler);
app.post("/log-request/batch", logRequestBatchHandler);

const main = async () => {
try {
const datasource = await AppDataSource.initialize();
console.log(
`Is AppDataSource Initialized? ${
datasource.isInitialized ? "Yes" : "No"
}`,
)
`Is AppDataSource Initialized? ${datasource.isInitialized ? "Yes" : "No"}`
);
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});
} catch (err) {
console.error(`CatchBlockInsideMain: ${err}`)
console.error(`CatchBlockInsideMain: ${err}`);
}
}
};

main().catch(err => {
console.error(`Error in main try block: ${err}`)
})
main().catch((err) => {
console.error(`Error in main try block: ${err}`);
});
39 changes: 22 additions & 17 deletions backend/src/services/get-endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
import { FindManyOptions, FindOptionsWhere } from "typeorm";
import { GetEndpointParams } from "../../types";
import { AppDataSource } from "../../data-source"
import { AppDataSource } from "../../data-source";
import { ApiEndpoint } from "../../../models";

export class GetEndpointsService {
static async getEndpoints(getEndpointParams: GetEndpointParams): Promise<ApiEndpoint[]> {
static async getEndpoints(
getEndpointParams: GetEndpointParams
): Promise<ApiEndpoint[]> {
try {
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint);
let whereConditions: FindOptionsWhere<ApiEndpoint> = {}
let paginationParams: FindManyOptions<ApiEndpoint> = {}
let whereConditions: FindOptionsWhere<ApiEndpoint> = {};
let paginationParams: FindManyOptions<ApiEndpoint> = {};
if (getEndpointParams?.environment) {
whereConditions = {
...whereConditions,
environment: getEndpointParams.environment
}
environment: getEndpointParams.environment,
};
}
if (getEndpointParams?.host) {
whereConditions = {
...whereConditions,
host: getEndpointParams.host
}
host: getEndpointParams.host,
};
}
if (getEndpointParams?.offset) {
paginationParams = {
...paginationParams,
skip: getEndpointParams.offset
}
skip: getEndpointParams.offset,
};
}
if (getEndpointParams?.limit) {
paginationParams = {
...paginationParams,
take: getEndpointParams.limit
}
take: getEndpointParams.limit,
};
}

const endpoints = await apiEndpointRepository.find({ where: whereConditions, ...paginationParams })

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
return endpoints;
} catch (err) {
console.error(`Error in Get Endpoints service: ${err}`)
console.error(`Error in Get Endpoints service: ${err}`);
}
}
}
}
11 changes: 5 additions & 6 deletions backend/src/services/log-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,27 @@ export class LogRequestService {

// Check for sensitive data in request parameters

const startTime = performance.now()
const startTime = performance.now();
if (requestParameters) {
for (const param of requestParameters) {
const field = param.name;
const matches = ScannerService.scan(param.value);
}
}
console.log(performance.now() - startTime)

await apiTraceRepository.save(apiTraceObj)
console.log(performance.now() - startTime);

await apiTraceRepository.save(apiTraceObj);

//TODO: Log Request in ApiEndpoint table
//TODO: Find sensitive data in request and response and add data classes and data paths to tables
} catch (err) {
console.error(`Error in Log Request service: ${err}`)
console.error(`Error in Log Request service: ${err}`);
}
}

static async logRequestBatch(traceParamsBatch: TraceParams[]) {
for (let i = 0; i < traceParamsBatch.length; i++) {
this.logRequest(traceParamsBatch[i])
this.logRequest(traceParamsBatch[i]);
}
}
}
Loading

0 comments on commit 4a90977

Please sign in to comment.