OPA SDKs
You can quickly connect an application to Enterprise OPA or Open Policy Agent (OPA) using the following SDKs.
These are developed and supported by Styra:
Getting Started
- TypeScript
- Java
- C#
npm:
npm add @styra/opa
Yarn:
yarn add @styra/opa
Gradle:
implementation 'com.styra:opa:XXX'
Maven:
<dependency>
<groupId>com.styra</groupId>
<artifactId>opa</artifactId>
<version>XXX</version>
</dependency>
Nuget:
dotnet add package Styra.Opa
Code Examples
- TypeScript
- Java
- C#
import { OPAClient } from "@styra/opa";
const serverURL = "http://opa-host:8181";
const opa = new OPAClient(serverURL);
const path = "authz/allow";
const input = {
subject: "alice",
action: "read",
resource: "/finance/reports/fy2038_budget.csv"
};
const result = await opa.evaluate(path, input);
console.log(result ? "allowed" : "denied");
import com.styra.opa.OPAClient;
import com.styra.opa.OPAException;
//
String opaURL = "http://opa-host:8181";
OPAClient opa = new OPAClient(opaURL);
java.util.Map<String,Object> input = java.util.Map.ofEntries(
entry("subject", "alice"),
entry("action", "read"),
entry("resource", "/finance/reports/fy2038_budget.csv")
);
java.util.List<Object> violations;
boolean allowed;
try {
allowed = opa.check("policy/allow", input);
violations = opa.evaluate("policy/violations", input);
} catch (OPAException e ) {
// Note that OPAException usually wraps other exception types, in
// case you need to do more complex error handling.
System.out.println("exception while making request against OPA: " + e);
throw e; // crash the program
}
System.out.println("allowed: " + allowed);
System.out.println("violations: " + violations);
using Styra.Opa;
private string serverURL = "http://opa-host:8181";
private string path = "authz/allow";
private OpaClient opa;
opa = new OPAClient(serverURL);
var input = new Dictionary<string, object>() {
{ "user", "alice" },
{ "action", "read" },
{"resource", "/finance/reports/fy2038_budget.csv"},
};
// (local variable) bool allowed
var allowed = await opa.check("authz/allow", input);
// (local variable) violations List<string>?
var violations = await opa.evaluate<List<string>>("authz/violations", input);
// Normal true/false cases...
if (allowed) {
// ...
} else {
Console.WriteLine("Violations: " + violations);
}
Support
Got suggestions or need help? We're here! Please reach out to us on the Styra Community Slack.