1 policy terraform open-policy-agent rego
我正在使用开放策略代理针对 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
]
cmone_resources = [name |
name := rm.child_modules[_].resources[_]
name.type == resource_type
]
cmtwo_resources = [name |
name := rm.child_modules[_].child_modules[_].resources[_]
name.type == resource_type
]
cm := array.concat(cmone_resources, cmtwo_resources)
all := array.concat(cm, root_resources)
}
Run Code Online (Sandbox Code Playgroud)
我已阅读内置函数的文档walk(x, [path, value])。文档在这里。我相信这个函数做了我想要它做的事情,但是根据给出的文档和我在其他地方找到的稀疏示例,我无法弄清楚如何让它按照我的预期工作。
我已经包含了一个带有非常基本设置和我定义的当前规则的游乐场。任何帮助将不胜感激。
小智 5
您走在正确的道路上,并且使用walk绝对是收集任意嵌套子资源的好方法。
首先,我们要探索步行的作用。它本质上会迭代我们正在遍历的对象中的所有节点,并为每个节点提供“路径”和当前节点值。路径将是一个键数组,就像对象一样:
{"a": {"b": {"c": 123}}}
Run Code Online (Sandbox Code Playgroud)
如果我们走一遍(下面使用opa runREPL 的示例:
> [path, value] = walk({"a": {"b": {"c": 123}}})
+---------------+-----------------------+
| path | value |
+---------------+-----------------------+
| [] | {"a":{"b":{"c":123}}} |
| ["a"] | {"b":{"c":123}} |
| ["a","b"] | {"c":123} |
| ["a","b","c"] | 123 |
+---------------+-----------------------+
Run Code Online (Sandbox Code Playgroud)
path我们看到我们有和值的所有路径和值组合value。resources您可以在部分规则(如您的规则)或推导式中迭代时捕获任何这些值。
所以..把这个转移到地形的东西上。如果我们修改游乐场示例以遍历示例输入(稍微修改一下以给事物提供一些唯一的名称),我们会得到:
walk_example[path] = value {
[path, value] := walk(tfstate)
}
Run Code Online (Sandbox Code Playgroud)
https://play.openpolicyagent.org/p/2u5shGbrV2
如果您查看结果值,walk_example我们可以看到我们期望必须处理的所有路径和值。
从那里开始,就需要resources对resource_types. 我们不会对集合进行迭代,而是将其用作查找来检查每种类型是否正常,并且我们将首先构建所有资源的完整集合(不按类型对它们进行分组)。原因是遍历输入 json 的所有节点非常昂贵,因此我们只想执行一次。随后,我们可以通过第二次按类型分组(根据需要)更快地遍历每个资源的完整列表。
更新版本看起来像:
walk_resources[resource] {
[path, value] := walk(tfstate)
# Attempt to iterate over "resources" of the value, if the key doesn't
# exist its OK, this iteration for walk will be undefined, and excluded
# from the results.
# Note: If you needed to be sure it was a "real" resource, and not some
# key you can perform additional validation on the path here!
resource := value.resources[_]
# check if the resource type was contained in the set of desired resource types
resource_types[resource.type]
}
Run Code Online (Sandbox Code Playgroud)
https://play.openpolicyagent.org/p/TyqMKDyWyh
^ 游乐场输入已更新,包括示例中的另一个级别的嵌套和类型。您可以看到原始resources输出缺少深度 3 资源,但该walk_resources集合包含所有预期的资源。
最后一部分,如果您想按类型对它们进行分组,请添加完整的规则,例如:
# list of all resources of a given type. given type must be defined in the resource_types variable above
resources = { resource_type: resources |
some resource_type
resource_types[resource_type]
resources := { resource |
walk_resources[resource]
resource.type == resource_type
}
}
Run Code Online (Sandbox Code Playgroud)
https://play.openpolicyagent.org/p/RlRZwibij9
它用一个推导式替换了原始resources规则,该推导式将迭代每个资源类型,然后收集与该类型匹配的资源。
我发现这些 terraform 资源帮助程序规则中存在一个额外的问题,那就是您需要引用“完整”规则,请参阅https://www.openpolicyagent.org/docs/latest/policy-language /#complete-definitions了解其含义的一些详细信息,而不是“部分”规则(在本例中是构建一组资源与将值分配给理解结果的规则)。问题在于,在编写本文时,OPA 将在内部缓存“完整”规则的值,而部分规则则不会。因此,如果您然后去编写一堆规则,例如:
deny[msg] {
r := resources["foo"]
# enforce something for resources of type "foo"...
...
}
deny[msg] {
r := resources["bar"]
# enforce something for resources of type "bar"...
...
}
Run Code Online (Sandbox Code Playgroud)
您要确保它resources每次都使用缓存的值,而不是重新计算该集合。您的规则的原始版本resources以及使用walk_resources我在这些示例中显示的规则都会遇到该问题。需要注意的一点是,如果您有较大的输入 tfplan,它可能会对性能产生相当大的影响。
| 归档时间: |
|
| 查看次数: |
2761 次 |
| 最近记录: |