Skip to content

Commit

Permalink
add dependencies, move things around, fix models and code issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Aug 4, 2022
1 parent 28ba588 commit b1df65c
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 116 deletions.
5 changes: 1 addition & 4 deletions backend/models/api-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseEntity, Column, CreateDateColumn, Entity, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { MatchedDataClass } from "./matched-data-class";
import { RestMethod } from "../src/enums";
import { RestMethod } from "@common/enums";

@Entity()
export class ApiEndpoint extends BaseEntity {
Expand All @@ -19,9 +19,6 @@ export class ApiEndpoint extends BaseEntity {
@Column({ nullable: false })
host: string

@Column({ nullable: false })
environment: string

@Column({ type: "integer"})
totalCalls: number

Expand Down
7 changes: 2 additions & 5 deletions backend/models/api-trace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseEntity, Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Meta, PairObject } from "../src/types";
import { RestMethod } from "../src/enums";
import { Meta, PairObject } from "@common/types";
import { RestMethod } from "@common/enums";
import { ApiEndpoint } from "./api-endpoint";

@Entity()
Expand All @@ -17,9 +17,6 @@ export class ApiTrace extends BaseEntity {
@Column({ nullable: false })
host: string

@Column({ nullable: false })
environment: string

@Column({ type: "enum", enum: RestMethod })
method: RestMethod

Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/get-endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import { GetEndpointsService } from "../../services/get-endpoints";
import { GetEndpointParams } from "../../types";
import { GetEndpointParams } from "@common/types";
import ApiResponseHandler from "../../api-response-handler";

export const getEndpointsHandler = async (req: Request, res: Response) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/log-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import { LogRequestService } from "../../services/log-request";
import { TraceParams } from "../../types";
import { TraceParams } from "@common/types";
import ApiResponseHandler from "../../api-response-handler";

export const logRequestSingleHandler = async (req: Request, res: Response) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/spec/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import yaml from "js-yaml";
import { JSONValue } from "../../types";
import { JSONValue } from "@common/types";
import { SpecService } from "../../services/spec";
import ApiResponseHandler from "../../api-response-handler";
import Error400BadRequest from "../../errors/error-400-bad-request";
Expand Down
24 changes: 0 additions & 24 deletions backend/src/enums.ts

This file was deleted.

3 changes: 1 addition & 2 deletions backend/src/services/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IsNull } from "typeorm";
import { isSuspectedParamater } from "../../utils";
import { ApiEndpoint, ApiTrace } from "../../../models";
import { AppDataSource } from "../../data-source";
import { RestMethod } from "../../enums";
import { RestMethod } from "@common/enums";

interface GenerateEndpoint {
parameterizedPath: string;
Expand Down Expand Up @@ -76,7 +76,6 @@ export class EndpointsService {
const apiEndpoint = new ApiEndpoint();
apiEndpoint.path = value.parameterizedPath;
apiEndpoint.host = value.traces[0].host;
apiEndpoint.environment = value.traces[0].environment;
apiEndpoint.totalCalls = value.traces.length;
apiEndpoint.method = value.traces[0].method;
apiEndpoint.owner = value.traces[0].owner;
Expand Down
8 changes: 1 addition & 7 deletions backend/src/services/get-endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FindManyOptions, FindOptionsWhere } from "typeorm";
import { GetEndpointParams } from "../../types";
import { GetEndpointParams } from "@common/types";
import { AppDataSource } from "../../data-source";
import { ApiEndpoint } from "../../../models";
import Error500InternalServer from "../../errors/error-500-internal-server";
Expand All @@ -12,12 +12,6 @@ export class GetEndpointsService {
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint);
let whereConditions: FindOptionsWhere<ApiEndpoint> = {};
let paginationParams: FindManyOptions<ApiEndpoint> = {};
if (getEndpointParams?.environment) {
whereConditions = {
...whereConditions,
environment: getEndpointParams.environment,
};
}
if (getEndpointParams?.host) {
whereConditions = {
...whereConditions,
Expand Down
9 changes: 3 additions & 6 deletions backend/src/services/log-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PairObject, TraceParams } from "../../types";
import { PairObject, TraceParams } from "@common/types";
import { ApiEndpoint, ApiTrace, MatchedDataClass } from "../../../models";
import { AppDataSource } from "../../data-source";
import { ScannerService } from "../scanner/scan";
import { DataClass } from "../../enums";
import { DataClass } from "@common/enums";
import Error500InternalServer from "../../errors/error-500-internal-server";

export class LogRequestService {
Expand Down Expand Up @@ -63,7 +63,6 @@ export class LogRequestService {
const apiTraceRepository = AppDataSource.getRepository(ApiTrace);
const path = traceParams?.request?.url?.path;
const method = traceParams?.request?.method;
const environment = traceParams?.meta?.environment;
const host = traceParams?.request?.url?.host;
const requestParameters = traceParams?.request?.url?.parameters;
const requestHeaders = traceParams?.request?.headers;
Expand All @@ -73,7 +72,6 @@ export class LogRequestService {
const apiTraceObj = new ApiTrace();
apiTraceObj.path = path;
apiTraceObj.method = method;
apiTraceObj.environment = environment;
apiTraceObj.host = host;
apiTraceObj.requestParameters = requestParameters;
apiTraceObj.requestHeaders = requestHeaders;
Expand All @@ -86,14 +84,13 @@ export class LogRequestService {
/** Create new Api Endpoint record or update existing */
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint);
let apiEndpoint = await apiEndpointRepository.findOne({
where: { path, method, environment, host },
where: { path, method, host },
relations: { sensitiveDataClasses: true },
});
if (!apiEndpoint) {
apiEndpoint = new ApiEndpoint();
apiEndpoint.path = path;
apiEndpoint.method = method;
apiEndpoint.environment = environment;
apiEndpoint.host = host;
apiEndpoint.totalCalls = 0;
apiEndpoint.sensitiveDataClasses = [];
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/scanner/scan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataClass } from "../../enums";
import { DataClass } from "@common/enums";
import {
ADDRESS_REGEXP,
COORDINATE_REGEXP,
Expand Down
28 changes: 21 additions & 7 deletions backend/src/services/spec/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { RestMethod } from "@common/enums";
import { ApiEndpoint } from "../../../models";
import Error400BadRequest from "../../errors/error-400-bad-request";
import { JSONValue } from "../../types";
import { JSONValue } from "@common/types";

export class SpecService {
static async uploadNewSpec(specObject: JSONValue) {
const hosts: string[] = [];
const apiEndpoints: ApiEndpoint[] = [];
const servers: any[] = specObject["servers"];
const paths: JSONValue = specObject["paths"];

if (!servers || servers?.length === 0) {
throw new Error400BadRequest("No servers found in spec file.");
}

servers.forEach((server) => {
if (server["url"]) {
hosts.push(server["url"]);
}
});

const pathKeys = Object.keys(paths);
pathKeys.forEach((path) => {
const methods = Object.keys(paths[path]);
methods.forEach((method) => {
servers.forEach((server) => {
if (server["url"]) {
const methodEnum = method.toUpperCase() as RestMethod;
const apiEndpoint = new ApiEndpoint();
apiEndpoint.path = path;
apiEndpoint.method = methodEnum;
apiEndpoint.host = server["url"];
}
});
})
})
console.log(hosts);
}
}
55 changes: 0 additions & 55 deletions backend/src/types.ts

This file was deleted.

5 changes: 4 additions & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"importHelpers": true,
"baseUrl": "src"
"baseUrl": "./src",
"paths": {
"@common/*": ["../../common/dist/*"]
}
},
"include": [
"src/**/*.ts",
Expand Down
3 changes: 3 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w"
},
"devDependencies": {
"typescript": "^4.7.4"
}
}
40 changes: 39 additions & 1 deletion common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,45 @@ export interface PairObject {
value: string;
}

export interface Url {
host: string;
path: string;
parameters: PairObject[];
}

export interface Request {
url: Url;
headers: PairObject[];
body: string;
method: RestMethod;
}

export interface Response {
status: number;
headers: PairObject[];
body: string;
}

export interface TraceParams {
request: Request;
response: Response;
meta: Meta;
}

export interface GetEndpointParams {
host?: string;
riskScore?: RiskScore;
offset?: number;
limit?: number;
}

export type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| Array<JSONValue>;

export interface ApiTrace {
uuid: string;
path: string;
Expand Down Expand Up @@ -46,7 +85,6 @@ export interface PIIField {

export interface Endpoint {
uuid: string;
environment: string;
host: string;
path: string;
method: string;
Expand Down
8 changes: 8 additions & 0 deletions common/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


typescript@^4.7.4:
version "4.7.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235"
integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==

0 comments on commit b1df65c

Please sign in to comment.