Apply Open Policy Agent (OPA) policies

Use middleware to apply Open Policy Agent (OPA) policies on incoming requests

The Open Policy Agent (OPA) HTTP middleware applys OPA Policies to incoming Dapr HTTP requests. This can be used to apply reusable authorization policies to app endpoints.

Component format

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: my-policy
  5. namespace: default
  6. spec:
  7. type: middleware.http.opa
  8. version: v1
  9. metadata:
  10. # `includedHeaders` is a comma-separated set of case-insensitive headers to include in the request input.
  11. # Request headers are not passed to the policy by default. Include to receive incoming request headers in
  12. # the input
  13. - name: includedHeaders
  14. value: "x-my-custom-header, x-jwt-header"
  15. # `defaultStatus` is the status code to return for denied responses
  16. - name: defaultStatus
  17. value: 403
  18. # `rego` is the open policy agent policy to evaluate. required
  19. # The policy package must be http and the policy must set data.http.allow
  20. - name: rego
  21. value: |
  22. package http
  23. default allow = true
  24. # Allow may also be an object and include other properties
  25. # For example, if you wanted to redirect on a policy failure, you could set the status code to 301 and set the location header on the response:
  26. allow = {
  27. "status_code": 301,
  28. "additional_headers": {
  29. "location": "https://my.site/authorize"
  30. }
  31. } {
  32. not jwt.payload["my-claim"]
  33. }
  34. # You can also allow the request and add additional headers to it:
  35. allow = {
  36. "allow": true,
  37. "additional_headers": {
  38. "x-my-claim": my_claim
  39. }
  40. } {
  41. my_claim := jwt.payload["my-claim"]
  42. }
  43. jwt = { "payload": payload } {
  44. auth_header := input.request.headers["Authorization"]
  45. [_, jwt] := split(auth_header, " ")
  46. [_, payload, _] := io.jwt.decode(jwt)
  47. }

You can prototype and experiment with policies using the official opa playground. For example, you can find the example policy above here.

Spec metadata fields

FieldDetailsExample
regoThe Rego policy languageSee above
defaultStatusThe status code to return for denied responseshttps://accounts.google.com, https://login.salesforce.com
includedHeadersA comma-separated set of case-insensitive headers to include in the request input. Request headers are not passed to the policy by default. Include to receive incoming request headers in the input“x-my-custom-header, x-jwt-header”

Dapr configuration

To be applied, the middleware must be referenced in configuration. See middleware pipelines.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Configuration
  3. metadata:
  4. name: appconfig
  5. spec:
  6. httpPipeline:
  7. handlers:
  8. - name: my-policy
  9. type: middleware.http.opa

Input

This middleware supplies a HTTPRequest as input.

HTTPRequest

The HTTPRequest input contains all the relevant information about an incoming HTTP Request.

  1. type Input struct {
  2. request HTTPRequest
  3. }
  4. type HTTPRequest struct {
  5. // The request method (e.g. GET,POST,etc...)
  6. method string
  7. // The raw request path (e.g. "/v2/my-path/")
  8. path string
  9. // The path broken down into parts for easy consumption (e.g. ["v2", "my-path"])
  10. path_parts string[]
  11. // The raw query string (e.g. "?a=1&b=2")
  12. raw_query string
  13. // The query broken down into keys and their values
  14. query map[string][]string
  15. // The request headers
  16. // NOTE: By default, no headers are included. You must specify what headers
  17. // you want to receive via `spec.metadata.includedHeaders` (see above)
  18. headers map[string]string
  19. // The request scheme (e.g. http, https)
  20. scheme string
  21. // The request body (e.g. http, https)
  22. body string
  23. }

Result

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:

  1. package http
  2. default allow = false

which is the same as:

  1. package http
  2. default allow = {
  3. "allow": false
  4. }

Changing the rejected response status code

When rejecting a request, you can override the status code the that gets returned. For example, if you wanted to return a 401 instead of a 403, you could do the following:

  1. package http
  2. default allow = {
  3. "allow": false,
  4. "status_code": 401
  5. }

Adding response headers

To redirect, add headers and set the status_code to the returned result:

  1. package http
  2. default allow = {
  3. "allow": false,
  4. "status_code": 301,
  5. "additional_headers": {
  6. "Location": "https://my.redirect.site"
  7. }
  8. }

Adding request headers

You can also set additional headers on the allowed request:

  1. package http
  2. default allow = false
  3. allow = { "allow": true, "additional_headers": { "X-JWT-Payload": payload } } {
  4. not input.path[0] == "forbidden"
  5. // Where `jwt` is the result of another rule
  6. payload := base64.encode(json.marshal(jwt.payload))
  7. }

Result structure

  1. type Result bool
  2. // or
  3. type Result struct {
  4. // Whether to allow or deny the incoming request
  5. allow bool
  6. // Overrides denied response status code; Optional
  7. status_code int
  8. // Sets headers on allowed request or denied response; Optional
  9. additional_headers map[string]string
  10. }

Last modified November 12, 2021 : Merge pull request #1949 from willtsai/az-staticwebapp-versioning (c40e456)