应用开放策略代理 (OPA) 策略

    The Open Policy Agent (OPA) HTTP middleware applys to incoming Dapr HTTP requests. 这可以用来将可重用的授权策略应用到应用终结点。

    您可以使用 官方 opa playground对策略进行原型设计和实验。 例如,。

    元数据字段规范

    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

    输入

    这个中间件提供了一个 HTTPRequest 作为输入。

    HTTPRequest 输入包含所有关于传入HTTP请求的透彻信息,但它的正文除外。

    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. // The raw query string (e.g. "?a=1&b=2")
    11. raw_query string
    12. // The query broken down into keys and their values
    13. query map[string][]string
    14. // The request headers
    15. // NOTE: By default, no headers are included. You must specify what headers
    16. // you want to receive via `spec.metadata.includedHeaders` (see above)
    17. // The request scheme (e.g. http, https)
    18. scheme string
    19. }
    20. method string
    21. // The raw request path (e.g. "/v2/my-path/")
    22. path string
    23. // The path broken down into parts for easy consumption (e.g. ["v2", "my-path"])
    24. path_parts string[]
    25. // The raw query string (e.g. "?a=1&b=2")
    26. raw_query string
    27. // The query broken down into keys and their values
    28. query map[string][]string
    29. // The request headers
    30. // NOTE: By default, no headers are included. You must specify what headers
    31. // you want to receive via `spec.metadata.includedHeaders` (see above)
    32. headers map[string]string
    33. // The request scheme (e.g. http, https)
    34. scheme string
    35. }

    等价于:

    1. package http
    2. }

    拒绝请求时,您可以覆盖返回的状态代码。 例如,如果您想退回 401 而不是 403,你可以这样做:

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

    你也可以在允许的请求上设置额外的头信息:

    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. }
    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. }

    相关链接