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

Fix OPA CI #44

Merged
merged 6 commits into from
Jan 23, 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
2 changes: 1 addition & 1 deletion .github/workflows/authz_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ jobs:
version: latest

- name: Run OPA Tests
run: make authz/rego/test
run: make authz/rego/test
25 changes: 0 additions & 25 deletions .github/workflows/authz_opa_ci.yml

This file was deleted.

20 changes: 12 additions & 8 deletions apps/authz/src/app/opa/opa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ export class OpaService {

constructor(private persistenceRepository: PersistenceRepository) {}

async onApplicationBootstrap() {
async onApplicationBootstrap(): Promise<void> {
this.logger.log('OPA Service boot')
this.opaEngine = await this.getOpaEngine()
}

async evaluate(input: RegoInput): Promise<OpaResult[]> {
this.opaEngine = await this.getOpaEngine()
const evalResult: { result: OpaResult }[] = await this.opaEngine.evaluate(input, 'main/evaluate')
return evalResult.map(({ result }) => result)
}

private async getOpaEngine(): Promise<OpaEngine> {
const policyWasmPath = OPA_WASM_PATH
const policyWasm = readFileSync(policyWasmPath)
const opaEngine = await loadPolicy(policyWasm, undefined, {
'time.now_ns': () => new Date().getTime() * 1000000
})
const data = await this.persistenceRepository.getEntityData()
opaEngine.setData(data)
this.opaEngine = opaEngine
}

async evaluate(input: RegoInput): Promise<OpaResult[]> {
if (!this.opaEngine) throw new Error('OPA Engine not initialized')
const evalResult: { result: OpaResult }[] = await this.opaEngine.evaluate(input, 'main/evaluate')
return evalResult.map(({ result }) => result)
return opaEngine
}
}
2 changes: 2 additions & 0 deletions apps/authz/src/opa/rego/policies/e2e.rego
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ permit[{"policyId": "test-permit-policy-1"}] := reason {
checkTransferTokenType({"transferNative"})
checkTransferTokenAddress({"eip155:137/slip44/966"})
checkTransferTokenOperation({"operator": "gte", "value": "1000000000000000000"})

approvalsRequired = [{
"approvalCount": 2,
"countPrincipal": false,
"approvalEntityType": "Narval::User",
"entityIds": ["aa@narval.xyz", "bb@narval.xyz"],
}]

approvals := getApprovalsResult(approvalsRequired)
reason := {
"type": "permit",
Expand Down