OPA SDKs
You can quickly connect an application to Enterprise OPA or Open Policy Agent (OPA) using the following SDKs.
Support
All SDKs are developed and supported by Styra unless noted with an asterisk (*)
Framework-specific integrations:
Install
- TypeScript
- Java
- C#
npm:
npm add @styra/opa
Yarn:
yarn add @styra/opa zod
note
Yarn does not install peer dependencies automatically. You will need to install zod
as shown above.
The Java SDK is published on Maven Central as com.styra/opa
. The Maven Central page includes up-to-date instructions to add it as a dependency to your Java project, tailored for a variety of build systems including Maven and Gradle.
Nuget:
dotnet add package Styra.Opa
Usage Examples
- TypeScript
- Java
- C#
import { OPAClient } from "@styra/opa";
const opa = new OPAClient("http://opa-host:8181");
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;
OPAClient opa = new OPAClient("http://opa-host:8181");
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 OpaClient opa = new OPAClient("http://opa-host:8181");
var input = new Dictionary<string, object>() {
{ "user", "alice" },
{ "action", "read" },
{"resource", "/finance/reports/fy2038_budget.csv"},
};
var allowed = await opa.check("authz/allow", input);
var violations = await opa.evaluate<List<string>>("authz/violations", input);
Console.WriteLine("Allowed: " + allowed);
Console.WriteLine("Violations: " + violations);
Support
Got suggestions or need help? We're here! Please reach out to us on the Styra Community Slack.
Further Reading
These SDKs are support Open Policy Agent (OPA) and Enterprise OPA.