Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/move to fastify #3618

Merged
merged 18 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
"dependencies": {
"@guardian/common": "^2.24.1",
"@guardian/interfaces": "^2.24.1",
"@fastify/formbody": "^7.4.0",
"@fastify/multipart": "^8.2.0",
"@fastify/static": "^7.0.0",
"@nestjs/common": "^9.4.1",
"@nestjs/core": "^9.4.1",
"@nestjs/jwt": "^10.0.3",
"@nestjs/microservices": "^9.4.1",
"@nestjs/platform-express": "^9.4.2",
"@nestjs/platform-fastify": "^9.4.2",
"@nestjs/swagger": "^6.3.0",
"@types/express-fileupload": "^1.4.1",
"async-mutex": "^0.4.0",
Expand Down
5 changes: 2 additions & 3 deletions api-gateway/src/api/service/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { InternalServerErrorDTO } from '../../middlewares/validation/schemas/err
import { ApplicationEnvironment } from '../../environment.js';
import { CACHE } from '../../constants/index.js';
import { UseCache } from '../../helpers/decorators/cache.js';
import { Auth } from '../../auth/auth.decorator.js';

/**
* User account route
Expand Down Expand Up @@ -222,12 +223,11 @@ export class AccountApi {
$ref: getSchemaPath(InternalServerErrorDTO)
}
})
// @UseGuards(AuthGuard)
@HttpCode(HttpStatus.OK)
@Get()
@UseCache()
@Auth(UserRole.STANDARD_REGISTRY)
async getAllAccounts(@Req() req): Promise<AccountsResponseDTO[]> {
// await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const authHeader = req.headers.authorization;
const token = authHeader?.split(' ')[1];
const users = new Users();
Expand All @@ -242,7 +242,6 @@ export class AccountApi {
throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
}
try {
await checkPermission(UserRole.STANDARD_REGISTRY)(user);
return await users.getAllUserAccounts() as any[];
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
Expand Down
22 changes: 13 additions & 9 deletions api-gateway/src/api/service/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ApiSecurity,
ApiTags
} from '@nestjs/swagger';
import { checkPermission } from '../../auth/authorization-helper.js';
import { UserRole } from '@guardian/interfaces';
import {
FilterDocumentsDTO,
Expand All @@ -27,6 +26,7 @@ import {
FilterToolsDTO,
CompareToolsDTO
} from '../../middlewares/validation/schemas/index.js';
import { Auth } from '../../auth/auth.decorator.js';

const ONLY_SR = ' Only users with the Standard Registry role are allowed to make the request.'

Expand Down Expand Up @@ -69,8 +69,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async searchPolicies(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const policyId = body ? body.policyId : null;
const user = req.user;
Expand Down Expand Up @@ -140,8 +140,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async comparePolicies(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const policyId1 = body ? body.policyId1 : null;
const policyId2 = body ? body.policyId2 : null;
Expand Down Expand Up @@ -220,8 +220,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async compareModules(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const moduleId1 = body ? body.moduleId1 : null;
const moduleId2 = body ? body.moduleId2 : null;
Expand Down Expand Up @@ -290,8 +290,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async compareSchemas(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const schemaId1 = body ? body.schemaId1 : null;
const schemaId2 = body ? body.schemaId2 : null;
Expand Down Expand Up @@ -352,6 +352,7 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER)
async compareDocuments(@Body() body, @Req() req): Promise<any> {
const guardians = new Guardians();
const documentId1 = body ? body.documentId1 : null;
Expand Down Expand Up @@ -436,6 +437,7 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER)
async compareTools(@Body() body, @Req() req): Promise<any> {
const guardians = new Guardians();
const toolId1 = body ? body.toolId1 : null;
Expand Down Expand Up @@ -524,8 +526,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async comparePoliciesExport(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const type = req.query ? req.query.type : null;
const policyId1 = body ? body.policyId1 : null;
Expand Down Expand Up @@ -603,8 +605,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async compareModulesExport(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const type = req.query ? req.query.type : null;
const moduleId1 = body ? body.moduleId1 : null;
Expand Down Expand Up @@ -674,8 +676,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async compareSchemasExport(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const type = req.query ? req.query.type : null;
const schemaId1 = body ? body.schemaId1 : null;
Expand Down Expand Up @@ -737,6 +739,7 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER)
async compareDocumentsExport(@Body() body, @Req() req): Promise<any> {
const guardians = new Guardians();
const type = req.query ? req.query.type : null;
Expand Down Expand Up @@ -821,6 +824,7 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY, UserRole.AUDITOR, UserRole.USER)
async compareToolsExport(@Body() body, @Req() req): Promise<any> {
const guardians = new Guardians();
const type = req.query ? req.query.type : null;
Expand Down Expand Up @@ -896,8 +900,8 @@ export class AnalyticsApi {
type: InternalServerErrorDTO
})
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async searchBlocks(@Body() body, @Req() req): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
const guardians = new Guardians();
const id = body ? body.id : null;
const config = body ? body.config : null;
Expand Down
18 changes: 9 additions & 9 deletions api-gateway/src/api/service/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {
Post,
Req,
Response,
UploadedFiles,
UseInterceptors,
} from '@nestjs/common';
import { checkPermission } from '../../auth/authorization-helper.js';
import {
ApiExtraModels,
ApiInternalServerErrorResponse,
Expand All @@ -32,7 +30,9 @@ import { InternalServerErrorDTO } from '../../middlewares/validation/schemas/err
import { ApiImplicitQuery } from '@nestjs/swagger/dist/decorators/api-implicit-query.decorator.js';
import { ArtifactDTOItem } from '../../middlewares/validation/schemas/artifacts.js';
import { ApiImplicitParam } from '@nestjs/swagger/dist/decorators/api-implicit-param.decorator.js';
import { FilesInterceptor } from '@nestjs/platform-express';
import { Auth } from '../../auth/auth.decorator.js';
import { AnyFilesInterceptor } from '../../helpers/interceptors/multipart.js';
import { UploadedFiles } from '../../helpers/decorators/file.js';

@Controller('artifacts')
@ApiTags('artifacts')
Expand Down Expand Up @@ -101,8 +101,8 @@ export class ArtifactApi {
})
@ApiExtraModels(ArtifactDTOItem, InternalServerErrorDTO)
@HttpCode(HttpStatus.OK)
@Auth(UserRole.STANDARD_REGISTRY)
async getArtifacts(@Req() req, @Response() res): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
try {
const guardians = new Guardians();
const options: any = {
Expand All @@ -119,7 +119,7 @@ export class ArtifactApi {
options.pageSize = req.query.pageSize;
}
const { artifacts, count } = await guardians.getArtifacts(options);
return res.setHeader('X-Total-Count', count).json(artifacts);
return res.header('X-Total-Count', count).send(artifacts);
} catch (error) {
new Logger().error(error, ['API_GATEWAY']);
throw error;
Expand Down Expand Up @@ -181,13 +181,13 @@ export class ArtifactApi {
}
})
@ApiExtraModels(ArtifactDTOItem, InternalServerErrorDTO)
@UseInterceptors(FilesInterceptor('artifacts'))
@UseInterceptors(AnyFilesInterceptor())
@HttpCode(HttpStatus.CREATED)
@Auth(UserRole.STANDARD_REGISTRY)
async uploadArtifacts(@Req() req, @UploadedFiles() files): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
try {
if (!files) {
throw new HttpException('There are no files to upload', HttpStatus.UNPROCESSABLE_ENTITY)
throw new HttpException('There are no files to upload', HttpStatus.BAD_REQUEST)
}
const owner = req.user.did;
const parentId = req.params.parentId;
Expand Down Expand Up @@ -245,8 +245,8 @@ export class ArtifactApi {
})
@ApiExtraModels(ArtifactDTOItem, InternalServerErrorDTO)
@HttpCode(HttpStatus.NO_CONTENT)
@Auth(UserRole.STANDARD_REGISTRY)
async deleteArtifact(@Req() req, @Response() res): Promise<any> {
await checkPermission(UserRole.STANDARD_REGISTRY)(req.user);
try {
const guardian = new Guardians();
await guardian.deleteArtifact(req.params.artifactId, req.user.did)
Expand Down
Loading
Loading