From 75a9be509a7fc27b3c62a590859db4c8621951c2 Mon Sep 17 00:00:00 2001 From: William Calderipe Date: Mon, 18 Mar 2024 11:36:22 +0100 Subject: [PATCH] Set data in the Open Policy Agent instance --- .../core/open-policy-agent.engine.ts | 12 ++++++------ .../core/type/open-policy-agent.type.ts | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apps/policy-engine/src/open-policy-agent/core/open-policy-agent.engine.ts b/apps/policy-engine/src/open-policy-agent/core/open-policy-agent.engine.ts index d64f8540c..caaeb5cbe 100644 --- a/apps/policy-engine/src/open-policy-agent/core/open-policy-agent.engine.ts +++ b/apps/policy-engine/src/open-policy-agent/core/open-policy-agent.engine.ts @@ -4,18 +4,16 @@ import { loadPolicy } from '@open-policy-agent/opa-wasm' import { resolve } from 'path' import { v4 } from 'uuid' import { OpenPolicyAgentException } from './exception/open-policy-agent.exception' +import { OpenPolicyAgentPolicy } from './type/open-policy-agent.type' +import { toData } from './util/evaluation.util' import { build } from './util/wasm-build.util' -type PromiseType> = T extends Promise ? U : never - -type OpaEngine = PromiseType> - export class OpenPolicyAgentEngine implements Engine { private policies: Policy[] private entities: Entities - private opa?: OpaEngine + private opa?: OpenPolicyAgentPolicy constructor(policies?: Policy[], entities?: Entities) { this.entities = entities || { @@ -44,7 +42,7 @@ export class OpenPolicyAgentEngine implements Engine { return this.policies } - getOpenPolicyAgentInstance(): OpaEngine | undefined { + getOpenPolicyAgentPolicy(): OpenPolicyAgentPolicy | undefined { return this.opa } @@ -70,6 +68,8 @@ export class OpenPolicyAgentEngine implements Engine { 'time.now_ns': () => new Date().getTime() * 1000000 }) + this.opa.setData(toData(this.getEntities())) + return this } catch (error) { throw new OpenPolicyAgentException({ diff --git a/apps/policy-engine/src/open-policy-agent/core/type/open-policy-agent.type.ts b/apps/policy-engine/src/open-policy-agent/core/type/open-policy-agent.type.ts index 69f424950..098d89840 100644 --- a/apps/policy-engine/src/open-policy-agent/core/type/open-policy-agent.type.ts +++ b/apps/policy-engine/src/open-policy-agent/core/type/open-policy-agent.type.ts @@ -9,6 +9,11 @@ import { UserRole } from '@narval/policy-engine-shared' import { Intent } from '@narval/transaction-request-intent' +import { loadPolicy } from '@open-policy-agent/opa-wasm' + +type PromiseType> = T extends Promise ? U : never + +export type OpenPolicyAgentInstance = PromiseType> export type Input = { action: Action