Apply Open Policy Agent (OPA) policies
The Open Policy Agent (OPA) HTTP middleware applys to incoming Dapr HTTP requests. This can be used to apply reusable authorization policies to app endpoints.
You can prototype and experiment with policies using the official opa playground. For example, .
Spec metadata fields
To be applied, the middleware must be referenced in . See middleware pipelines.
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: appconfig
spec:
httpPipeline:
handlers:
- name: my-policy
type: middleware.http.opa
Input
The HTTPRequest
input contains all the relevant information about an incoming HTTP Request except it’s body.
type Input struct {
request HTTPRequest
}
type HTTPRequest struct {
method string
// The raw request path (e.g. "/v2/my-path/")
path string
// The path broken down into parts for easy consumption (e.g. ["v2", "my-path"])
path_parts string[]
// The raw query string (e.g. "?a=1&b=2")
// The query broken down into keys and their values
query map[string][]string
// The request headers
// NOTE: By default, no headers are included. You must specify what headers
// you want to receive via `spec.metadata.includedHeaders` (see above)
headers map[string]string
// The request scheme (e.g. http, https)
scheme string
}
The policy must set data.http.allow
with either a boolean
value, or an object
value with an allow
boolean property. A true
allow
will allow the request, while a false
value will reject the request with the status specified by defaultStatus
. The following policy, with defaults, demonstrates a 403 - Forbidden
for all requests:
which is the same as:
package http
default allow = {
"allow": false
}
default allow = {
"allow": false,
}
To redirect, add headers and set the status_code
to the returned result:
You can also set additional headers on the allowed request:
package http
default allow = false
allow = { "allow": true, "additional_headers": { "X-JWT-Payload": payload } } {
not input.path[0] == "forbidden"
// Where `jwt` is the result of another rule
payload := base64.encode(json.marshal(jwt.payload))
}
type Result bool
// or
type Result struct {
// Whether to allow or deny the incoming request
allow bool
// Overrides denied response status code; Optional
status_code int
// Sets headers on allowed request or denied response; Optional
additional_headers map[string]string
}