1

试图理解 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 解决此问题?我是否缺少有关正确使用它的方法?

4

1 回答 1

2

查看文档的“For All”部分:

关于正在发生的事情的更多解释:https ://www.openpolicyagent.org/docs/latest/policy-language/#universal-quantification-for-all

快速示例:https ://www.openpolicyagent.org/docs/latest/policy-reference/#for-all

根据您的回复开放政策代理 - 错误与无,所描述的更新政策如下所示:

allowed_locations := ["eastus", "westus"]

exists(array, value) {
    array[_] == value
}

not_exists(array, value) {
    not exists(array, value)
}

all_resources_in_region {
    not any_resource_not_in_region
}

any_resource_not_in_region {
    not_exists(allowed_locations, input.planned_values.root_module.resource[_].values.location)
}

游乐场示例:https ://play.openpolicyagent.org/p/f1bI2Ddc9D

于 2020-07-14T19:30:23.070 回答