Enforce Policy Using Open Policy Agent (OPA)
EnRoute Technical Reference
Open Policy Agent (OPA) Plugin
EnRoute integrates with Open Policy Agent (OPA) to enforce policy on the incoming request.
Enforcing policy using OPA provides clear separation of policy without embedding it in code.
OPA integration is achieved using a plugin in EnRoute. Attaching the OPA plugin to a GatewayHost
results in enforcing policy for the application associated with the GatewayHost
Quick Introduction to OPA
The root of OPA evaluation tree is a query that evaluates a chain of OPA rules. OPA rules are defined in a namespaced OPA module.
Here is a simple example of OPA module defined in package opa.auth
-
package opa.auth
# Access request inside envoy CheckRequest
import input.attributes.request.http as http_request
import input.parsed_path
default allow = false
allow {
http_request.host == "helloenroute.enroutedemo.com"
parsed_path[0] == "v1/helloenroute/get"
http_request.method == "GET"
}
allow {
http_request.host == "helloenroute.enroutedemo.com"
parsed_path[0] == "v1/helloenroute/post"
http_request.method == "POST"
http_request.headers["X-Fowarded-For"][_] == "10.0.1.1"
}
Query
# Query rooted at ```data.opa.auth.allow``` (for package ```opa.auth```)
data.opa.auth.allow
The above construct provides a way to whitelist requets. Everything is blocked except -
- request is destined to host
helloenroute.enroutedemo.com
AND path isv1/helloenroute/get
AND request method isGET
OR - request is destined to host
helloenroute.enroutedemo.com
AND path isv1/helloenroute/post
AND request method isPOST
and theX-Forwarded-For
header is set to10.0.1.1
The OPA Section on Policy Evaluation describes this in more detail.
OPA System Diagram
- On an incoming request, Envoy makes a request to get a policy decision
- OPA evaluates policy and returns a result
OPA Filter Configuration
OPA Filter Config for Kubernetes | OPA Filter Config for Standalone |
|
|
---|
Notes
OPA is an enterprise plugin
OPA plugin is a global HttpFilter based on ext_authz. It sets configuration on the Listener and is applicable to all GatewayHost
when defined.