-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 [EXL-72] support storing code configuration
support storing code configuration
- Loading branch information
tal-rofe
committed
Oct 28, 2022
1 parent
f1c5e53
commit 134f689
Showing
19 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum FileType { | ||
Json, | ||
Yaml, | ||
Js, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
apps/backend/src/modules/user/modules/inline-policies/classes/set-code-configuration.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEnum, IsString, MaxLength } from 'class-validator'; | ||
|
||
import { IsNullable } from '@/decorators/is-nullable.decorator'; | ||
import { FileType } from '@/models/file-type'; | ||
|
||
export class SetCodeConfigurationDto { | ||
@ApiProperty({ type: String, description: 'Code configuration', example: '{ root: true }' }) | ||
@IsString() | ||
@IsNullable() | ||
@MaxLength(1000) | ||
readonly code!: string | null; | ||
|
||
@ApiProperty({ enum: FileType, description: 'The file type' }) | ||
@IsEnum(FileType) | ||
readonly type!: FileType; | ||
} |
9 changes: 9 additions & 0 deletions
9
...odules/user/modules/inline-policies/commands/contracts/set-code-configuration.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { FileType } from '@/models/file-type'; | ||
|
||
export class SetCodeConfigurationContract { | ||
constructor( | ||
public readonly policyId: string, | ||
public readonly code: string | null, | ||
public readonly type: FileType, | ||
) {} | ||
} |
8 changes: 7 additions & 1 deletion
8
apps/backend/src/modules/user/modules/inline-policies/commands/handlers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { DeleteHandler } from './delete.handler'; | ||
import { EditLabelHandler } from './edit-label.handler'; | ||
import { SetCodeConfigurationHandler } from './set-code-configuration.handler'; | ||
import { SetFileListHandler } from './set-file-list.handler'; | ||
|
||
export const CommandHandlers = [EditLabelHandler, DeleteHandler, SetFileListHandler]; | ||
export const CommandHandlers = [ | ||
EditLabelHandler, | ||
DeleteHandler, | ||
SetFileListHandler, | ||
SetCodeConfigurationHandler, | ||
]; |
17 changes: 17 additions & 0 deletions
17
.../modules/user/modules/inline-policies/commands/handlers/set-code-configuration.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { SetCodeConfigurationContract } from '../contracts/set-code-configuration.contract'; | ||
import { parseInput } from '../../utils/parsers'; | ||
|
||
@CommandHandler(SetCodeConfigurationContract) | ||
export class SetCodeConfigurationHandler implements ICommandHandler<SetCodeConfigurationContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: SetCodeConfigurationContract) { | ||
const parsedCodeInput = parseInput(contract.code, contract.type); | ||
|
||
await this.dbInlinePolicyService.setCodeConfiguration(contract.policyId, parsedCodeInput); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apps/backend/src/modules/user/modules/inline-policies/set-code-configuration.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Body, Controller, HttpCode, HttpStatus, Logger, Param, Patch, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './inline-policies.routes'; | ||
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard'; | ||
import { SetCodeConfigurationDto } from './classes/set-code-configuration.dto'; | ||
import { SetCodeConfigurationContract } from './commands/contracts/set-code-configuration.contract'; | ||
|
||
@ApiTags('Inline Policies') | ||
@Controller(Routes.CONTROLLER) | ||
export class SetCodeConfigurationController { | ||
private readonly logger = new Logger(SetCodeConfigurationController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@ApiOperation({ description: "Set policy's configuration using a code" }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ description: 'If successfully set configuration of the policy' }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to set configuration of the policy' }) | ||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Patch(Routes.SET_CODE_CONFIGURATION) | ||
@HttpCode(HttpStatus.OK) | ||
public async setCodeConfiguration( | ||
@CurrentUserId() userId: string, | ||
@Body() setCodeConfigurationDto: SetCodeConfigurationDto, | ||
@Param('policy_id') policyId: string, | ||
): Promise<void> { | ||
this.logger.log( | ||
`Will try to set a policy's configuration with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
await this.commandBus.execute<SetCodeConfigurationContract, void>( | ||
new SetCodeConfigurationContract( | ||
policyId, | ||
setCodeConfigurationDto.code, | ||
setCodeConfigurationDto.type, | ||
), | ||
); | ||
|
||
this.logger.log( | ||
`Successfully set a policy configuration an ID: "${policyId}" for a user with an Id: "${userId}"`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
apps/backend/src/modules/user/modules/inline-policies/utils/parsers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import yaml from 'js-yaml'; | ||
import { VM } from 'vm2'; | ||
|
||
import { FileType } from '@/models/file-type'; | ||
|
||
const nodeVirtualMachine = new VM({ sandbox: { module: {} } }); | ||
|
||
/** | ||
* The function receives a JSON-like input and parse it to an object | ||
* @param input the input to parse | ||
* @returns parsed object | ||
*/ | ||
const parseJson = (input: string) => { | ||
try { | ||
const parsedObject = JSON.parse(input); | ||
|
||
if (typeof parsedObject !== 'object') { | ||
throw new BadRequestException(); | ||
} | ||
|
||
return parsedObject as object; | ||
} catch { | ||
throw new BadRequestException(); | ||
} | ||
}; | ||
|
||
/** | ||
* The function receives a YAML-like input and parse it to an object | ||
* @param input the input to parse | ||
* @returns parsed object | ||
*/ | ||
const parseYaml = (input: string) => { | ||
try { | ||
const parsedObject = yaml.load(input); | ||
|
||
if (typeof parsedObject !== 'object') { | ||
throw new BadRequestException(); | ||
} | ||
|
||
return parsedObject as object; | ||
} catch { | ||
throw new BadRequestException(); | ||
} | ||
}; | ||
|
||
/** | ||
* The function receives a commonJs-exported-like input and parse it to an object | ||
* @param input the input to parse | ||
* @returns parsed object | ||
*/ | ||
const parseJs = (input: string) => { | ||
try { | ||
const parsedObject = nodeVirtualMachine.run(input); | ||
|
||
if (typeof parsedObject !== 'object') { | ||
throw new BadRequestException(); | ||
} | ||
|
||
return parsedObject as object; | ||
} catch { | ||
throw new BadRequestException(); | ||
} | ||
}; | ||
|
||
/** | ||
* The function receives an input (string, or null) and parses it to an object (or null if received null) | ||
* @param input the input to parse | ||
* @param type the representation type of the input | ||
* @returns parsed object | ||
*/ | ||
export const parseInput = (input: string | null, type: FileType) => { | ||
if (input === null) { | ||
return null; | ||
} | ||
|
||
if (type === FileType.Json) { | ||
return parseJson(input); | ||
} | ||
|
||
if (type === FileType.Yaml) { | ||
return parseYaml(input); | ||
} | ||
|
||
return parseJs(input); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.