Skip to content

Commit f52b956

Browse files
authored
Merge pull request #27 from Libertech-FR/backend-feature
implements auth + backends execution
2 parents ef827f7 + 5a1daa1 commit f52b956

19 files changed

+328
-115
lines changed

.env.example

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
LOG_LEVEL="info"
2-
NAME_QUEUE="backend"
3-
SECRET="secret"
1+
# SESAME_LOG_LEVEL="info"
2+
# SESAME_NAME_QUEUE="sesame"
3+
SESAME_JWT_SECRET="zeaezazeaezazaeeazrftrqezfqfqszewfsqddfqsqsqsdqdsqdsqdzsdqzsdqzs"
44

5-
REDIS_URI="redis://sesame-redis:6379"
6-
MONGO_URI="mongodb://sesame-mongodb:27017/sesame"
7-
SWAGGER_PATH="/swagger"
8-
SWAGGER_API="swagger.json"
5+
SESAME_REDIS_URI="redis://sesame-redis:6379"
6+
SESAME_MONGO_URI="mongodb://sesame-mongodb:27017/sesame"

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ APP_PORT = 4002
33
IMG_NAME = "ghcr.io/libertech-fr/sesame-orchestrator"
44
BASE_NAME = "sesame"
55
APP_NAME = "sesame-orchestrator"
6+
PLATFORM = "linux/amd64"
67

78
.DEFAULT_GOAL := help
89
help:
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
import { ConfigService } from '@nestjs/config';
21
import { Queue, QueueEvents } from 'bullmq';
3-
import Redis from 'ioredis';
2+
import { AbstractService, AbstractServiceContext } from './abstract.service';
3+
import { Redis, getRedisConnectionToken } from '@nestjs-modules/ioredis';
4+
import { OnModuleInit } from '@nestjs/common';
5+
import { ConfigService } from '@nestjs/config';
6+
7+
export abstract class AbstractQueueProcessor extends AbstractService implements OnModuleInit {
8+
protected config: ConfigService;
9+
10+
private redis: Redis;
11+
protected _queue: Queue;
12+
public queueEvents: QueueEvents;
13+
14+
public get queue(): Queue {
15+
return this._queue;
16+
}
417

5-
export abstract class AbstractQueueProcessor {
6-
protected readonly queue: Queue;
7-
protected readonly queueEvents: QueueEvents;
8-
public constructor(
9-
protected readonly config: ConfigService,
10-
protected readonly redis: Redis,
11-
) {
12-
this.queue = new Queue(this.config.get('nameQueue'), {
18+
public constructor(context?: AbstractServiceContext) {
19+
super(context);
20+
if (!this.moduleRef) throw new Error('ModuleRef is not defined in ' + this.constructor.name);
21+
}
22+
23+
public async onModuleInit() {
24+
this.config = this.moduleRef.get<ConfigService>(ConfigService, { strict: false });
25+
this.redis = this.moduleRef.get<Redis>(getRedisConnectionToken(), { strict: false });
26+
27+
this._queue = new Queue(this.config.get<string>('application.nameQueue'), {
1328
connection: this.redis,
1429
});
15-
this.queueEvents = new QueueEvents(this.config.get('nameQueue'), {
30+
this.queueEvents = new QueueEvents(this.config.get<string>('application.nameQueue'), {
1631
connection: this.redis,
1732
});
33+
34+
this.queueEvents.on('failed', (errors) => {
35+
this.logger.warn('Queue failed', errors);
36+
});
1837
}
1938
}

src/_common/pipes/dto-validation.pipe.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Injectable,
88
Scope,
99
Inject,
10+
ArgumentMetadata,
1011
} from '@nestjs/common';
1112
import { REQUEST } from '@nestjs/core';
1213
import { Request } from 'express';
@@ -17,6 +18,10 @@ interface ValidationRecursive {
1718

1819
@Injectable({ scope: Scope.REQUEST })
1920
export class DtoValidationPipe extends ValidationPipe {
21+
public async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
22+
return (await super.transform(value, metadata)) || value;
23+
}
24+
2025
public constructor(@Inject(REQUEST) protected readonly request: Request) {
2126
super({
2227
transform: true,

src/app.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Controller, Get, Res } from '@nestjs/common';
22
import { AppService } from './app.service';
33
import { Response } from 'express';
44
import { AbstractController } from '~/_common/abstracts/abstract.controller';
5+
import { Public } from './_common/decorators/public.decorator';
56

7+
@Public()
68
@Controller()
79
export class AppController extends AbstractController {
810
constructor(private readonly appService: AppService) {

src/app.module.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { RedisOptions } from 'ioredis';
1111
import { AppController } from './app.controller';
1212
import { AppService } from './app.service';
1313
import { RequestContextModule } from 'nestjs-request-context';
14+
import { APP_FILTER, APP_GUARD, APP_PIPE } from '@nestjs/core';
15+
import { AuthGuard } from './_common/guards/auth.guard';
16+
import { MongooseValidationFilter } from './_common/filters/mongoose-validation.filter';
17+
import { DtoValidationPipe } from './_common/pipes/dto-validation.pipe';
1418

1519
@Module({
1620
imports: [
@@ -58,6 +62,24 @@ import { RequestContextModule } from 'nestjs-request-context';
5862
ManagementModule.register(),
5963
],
6064
controllers: [AppController],
61-
providers: [AppService],
65+
providers: [
66+
AppService,
67+
{
68+
provide: APP_GUARD,
69+
useClass: AuthGuard,
70+
},
71+
// {
72+
// provide: APP_FILTER,
73+
// useClass: AllExceptionFilter,
74+
// },
75+
{
76+
provide: APP_FILTER,
77+
useClass: MongooseValidationFilter,
78+
},
79+
{
80+
provide: APP_PIPE,
81+
useClass: DtoValidationPipe,
82+
},
83+
],
6284
})
6385
export class AppModule {}

src/config.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface MongoosePlugin {
1313
export interface ConfigInstance {
1414
application: {
1515
logLevel: string;
16+
nameQueue: string;
1617
bodyParser: {
1718
limit: string;
1819
};
@@ -38,13 +39,12 @@ export interface ConfigInstance {
3839
api: string;
3940
options: SwaggerCustomOptions;
4041
};
41-
nameQueue: string;
42-
secret: string;
4342
}
4443

4544
export default (): ConfigInstance => ({
4645
application: {
47-
logLevel: process.env['LOG_LEVEL'] || 'info',
46+
logLevel: process.env['SESAME_LOG_LEVEL'] || 'info',
47+
nameQueue: process.env['SESAME_NAME_QUEUE'] || 'sesame',
4848
bodyParser: {
4949
limit: '500mb',
5050
},
@@ -63,14 +63,14 @@ export default (): ConfigInstance => ({
6363
},
6464
},
6565
ioredis: {
66-
uri: process.env['REDIS_URI'] || 'redis://localhost:6379/0',
66+
uri: process.env['SESAME_REDIS_URI'] || 'redis://localhost:6379/0',
6767
options: {
6868
showFriendlyErrorStack: true,
6969
maxRetriesPerRequest: null,
7070
},
7171
},
7272
mongoose: {
73-
uri: process.env['MONGO_URI'] || 'mongodb://localhost:27017/backend',
73+
uri: process.env['SESAME_MONGO_URI'] || 'mongodb://localhost:27017/backend',
7474
options: {
7575
directConnection: true,
7676
},
@@ -88,15 +88,13 @@ export default (): ConfigInstance => ({
8888
/**
8989
* @see https://randomkeygen.com/
9090
*/
91-
secret: process.env['JWT_SECRET'],
91+
secret: process.env['SESAME_JWT_SECRET'],
9292
// jwksUri: 'http://127.0.0.1:2000/jwks',
9393
},
9494
},
95-
nameQueue: process.env['NAME_QUEUE'] || 'backend',
96-
secret: process.env['SECRET'] || 'mySecret',
9795
swagger: {
98-
path: process.env['SWAGGER_PATH'] || '/swagger',
99-
api: process.env['SWAGGER_API'] || '/swagger/json',
96+
path: '/swagger',
97+
api: '/swagger/json',
10098
options: {
10199
swaggerOptions: {
102100
persistAuthorization: true,

src/core/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class AuthService extends AbstractService implements OnModuleInit {
100100
access_token: string;
101101
refresh_token?: string;
102102
}> {
103-
const scopes = ['teaket'];
103+
const scopes = ['sesame'];
104104
if (refresh_token === false) scopes.push('offline');
105105
const jwtid = `${identity._id}_${randomBytes(16).toString('hex')}`;
106106
const access_token = this.jwtService.sign(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IsEnum, IsNotEmpty, IsObject, IsOptional } from 'class-validator';
2+
import { ActionType } from '../_enum/action-type.enum';
3+
import { ApiProperty } from '@nestjs/swagger';
4+
5+
export class ExecuteJobDto {
6+
@IsNotEmpty()
7+
@IsEnum(ActionType)
8+
@ApiProperty({ type: String })
9+
public action: ActionType;
10+
11+
@IsOptional()
12+
@IsObject()
13+
@ApiProperty({ type: Object })
14+
public payload: Record<string | number, any>;
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export enum ActionType {
2+
LIST_BACKENDS = 'LIST_BACKENDS',
3+
PING_TARGET = 'PING_TARGET',
4+
IDENTITY_CREATE = 'IDENTITY_CREATE',
5+
IDENTITY_UPDATE = 'IDENTITY_UPDATE',
6+
IDENTITY_DELETE = 'IDENTITY_DELETE',
7+
IDENTITY_PASSWORD_RESET = 'IDENTITY_PASSWORD_RESET',
8+
IDENTITY_PASSWORD_CHANGE = 'IDENTITY_PASSWORD_CHANGE',
9+
}

0 commit comments

Comments
 (0)