Skip to content
Snippets Groups Projects

NAT-402/integrate GUI with our custom OPA server

2 files
+ 29
1
Compare changes
  • Side-by-side
  • Inline

Files

+ 28
1
 
import { loadPolicy } from "@open-policy-agent/opa-wasm";
 
 
export async function createPolicyCheck(user?: Partial<Oidc.Profile>) {
export async function createPolicyCheck(user?: Partial<Oidc.Profile>) {
if (!user) {
if (!user) {
return () => true;
return () => true;
}
}
 
 
const opaBundletUrl = process.env.REACT_APP_OPA_BUNDLE_URL;
 
 
if (typeof opaBundletUrl === 'undefined') {
 
throw new Error('REACT_APP_OPA_BUNDLE_URL is not defined');
 
}
 
 
const policyResult = await fetch(opaBundletUrl);
 
const policyWasm = await policyResult.arrayBuffer();
try {
try {
 
const policy = await loadPolicy(policyWasm);
 
function allowed(resource: string): boolean {
function allowed(resource: string): boolean {
return true;
const input: any = {
 
resource: resource,
 
method: "GET",
 
...user,
 
};
 
 
const resultSet = policy.evaluate(input);
 
 
if (resultSet == null || resultSet.length === 0) {
 
console.error("evaluation error", resultSet);
 
return false;
 
}
 
 
return resultSet[0].result;
}
}
return allowed;
return allowed;
} catch {
} catch {
Loading