Compare commits

...
5 Commits
Author SHA1 Message Date
Arthur Simas d89573bc79 FIX: creating swap file 2022-06-06 17:35:26 -03:00
Gabriel Amorim 03ed4d88a0 FIX: validate cron
Merge pull request #101 from dadosfera/fix/validate-cron
2022-05-31 16:33:05 -03:00
Gabriel Rosa 329bbf2e4d Better cron validation 2022-05-31 16:10:21 -03:00
Gabriel Rosa ae069a0e00 removed unused imports 2022-05-31 12:04:32 -03:00
Gabriel Rosa d3bcbdc43c validating cron when creating or updating input 2022-05-31 11:34:28 -03:00
7 changed files with 1435 additions and 1487 deletions
+3
View File
@@ -0,0 +1,3 @@
container_commands:
01setup_swap:
command: "bash .ebextensions/setup_swap.sh"
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
SWAPFILE=/var/swapfile
SWAP_MEGABYTES=1024
if [ -f $SWAPFILE ]; then
echo "Swapfile $SWAPFILE found, assuming already setup"
exit;
fi
/bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
/bin/chmod 600 $SWAPFILE
/sbin/mkswap $SWAPFILE
/sbin/swapon $SWAPFILE
+1
View File
@@ -0,0 +1 @@
18
+1373 -1482
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -33,6 +33,7 @@
"@nestjs/swagger": "^5.2.1", "@nestjs/swagger": "^5.2.1",
"@victorradael/protospack": "2.4.2-1", "@victorradael/protospack": "2.4.2-1",
"axios": "^0.25.0", "axios": "^0.25.0",
"cron-parser": "^4.4.0",
"dotenv": "^14.2.0", "dotenv": "^14.2.0",
"helmet": "^5.0.2", "helmet": "^5.0.2",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
@@ -42,8 +43,8 @@
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
"swagger-ui-express": "^4.3.0" "swagger-ui-express": "^4.3.0"
}, },
"overrides":{ "overrides": {
"busboy@<1.6.0":"1.6.0" "multer": "1.4.5-lts.1"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^8.2.4", "@nestjs/cli": "^8.2.4",
+4 -3
View File
@@ -32,9 +32,10 @@ export class LoggerMiddleware implements NestMiddleware {
const permissions = jwtDecoded.user.permissions; const permissions = jwtDecoded.user.permissions;
const clienId = jwtDecoded.user.customerId; const clientId = jwtDecoded.user.customerId;
const customer = jwtDecoded.user.customer; const customer = jwtDecoded.user.customer;
const userId = jwtDecoded.user.id; const userId = jwtDecoded.user.id;
const customer_tier = jwtDecoded.user.customer_tier;
await verifyToken(accessToken); await verifyToken(accessToken);
@@ -55,11 +56,11 @@ export class LoggerMiddleware implements NestMiddleware {
if (havePermission) { if (havePermission) {
request.body.info = { request.body.info = {
customer_id: clienId, customer_id: clientId,
user_id: userId, user_id: userId,
customer, customer,
customer_tier,
}; };
next(); next();
} else { } else {
throw new ForbiddenException(); throw new ForbiddenException();
+37
View File
@@ -1,16 +1,52 @@
import { Body, HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { Body, HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Timeout } from '@nestjs/schedule'; import { Timeout } from '@nestjs/schedule';
import CronParser, { CronExpression } from 'cron-parser';
import { InputsClientService } from 'src/clients/inputs/client.service'; import { InputsClientService } from 'src/clients/inputs/client.service';
import { IIdRequest, Info } from 'src/clients/inputs/interfaces'; import { IIdRequest, Info } from 'src/clients/inputs/interfaces';
@Injectable() @Injectable()
export class InputsService { export class InputsService {
constructor(private inputClient: InputsClientService) {} constructor(private inputClient: InputsClientService) {}
secondsInADay = 60 * 60 * 24;
secondsInAnHour = 60 * 60;
adjustInputPayload(payload) { adjustInputPayload(payload) {
return payload?.input_s3 || payload?.input_jdbc; return payload?.input_s3 || payload?.input_jdbc;
} }
getDifferenceInSeconds(date1: Date, date2: Date) {
const diffInMs = Math.abs(date2.getTime() - date1.getTime());
return diffInMs / 1000;
}
validateCron(data) {
const { info, cron } = data;
const { customer_tier } = info;
if (!cron) return;
let interval: CronExpression;
try {
interval = CronParser.parseExpression(cron);
} catch (error) {
throw new HttpException(
'Intervalo de tempo inválido',
HttpStatus.BAD_REQUEST,
);
}
const nextDate = interval.next().toDate();
const afterNextDate = interval.next().toDate();
const secondsApart = this.getDifferenceInSeconds(nextDate, afterNextDate);
if (customer_tier === 'BASIC' && secondsApart < this.secondsInADay) {
throw new HttpException(
'Intervalo de tempo não pode ser inferior a um dia.',
HttpStatus.FORBIDDEN,
);
} else if (secondsApart < this.secondsInAnHour) {
throw new HttpException(
'Intervalo de tempo não pode ser inferior a uma hora.',
HttpStatus.FORBIDDEN,
);
}
}
async create(@Body() data) { async create(@Body() data) {
this.validateCron(data);
try { try {
if ( if (
data.plugin == 'csv' || data.plugin == 'csv' ||
@@ -65,6 +101,7 @@ export class InputsService {
} }
async update(id: string, data, info: Info) { async update(id: string, data, info: Info) {
this.validateCron({ ...data, info });
try { try {
const updateInputResponse: any = await this.inputClient.update({ const updateInputResponse: any = await this.inputClient.update({
id, id,