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 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..89db423 --- /dev/null +++ b/src/routes/v1/Aws.ts @@ -0,0 +1,28 @@ +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 { 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 || 'us-east-1'} \ + -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": {