-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rego template endpoint with DTOs (#96)
- Loading branch information
Samuel
authored
Feb 12, 2024
1 parent
df6e274
commit 532c28b
Showing
37 changed files
with
1,454 additions
and
559 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 |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import { AccountClassification, AccountType, Action, Alg, Signature, UserRole } from '@narval/authz-shared' | ||
import { | ||
AccountClassification, | ||
AccountType, | ||
Action, | ||
Alg, | ||
EntityType, | ||
Signature, | ||
UserRole, | ||
ValueOperators | ||
} from '@narval/authz-shared' | ||
import { Intents } from '@narval/transaction-request-intent' | ||
import { HttpStatus, INestApplication } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { readFileSync, unlinkSync } from 'fs' | ||
import request from 'supertest' | ||
import { AppModule } from '../../../app/app.module' | ||
import { AAUser, AAUser_Credential_1 } from '../../../app/persistence/repository/mock_data' | ||
import { PersistenceModule } from '../../../shared/module/persistence/persistence.module' | ||
import { TestPrismaService } from '../../../shared/module/persistence/service/test-prisma.service' | ||
import { Organization } from '../../../shared/types/entities.types' | ||
import { Criterion, Then, TimeWindow } from '../../../shared/types/policy.type' | ||
import { load } from '../../app.config' | ||
|
||
const REQUEST_HEADER_ORG_ID = 'x-org-id' | ||
|
@@ -436,4 +448,140 @@ describe('Admin Endpoints', () => { | |
expect(status).toEqual(HttpStatus.CREATED) | ||
}) | ||
}) | ||
|
||
describe('POST /policies', () => { | ||
it('sets the organization policies', async () => { | ||
const payload = { | ||
authentication, | ||
approvals, | ||
request: { | ||
action: Action.SET_POLICY_RULES, | ||
nonce: 'random-nonce-111', | ||
data: [ | ||
{ | ||
then: Then.PERMIT, | ||
name: 'examplePermitPolicy', | ||
when: [ | ||
{ | ||
criterion: Criterion.CHECK_RESOURCE_INTEGRITY, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_NONCE_EXISTS, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_ACTION, | ||
args: [Action.SIGN_TRANSACTION] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_PRINCIPAL_ID, | ||
args: ['[email protected]'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_WALLET_ID, | ||
args: ['eip155:eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TYPE, | ||
args: [Intents.TRANSFER_NATIVE] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TOKEN, | ||
args: ['eip155:137/slip44:966'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_AMOUNT, | ||
args: { | ||
currency: '*', | ||
operator: ValueOperators.LESS_THAN_OR_EQUAL, | ||
value: '1000000000000000000' | ||
} | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_APPROVALS, | ||
args: [ | ||
{ | ||
approvalCount: 2, | ||
countPrincipal: false, | ||
approvalEntityType: EntityType.User, | ||
entityIds: ['[email protected]', '[email protected]'] | ||
}, | ||
{ | ||
approvalCount: 1, | ||
countPrincipal: false, | ||
approvalEntityType: EntityType.UserRole, | ||
entityIds: [UserRole.ADMIN] | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
then: Then.FORBID, | ||
name: 'exampleForbidPolicy', | ||
when: [ | ||
{ | ||
criterion: Criterion.CHECK_RESOURCE_INTEGRITY, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_NONCE_EXISTS, | ||
args: null | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_ACTION, | ||
args: [Action.SIGN_TRANSACTION] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_PRINCIPAL_ID, | ||
args: ['[email protected]'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_WALLET_ID, | ||
args: ['eip155:eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TYPE, | ||
args: [Intents.TRANSFER_NATIVE] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_INTENT_TOKEN, | ||
args: ['eip155:137/slip44:966'] | ||
}, | ||
{ | ||
criterion: Criterion.CHECK_SPENDING_LIMIT, | ||
args: { | ||
limit: '1000000000000000000', | ||
timeWindow: { | ||
type: TimeWindow.ROLLING, | ||
value: 43200 | ||
}, | ||
filters: { | ||
tokens: ['eip155:137/slip44:966'], | ||
users: ['[email protected]'] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/admin/policies') | ||
.set(REQUEST_HEADER_ORG_ID, org.uid) | ||
.send(payload) | ||
|
||
expect(body.policies).toMatchObject(payload.request.data) | ||
expect(status).toEqual(HttpStatus.CREATED) | ||
|
||
const path = `./apps/authz/src/opa/rego/generated/${body.fileId}.rego` | ||
const rego = readFileSync(path, 'utf-8') | ||
expect(rego).toBeDefined() | ||
|
||
unlinkSync(path) | ||
}) | ||
}) | ||
}) |
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
30 changes: 30 additions & 0 deletions
30
apps/authz/src/app/http/rest/dto/policy-rules/set-policy-rules-request.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,30 @@ | ||
import { Action } from '@narval/authz-shared' | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { Type } from 'class-transformer' | ||
import { IsArray, IsDefined, IsString, Matches, ValidateNested } from 'class-validator' | ||
import { Policy } from '../../../../../shared/types/policy.type' | ||
import { BaseActionDto } from '../base-action.dto' | ||
import { BaseAdminRequestPayloadDto } from '../base-admin-request-payload.dto' | ||
|
||
export class SetPolicyRulesDto extends BaseActionDto { | ||
@IsDefined() | ||
@IsString() | ||
@Matches(Action.SET_POLICY_RULES) | ||
@ApiProperty() | ||
action: typeof Action.SET_POLICY_RULES | ||
|
||
@IsDefined() | ||
@IsArray() | ||
@Type(() => Policy) | ||
@ValidateNested({ each: true }) | ||
@ApiProperty() | ||
data: Policy[] | ||
} | ||
|
||
export class SetPolicyRulesRequestDto extends BaseAdminRequestPayloadDto { | ||
@IsDefined() | ||
@ValidateNested() | ||
@Type(() => SetPolicyRulesDto) | ||
@ApiProperty() | ||
request: SetPolicyRulesDto | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/authz/src/app/http/rest/dto/policy-rules/set-policy-rules-response.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,20 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { Type } from 'class-transformer' | ||
import { IsDefined, IsString, ValidateNested } from 'class-validator' | ||
import { Policy } from '../../../../../shared/types/policy.type' | ||
|
||
export class SetPolicyRulesResponseDto { | ||
@IsDefined() | ||
@IsString() | ||
fileId: string | ||
|
||
@IsDefined() | ||
@Type(() => Policy) | ||
@ValidateNested() | ||
@ApiProperty({ type: () => Policy, isArray: true }) | ||
policies: Policy[] | ||
|
||
constructor(partial: Partial<SetPolicyRulesResponseDto>) { | ||
Object.assign(this, partial) | ||
} | ||
} |
Oops, something went wrong.