0

在 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]。那么有人可以帮我指出这两者之间的区别吗?

4

1 回答 1

1

Rego 中的赋值运算符是:=,==是比较的,=运算符是统一的。有一个部分描述了docs 中的差异,但简单地说,统一结合了赋值比较,所以在你的例子中,假设input.path有两个元素,第二个元素的值将被分配给profile_id.

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

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

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

于 2022-01-19T11:25:47.820 回答