Skip to content

Commit

Permalink
(chore) add validation error message based on type
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Aug 31, 2022
1 parent e031b34 commit 0d099bd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 54 deletions.
13 changes: 12 additions & 1 deletion backend/src/services/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ export const validateSpecSchema = (schema: any, version?: number) => {
.replace(/\//g, ".")
.replace(/~1/g, "/")
.slice(1)
res.push(`${error.message} for field ${field}`)
let message = ""
switch (error.keyword) {
case "required":
message = `Required property '${error.params?.["missingProperty"]}' is missing from ${field}`
break
case "additionalProperties":
message = `Must not have additional property '${error.params?.["additionalProperty"]}' for ${field}`
break
default:
message = `${message} of ${field}`
}
res.push(message)
}
return res
}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/suricata_setup/aws-services/aws_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ export async function aws_mirror_target_creation({
accessKeyId: access_id,
},
region: region,
});
let resp = await create_mirror_target(client, destination_eni_id, id);
client.destroy();
})
let resp = await create_mirror_target(client, destination_eni_id, id)
client.destroy()
return {
success: "OK",
status: "IN-PROGRESS",
Expand Down
18 changes: 9 additions & 9 deletions backend/src/suricata_setup/aws-services/create-ec2-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
DescribeInstancesCommand,
TerminateInstancesCommand,
DeleteKeyPairCommand,
} from "@aws-sdk/client-ec2";
} from "@aws-sdk/client-ec2"

import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts"

Expand Down Expand Up @@ -441,18 +441,18 @@ export class EC2_CONN {
}

public async delete_ec2_instance(instance_id: string) {
let conn = this.get_conn();
let conn = this.get_conn()
await conn.send(
new TerminateInstancesCommand({ InstanceIds: [instance_id] })
);
return true;
new TerminateInstancesCommand({ InstanceIds: [instance_id] }),
)
return true
}

public async delete_keypair(keypair_id: string, key_name: string) {
let conn = this.get_conn();
let conn = this.get_conn()
await conn.send(
new DeleteKeyPairCommand({ KeyPairId: keypair_id, KeyName: key_name })
);
return true;
new DeleteKeyPairCommand({ KeyPairId: keypair_id, KeyName: key_name }),
)
return true
}
}
42 changes: 19 additions & 23 deletions backend/src/suricata_setup/aws-services/delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EC2Client } from "@aws-sdk/client-ec2";
import { STEP_RESPONSE } from "@common/types";
import { randomUUID } from "crypto";
import { EC2_CONN } from "./create-ec2-instance";
import { EC2Client } from "@aws-sdk/client-ec2"
import { STEP_RESPONSE } from "@common/types"
import { randomUUID } from "crypto"
import { EC2_CONN } from "./create-ec2-instance"
import {
delete_mirror_filter,
delete_mirror_session,
delete_mirror_target,
} from "./mirroring";
} from "./mirroring"

export async function delete_aws_data(aws: STEP_RESPONSE["data"]) {
let client = new EC2Client({
Expand All @@ -15,44 +15,40 @@ export async function delete_aws_data(aws: STEP_RESPONSE["data"]) {
secretAccessKey: aws.secret_access_key,
},
region: aws.region,
});
})
// Delete EC2 Mirror Session
try {
await delete_mirror_session(client, aws.mirror_session_id, randomUUID());
await delete_mirror_session(client, aws.mirror_session_id, randomUUID())
} catch (err) {
throw new Error(
`Couldn't delete EC2 Mirror Session ${aws.mirror_session_id}`
);
`Couldn't delete EC2 Mirror Session ${aws.mirror_session_id}`,
)
}
// Delete EC2 Mirror Filter
try {
await delete_mirror_filter(client, aws.mirror_filter_id, randomUUID());
await delete_mirror_filter(client, aws.mirror_filter_id, randomUUID())
} catch (err) {
throw new Error(
`Couldn't delete EC2 Mirror Filter ${aws.mirror_filter_id}`
);
throw new Error(`Couldn't delete EC2 Mirror Filter ${aws.mirror_filter_id}`)
}
// Delete EC2 Mirror Target
try {
await delete_mirror_target(client, aws.mirror_target_id, randomUUID());
await delete_mirror_target(client, aws.mirror_target_id, randomUUID())
} catch (err) {
throw new Error(
`Couldn't delete EC2 Mirror Target ${aws.mirror_target_id}`
);
throw new Error(`Couldn't delete EC2 Mirror Target ${aws.mirror_target_id}`)
}

let conn = new EC2_CONN(aws.access_id, aws.secret_access_key, aws.region);
let conn = new EC2_CONN(aws.access_id, aws.secret_access_key, aws.region)
// Delete EC2 Instance
try {
await conn.delete_ec2_instance(aws.mirror_instance_id);
await conn.delete_ec2_instance(aws.mirror_instance_id)
} catch (err) {
throw new Error(`Couldn't delete EC2 instance ${aws.mirror_instance_id}`);
throw new Error(`Couldn't delete EC2 instance ${aws.mirror_instance_id}`)
}
// Delete EC2 Keypair
try {
await conn.delete_keypair(aws.keypair_id, aws.keypair_name);
await conn.delete_keypair(aws.keypair_id, aws.keypair_name)
} catch (err) {
throw new Error(`Couldn't delete EC2 Keyapair ${aws.keypair}`);
throw new Error(`Couldn't delete EC2 Keyapair ${aws.keypair}`)
}
return `Deleted connection ${aws.id}`;
return `Deleted connection ${aws.id}`
}
22 changes: 11 additions & 11 deletions backend/src/suricata_setup/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { RedisClient } from "utils/redis";
import { RedisClient } from "utils/redis"

async function addToRedis(key, data: Object) {
const redisClient = RedisClient.getInstance();
await redisClient.set(key, JSON.stringify(data));
const redisClient = RedisClient.getInstance()
await redisClient.set(key, JSON.stringify(data))
}
function addToRedisFromPromise(key, data: Promise<Object>) {
const redisClient = RedisClient.getInstance();
const redisClient = RedisClient.getInstance()
data
.then((resp) => redisClient.set(key, JSON.stringify(resp)))
.catch((err) => redisClient.set(key, JSON.stringify(err)));
.then(resp => redisClient.set(key, JSON.stringify(resp)))
.catch(err => redisClient.set(key, JSON.stringify(err)))
}

async function getFromRedis(key) {
const redisClient = RedisClient.getInstance();
return JSON.parse(await redisClient.get(key));
const redisClient = RedisClient.getInstance()
return JSON.parse(await redisClient.get(key))
}

async function deleteKeyFromRedis(key) {
const redisClient = RedisClient.getInstance();
return await redisClient.del([key]);
const redisClient = RedisClient.getInstance()
return await redisClient.del([key])
}

export { addToRedis, addToRedisFromPromise, getFromRedis, deleteKeyFromRedis };
export { addToRedis, addToRedisFromPromise, getFromRedis, deleteKeyFromRedis }
2 changes: 1 addition & 1 deletion backend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getPathTokens = (path: string): string[] => {
return ["/"]
}
const tokens = path.split("/")
return tokens.filter((token) => token.length > 0)
return tokens.filter(token => token.length > 0)
}

export const getPathRegex = (path: string): string => {
Expand Down
12 changes: 6 additions & 6 deletions backend/src/utils/redis.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import IORedis from "ioredis";
import IORedis from "ioredis"

export class RedisClient {
private static instance: RedisClient;
private static client: IORedis;
private static instance: RedisClient
private static client: IORedis

private constructor(url) {
RedisClient.client = new IORedis(url);
RedisClient.client = new IORedis(url)
}

public static getInstance(): IORedis {
if (!RedisClient.instance) {
RedisClient.instance = new RedisClient(process.env.REDIS_URL);
RedisClient.instance = new RedisClient(process.env.REDIS_URL)
}

return RedisClient.client;
return RedisClient.client
}
}

0 comments on commit 0d099bd

Please sign in to comment.