标签: rego

在 OPA rego 中使用 OR 条件

我想使用 OR 运算来组合以下条件:

  • 我的计数arr不等于0
  • 我的email不包含"test.com"

目前我正在使用内置函数any()

any([count(arr) != 0, not contains(email, "test.com")])
Run Code Online (Sandbox Code Playgroud)

然而我的规则产生了错误。

我怎样才能一次性实现并改进这一目标?

open-policy-agent rego

6
推荐指数
2
解决办法
1万
查看次数

Rego测试:如何测试“不否认”?

我正在探索使用opa test以下简单规则来测试我的 Rego 策略:

\n\n
deny["Must be allowed"] {\n  input.allowed == "no"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我能够在被拒绝的情况下成功测试这一点:

\n\n
test_denied_example {\n  deny with input as {"allowed":"no"}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,当我尝试针对应该允许的情况对其进行测试时,如下所示:

\n\n
test_allowed_example {\n  not deny with input as {"allowed":"yes"}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到错误:

\n\n
data.example.test_allowed_example: FAIL (330.534\xc2\xb5s)\n\n  Enter data.example.test_allowed_example = _\n  | Enter data.example.test_allowed_example\n  | | Fail not data.example.deny with input as {"allowed": "yes"}\n  | Fail data.example.test_allowed_example = _\n
Run Code Online (Sandbox Code Playgroud)\n\n

test_allowed_example除了知道这是失败的测试之外,我无法真正解析此错误消息。

\n\n

如何正确测试允许(而不是拒绝)输入的情况?

\n

testing open-policy-agent rego

4
推荐指数
1
解决办法
3241
查看次数

开放策略代理满足所有数组项的条件

试图解决这个问题一段时间 - 我有一个包含数组的 JSON 输入,请说如下:

{
    "array" : [
        {"foo": "bar"},
        {"foo": "buzz"},
        {"misbehaving": "object"}
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的目标是验证数组中的所有对象是否满足具有名为 foo 的字段的条件(实际用例是确保云部署中的所有资源都有标签)。我的问题是标准 rego 表达式被评估为“至少”而不是“全部”,这意味着表达式如下:

all_have_foo_field {
    input.array.foo
}
Run Code Online (Sandbox Code Playgroud)

总是返回 true,即使某些对象不满足这一点。我已经看过这个,但是评估一个正则表达式返回true或者false当我的策略检查字段是否存在时,这意味着如果它不存在,我会收到一个“var_is_unsafe”错误。

有任何想法吗?

open-policy-agent rego

3
推荐指数
1
解决办法
1468
查看次数

OPA/Rego 对数组的每个元素执行函数

我是 OPA/Rego 的新手,我正在尝试编写一个策略来检查 Azure 网络安全组是否包含我在阵列上定义的所有规则

package sample
default compliant = false
toSet(arr) = {x | x := arr[_]}
checkProperty(rule, index, propertySingular, propertyPlural) = true
{
    object.get(input.properties.securityRules[index].properties, propertySingular, "") == object.get(rule, propertySingular, "")
    count(toSet(object.get(input.properties.securityRules[index].properties, propertyPlural, [])) - toSet(object.get(rule, propertyPlural, []))) == 0
}
existRule(rule) = true
{
    input.properties.securityRules[i].name == rule.name
    input.properties.securityRules[i].properties.provisioningState == rule.provisioningState
    input.properties.securityRules[i].properties.description == rule.description
    input.properties.securityRules[i].properties.protocol == rule.protocol
    checkProperty(rule, i, "sourcePortRange", "sourcePortRanges")
    checkProperty(rule, i, "destinationPortRange", "destinationPortRanges")
    checkProperty(rule, i, "sourceAddressPrefix", "sourceAddressPrefixes")
    checkProperty(rule, i, "destinationAddressPrefix", "destinationAddressPrefixes")
    input.properties.securityRules[i].properties.access == rule.access
    input.properties.securityRules[i].properties.priority == rule.priority …
Run Code Online (Sandbox Code Playgroud)

open-policy-agent rego

3
推荐指数
1
解决办法
2096
查看次数

OPA (Rego) 作为 Go lib:如何应用外部数据?

我按照https://www.openpolicyagent.org/docs/latest/#5-try-opa-as-a-go-library的示例进行操作。重要代码片段:

r := rego.New(
rego.Query("x = data.example.allow"),
rego.Load([]string{"./example.rego"}, nil)
...
rs, err := query.Eval(ctx, rego.EvalInput(input))
...
Run Code Online (Sandbox Code Playgroud)

如何添加外部数据 ( data.json) 以便我可以data.wantedName在 rego 策略中使用它来访问它?

我尝试通读 go 文档和示例,但找不到任何有用的信息。

谢谢!

go open-policy-agent rego

2
推荐指数
1
解决办法
2296
查看次数

如何在 Rego 中发出 HTTP GET 请求

我想向 rego 中的 url 发出 get 请求。但它引发了Invalid argument: unallowedbuilt-in function call in rego module: http.send错误 这是我的代码。

package play

default hello = false

hello {
    response := http.send({
        "method" : "GET",
        "url": "http://localhost:8181/v1/data/example"
    })
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

open-policy-agent rego

1
推荐指数
1
解决办法
3593
查看次数

Rego规则中的参数[开放策略代理]

如何在Rego规则中使用参数?我会有这样的东西:

deny[reason] {
  input.request.kind.kind == "Route"
  not valid_route_request[label]
  reason := sprintf("missing or wrong router selector label: %v", [label])
}

valid_route_request[label] {
  requester := input.request.userInfo.username
  some i # iterate on all users
  requester == data.kubernetes.users[i].metadata.name
  label := input.request.object.metadata.labels["router-selector"]
  label == data.kubernetes.users[i].metadata.annotations[router_selector_key]
}
Run Code Online (Sandbox Code Playgroud)

wherelabel用于构建错误消息。我从 OPA 收到错误:var label is unsafe ...

总的来说,我还是不太清楚如何在Rego中传递参数。

open-policy-agent rego

1
推荐指数
1
解决办法
9935
查看次数

使用 walk 通过 rego 递归聚合 terraform 状态下的资源

我正在使用开放策略代理针对 terraform 状态的 JSON 输出编写策略。

这是状态文件的结构:

{
  "format_version": "0.1",
  "terraform_version": "0.12.28",
  "values": {
    "root_module": {
      "resources": [],
      "child_modules": [
        {
          "resources": [],
          "address": "",
          "child_modules": [
            {
              "resources": [],
              "address": "",
              "child_modules": [
                {}
              ]
            }
          ]
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我定义了这个令人讨厌的规则来实现我想要的,但这显然不是聚合这些资源的理想方式。

resources[resource_type] = all {
    some resource_type
    resource_types[resource_type]
    rm := tfstate.values.root_module

    # I think the below can be simplified with the built in "walk" function TODO: do that.
    root_resources := [name |
        name := rm.resources[_]
        name.type == resource_type …
Run Code Online (Sandbox Code Playgroud)

policy terraform open-policy-agent rego

1
推荐指数
1
解决办法
2761
查看次数

标签 统计

open-policy-agent ×8

rego ×8

go ×1

policy ×1

terraform ×1

testing ×1