-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ffdd67
commit 35043bb
Showing
2 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
apps/policy-engine/src/engine/core/service/evaluation.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,39 @@ | ||
import { EvaluationRequest, EvaluationResponse } from '@narval/policy-engine-shared' | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { OpenPolicyAgentEngine } from '../../../open-policy-agent/core/open-policy-agent.engine' | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
import { TenantService } from '../../../tenant/core/service/tenant.service' | ||
|
||
@Injectable() | ||
export class EvaluationService { | ||
constructor(private tenantService: TenantService) {} | ||
|
||
async evaluate(clientId: string, evaluation: EvaluationRequest): Promise<EvaluationResponse> { | ||
const [entityStore, policyStore] = await Promise.all([ | ||
this.tenantService.findEntityStore(clientId), | ||
this.tenantService.findPolicyStore(clientId) | ||
]) | ||
|
||
if (!entityStore) { | ||
throw new ApplicationException({ | ||
message: 'Missing client entity store', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { clientId } | ||
}) | ||
} | ||
|
||
if (!policyStore) { | ||
throw new ApplicationException({ | ||
message: 'Missing client entity store', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { clientId } | ||
}) | ||
} | ||
|
||
// WARN: Loading a new engine is an IO bounded process due to the Rego | ||
// transpilation and WASM build. | ||
const engine = await new OpenPolicyAgentEngine(policyStore.data, entityStore.data).load() | ||
|
||
return engine.evaluate(evaluation) | ||
} | ||
} |
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