diff --git a/aws/src/lambdas/getBlockedDomains/index.js b/aws/src/lambdas/getBlockedDomains/index.js new file mode 100644 index 0000000..8a3cf4a --- /dev/null +++ b/aws/src/lambdas/getBlockedDomains/index.js @@ -0,0 +1,53 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const promise_1 = require("mysql2/promise"); +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, +}; +const handler = async (event) => { + console.log('EVENT: \n' + JSON.stringify(event, null, 2)); + let request_body; + try { + if (event.body) { + request_body = JSON.parse(event.body); + } + else { + console.log("No body found"); + return { + statusCode: 400, + body: JSON.stringify({ message: "No body found" }) + }; + } + } + catch (error) { + console.error("Invalid JSON format", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Invalid JSON format" }), + }; + } + const { uuid } = request_body; + const connection = await (0, promise_1.createConnection)(dbConfig); + ; + try { + const [rows] = await connection.execute('SELECT domain FROM blocked_domains WHERE uuid = ?;', [uuid]); + console.log(rows); + await connection.end(); + const domains = rows.map(row => row.domain); + return { + statusCode: 200, + body: JSON.stringify(domains) + }; + } + catch (error) { + console.error("Error inserting data:", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Error with DB: " + error }) + }; + } +}; +module.exports = { handler }; diff --git a/aws/src/lambdas/getBlockedDomains/index.ts b/aws/src/lambdas/getBlockedDomains/index.ts new file mode 100644 index 0000000..ec829cd --- /dev/null +++ b/aws/src/lambdas/getBlockedDomains/index.ts @@ -0,0 +1,55 @@ +import { APIGatewayEvent } from 'aws-lambda'; +import { createConnection, RowDataPacket } from 'mysql2/promise'; + +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, +}; + +const handler = async (event: APIGatewayEvent) => { + console.log('EVENT: \n' + JSON.stringify(event, null, 2)); + let request_body; + try { + if (event.body) { + request_body = JSON.parse(event.body) + } else { + console.log("No body found"); + return { + statusCode: 400, + body: JSON.stringify({ message: "No body found" }) + }; + } + } catch (error) { + console.error("Invalid JSON format", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Invalid JSON format" }), + }; + } + + const { uuid } = request_body; + const connection = await createConnection(dbConfig);; + try { + const [rows] = await connection.execute( + 'SELECT domain FROM blocked_domains WHERE uuid = ?;', + [uuid] + ); + console.log(rows); + await connection.end(); + const domains = rows.map(row => row.domain); + return { + statusCode: 200, + body: JSON.stringify(domains) + } + } catch (error) { + console.error("Error inserting data:", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Error with DB: " + error }) + }; + } +} + +module.exports = { handler }; \ No newline at end of file diff --git a/aws/src/lambdas/getBlockedDomains/package.json b/aws/src/lambdas/getBlockedDomains/package.json new file mode 100644 index 0000000..a5b9b38 --- /dev/null +++ b/aws/src/lambdas/getBlockedDomains/package.json @@ -0,0 +1,14 @@ +{ + "scripts": { + "build": "npx tsc", + "zip": "(zip getBlockedDomains.zip index.js node_modules || powershell Compress-Archive -Path index.js, node_modules -DestinationPath getBlockedDomains.zip)", + "move": "(mv getBlockedDomains.zip ../../terraform || powershell Move-Item getBlockedDomains.zip ../../terraform)" + }, + "devDependencies": { + "@types/aws-lambda": "^8.10.147", + "typescript": "^5.7.3" + }, + "dependencies": { + "mysql2": "^3.13.0" + } +} \ No newline at end of file diff --git a/aws/src/lambdas/getBlockedDomains/tsconfig.json b/aws/src/lambdas/getBlockedDomains/tsconfig.json new file mode 100644 index 0000000..c40e4a5 --- /dev/null +++ b/aws/src/lambdas/getBlockedDomains/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "CommonJS", + "lib": ["ES2020"], + "moduleResolution": "Node", + "rootDir": "./", + "strict": true, + "esModuleInterop": true + } +} \ No newline at end of file diff --git a/aws/src/lambdas/insertBlockedDomain/index.js b/aws/src/lambdas/insertBlockedDomain/index.js new file mode 100644 index 0000000..40d09a4 --- /dev/null +++ b/aws/src/lambdas/insertBlockedDomain/index.js @@ -0,0 +1,51 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const promise_1 = require("mysql2/promise"); +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, +}; +const handler = async (event) => { + console.log('EVENT: \n' + JSON.stringify(event, null, 2)); + let request_body; + try { + if (event.body) { + request_body = JSON.parse(event.body); + } + else { + console.log("No body found"); + return { + statusCode: 400, + body: JSON.stringify({ message: "No body found" }) + }; + } + } + catch (error) { + console.error("Invalid JSON format", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Invalid JSON format" }), + }; + } + const { uuid, domain } = request_body; + const connection = await (0, promise_1.createConnection)(dbConfig); + try { + const [rows] = await connection.execute('INSERT INTO blocked_domains (uuid, domain) VALUES (?, ?);', [uuid, domain]); + console.log(rows); + await connection.end(); + return { + statusCode: 200, + body: JSON.stringify({ message: "Domain inserted successfully" }) + }; + } + catch (error) { + console.error("Error inserting data:", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Error with DB" }) + }; + } +}; +module.exports = { handler }; diff --git a/aws/src/lambdas/insertBlockedDomain/index.ts b/aws/src/lambdas/insertBlockedDomain/index.ts new file mode 100644 index 0000000..4f67747 --- /dev/null +++ b/aws/src/lambdas/insertBlockedDomain/index.ts @@ -0,0 +1,54 @@ +import { APIGatewayEvent } from "aws-lambda"; +import { createConnection } from 'mysql2/promise'; + +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, +}; + +const handler = async (event: APIGatewayEvent) => { + console.log('EVENT: \n' + JSON.stringify(event, null, 2)); + let request_body; + try { + if (event.body) { + request_body = JSON.parse(event.body) + } else { + console.log("No body found"); + return { + statusCode: 400, + body: JSON.stringify({ message: "No body found" }) + }; + } + } catch (error) { + console.error("Invalid JSON format", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Invalid JSON format" }), + }; + } + + const { uuid, domain } = request_body; + const connection = await createConnection(dbConfig); + try { + const [rows] = await connection.execute( + 'INSERT INTO blocked_domains (uuid, domain) VALUES (?, ?);', + [uuid, domain] + ); + console.log(rows); + await connection.end(); + return { + statusCode: 200, + body: JSON.stringify({ message: "Domain inserted successfully" }) + } + } catch (error) { + console.error("Error inserting data:", error); + return { + statusCode: 400, + body: JSON.stringify({ message: "Error with DB" }) + }; + } +} + +module.exports = { handler }; \ No newline at end of file diff --git a/aws/src/lambdas/insertBlockedDomain/package.json b/aws/src/lambdas/insertBlockedDomain/package.json new file mode 100644 index 0000000..b672089 --- /dev/null +++ b/aws/src/lambdas/insertBlockedDomain/package.json @@ -0,0 +1,14 @@ +{ + "scripts": { + "build": "npx tsc", + "zip": "(zip insertBlockedDomain.zip index.js node_modules || powershell Compress-Archive -Path index.js, node_modules -DestinationPath insertBlockedDomain.zip)", + "move": "(mv insertBlockedDomain.zip ../../terraform || powershell Move-Item insertBlockedDomain.zip ../../terraform)" + }, + "devDependencies": { + "@types/aws-lambda": "^8.10.147", + "typescript": "^5.7.3" + }, + "dependencies": { + "mysql2": "^3.13.0" + } +} \ No newline at end of file diff --git a/aws/src/lambdas/insertBlockedDomain/tsconfig.json b/aws/src/lambdas/insertBlockedDomain/tsconfig.json new file mode 100644 index 0000000..c40e4a5 --- /dev/null +++ b/aws/src/lambdas/insertBlockedDomain/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "CommonJS", + "lib": ["ES2020"], + "moduleResolution": "Node", + "rootDir": "./", + "strict": true, + "esModuleInterop": true + } +} \ No newline at end of file diff --git a/aws/src/terraform/api_gateway.tf b/aws/src/terraform/api_gateway.tf index 1fa8454..d0a5fa6 100644 --- a/aws/src/terraform/api_gateway.tf +++ b/aws/src/terraform/api_gateway.tf @@ -55,6 +55,20 @@ resource "aws_apigatewayv2_integration" "calchash_integration" { integration_method = "POST" } +resource "aws_apigatewayv2_integration" "getBlockedDomains_integration" { + api_id = aws_apigatewayv2_api.http_api.id + integration_type = "AWS_PROXY" + integration_uri = aws_lambda_function.getBlockedDomains_lambda.invoke_arn + integration_method = "POST" +} + +resource "aws_apigatewayv2_integration" "insertBlockedDomain_integration" { + api_id = aws_apigatewayv2_api.http_api.id + integration_type = "AWS_PROXY" + integration_uri = aws_lambda_function.insertBlockedDomain_lambda.invoke_arn + integration_method = "POST" +} + resource "aws_apigatewayv2_route" "signup" { api_id = aws_apigatewayv2_api.http_api.id route_key = "POST /signup" @@ -91,6 +105,18 @@ resource "aws_apigatewayv2_route" "calculateHash" { target = "integrations/${aws_apigatewayv2_integration.calchash_integration.id}" } +resource "aws_apigatewayv2_route" "getBlockedDomains" { + api_id = aws_apigatewayv2_api.http_api.id + route_key = "POST /getBlockedDomains" + target = "integrations/${aws_apigatewayv2_integration.getBlockedDomains_integration.id}" +} + +resource "aws_apigatewayv2_route" "insertBlockedDomain" { + api_id = aws_apigatewayv2_api.http_api.id + route_key = "POST /insertBlockedDomain" + target = "integrations/${aws_apigatewayv2_integration.insertBlockedDomain_integration.id}" +} + resource "aws_lambda_permission" "apigw-lambda" { statement_id = "AllowExecutionFromAPIGateway" action = "lambda:InvokeFunction" @@ -139,6 +165,22 @@ resource "aws_lambda_permission" "apigw-lambda_getUserInfo" { source_arn = "arn:aws:execute-api:${var.AWS_REGION}:${var.AWS_ACCOUNT_ID}:${aws_apigatewayv2_api.http_api.id}/*" } +resource "aws_lambda_permission" "apigw-lambda_getBlockedDomains" { + statement_id = "AllowExecutionFromAPIGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.getBlockedDomains_lambda.function_name + principal = "apigateway.amazonaws.com" + source_arn = "arn:aws:execute-api:${var.AWS_REGION}:${var.AWS_ACCOUNT_ID}:${aws_apigatewayv2_api.http_api.id}/*" +} + +resource "aws_lambda_permission" "apigw-lambda_insertBlockedDomain" { + statement_id = "AllowExecutionFromAPIGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.insertBlockedDomain_lambda.function_name + principal = "apigateway.amazonaws.com" + source_arn = "arn:aws:execute-api:${var.AWS_REGION}:${var.AWS_ACCOUNT_ID}:${aws_apigatewayv2_api.http_api.id}/*" +} + resource "aws_apigatewayv2_route" "generate_password" { api_id = aws_apigatewayv2_api.http_api.id route_key = "POST /gen_pass" @@ -152,7 +194,6 @@ resource "aws_apigatewayv2_integration" "genpass_integration" { integration_method = "POST" } - resource "aws_lambda_permission" "apigw-lambda_passgen" { statement_id = "AllowExecutionFromAPIGateway" action = "lambda:InvokeFunction" diff --git a/aws/src/terraform/calcHash.zip b/aws/src/terraform/calcHash.zip new file mode 100644 index 0000000..550a2ea Binary files /dev/null and b/aws/src/terraform/calcHash.zip differ diff --git a/aws/src/terraform/gen_pass.tf b/aws/src/terraform/gen_pass.tf index bfbf95a..17ed835 100644 --- a/aws/src/terraform/gen_pass.tf +++ b/aws/src/terraform/gen_pass.tf @@ -1,8 +1,8 @@ resource "aws_lambda_function" "gen_pass_lambda" { - filename = "${path.cwd}/genPass.zip" - function_name = "gen_password" - role = aws_iam_role.iam_role2.arn - handler = "gen_password/index.handler" - runtime = "nodejs22.x" + filename = "${path.cwd}/genPass.zip" + function_name = "gen_password" + role = aws_iam_role.iam_role.arn + handler = "gen_password/index.handler" + runtime = "nodejs22.x" source_code_hash = filebase64sha256("${path.cwd}/genPass.zip") } \ No newline at end of file diff --git a/aws/src/terraform/getBlockedDomains.tf b/aws/src/terraform/getBlockedDomains.tf new file mode 100644 index 0000000..9ece04e --- /dev/null +++ b/aws/src/terraform/getBlockedDomains.tf @@ -0,0 +1,23 @@ +resource "aws_lambda_function" "getBlockedDomains_lambda" { + filename = "${path.cwd}/getBlockedDomains.zip" + function_name = "getBlockedDomains" + role = aws_iam_role.iam_role.arn + handler = "getBlockedDomains/index.handler" + runtime = "nodejs22.x" + timeout = 29 + source_code_hash = filebase64sha256("${path.cwd}/getBlockedDomains.zip") + + vpc_config { + subnet_ids = ["subnet-08f7876b20ec2648d"] + security_group_ids = ["sg-097af9b3d4b9cc7a3"] + } + + environment { + variables = { + DB_HOST = var.db_host + DB_USER = var.db_user + DB_PASS = var.db_pass + DB_NAME = var.db_name + } + } +} \ No newline at end of file diff --git a/aws/src/terraform/getBlockedDomains.zip b/aws/src/terraform/getBlockedDomains.zip new file mode 100644 index 0000000..e479f77 Binary files /dev/null and b/aws/src/terraform/getBlockedDomains.zip differ diff --git a/aws/src/terraform/insertBlockedDomain.tf b/aws/src/terraform/insertBlockedDomain.tf new file mode 100644 index 0000000..a3c05c5 --- /dev/null +++ b/aws/src/terraform/insertBlockedDomain.tf @@ -0,0 +1,23 @@ +resource "aws_lambda_function" "insertBlockedDomain_lambda" { + filename = "${path.cwd}/insertBlockedDomain.zip" + function_name = "insertBlockedDomain" + role = aws_iam_role.iam_role.arn + handler = "insertBlockedDomain/index.handler" + runtime = "nodejs22.x" + timeout = 29 + source_code_hash = filebase64sha256("${path.cwd}/insertBlockedDomain.zip") + + vpc_config { + subnet_ids = ["subnet-08f7876b20ec2648d"] + security_group_ids = ["sg-097af9b3d4b9cc7a3"] + } + + environment { + variables = { + DB_HOST = var.db_host + DB_USER = var.db_user + DB_PASS = var.db_pass + DB_NAME = var.db_name + } + } +} \ No newline at end of file diff --git a/aws/src/terraform/insertBlockedDomain.zip b/aws/src/terraform/insertBlockedDomain.zip new file mode 100644 index 0000000..6bdcdc5 Binary files /dev/null and b/aws/src/terraform/insertBlockedDomain.zip differ