【问题标题】:OPA/rego result is true even if a comparison evaluates to false即使比较评估为假,OPA/rego 结果也为真
【发布时间】:2022-01-21 21:22:13
【问题描述】:

我刚开始使用 OPA,所以很有可能我做错了什么。

我有以下输入:

{
  "request": {
    "principalId": "user1",
    "scope": "/workspaces/1/environments/dev/deployments/123",
    "requiredPermissions": [
      "Deployments.ReadWrite",
      "Foo.Bar"
    ]
  }
}

我想确保用户拥有所有必需的权限。我已经有了所需的变量:

#// this is opa/rego value

"principal_roles_at_requested_scope": [
              "Deployments.Read",
              "Deployments.ReadWrite",
              "WorkspaceEnvironments.Read",
              "Workspaces.Read"
            ]

这应该将allow 设置为false,因为Foo.Bar 不在principal_roles_at_requested_scope 集合中,但它被评估为true

allow {
    some i
    input.request.requiredPermissions[i] in principal_roles_at_requested_scope
}

另一方面,这有效,但显然不能使用:

allow {
    input.request.requiredPermissions[0] in principal_roles_at_requested_scope
    input.request.requiredPermissions[1] in principal_roles_at_requested_scope
}

【问题讨论】:

    标签: open-policy-agent rego


    【解决方案1】:

    好的,

    感谢this 我已经弄明白了。

    这样就解决了:

    any_missing_permissions {
        some v in input.request.requiredPermissions
        not v in principal_roles_at_requested_scope
    }
    
    allow {
        #// Each permission required in the request has to be available
        #// at the requested scope
        not any_missing_permissions
    }
    

    【讨论】:

    • 不错!下一版本的 OPA 将采用“every”关键字来简化此类检查:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多