【发布时间】:2020-07-14 09:21:31
【问题描述】:
试图理解 OPA 中的虚假概念。我的情况是这样的——我需要验证所有云资源是否都在 AWS 的允许区域内。我现在拥有的是:
allowed_locations := ["eastus", "westus"]
exists(array, value) {
array[_] == value
}
all_resources_in_allowed_regions {
not any_resource_not_in_allowed_regions
}
any_resource_not_in_allowed_regions {
some index
exists(allowed_locations, input.planned_values.root_module.resources[index].values.location) != true
}
问题是,我认为当策略/函数的结果不正确时,我会遗漏一些东西 - 例如,exists(allowed_locations, "westeurope") 的结果不是错误的,而是某种“未定义”,这意味着exists(allowed_locations, "westeurope") != true 的结果也是“未定义”,这意味着分配了 all_resources_in_allowed_regions not "undefined" 这是真的。
您将如何使用 OPA 解决此问题?我是否遗漏了一些关于正确使用它的方法?
【问题讨论】:
-
我找到了一些解决方案:定义一个
not_exists函数,它就是not exists(array, value),然后配置一个any_resource_not_in_region { not_exists(allowed_locations, resource[_].region)}的策略,然后是all_resources_in_region { not any_resource_not_in_region} -
答案似乎还不错——请考虑接受。
标签: cloud open-policy-agent rego