Open Policy Agent – 部分策略评估和 http.send

huangapple go评论50阅读模式
英文:

Open Policy Agent - partial policy evaluation and http.send

问题

I apologize for any confusion, but it seems like you want a translation of the text you provided. Here is the translation of the text excluding the code:

使用 OPA,我想实现数据查询的保护和过滤。有一篇关于此的博客文章:
https://blog.openpolicyagent.org/write-policy-in-opa-enforce-policy-in-sql-d9d24db93bf4

我们正在使用模块/微服务架构,需要从不同的模块/服务请求数据进行策略评估。

我的目标是部分评估 Rego 中定义的策略,并通过 http.send 请求来自其他服务的所需数据(https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-http-httpsend)。部分评估策略的结果应该用于生成 SQL 查询,以确保用户根据策略只获取他们有权限访问的数据。部分评估策略的结果应该包含所有必要的值,用于服务的数据模型过滤,因此不需要进一步查询其他服务。

策略示例:

allow {
  input.method = "GET"
  input.path = ["entity"]
  allowed[entity]
}

allowed[entity] {
   entities = data.entities[_] # data.entities is unknown
   userEntities2 := http.send({
      "method": "GET",
      "url": concat("", ["/service2/users/", input.userId, "/entity2"]),
   }).body
   userEntities2Ids := [ui.id | ui = userEntities2[_]]
   entity.entities2Ids[_] = userEntities2Ids[_]
}

工作流/架构草图:
Open Policy Agent – 部分策略评估和 http.send

不幸的是,当调用 v1/compile 时,HTTP 请求似乎未执行和评估。如果我调用 /v1/data,那么结果是 {"allow": false,"allowed": [],"denied": []}。我该如何解决这个问题?

英文:

With OPA, I would like to implement data query protection and filtering. There is this blog article about this:
https://blog.openpolicyagent.org/write-policy-in-opa-enforce-policy-in-sql-d9d24db93bf4

We are working with a module / microservice architecture where it is necessary to request data from different modules / services for policy evaluation.

My goal is to evaluate the policies defined in Rego partially and to request the required data from other services via http.send (https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-http-httpsend). The result of the partially evaluated policies should then be used to generate SQL queries that ensure that users only get the data they have access to according to the policies. The result of the partially evaluated policies should contain all necessary values for the filtering of the data model of the service, so that no further queries to other services are necessary.

Policy Example:

allow {
  input.method = "GET"
  input.path = ["entity"]
  allowed[entity]
}

allowed[entity] {
   entities = data.entities[_] # data.entities is unknown
   userEntities2 := http.send({
      "method": "GET",
      "url": concat("", ["/service2/users/", input.userId, "/entity2"]),
   }).body
   userEntities2Ids := [ui.id | ui = userEntities2[_]]
   entity.entities2Ids[_] = userEntities2Ids[_]
}

Workflow / Architecture sketch:
Open Policy Agent – 部分策略评估和 http.send

Unfortunately, the HTTP requests do not seem to be executed and evaluated when v1/compile is called. If I call /v1/data, then the result is {"allow": false,"allowed": [],"denied": []}. How can I solve the problem?

答案1

得分: 3

抱歉,HTTP请求似乎在调用v1/compile时未被执行和评估。

你是正确的,http.send 被排除在部分评估(PE)之外。原因是它被设计为在策略评估期间拉取最新数据的一种方式。如果考虑这样的情况,如果在PE期间拉取了数据,生成的查询可能在完全评估时不代表世界的状态。

当然还有其他选择:

  1. 通过自己的方式提供数据 - 重载 input 或者在用于PE的存储中提供一些内容。
  2. 通过golang代码运行部分评估 - 我认为你应该能够控制不安全内置函数的列表(但我对此不是100%确定)。
  3. 通过golang代码,你可以编写自己的内置函数。它不会被认为是已知的(因此被视为不安全),只要所有参数都被定义,调用应该在部分评估期间执行。

我可以详细说明,但我需要了解更多关于你的情况的信息。

英文:

> Unfortunately, the HTTP requests do not seem to be executed and evaluated when v1/compile is called.

You're correct, http.send is excluded from partial eval (PE). The reason is that it was meant as a way to pull in the most recent data during policy evaluation. Thinking about it like that, if you pulled in data for PE, the generated queries might not represent the state of the world by the time they're fully evaluated.

Of course there are options, still:

  1. provide the data by your own means -- overloading input, or by providing something in data.external with the store used for PE
  2. run the partial evaluation through golang code -- I believe you should be able to control the list of unsafe builtins (but I'm not 100% sure about this)
  3. through golang code, you could write your own builtin. It wouldn't be known (and thus considered unsafe), and as long as all arguments are defined, the call should be executed during partial eval.

I can elaborate, but I'll need to know more about your scenario for that.

huangapple
  • 本文由 发表于 2023年5月22日 15:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定