Skip to content

Commit

Permalink
Merge pull request #234 from andrechristikan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
andrechristikan authored Sep 30, 2022
2 parents 11f30b1 + fd85d51 commit 169ff22
Show file tree
Hide file tree
Showing 12 changed files with 653 additions and 495 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ HTTP_VERSIONING_ENABLE=true
HTTP_VERSION=1

DEBUGGER_HTTP_WRITE_INTO_FILE=false
DEBUGGER_HTTP_WRITE_INTO_CONSOLE=false
DEBUGGER_SYSTEM_WRITE_INTO_FILE=false
DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE=false

MIDDLEWARE_TIMESTAMP_TOLERANCE=3h
MIDDLEWARE_TIMEOUT=30s
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ jobs:
echo HTTP_VERSION="1" >> .env
echo DEBUGGER_HTTP_WRITE_INTO_FILE="false" >> .env
echo DEBUGGER_HTTP_WRITE_INTO_CONSOLE="false" >> .env
echo DEBUGGER_SYSTEM_WRITE_INTO_FILE="false" >> .env
echo DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE="false" >> .env
echo MIDDLEWARE_TIMESTAMP_TOLERANCE="5m" >> .env
echo MIDDLEWARE_TIMEOUT="30s" >> .env
Expand Down
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,70 @@ Detail information about the environment
| KAFKA\_BROKERS | `string` | List of brokers, separator `,` |
| KAFKA\_CONSUMER\_GROUP | `string` | Consumer group |

## Api Key Encryption

> ApiKeyHashed using `sha256` encryption, and then encryption for `dataObject` is `AES256`. Please keep the `secret` keep private.
To do the encryption

1. Concat the `key` and `secret`.

```typescript
const apiKeyString = `${key}:${secret}`;
```

2. Encryption `apiKeyString` with sha256

```typescript
const apiKeyHashed = this.helperHashService.sha256(`${key}:${secret}`);
// e11a023bc0ccf713cb50de9baa5140e59d3d4c52ec8952d9ca60326e040eda54
```

3. Then put the `apiKeyHashed` into `dataObject`

```typescript
const timestamp = this.helperDateService.timestamp();
const data: IAuthApiRequestHashedData = {
key: "qwertyuiop12345zxcvbnmkjh",
timestamp,
hash: apiKeyHashed,
}
```

4. Encryption the `dataObject` with `AES 256`

> These data `encryptionKey`, and `passphrase` can be find in database.

```typescript
const passphrase = 'cuwakimacojulawu'; // <--- IV for encrypt AES 256
const encryptionKey = 'opbUwdiS1FBsrDUoPgZdx';
const apiKeyEncryption = await authApiService.encryptApiKey(
data,
encryptionKey,
passphrase
);
```

5. Last, combine the `key` and `apiKeyEncryption`

```typescript
const xApiKey = `${key}:${apiEncryption}`;
```

6. Send into request. Put the `xApiKey` in request headers

```json
{
"headers": {
"x-api-key": "${xApiKey}",
...
...
...
}
}
```

## Adjust Mongoose Setting

> Just is case, if your mongodb version is < 5
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ack-nestjs-mongoose-kafka",
"version": "2.3.0",
"version": "2.3.1",
"description": "Ack NestJs Mongoose Kafka",
"repository": {
"type": "git",
Expand Down Expand Up @@ -59,7 +59,7 @@
"rollback:user": "nestjs-command remove:user"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.180.0",
"@aws-sdk/client-s3": "^3.181.0",
"@faker-js/faker": "^7.5.0",
"@joi/date": "^2.1.0",
"@nestjs/axios": "^0.1.0",
Expand Down Expand Up @@ -87,14 +87,14 @@
"express-rate-limit": "^6.6.0",
"geolib": "^3.3.3",
"helmet": "^6.0.0",
"joi": "^17.6.1",
"joi": "^17.6.2",
"kafkajs": "^2.2.0",
"moment": "^2.29.4",
"mongoose": "^6.6.2",
"morgan": "^1.10.0",
"nest-winston": "^1.7.0",
"nestjs-command": "^3.1.2",
"nestjs-i18n": "^9.1.10",
"nestjs-i18n": "^9.2.0",
"passport": "^0.6.0",
"passport-headerapikey": "^1.2.2",
"passport-jwt": "^4.0.0",
Expand All @@ -111,7 +111,7 @@
"yarn": "^1.22.19"
},
"devDependencies": {
"@nestjs/cli": "^9.1.3",
"@nestjs/cli": "^9.1.4",
"@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.1.2",
"@types/aws-lambda": "^8.10.106",
Expand All @@ -123,8 +123,8 @@
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.14",
"@types/express-rate-limit": "^6.0.0",
"@types/jest": "^29.0.3",
"@types/lodash": "^4.14.185",
"@types/jest": "^29.1.1",
"@types/lodash": "^4.14.186",
"@types/morgan": "^1.9.3",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
Expand All @@ -136,19 +136,19 @@
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"cspell": "^6.10.1",
"cspell": "^6.11.1",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"husky": "^8.0.1",
"jest": "^29.0.3",
"jest": "^29.1.1",
"prettier": "^2.7.1",
"serverless-dotenv-plugin": "^4.0.2",
"serverless-offline": "^10.3.2",
"serverless-offline": "^11.0.0",
"serverless-plugin-optimize": "^4.2.1-rc.1",
"serverless-plugin-warmup": "^8.0.0",
"supertest": "^6.2.4",
"ts-jest": "^29.0.2",
"ts-jest": "^29.0.3",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"ts-prune": "^0.10.3",
Expand Down
8 changes: 7 additions & 1 deletion src/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ import configs from 'src/configs';
DEBUGGER_HTTP_WRITE_INTO_FILE: Joi.boolean()
.default(false)
.required(),
DEBUGGER_HTTP_WRITE_INTO_CONSOLE: Joi.boolean()
.default(false)
.required(),
DEBUGGER_SYSTEM_WRITE_INTO_FILE: Joi.boolean()
.default(false)
.required(),
DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE: Joi.boolean()
.default(false)
.required(),

MIDDLEWARE_TIMESTAMP_TOLERANCE: Joi.string()
.default('5m')
Expand Down Expand Up @@ -131,7 +137,7 @@ import configs from 'src/configs';
PaginationModule,
ErrorModule,
LoggerModule,
DebuggerModule,
DebuggerModule.register(),
ResponseModule,
RequestModule,
MiddlewareModule,
Expand Down
56 changes: 40 additions & 16 deletions src/common/debugger/debugger.module.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import { Global, Module } from '@nestjs/common';
import { DynamicModule, Global, Module } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import { DebuggerOptionService } from 'src/common/debugger/services/debugger.options.service';
import { DebuggerService } from './services/debugger.service';

@Module({
providers: [DebuggerOptionService, DebuggerService],
exports: [DebuggerOptionService, DebuggerService],
providers: [DebuggerOptionService],
exports: [DebuggerOptionService],
imports: [],
})
export class DebuggerOptionsModule {}

@Global()
@Module({
providers: [DebuggerService],
exports: [DebuggerService],
imports: [
WinstonModule.forRootAsync({
inject: [DebuggerOptionService],
imports: [DebuggerOptionsModule],
useFactory: (debuggerOptionsService: DebuggerOptionService) =>
debuggerOptionsService.createLogger(),
}),
],
})
export class DebuggerModule {}
@Module({})
export class DebuggerModule {
static register(): DynamicModule {
console.log(
'process.env.DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE',
process.env.DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE
);
if (
process.env.DEBUGGER_SYSTEM_WRITE_INTO_CONSOLE === 'true' ||
process.env.DEBUGGER_SYSTEM_WRITE_INTO_FILE === 'true'
) {
return {
module: DebuggerModule,
controllers: [],
providers: [DebuggerService],
exports: [DebuggerService],
imports: [
WinstonModule.forRootAsync({
inject: [DebuggerOptionService],
imports: [DebuggerOptionsModule],
useFactory: (
debuggerOptionsService: DebuggerOptionService
) => debuggerOptionsService.createLogger(),
}),
],
};
}

return {
module: DebuggerModule,
providers: [],
exports: [],
controllers: [],
imports: [],
};
}
}
47 changes: 26 additions & 21 deletions src/common/error/filters/error.http.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ArgumentsHost,
HttpException,
HttpStatus,
Optional,
} from '@nestjs/common';
import { HttpArgumentsHost } from '@nestjs/common/interfaces';
import { ConfigService } from '@nestjs/config';
Expand All @@ -30,8 +31,8 @@ import { IRequestApp } from 'src/common/request/interfaces/request.interface';
@Catch()
export class ErrorHttpFilter implements ExceptionFilter {
constructor(
@Optional() private readonly debuggerService: DebuggerService,
private readonly configService: ConfigService,
private readonly debuggerService: DebuggerService,
private readonly messageService: MessageService,
private readonly httpAdapterHost: HttpAdapterHost,
private readonly helperDateService: HelperDateService
Expand Down Expand Up @@ -69,16 +70,18 @@ export class ErrorHttpFilter implements ExceptionFilter {
const { customLang } = ctx.getRequest<IRequestApp>();

// Debugger
this.debuggerService.error(
request && request.id ? request.id : ErrorHttpFilter.name,
{
description: exception.message,
class: __class,
function: __function,
path: __path,
},
exception
);
try {
this.debuggerService.error(
request && request.id ? request.id : ErrorHttpFilter.name,
{
description: exception.message,
class: __class,
function: __function,
path: __path,
},
exception
);
} catch (err: any) {}

// Restructure
const response = exception.getResponse();
Expand Down Expand Up @@ -161,16 +164,18 @@ export class ErrorHttpFilter implements ExceptionFilter {
)) as string;

// Debugger
this.debuggerService.error(
ErrorHttpFilter.name,
{
description: message,
class: ErrorHttpFilter.name,
function: 'catch',
path: __path,
},
exception
);
try {
this.debuggerService.error(
ErrorHttpFilter.name,
{
description: message,
class: ErrorHttpFilter.name,
function: 'catch',
path: __path,
},
exception
);
} catch (err: any) {}

const responseBody = {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
Expand Down
Loading

0 comments on commit 169ff22

Please sign in to comment.