Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
samteb committed Feb 7, 2024
1 parent d34c0be commit ed11311
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 23 deletions.
3 changes: 2 additions & 1 deletion apps/authz/src/app/http/rest/controller/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
CreateUserRequest,
RegisterTokensRequest,
RegisterWalletRequest,
SetPolicyRulesRequest,
UpdateUserRequest
} from '@narval/authz-shared'
import { Body, Controller, Logger, Patch, Post } from '@nestjs/common'
Expand Down Expand Up @@ -142,7 +143,7 @@ export class AdminController {

@Post('/policy-rules')
async setPolicyRules(@Body() body: SetPolicyRulesRequestDto) {
const payload = body as any
const payload: SetPolicyRulesRequest = body

Check failure on line 146 in apps/authz/src/app/http/rest/controller/admin.controller.ts

View workflow job for this annotation

GitHub Actions / Build and test

Type 'SetPolicyRulesRequestDto' is not assignable to type 'SetPolicyRulesRequest'.

Check failure on line 146 in apps/authz/src/app/http/rest/controller/admin.controller.ts

View workflow job for this annotation

GitHub Actions / Build and test

Type 'SetPolicyRulesRequestDto' is not assignable to type 'SetPolicyRulesRequest'.

const policyRules = await this.adminService.setPolicyRules(payload)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Action, Criterion } from '@narval/authz-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined, IsEnum } from 'class-validator'

export class ActionCriterionDto {
@IsDefined()
@ApiProperty()
criterion: typeof Criterion.CHECK_ACTION

@IsDefined()
@IsEnum(Action, { each: true })
@ApiProperty({ isArray: true, enum: Action })
args: Action[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Criterion } from '@narval/authz-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined } from 'class-validator'

export class ResourceIntegrityCriterionDto {
@IsDefined()
@ApiProperty()
criterion: typeof Criterion.CHECK_RESOURCE_INTEGRITY

@ApiProperty()
args: null
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Criterion } from '@narval/authz-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined, IsIn } from 'class-validator'
import { ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsDefined, IsIn, ValidateNested } from 'class-validator'
import { ActionCriterionDto } from './criteria/action-criterion.dto'
import { ResourceIntegrityCriterionDto } from './criteria/resource-integrity-criterion.dto'

export class PolicyCriterionDto {
@IsIn(Object.values(Criterion))
@IsDefined()
@ApiProperty({
enum: Object.values(Criterion)
})
@ApiProperty({ enum: Object.values(Criterion) })
criterion: Criterion

args: any
@ValidateNested()
@Type((opts) => {

Check failure on line 15 in apps/authz/src/app/http/rest/dto/policy-rules/policy-criterion.dto.ts

View workflow job for this annotation

GitHub Actions / Build and test

Argument of type '(opts: TypeHelpOptions | undefined) => typeof ActionCriterionDto | typeof ResourceIntegrityCriterionDto | undefined' is not assignable to parameter of type '(type?: TypeHelpOptions | undefined) => Function'.

Check failure on line 15 in apps/authz/src/app/http/rest/dto/policy-rules/policy-criterion.dto.ts

View workflow job for this annotation

GitHub Actions / Build and test

Argument of type '(opts: TypeHelpOptions | undefined) => typeof ActionCriterionDto | typeof ResourceIntegrityCriterionDto | undefined' is not assignable to parameter of type '(type?: TypeHelpOptions | undefined) => Function'.
if (opts?.object.criterion === Criterion.CHECK_ACTION) {
return ActionCriterionDto
}
if (opts?.object.criterion === Criterion.CHECK_RESOURCE_INTEGRITY) {
return ResourceIntegrityCriterionDto
}
})
@IsDefined()
@ApiProperty({
oneOf: [{ $ref: getSchemaPath(ActionCriterionDto) }, { $ref: getSchemaPath(ResourceIntegrityCriterionDto) }]
})
args: ActionCriterionDto | ResourceIntegrityCriterionDto
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseActionDto } from '@app/authz/app/http/rest/dto/base-action.dto'
import { BaseAdminRequestPayloadDto } from '@app/authz/app/http/rest/dto/base-admin-request-payload.dto'
import { Action } from '@narval/authz-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined, IsIn, ValidateNested } from 'class-validator'
import { BaseActionDto } from '../base-action.dto'
import { BaseAdminRequestPayloadDto } from '../base-admin-request-payload.dto'
import { PolicyCriterionBuilderDto } from './policy-criterion-builder.dto'

export class SetPolicyRulesDto extends BaseActionDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

test_resource {
checkTransferResourceIntegrity with input as request
checkResourceIntegrity with input as request
with data.entities as entities

wallet = resource with input as request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

permit[{"policyId": "examplePermitPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand All @@ -14,7 +14,7 @@ package main
}

forbid[{"policyId": "exampleForbidPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

permit[{"policyId": "examplePermitPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand All @@ -14,7 +14,7 @@ permit[{"policyId": "examplePermitPolicy" }] = reason {
}

forbid[{"policyId": "exampleForbidPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

permit[{"policyId": "examplePermitPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand All @@ -14,7 +14,7 @@ package main
}

forbid[{"policyId": "exampleForbidPolicy" }] = reason {
checkTransferResourceIntegrity
checkResourceIntegrity
checkNonceExists
checkAction({"signTransaction"})
checkPrincipalId({"matt@narval.xyz"})
Expand Down
2 changes: 1 addition & 1 deletion apps/authz/src/opa/rego/lib/criteria/resource.rego
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import future.keywords.in

resource = data.entities.wallets[input.resource.uid]

checkTransferResourceIntegrity {
checkResourceIntegrity {
checkAction({"signTransaction"})
transactionRequestFromAddress = input.transactionRequest.from
resourceAddress = extractAddressFromCaip10(input.resource.uid)
Expand Down
4 changes: 2 additions & 2 deletions apps/authz/src/opa/template/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const examplePermitPolicy: PolicyCriterionBuilder = {
name: 'examplePermitPolicy',
when: [
{
criterion: Criterion.CHECK_TRANSFER_RESOURCE_INTEGRITY,
criterion: Criterion.CHECK_RESOURCE_INTEGRITY,
args: null
},
{
Expand Down Expand Up @@ -62,7 +62,7 @@ export const exampleForbidPolicy: PolicyCriterionBuilder = {
name: 'exampleForbidPolicy',
when: [
{
criterion: Criterion.CHECK_TRANSFER_RESOURCE_INTEGRITY,
criterion: Criterion.CHECK_RESOURCE_INTEGRITY,
args: null
},
{
Expand Down
8 changes: 4 additions & 4 deletions packages/authz-shared/src/lib/type/policy-builder.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type Then = (typeof Then)[keyof typeof Then]

export const Criterion = {
CHECK_ACTION: 'checkAction',
CHECK_TRANSFER_RESOURCE_INTEGRITY: 'checkTransferResourceIntegrity',
CHECK_RESOURCE_INTEGRITY: 'checkResourceIntegrity',
CHECK_PRINCIPAL_ID: 'checkPrincipalId',
CHECK_PRINCIPAL_ROLE: 'checkPrincipalRole',
CHECK_PRINCIPAL_GROUP: 'checkPrincipalGroup',
Expand Down Expand Up @@ -119,8 +119,8 @@ type ActionCriterion = {
args: Action[]
}

type TransferResourceIntegrityCriterion = {
criterion: typeof Criterion.CHECK_TRANSFER_RESOURCE_INTEGRITY
type ResourceIntegrityCriterion = {
criterion: typeof Criterion.CHECK_RESOURCE_INTEGRITY
args: null
}

Expand Down Expand Up @@ -286,7 +286,7 @@ type SpendingLimitCriterion = {

export type PolicyCriterion =
| ActionCriterion
| TransferResourceIntegrityCriterion
| ResourceIntegrityCriterion
| PrincipalIdCriterion
| PrincipalRoleCriterion
| PrincipalGroupCriterion
Expand Down

0 comments on commit ed11311

Please sign in to comment.