【问题标题】:What is the difference between Assignment(=) operator and Equality(==) operator in OPA rule bodyOPA规则正文中的赋值(=)运算符和相等(==)运算符有什么区别
【发布时间】:2022-01-19 06:10:23
【问题描述】:

在 OPA 文档 https://www.openpolicyagent.org/docs/latest/policy-testing/ 中有一个策略定义,如下所示:


allow {
    input.path == ["users"]
    input.method == "POST"
}

allow {
    some profile_id
    input.path = ["users", profile_id]
    input.method == "GET"
    profile_id == input.user_id
}

在第一条规则中是 input.path == ["users"] 而在第二条规则中是 input.path = ["users", profile_id] .那么有人可以帮我指出这两者之间的区别吗?

【问题讨论】:

    标签: open-policy-agent rego


    【解决方案1】:

    Rego 中的赋值运算符为:=== 用于比较,= 运算符用于统一。有一个部分描述了in the docs 的区别,但简单地说,统一结合了赋值比较,所以在你的例子中,假设input.path 有两个元素,第二个元素的值将被分配给@ 987654326@.

    如果您愿意,可以编写策略来拆分比较和分配:

    allow {
        count(input.path) == 2
        input.path[0] == "users"
        profile_id := input.path[1]
        input.method == "GET"
        profile_id == input.user_id
    }
    

    这可以说是有点混乱。然而,应谨慎使用统一,因为在大多数其他情况下,将分配与比较分开会更清楚。

    【讨论】:

    • 感谢您的澄清。
    猜你喜欢
    • 2018-04-20
    • 2010-12-17
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多