Compare commits

...
8 Commits
Author SHA1 Message Date
arthur simas cc3b34d87e Merge pull request #119 from dadosfera/fix/error-handling
FIX: added exception filter to catch unhandled errors
2022-07-07 20:28:44 -03:00
Arthur Simas 88bc7fe625 FIX: added exception filter to catch unhandled errors 2022-07-07 20:24:27 -03:00
Gabriel Amorim 137f129e13 Merge pull request #118 from dadosfera/conditional-apm-import
FIX: Conditional apm import
2022-07-07 20:22:55 -03:00
Gabriel Rosa f49c5ac1c1 FIX: conditionally importing apm 2022-07-07 20:04:42 -03:00
Gabriel Rosa d288a4f357 FIX: conditionally importing apm 2022-07-07 18:18:53 -03:00
Gabriel Rosa 9f009309cb FIX: conditionally importing apm 2022-07-07 17:51:28 -03:00
arthur simas 366987399e Merge pull request #117 from dadosfera/refactor/protospack-v2
FEAT: protospack-v2 updated
2022-07-07 16:05:00 -03:00
Arthur Simas 4c49dbc0ea FEAT: protospack-v2 updated 2022-07-07 15:00:59 -03:00
9 changed files with 51 additions and 69 deletions
+7 -7
View File
@@ -36,7 +36,7 @@
"passport-hubspot-oauth2": "^1.0.3", "passport-hubspot-oauth2": "^1.0.3",
"passport-mailchimp": "^1.1.0", "passport-mailchimp": "^1.1.0",
"protospack": "2.5.1", "protospack": "2.5.1",
"protospack-v2": "1.1.1", "protospack-v2": "3.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
@@ -8997,9 +8997,9 @@
} }
}, },
"node_modules/protospack-v2": { "node_modules/protospack-v2": {
"version": "1.1.1", "version": "3.0.0",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-1.1.1.tgz", "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.0.0.tgz",
"integrity": "sha512-BqIJSORaqauAT9kF4/cP/uJNCyoT3KxkVkamd7SWARMxnywFB/mpYJg/VDGcyvVOAU4IOLyjnXkrvRuylE0Q2w==", "integrity": "sha512-GEFbf8+vVaq/lA28K4nFf7bOSlghQJn/aeVnrYzh4YJUaa1+ccNz3uQs9p5OGbfjt2u/fFE5rHGEBUtueAA8KA==",
"dependencies": { "dependencies": {
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
"ts-proto": "^1.112.2" "ts-proto": "^1.112.2"
@@ -17198,9 +17198,9 @@
} }
}, },
"protospack-v2": { "protospack-v2": {
"version": "1.1.1", "version": "3.0.0",
"resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-1.1.1.tgz", "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.0.0.tgz",
"integrity": "sha512-BqIJSORaqauAT9kF4/cP/uJNCyoT3KxkVkamd7SWARMxnywFB/mpYJg/VDGcyvVOAU4IOLyjnXkrvRuylE0Q2w==", "integrity": "sha512-GEFbf8+vVaq/lA28K4nFf7bOSlghQJn/aeVnrYzh4YJUaa1+ccNz3uQs9p5OGbfjt2u/fFE5rHGEBUtueAA8KA==",
"requires": { "requires": {
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
"ts-proto": "^1.112.2" "ts-proto": "^1.112.2"
+1 -1
View File
@@ -51,7 +51,7 @@
"passport-hubspot-oauth2": "^1.0.3", "passport-hubspot-oauth2": "^1.0.3",
"passport-mailchimp": "^1.1.0", "passport-mailchimp": "^1.1.0",
"protospack": "2.5.1", "protospack": "2.5.1",
"protospack-v2": "1.1.1", "protospack-v2": "3.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
@@ -0,0 +1,22 @@
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
import { Response } from 'express';
import ErrorBuilder from '../utils/ErrorBuilder';
@Catch(Error)
export class GrpcToHttpExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
// return this exception directly if is already of type ErrorBuilder,
// else transform it using the ErrorBuilder
const err =
exception.constructor.name === ErrorBuilder.name
? exception
: new ErrorBuilder(exception.details);
const { statusCode } = err.response;
return response.status(statusCode).json(err.response);
}
}
+1 -4
View File
@@ -1,20 +1,17 @@
import 'elastic-apm-node/start'; if (process.env.ENV !== 'local') require('elastic-apm-node/start');
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { writeFileSync } from 'fs';
import helmet from 'helmet'; import helmet from 'helmet';
import { DadosferaLogger } from 'dadosfera-logs'; import { DadosferaLogger } from 'dadosfera-logs';
import documentEmpty from '../swagger_empty.json'; import documentEmpty from '../swagger_empty.json';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
DadosferaLogger.setupLogger({ DadosferaLogger.setupLogger({
serviceName: 'maestro', serviceName: 'maestro',
serviceEnvironment: process.env.ENV, serviceEnvironment: process.env.ENV,
}); });
const logger = new DadosferaLogger().logger; const logger = new DadosferaLogger().logger;
const orginalWinstonLog = logger.log.bind(logger); const orginalWinstonLog = logger.log.bind(logger);
+3
View File
@@ -6,6 +6,7 @@ import {
HttpCode, HttpCode,
HttpStatus, HttpStatus,
Inject, Inject,
UseFilters,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
import { import {
@@ -22,8 +23,10 @@ import {
import { AuthClientService } from './auth.service'; import { AuthClientService } from './auth.service';
import { DadosferaLogger } from 'dadosfera-logs'; import { DadosferaLogger } from 'dadosfera-logs';
import { GrpcToHttpExceptionFilter } from '../../error/grpc-to-http-exception.filter';
@ApiTags('Auth') @ApiTags('Auth')
@UseFilters(new GrpcToHttpExceptionFilter())
@Controller('auth') @Controller('auth')
export class AuthController { export class AuthController {
logger: any; logger: any;
+12 -30
View File
@@ -1,35 +1,23 @@
import { OnModuleInit, Inject, Injectable } from '@nestjs/common'; import { OnModuleInit, Inject, Injectable } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices'; import { ClientGrpc } from '@nestjs/microservices';
import { DadosferaLogger } from 'dadosfera-logs'; import { DadosferaLogger } from 'dadosfera-logs';
import { lastValueFrom } from 'rxjs';
import { ProtoServices } from 'protospack-v2/dist/lib/Duc'; import { ProtoServices } from 'protospack-v2/dist/lib/Duc';
import { AuthProtoService as AuthServiceInterface } from 'protospack-v2/dist/lib/Duc/interfaces/write-service'; import { AuthProtoService as AuthServiceInterface } from 'protospack-v2/dist/lib/Duc/interfaces/write-service';
import { import {
AuthGetPublicKeysResponse,
AuthSignInRequest, AuthSignInRequest,
AuthSignInResponse,
AuthRefreshAccessTokenRequest, AuthRefreshAccessTokenRequest,
AuthRefreshAccessTokenResponse,
AuthEnableTotpMfaRequest, AuthEnableTotpMfaRequest,
AuthEnableTotpMfaResponse,
AuthDisableTotpMfaRequest, AuthDisableTotpMfaRequest,
AuthDisableTotpMfaResponse,
AuthDismissTotpMfaRequest, AuthDismissTotpMfaRequest,
AuthDismissTotpMfaResponse,
AuthVerifyTotpMfaRequest, AuthVerifyTotpMfaRequest,
AuthVerifyTotpMfaResponse,
AuthChangePasswordRequest, AuthChangePasswordRequest,
AuthChangePasswordResponse,
AuthResetPasswordRequest, AuthResetPasswordRequest,
AuthResetPasswordResponse,
AuthVerifyResetPasswordCodeRequest, AuthVerifyResetPasswordCodeRequest,
AuthVerifyResetPasswordCodeResponse,
AuthConfirmResetPasswordRequest, AuthConfirmResetPasswordRequest,
AuthConfirmResetPasswordResponse,
} from 'protospack-v2/dist/lib/Duc/interfaces/messages'; } from 'protospack-v2/dist/lib/Duc/interfaces/messages';
import grpcHandler from '../../utils/grpcHandler';
@Injectable() @Injectable()
export class AuthClientService implements OnModuleInit { export class AuthClientService implements OnModuleInit {
logger: any; logger: any;
@@ -52,15 +40,13 @@ export class AuthClientService implements OnModuleInit {
async getPublicKeys() { async getPublicKeys() {
this.logger.info('GetPublicKeys'); this.logger.info('GetPublicKeys');
return grpcHandler<AuthGetPublicKeysResponse>( return lastValueFrom(this.authService.AuthGetPublicKeys({}));
this.authService.AuthGetPublicKeys({}),
);
} }
async signIn({ username, password, totp }: AuthSignInRequest) { async signIn({ username, password, totp }: AuthSignInRequest) {
this.logger.info('SignIn'); this.logger.info('SignIn');
return grpcHandler<AuthSignInResponse>( return lastValueFrom(
this.authService.AuthSignIn({ username, password, totp }), this.authService.AuthSignIn({ username, password, totp }),
); );
} }
@@ -68,7 +54,7 @@ export class AuthClientService implements OnModuleInit {
async refreshAccessToken({ refreshToken }: AuthRefreshAccessTokenRequest) { async refreshAccessToken({ refreshToken }: AuthRefreshAccessTokenRequest) {
this.logger.info('RefreshAccessToken'); this.logger.info('RefreshAccessToken');
return grpcHandler<AuthRefreshAccessTokenResponse>( return lastValueFrom(
this.authService.AuthRefreshAccessToken({ refreshToken }), this.authService.AuthRefreshAccessToken({ refreshToken }),
); );
} }
@@ -80,7 +66,7 @@ export class AuthClientService implements OnModuleInit {
}: AuthChangePasswordRequest) { }: AuthChangePasswordRequest) {
this.logger.info('ChangePassword'); this.logger.info('ChangePassword');
return grpcHandler<AuthChangePasswordResponse>( return lastValueFrom(
this.authService.AuthChangePassword({ this.authService.AuthChangePassword({
accessToken, accessToken,
oldPassword, oldPassword,
@@ -92,9 +78,7 @@ export class AuthClientService implements OnModuleInit {
async resetPassword({ username }: AuthResetPasswordRequest) { async resetPassword({ username }: AuthResetPasswordRequest) {
this.logger.info('resetPassword'); this.logger.info('resetPassword');
return grpcHandler<AuthResetPasswordResponse>( return lastValueFrom(this.authService.AuthResetPassword({ username }));
this.authService.AuthResetPassword({ username }),
);
} }
async verifyResetPasswordCode({ async verifyResetPasswordCode({
@@ -103,7 +87,7 @@ export class AuthClientService implements OnModuleInit {
}: AuthVerifyResetPasswordCodeRequest) { }: AuthVerifyResetPasswordCodeRequest) {
this.logger.info('verifyResetPasswordCode'); this.logger.info('verifyResetPasswordCode');
return grpcHandler<AuthVerifyResetPasswordCodeResponse>( return lastValueFrom(
this.authService.AuthVerifyResetPasswordCode({ username, code }), this.authService.AuthVerifyResetPasswordCode({ username, code }),
); );
} }
@@ -115,7 +99,7 @@ export class AuthClientService implements OnModuleInit {
}: AuthConfirmResetPasswordRequest) { }: AuthConfirmResetPasswordRequest) {
this.logger.info('confirmResetPassword'); this.logger.info('confirmResetPassword');
return grpcHandler<AuthConfirmResetPasswordResponse>( return lastValueFrom(
this.authService.AuthConfirmResetPassword({ this.authService.AuthConfirmResetPassword({
username, username,
code, code,
@@ -127,7 +111,7 @@ export class AuthClientService implements OnModuleInit {
async enableTotpMFA({ accessToken, password }: AuthEnableTotpMfaRequest) { async enableTotpMFA({ accessToken, password }: AuthEnableTotpMfaRequest) {
this.logger.info('enableTotpMFA'); this.logger.info('enableTotpMFA');
return grpcHandler<AuthEnableTotpMfaResponse>( return lastValueFrom(
this.authService.AuthEnableTotpMfa({ accessToken, password }), this.authService.AuthEnableTotpMfa({ accessToken, password }),
); );
} }
@@ -135,7 +119,7 @@ export class AuthClientService implements OnModuleInit {
async disableTotpMFA({ accessToken, password }: AuthDisableTotpMfaRequest) { async disableTotpMFA({ accessToken, password }: AuthDisableTotpMfaRequest) {
this.logger.info('disableTotpMFA'); this.logger.info('disableTotpMFA');
return grpcHandler<AuthDisableTotpMfaResponse>( return lastValueFrom(
this.authService.AuthDisableTotpMfa({ accessToken, password }), this.authService.AuthDisableTotpMfa({ accessToken, password }),
); );
} }
@@ -143,15 +127,13 @@ export class AuthClientService implements OnModuleInit {
async dismissTotpMFA({ accessToken }: AuthDismissTotpMfaRequest) { async dismissTotpMFA({ accessToken }: AuthDismissTotpMfaRequest) {
this.logger.info('dismissTotpMFA'); this.logger.info('dismissTotpMFA');
return grpcHandler<AuthDismissTotpMfaResponse>( return lastValueFrom(this.authService.AuthDismissTotpMfa({ accessToken }));
this.authService.AuthDismissTotpMfa({ accessToken }),
);
} }
async verifyTotp({ accessToken, totp }: AuthVerifyTotpMfaRequest) { async verifyTotp({ accessToken, totp }: AuthVerifyTotpMfaRequest) {
this.logger.info('disableTotpMFA'); this.logger.info('disableTotpMFA');
return grpcHandler<AuthVerifyTotpMfaResponse>( return lastValueFrom(
this.authService.AuthVerifyTotpMfa({ accessToken, totp }), this.authService.AuthVerifyTotpMfa({ accessToken, totp }),
); );
} }
@@ -5,15 +5,12 @@ import {
Injectable, Injectable,
} from '@nestjs/common'; } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices'; import { ClientGrpc } from '@nestjs/microservices';
import { lastValueFrom } from 'rxjs';
import { ProtoServices } from 'protospack-v2/dist/lib/Duc'; import { ProtoServices } from 'protospack-v2/dist/lib/Duc';
import { PermissionsProtoService as PermissionsServiceInterface } from 'protospack-v2/dist/lib/Duc/interfaces/write-service'; import { PermissionsProtoService as PermissionsServiceInterface } from 'protospack-v2/dist/lib/Duc/interfaces/write-service';
import { import { InjectPermissionsRequest } from 'protospack-v2/dist/lib/Duc/interfaces/messages';
Empty,
InjectPermissionsRequest,
} from 'protospack-v2/dist/lib/Duc/interfaces/messages';
import { DadosferaLogger } from 'dadosfera-logs'; import { DadosferaLogger } from 'dadosfera-logs';
import grpcHandler from '../../utils/grpcHandler';
import { Permissions } from '../../authentication/permissions.enum'; import { Permissions } from '../../authentication/permissions.enum';
import ErrorBuilder from '../../utils/ErrorBuilder'; import ErrorBuilder from '../../utils/ErrorBuilder';
@@ -66,7 +63,7 @@ export class PermissionsClientService
async injectPermissions({ permissions }: InjectPermissionsRequest) { async injectPermissions({ permissions }: InjectPermissionsRequest) {
this.logger.info('InjectPermissions'); this.logger.info('InjectPermissions');
return grpcHandler<Empty>( return lastValueFrom(
this.permissionsService.InjectPermissions({ permissions }), this.permissionsService.InjectPermissions({ permissions }),
); );
} }
+2 -2
View File
@@ -3,7 +3,7 @@ import { RpcException } from '@nestjs/microservices';
import ErrorCodes from './errorCodes'; import ErrorCodes from './errorCodes';
function enrichErrorCode(code: string) { export function EnrichErrorCode(code: string) {
switch (code) { switch (code) {
case ErrorCodes.AUTH.WRONG_CREDENTIALS: case ErrorCodes.AUTH.WRONG_CREDENTIALS:
return { return {
@@ -138,7 +138,7 @@ export default class ErrorBuilder extends HttpException {
logger.log(code); logger.log(code);
} }
const { statusCode, message, error, code: rCode } = enrichErrorCode(code); const { statusCode, message, error, code: rCode } = EnrichErrorCode(code);
super({ statusCode, message, error, code: rCode }, statusCode); super({ statusCode, message, error, code: rCode }, statusCode);
this.code = code; this.code = code;
} }
-19
View File
@@ -1,19 +0,0 @@
import { Logger } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { from } from 'rxjs';
import ErrorBuilder from './ErrorBuilder';
const logger = new Logger();
export default async function grpcHandler<T>(method: Promise<T>) {
return new Promise<T>((resolve, reject) => {
from(method).subscribe({
next: resolve,
error: reject,
complete: () => logger.log('done'),
});
}).catch((err: RpcException) => {
throw new ErrorBuilder(err);
});
}