[英]Open Policy Agent - check existence of elements in a list
剛剛開始處理conftest和OPA ,我正在嘗試驗證示例 kubernetes 部署清單,以確保它包含列表中的特定鍵(即所有containers
都存在image
)
這是一個示例輸入
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- image: hub.com/img1:tag1
imagePullPolicy: Always
我認為這個策略應該檢查所有容器的image
是否存在:
deny[reason] {
input.kind == "Deployment"
some i
not input.spec.template.spec.containers[i].image
reason := "Container.image not found"
}
但是conftest
拋出一個錯誤,抱怨not input.spec.template.spec.containers[i].image
expression is unsafe
任何有關如何處理此案例的評論/建議表示贊賞。
確保否定表達式( not ...
)中的變量在規則中的另一個非否定表達式中分配。 例如:
deny[reason] {
input.kind == "Deployment"
container := input.spec.template.spec.containers[_]
not container.image
reason := "Container.image not found"
}
在這個版本中,否定表達式中唯一的變量是container
。 container
變量在前一行賦值,所以表達式是安全的。
OPA 抱怨not input.spec.template.spec.containers[i].image
因為它會搜索所有使規則中的表達式為true 的變量賦值。 由於變量i
沒有分配其他地方,將有作業的無限數量到i
滿足not input.spec.template.spec.containers[i].image
...例如, i = 100
, i = -1
, i = "foo"
等。由於我們要保證策略執行終止,因此 OPA 拒絕該表達式。
有關安全的更多信息和示例,請訪問: https : //www.openpolicyagent.org/docs/latest/faq/#safety
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.