OPA Java HTTP Client SDK
This is a Java HTTP client for Open Policy Agent. It calls a running OPA server over its REST API to evaluate policies — it does not embed the policy engine in your JVM. If you need to evaluate Rego policies in-process, see open-policy-agent/java-opa-sdk or Wasm instead.
SDK Installation
This package is published on Maven Central as io.github.open-policy-agent/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.
If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):
On Linux/MacOS:
./gradlew publishToMavenLocal -Pskip.signing
On Windows:
gradlew.bat publishToMavenLocal -Pskip.signing
SDK Example Usage (high-level)
package org.example;
import io.github.open_policy_agent.opa.OPAClient;
import io.github.open_policy_agent.opa.OPAException;
import java.util.Map;
import java.util.List;
import static java.util.Map.entry;
public class App {
public static void main(String[] args) throws Exception {
// Create an OPA instance, this handles any state needed for interacting
// with OPA, and can be re-used for multiple requests if needed.
String opaURL = "http://localhost:8181";
OPAClient opa = new OPAClient(opaURL);
// This will be the input to our policy.
java.util.Map<String,Object> input = java.util.Map.ofEntries(
entry("subject", "alice"),
entry("action", "read"),
entry("resource", "/finance/reports/fy2038_budget.csv")
);
// We will read the list of policy violations, and whether the request
// is allowed or not into these.
java.util.List<Object> violations;
boolean allowed;
// Perform the request against OPA.
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);
}
}
Note
For low-level SDK usage, see the sections below.
OPA OpenAPI HTTP Client (low-level)
Summary
For more information about the API: EOPA documentation
Table of Contents
SDK Example Usage
Example 1
package hello.world;
import io.github.open_policy_agent.opa.openapi.OpaApiClient;
import io.github.open_policy_agent.opa.openapi.models.errors.ClientError;
import io.github.open_policy_agent.opa.openapi.models.errors.ServerError;
import io.github.open_policy_agent.opa.openapi.models.operations.ExecuteDefaultPolicyWithInputResponse;
import io.github.open_policy_agent.opa.openapi.models.shared.Input;
import java.lang.Exception;
public class Application {
public static void main(String[] args) throws ClientError, ServerError, Exception {
OpaApiClient sdk = OpaApiClient.builder()
.build();
ExecuteDefaultPolicyWithInputResponse res = sdk.executeDefaultPolicyWithInput()
.input(Input.of(4963.69))
.call();
if (res.result().isPresent()) {
// handle response
}
}
}
Example 2
package hello.world;
import io.github.open_policy_agent.opa.openapi.OpaApiClient;
import io.github.open_policy_agent.opa.openapi.models.errors.ClientError;
import io.github.open_policy_agent.opa.openapi.models.errors.ServerError;
import io.github.open_policy_agent.opa.openapi.models.operations.*;
import io.github.open_policy_agent.opa.openapi.models.shared.Input;
import java.lang.Exception;
public class Application {
public static void main(String[] args) throws ClientError, ServerError, Exception {
OpaApiClient sdk = OpaApiClient.builder()
.build();
ExecutePolicyWithInputRequest req = ExecutePolicyWithInputRequest.builder()
.path("app/rbac")
.requestBody(ExecutePolicyWithInputRequestBody.builder()
.input(Input.of(true))
.build())
.build();
ExecutePolicyWithInputResponse res = sdk.executePolicyWithInput()
.request(req)
.call();
if (res.successfulPolicyResponse().isPresent()) {
// handle response
}
}
}
Example 3
package hello.world;
import io.github.open_policy_agent.opa.openapi.OpaApiClient;
import io.github.open_policy_agent.opa.openapi.models.errors.BatchServerError;
import io.github.open_policy_agent.opa.openapi.models.errors.ClientError;
import io.github.open_policy_agent.opa.openapi.models.operations.*;
import io.github.open_policy_agent.opa.openapi.models.shared.Input;
import java.lang.Exception;
import java.util.Map;
public class Application {
public static void main(String[] args) throws ClientError, BatchServerError, Exception {
OpaApiClient sdk = OpaApiClient.builder()
.build();
ExecuteBatchPolicyWithInputRequest req = ExecuteBatchPolicyWithInputRequest.builder()
.path("app/rbac")
.requestBody(ExecuteBatchPolicyWithInputRequestBody.builder()
.inputs(Map.ofEntries(
Map.entry("key", Input.of(6919.52))))
.build())
.build();
ExecuteBatchPolicyWithInputResponse res = sdk.executeBatchPolicyWithInput()
.request(req)
.call();
if (res.batchSuccessfulPolicyEvaluation().isPresent()) {
// handle response
}
}
}
Available Resources and Operations
Available methods
Server Selection
Override Server URL Per-Client
The default server can be overridden globally using the .serverURL(String serverUrl) builder method when initializing the SDK client instance. For example: