【问题标题】:open policy agent - false vs none开放策略代理 - 错误与无
【发布时间】: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


【解决方案1】:

查看文档的“适用于所有人”部分:

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

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

根据您的回复 open policy agent - false vs none,更新后的政策如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2021-12-17
    • 2020-09-05
    相关资源
    最近更新 更多