应用开放策略代理 (OPA) 策略
The Open Policy Agent (OPA) HTTP middleware applys to incoming Dapr HTTP requests. 这可以用来将可重用的授权策略应用到应用终结点。
您可以使用 官方 opa playground对策略进行原型设计和实验。 例如,。
元数据字段规范
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: appconfig
spec:
httpPipeline:
handlers:
- name: my-policy
type: middleware.http.opa
输入
这个中间件提供了一个 HTTPRequest 作为输入。
HTTPRequest
输入包含所有关于传入HTTP请求的透彻信息,但它的正文除外。
type Input struct {
request HTTPRequest
}
type HTTPRequest struct {
// The request method (e.g. GET,POST,etc...)
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"])
// The raw query string (e.g. "?a=1&b=2")
raw_query string
// 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)
// The request scheme (e.g. http, https)
scheme string
}
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")
raw_query string
// 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
}
等价于:
package http
}
拒绝请求时,您可以覆盖返回的状态代码。 例如,如果您想退回 401
而不是 403
,你可以这样做:
package http
default allow = {
"allow": false,
"status_code": 401
}
你也可以在允许的请求上设置额外的头信息:
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
}