From 78ab9cf6d52933ca3b7843622ad5000432de36a1 Mon Sep 17 00:00:00 2001 From: key kim Date: Sat, 3 Oct 2020 00:06:26 +0900 Subject: [PATCH 1/4] Add get all security group api --- src/entities/PromiseStatus.ts | 23 +++++++++++++++++++++++ src/helper/shell.ts | 23 +++++++++++++++++++++++ src/routes/index.ts | 3 ++- src/routes/v1/Aws.ts | 27 +++++++++++++++++++++++++++ tsconfig.json | 2 +- 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/entities/PromiseStatus.ts create mode 100644 src/helper/shell.ts create mode 100644 src/routes/v1/Aws.ts diff --git a/src/entities/PromiseStatus.ts b/src/entities/PromiseStatus.ts new file mode 100644 index 0000000..662b7e7 --- /dev/null +++ b/src/entities/PromiseStatus.ts @@ -0,0 +1,23 @@ +export enum promiseStatus { + "OK", + "FATAL", + "ERROR" +} + +export interface IPromiseStatus { + result: promiseStatus; + message: string; +} + +class PromiseType implements IPromiseStatus { + + public result: promiseStatus; + public message: string; + + constructor(result: number, message: string) { + this.result = result + this.message = message + } +} + +export default PromiseType; diff --git a/src/helper/shell.ts b/src/helper/shell.ts new file mode 100644 index 0000000..446d2b1 --- /dev/null +++ b/src/helper/shell.ts @@ -0,0 +1,23 @@ +import PromiseType, { promiseStatus } from '@entities/PromiseStatus'; + +async function execShellCommand(cmd:string):Promise { + const exec = require('child_process').exec; + return new Promise((resolve, reject) => { + exec(cmd, (error, stdout, stderr) => { + if (error) { + console.log(error) + reject(new PromiseType(promiseStatus.FATAL, error)); + } + if (stdout) { + console.log(stdout) + resolve(new PromiseType(promiseStatus.OK, stdout)) + } + if (stderr) { + console.log(stderr) + reject(new PromiseType(promiseStatus.ERROR, stderr)) + } + }); + }); +} + +export {execShellCommand}; \ No newline at end of file diff --git a/src/routes/index.ts b/src/routes/index.ts index c037829..02822d4 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,11 +1,12 @@ import { Router } from 'express'; import UserRouter from './Users'; - +import AwsRouterV1 from './v1/Aws'; // Init router and path const router = Router(); // Add sub-routes router.use('/users', UserRouter); +router.use('/v1/aws', AwsRouterV1) // Export the base-router export default router; diff --git a/src/routes/v1/Aws.ts b/src/routes/v1/Aws.ts new file mode 100644 index 0000000..cd2ff23 --- /dev/null +++ b/src/routes/v1/Aws.ts @@ -0,0 +1,27 @@ +import PromiseType from '@entities/PromiseStatus'; +import { Request, Response, Router } from 'express'; +import { execShellCommand } from "../../helper/shell"; + +// Init shared +const router = Router(); + + +router.get('/securitygroups', async (req: Request, res: Response) => { + const { region, access_key, secret_key } = req.body; + + let ret:PromiseType = await execShellCommand(`docker-compose -f helper/docker-compose.yml run \ + -e AWS_DEFAULT_REGION=${region} \ + -e AWS_ACCESS_KEY_ID=${access_key} \ + -e AWS_SECRET_ACCESS_KEY=${secret_key} \ + --rm aws ec2 describe-security-groups \ + `); + if (ret.result == 1 || ret.result == 2) { + return res.status(400).end(ret.message); + } + + return res.status(200).send(JSON.parse(ret.message)); +}); + + +export default router; + diff --git a/tsconfig.json b/tsconfig.json index af4a875..6760ab4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "outDir": "dist", "removeComments": true, "sourceMap": true, - "strict": true, + "strict": false, "target": "es6", "moduleResolution": "node", "paths": { From d160e695ca59ba425502dec5c7adf0b65a0722cd Mon Sep 17 00:00:00 2001 From: key kim Date: Sat, 3 Oct 2020 00:07:15 +0900 Subject: [PATCH 2/4] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3b6e86a..255adeb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.rest # OS X .DS_Store* Icon? @@ -24,4 +25,4 @@ package-lock.json coverage # Benchmarking -benchmarks/graphs \ No newline at end of file +benchmarks/graphs From f1307ae6aa3ec323a8159f81bee4842de2896910 Mon Sep 17 00:00:00 2001 From: key kim Date: Sat, 3 Oct 2020 12:42:29 +0900 Subject: [PATCH 3/4] Parse /securitygroups data from body to header --- src/routes/v1/Aws.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/v1/Aws.ts b/src/routes/v1/Aws.ts index cd2ff23..40520a9 100644 --- a/src/routes/v1/Aws.ts +++ b/src/routes/v1/Aws.ts @@ -7,7 +7,7 @@ const router = Router(); router.get('/securitygroups', async (req: Request, res: Response) => { - const { region, access_key, secret_key } = req.body; + const { region, access_key, secret_key } = req.headers; let ret:PromiseType = await execShellCommand(`docker-compose -f helper/docker-compose.yml run \ -e AWS_DEFAULT_REGION=${region} \ From aa143e63c223ea61cd7aa46f0d0d61bea103ae3b Mon Sep 17 00:00:00 2001 From: key kim Date: Sat, 3 Oct 2020 13:08:27 +0900 Subject: [PATCH 4/4] parameterize region as query --- src/routes/v1/Aws.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/routes/v1/Aws.ts b/src/routes/v1/Aws.ts index 40520a9..89db423 100644 --- a/src/routes/v1/Aws.ts +++ b/src/routes/v1/Aws.ts @@ -7,10 +7,11 @@ const router = Router(); router.get('/securitygroups', async (req: Request, res: Response) => { - const { region, access_key, secret_key } = req.headers; - + const { access_key, secret_key } = req.headers; + const { region } = req.query + let ret:PromiseType = await execShellCommand(`docker-compose -f helper/docker-compose.yml run \ - -e AWS_DEFAULT_REGION=${region} \ + -e AWS_DEFAULT_REGION=${region || 'us-east-1'} \ -e AWS_ACCESS_KEY_ID=${access_key} \ -e AWS_SECRET_ACCESS_KEY=${secret_key} \ --rm aws ec2 describe-security-groups \