-
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: 🔥 support inline policies API
support inline policies API
- Loading branch information
Tal Rofe
committed
Jun 7, 2022
1 parent
d5e8f7a
commit 6f070ce
Showing
37 changed files
with
519 additions
and
29 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
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,6 @@ | ||
export enum PolicyLibrary { | ||
ESLINT = 'ESLINT', | ||
STYLELINT = 'STYLELINT', | ||
PRETTIER = 'PRETTIER', | ||
INFLINT = 'INFLINT', | ||
} |
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
85 changes: 85 additions & 0 deletions
85
apps/backend/src/modules/database/inline-policy.service.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,85 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
import { PolicyLibrary } from '@/models/policy-library'; | ||
|
||
import { PrismaService } from './prisma.service'; | ||
|
||
@Injectable() | ||
export class DBInlinePolicyService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
public async createInlinePolicy(groupId: string, label: string, library: PolicyLibrary) { | ||
const createdInlinePolicy = await this.prisma.inlinePolicy.create({ | ||
data: { groupId, label, library }, | ||
select: { id: true }, | ||
}); | ||
|
||
return createdInlinePolicy.id; | ||
} | ||
|
||
public async deleteInlinePolicy(inlinePolicyId: string) { | ||
await this.prisma.inlinePolicy.delete({ where: { id: inlinePolicyId } }); | ||
} | ||
|
||
public async updateConfiguration(inlinePolicyId: string, configuration: Record<string, unknown>) { | ||
await this.prisma.inlinePolicy.update({ | ||
where: { id: inlinePolicyId }, | ||
data: { configuration: configuration as Prisma.JsonObject }, | ||
}); | ||
} | ||
|
||
public async doesInlinePolicyBelongUser(inlinePolicyId: string, userId: string) { | ||
const inlinePolicyDB = await this.prisma.inlinePolicy.findFirst({ | ||
where: { id: inlinePolicyId, group: { userId } }, | ||
}); | ||
|
||
return inlinePolicyDB !== null; | ||
} | ||
|
||
public async addRule(inlinePolicyId: string, rule: Record<string, unknown>) { | ||
const inlinePolicyDB = await this.prisma.inlinePolicy.findFirst({ | ||
where: { id: inlinePolicyId }, | ||
select: { rules: true }, | ||
rejectOnNotFound: true, | ||
}); | ||
|
||
let newInlinePolicyRules: Prisma.JsonObject; | ||
|
||
if (!inlinePolicyDB.rules) { | ||
newInlinePolicyRules = rule as Prisma.JsonObject; | ||
} else { | ||
newInlinePolicyRules = { | ||
...(inlinePolicyDB.rules as Prisma.JsonObject), | ||
...rule, | ||
} as Prisma.JsonObject; | ||
} | ||
|
||
await this.prisma.inlinePolicy.update({ | ||
where: { id: inlinePolicyId }, | ||
data: { rules: newInlinePolicyRules }, | ||
}); | ||
} | ||
|
||
public async removeRule(inlinePolicyId: string, ruleName: string) { | ||
const inlinePolicyDB = await this.prisma.inlinePolicy.findFirst({ | ||
where: { id: inlinePolicyId }, | ||
select: { rules: true }, | ||
rejectOnNotFound: true, | ||
}); | ||
|
||
if (!inlinePolicyDB.rules) { | ||
return; | ||
} | ||
|
||
const rulesWithoutRule = { | ||
...(inlinePolicyDB.rules as Prisma.JsonObject), | ||
[ruleName]: undefined, | ||
}; | ||
|
||
await this.prisma.inlinePolicy.update({ | ||
where: { id: inlinePolicyId }, | ||
data: { rules: rulesWithoutRule }, | ||
}); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
apps/backend/src/modules/user/modules/inline-policies/add-rule.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,40 @@ | ||
import { Body, Controller, Logger, Param, Post, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
|
||
import Routes from './inline-policies.routes'; | ||
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard'; | ||
import { AddRuleDto } from './classes/add-rule.dto'; | ||
import { AddRuleContract } from './commands/contracts/add-rule.contract'; | ||
|
||
@Controller(Routes.CONTROLLER) | ||
export class AddRuleController { | ||
private readonly logger = new Logger(AddRuleController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Post(Routes.ADD_RULE) | ||
public async addRule( | ||
@Param('policy_id') policyId: string, | ||
@Body() addRuleDto: AddRuleDto, | ||
): Promise<void> { | ||
this.logger.log(`Will try to add rule for an inline policy with an Id: "${policyId}"`); | ||
|
||
await this.commandBus.execute<AddRuleContract, void>(new AddRuleContract(policyId, addRuleDto.rule)); | ||
|
||
this.logger.log(`Successfully added a rule for an inline policy Id: "${policyId}"`); | ||
} | ||
|
||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Post(Routes.EDIT_RULE) | ||
public async editRule( | ||
@Param('policy_id') policyId: string, | ||
@Body() editRuleDto: AddRuleDto, | ||
): Promise<void> { | ||
this.logger.log(`Will try to edit rule for an inline policy with an Id: "${policyId}"`); | ||
|
||
await this.commandBus.execute<AddRuleContract, void>(new AddRuleContract(policyId, editRuleDto.rule)); | ||
|
||
this.logger.log(`Successfully edited a rule for an inline policy Id: "${policyId}"`); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/modules/user/modules/inline-policies/classes/add-rule.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,6 @@ | ||
import { IsJSON } from 'class-validator'; | ||
|
||
export class AddRuleDto { | ||
@IsJSON() | ||
readonly rule!: string; | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/backend/src/modules/user/modules/inline-policies/classes/create-inline.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,12 @@ | ||
import { IsEnum, IsString, MinLength } from 'class-validator'; | ||
|
||
import { PolicyLibrary } from '@/models/policy-library'; | ||
|
||
export class CreateInlineDto { | ||
@IsString() | ||
@MinLength(1) | ||
readonly label!: string; | ||
|
||
@IsEnum(PolicyLibrary) | ||
readonly library!: PolicyLibrary; | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/backend/src/modules/user/modules/inline-policies/classes/remove-rule.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,7 @@ | ||
import { IsString, MinLength } from 'class-validator'; | ||
|
||
export class RemoveRuleDto { | ||
@IsString() | ||
@MinLength(1) | ||
readonly ruleName!: string; | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/backend/src/modules/user/modules/inline-policies/classes/update-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,6 @@ | ||
import { IsJSON } from 'class-validator'; | ||
|
||
export class UpdateConfigurationDto { | ||
@IsJSON() | ||
readonly configuration!: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
.../backend/src/modules/user/modules/inline-policies/commands/contracts/add-rule.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,3 @@ | ||
export class AddRuleContract { | ||
constructor(public readonly policyId: string, public readonly rule: string) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
...end/src/modules/user/modules/inline-policies/commands/contracts/delete-inline.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,3 @@ | ||
export class DeleteInlineContract { | ||
constructor(public readonly policyId: string) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ckend/src/modules/user/modules/inline-policies/commands/contracts/remove-rule.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,3 @@ | ||
export class RemoveRuleContract { | ||
constructor(public readonly policyId: string, public readonly ruleName: string) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../modules/user/modules/inline-policies/commands/contracts/update-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,3 @@ | ||
export class UpdateConfigurationContract { | ||
constructor(public readonly policyId: string, public readonly configuration: string) {} | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/backend/src/modules/user/modules/inline-policies/commands/handlers/add-rule.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,16 @@ | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { AddRuleContract } from '../contracts/add-rule.contract'; | ||
|
||
@CommandHandler(AddRuleContract) | ||
export class AddRuleHandler implements ICommandHandler<AddRuleContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: AddRuleContract) { | ||
const parsedRule = JSON.parse(contract.rule); | ||
|
||
await this.dbInlinePolicyService.addRule(contract.policyId, parsedRule); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ckend/src/modules/user/modules/inline-policies/commands/handlers/delete-inline.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,14 @@ | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { DeleteInlineContract } from '../contracts/delete-inline.contract'; | ||
|
||
@CommandHandler(DeleteInlineContract) | ||
export class DeleteInlineHandler implements ICommandHandler<DeleteInlineContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: DeleteInlineContract) { | ||
await this.dbInlinePolicyService.deleteInlinePolicy(contract.policyId); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AddRuleHandler } from './add-rule.handler'; | ||
import { DeleteInlineHandler } from './delete-inline.handler'; | ||
import { RemoveRuleHandler } from './remove-rule.handler'; | ||
import { UpdateConfigurationHandler } from './update-configuration.handler'; | ||
|
||
export const CommandHandlers = [ | ||
DeleteInlineHandler, | ||
UpdateConfigurationHandler, | ||
AddRuleHandler, | ||
RemoveRuleHandler, | ||
]; |
14 changes: 14 additions & 0 deletions
14
...backend/src/modules/user/modules/inline-policies/commands/handlers/remove-rule.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,14 @@ | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { RemoveRuleContract } from '../contracts/remove-rule.contract'; | ||
|
||
@CommandHandler(RemoveRuleContract) | ||
export class RemoveRuleHandler implements ICommandHandler<RemoveRuleContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: RemoveRuleContract) { | ||
await this.dbInlinePolicyService.removeRule(contract.policyId, contract.ruleName); | ||
} | ||
} |
Oops, something went wrong.